0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pygameでRPG(レベルアップ)

Last updated at Posted at 2025-06-15

テスト中

新RPGの目次

テキスト上昇

ここにコードがあります

import pygame
import sys
from pygame.locals import *


# 改行してテキストを描画する関数(バトルメッセージ)
def draw_text(surface, lines, pos, font, color=(255, 255, 255)):
    x, y = pos
    for i, line in enumerate(lines):
        if i <= 3:
            text_surface = font.render(line, True, color)
            surface.blit(text_surface, (x, y))
            y += int(text_surface.get_height() * 1.5)

pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("文字上昇")

# ロード
# ここにフォントのパスを貼り付ける
font = pygame.font.Font(,20)

# 色
black = (0, 0, 0)
white = (255,255,255)

lines = ["さようなら"]
text_up = False
up_num = 0

clock = pygame.time.Clock()
while True:
    clock.tick(60)
    screen.fill(black)
    draw_text(screen,lines,(270, 230 - up_num),font)
    if text_up:
        up_num += 1
        if up_num == 30:
            text_up = not text_up
            up_num = 0
            del lines[0]        
        
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_z and not text_up:
                lines.append("こんにちは")
                if len(lines) == 4:
                    text_up = True



ここにコードの説明があります

📝 改行してテキストを描画する関数(バトルメッセージ)

このコードは、🎮 ゲームのバトルメッセージを画面に描画し、最大4行まで表示できる ようにする仕組みを実装しています。
テキストが一定数に達すると、⬆️ 上方向へ移動し、古いメッセージが削除される動作 を実現します。


1️⃣ テキスト描画処理

def draw_text(surface, lines, pos, font, color=(255, 255, 255)):
    x, y = pos
    for i, line in enumerate(lines):  # 🔢 インデックスと要素を取得
        if i <= 3:  # ✅ 最大4行まで描画
            text_surface = font.render(line, True, color)  # 🖌 テキストを描画
            surface.blit(text_surface, (x, y))  # 📍 画面に描画
            y += int(text_surface.get_height() * 1.5)  # 📏 1.5倍の間隔を空ける

💡 enumerate() を使うことで lines のインデックスと要素を同時に取得 し、最大4行 のテキストを描画します。
間隔は 🆙 テキストの高さ × 1.5 に設定されています。


2️⃣ テキスト管理

lines = ["さようなら"]  # 🗒️ テキストのリスト
text_up = False  # 🔄 テキスト上昇のフラグ
up_num = 0  # 🔢 y座標の減少値

ここでは、✍️ 表示するテキストを管理するリストと、テキストを上昇させるフラグを準備 しています。


3️⃣ テキストの上昇処理

draw_text(screen, lines, (270, 230 - up_num), font)  # 📊 y座標を調整して描画

if text_up:
    up_num += 1  # 🔺 テキストを上昇させる
    if up_num == 30:  # 📏 上昇が一定値に達したら
        text_up = not text_up  # 🔄 フラグをリセット
        up_num = 0  # 🔃 上昇値を初期化
        del lines[0]  # ✂️ 一番上のテキストを削除
  • up_num == 30 は、🆙 テキストの高さ × 1.5 に相当します。
  • テキストが4行になったら、🗑️ 一番上のテキストを削除し、3行に戻します。

4️⃣ キー入力によるテキスト追加

elif event.type == KEYDOWN:
    if event.key == K_z and not text_up:  # ⌨️ Zキーが押されたら
        lines.append("こんにちは")  # ➕ 新しいテキストを追加
        if len(lines) == 4:  # 🔢 4行になったら
            text_up = True  # 🆙 テキストを上昇させるフラグを立てる
  • K_z キーを押した際に "こんにちは"lines に追加 ✍️。
  • lines の要素数が 4行になったら text_up = True にして、🔺 テキストを上昇させる準備 を開始。

🔍 まとめ

このコードは、🖥️ 最大4行のテキストを表示し、一定数に達したら⬆️ 上昇し、古いメッセージを削除する 処理を実装しています。

enumerate() を活用してインデックスと要素を取得🔢
テキストの高さ × 1.5 の間隔で描画📏
Zキーでテキストを追加し、4行になると上昇開始⬆️
上昇が一定値に達すると一番上のテキストを削除🗑️

ステータス上昇

ここにコードがあります

import random

lines = []
hit_and_miss = [0, 1]
up_list = []

# ステータスアップの種類
status_names = ["ちから", "すばやさ", "まもり", "最大HP", "最大MP"]

# 各ステータスの上昇値を決定
for _ in range(5):
    loto = random.choice(hit_and_miss)
    up_list.append(random.randint(1, 3) if loto == 1 else 0)

# ステータスアップのメッセージ作成
for i, value in enumerate(up_list):
    if value:
        lines.append(f"{status_names[i]}{value}ポイント 上がった!")

for i in lines:
    print(i)



ここにコードの説明があります

🎮 ステータスアップの処理(バトルメッセージ)

このコードは RPGのステータスアップ処理 を行うものです。
各ステータスの 上昇値(あり/なし) をランダムに決定し、メッセージとして表示します。


1️⃣ 変数の準備

lines = []  # 🗒️ テキストのリスト(結果メッセージ)
hit_and_miss = [0, 1]  # 🎯 ありなしの判定に使う(0: なし, 1: あり)
up_list = []  # 📊 ステータスの上昇値を格納するリスト

# ✨ ステータスアップの種類
status_names = ["ちから", "すばやさ", "まもり", "最大HP", "最大MP"]

💡 status_names各ステータスの名称 を保持するリストです。


2️⃣ ステータスアップの決定

for _ in range(5):  # 🔁 5回繰り返し(各ステータス分)
    loto = random.choice(hit_and_miss)  # 🎲 0か1をランダムに選択
    
    up_list.append(random.randint(1, 3) if loto == 1 else 0)  # 📈 ありなら1~3を選び、なしなら0
  • random.choice(hit_and_miss) を使い、 0(あり)か 1(なし)を決定
  • あり (loto == 1) の場合、ステータス上昇値を 1~3 の範囲で決定 🔢
  • なし (loto == 0) の場合、上昇値は 0(変化なし) 🛑

3️⃣ ステータスアップのメッセージ作成

for i, value in enumerate(up_list):  # 🔢 インデックスと要素を取得
    if value:  # ✅ ステータス上昇値が1以上ならメッセージ作成
        lines.append(f"{status_names[i]}{value}ポイント 上がった!")  # 📝 メッセージをリストに追加
  • enumerate() を使い、 up_list のインデックスとステータス上昇値を取得 🔄
  • 上昇値が 0 ならスキップ 🚫
  • 上昇値が 1以上 の場合、メッセージを作成して lines に追加 🎉
  • Pythonでは、数値 0 は "偽 (False)"、1以上 の数値は "真 (True)" として扱われます 🎉

🔍 まとめ

各ステータスの上昇をランダムに決定 🎲
上昇値が1以上ならメッセージを生成 ✍️
なし(0)ならメッセージは作成せずスキップ 🚫

レベルアップ

ここにコードがあります

import pygame
from pygame import *
import sys
import time
import random
import numpy as np
from decimal import Decimal, getcontext
from decimal import Decimal, ROUND_DOWN

# 部分一致で値を取得する関数
def get_value_by_partial_key(dictionary, partial_key):
    for key in dictionary:
        if key in partial_key:
            return dictionary[key]
    return None

# 改行してテキストを描画する関数(バトルメッセージ)
def draw_text(surface, lines, pos, font, color=(255, 255, 255)):
    x, y = pos
    for i, line in enumerate(lines):
        if i <= 3:
            text_surface = font.render(line, True, color)
            surface.blit(text_surface, (x, y))
            y += int(text_surface.get_height() * 1.5)

# 2列×3行のテキストを描画する関数(ステータスウィンドウ・コマンドウィンドゥ)
def draw_text_grid(screen, text_list, start_x, start_y, spacing_x, spacing_y):
    for col_index, col in enumerate(text_list):  # 列ごとに処理
        for row_index, text in enumerate(col):  # 各行のテキストを描画
            text_surface = font.render(text, True, white)
            x = start_x + col_index * spacing_x  # 列ごとにX座標を調整
            y = start_y + row_index * spacing_y  # 行ごとにY座標を調整
            screen.blit(text_surface, (x, y))

# ここのpathはそのままにしておく(パスを貼り付けるところではない)
def bgm(path):
    pygame.mixer.music.load(path)
    pygame.mixer.music.play(-1)

pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('レベルアップ')

# ここにフォントのパスを貼り付ける
font = pygame.font.Font(, 18)
# ここにカーソル画像のパスを貼り付ける
cursor = pygame.image.load()
# ここにモンスター画像のパスを貼り付ける
monsterA_image = pygame.image.load()
# ここにモンスター画像のパスを貼り付ける
monsterB_image = pygame.image.load()
# ここにモンスター画像のパスを貼り付ける
monsterC_image = pygame.image.load()

# SE
# ここにボタン音のパスを貼り付ける
sound = pygame.mixer.Sound()
# ここにダメージ音のパスを貼り付ける
sound2 = pygame.mixer.Sound()
# ここにレベルアップのパスを貼り付ける
sound3 = pygame.mixer.Sound()

# BGM
# ここに戦闘のBGMのパスを貼り付ける
battle_bgm = 
# ここに勝ったときのBGMのパスを貼り付ける
win_bgm = 
# ここに負けたときのBGMのパスを貼り付ける
lose_bgm = 

bgm(battle_bgm)    

# モンスター
nameA = ""
nameB = ""
nameC = ""
# モンスターのグループ
# 全角スペースで区切ります
# ex) "スライム ウルフ"
# ひとグループ5体までです
groupA = ""
groupB = ""
groupC = ""
monster_list = [groupA,groupB,groupC]


# 色
white = (255, 255, 255)

# カーソル
alpha = 220
delta = 5

lines = ["モンスターが あらわれた!"]
text_up = False
up_num = 0


battle_message = True
command = False
select = False
masic_select = False
defeat = False

actor_index = 0
select_index = 0
masic_index = 0
chara_index = 0
cmd_index = 0
cmd_num1 = 0
cmd_num2 = 0

phase = 0
masic_phase = 0

# 枠点滅
status_flash = 0
status_visible = 0
mon_flash = 0
mon_visible = 0
player_damage = 0
mon_damage = 0

# まほうエフェクト
masic_effect = 0
# まほうフレーム
masic_frame = 0
# まほうサーフェイス
damera_surface = pygame.Surface((width,height),SRCALPHA)
damera_surface.fill((255,201,14,128))
hoimun_surface = pygame.Surface((width,height),SRCALPHA)
hoimun_surface.fill((255,255,255,128))
# まほうリスト
damera = {"masic_name":"ダメラ","MP":2,"数値":10,"masic_effect":1,"masic_type":"攻撃まほう","surface":damera_surface}
hoimun = {"masic_name":"ホイムン","MP":3,"数値":30,"masic_effect":2,"masic_type":"補助まほう","surface":hoimun_surface}
masic_list = [damera,hoimun]
cmd_cursor = False
# キャラリスト
chara_list = ["ゆうしゃ","せんし"]

# レクト
select_rect = pygame.Rect(width * 0.4,height * 0.67,220,140)
select_rectx,select_recty = select_rect.topleft
cmd_rect = pygame.Rect(20, height * 0.67, 220, 140)
cmd_rectx,cmd_recty = cmd_rect.topleft
message_rect = pygame.Rect(20, height * 0.67, 600, 140)
message_rectx,message_recty = message_rect.topleft


# 2列×3行のテキストデータ
text_list = [
    ["攻撃", "魔法", "ガード"],  # 1列目
    ["アイテム", "装備", "逃げる"]  # 2列目
]

status_data = []

# 勇者の初期ステータス
player_name = "ゆうしゃ"
player_Max_HP = 53
player_HP = 53
player_Max_MP = 5
player_MP = 5
player_strength = 23
player_quick = 6
player_deffence = 0
player_level = 1

# レベルアップ
level_up_list = [7,23,47,110,220,50,800,1300,2000,2900,4000,5500]
level_up_index = 0
hit_and_miss = [0,1]
Exp = 0
Gold = 0
# ステータスアップの種類
status_names = ["ちから", "すばやさ", "まもり", "最大HP", "最大MP"]
# ステータスの名称を辞書save_dataのキーに置き換えるための辞書
status_names_dict = {"ちから":"strength","すばやさ":"quickness","まもり":"deffence","最大HP":"player_Max_HP","最大MP":"player_Max_MP"}
# 上昇値のリスト
up_list = []

# 2列×3行のテキストデータ
text_list2 = [
    ["H", "M", ""],  # 1列目
    [f"{player_HP}", f"{player_MP}", f"{player_level}"]  # 2列目
]

# モンスターの初期ステータス
mon_images_dict = {nameA: monsterA_image,
                   nameB: monsterB_image,
                   nameC: monsterC_image}
mon_HP_dict = {nameA: 4,nameB: 6,nameC: 8}
mon_strength_dict = {nameA: 3,nameB: 25,nameC: 6}
mon_deffence_dict = {nameA: 4,nameB: 4,nameC: 8}
mon_quickness_dict = {nameA:3,nameB:4,nameC:8}
mon_EXP_dict = {nameA: 1,nameB: 3,nameC: 4}
mon_Gold_dict = {nameA: 4,nameB: 5,nameC: 10}
mon = random.choice(monster_list)
mon_group_list = mon.split()
mon_group_list2 = mon_group_list.copy()
mon_EXP = 0
mon_Gold = 0

for i in range(len(mon_group_list)):  
    status_data.append({
        "name": f"{mon_group_list[i]}",
        "HP": get_value_by_partial_key(mon_HP_dict,mon_group_list[i]),
        "strength": get_value_by_partial_key(mon_strength_dict,mon_group_list[i]),
        "deffence": get_value_by_partial_key(mon_deffence_dict,mon_group_list[i]),
        "quickness": get_value_by_partial_key(mon_quickness_dict,mon_group_list[i]),
        "image": get_value_by_partial_key(mon_images_dict,mon_group_list[i]),
        "attack_on": 0
    })

status_data.append({"name": f"{player_name}","HP":player_HP,"MP":player_MP,
                    "strength":player_strength,"deffence":player_deffence,
                    "quickness": player_quick,"player_Max_HP":player_Max_HP,"player_Max_MP":player_Max_MP})


# モンスターの位置を定義(奇数・偶数で異なる)
monster_positions = {
    1: [0.5],  # 1体
    2: [0.4, 0.6],  # 2体
    3: [0.3, 0.5, 0.7],  # 3体
    4: [0.2, 0.4, 0.6, 0.8],  # 4体
    5: [0.1, 0.3, 0.5, 0.7, 0.9]  # 5体
}

# モンスターの位置を設定
monster_rects = [
    get_value_by_partial_key(mon_images_dict,mon_group_list[i]).get_rect(center=(width * monster_positions[len(mon_group_list)][i], height * 0.5))
    for i in range(len(mon_group_list))
]


running = True
clock = pygame.time.Clock()

while running:
    clock.tick(60)
    # 画面を黒色に塗りつぶし
    screen.fill((0, 0, 0))
    # ステータスウィンドゥ
    if status_visible == 0:
        pygame.draw.line(screen, white, (20, 58),(118, 58))
        pygame.draw.rect(screen, white, (20, 20, 102, 134), 3,5)
        text = font.render("ゆうしゃ", True, white)
        screen.blit(text,(35, 35))
        draw_text_grid(screen, text_list2, start_x=32, start_y=65, spacing_x=40, spacing_y=30)  # テキスト描画
        text_list2 = [["H", "M", ""],[f"{player_HP}", f"{player_MP}", f"{player_level}"]]
    # モンスターの画像表示
    if mon_group_list:
        for i in range(len(mon_group_list)):
            if select_index == i and mon_visible == 0:
                screen.blit(get_value_by_partial_key(mon_images_dict,mon_group_list[i]), (monster_rects[i].topleft))
            if select_index != i:
                screen.blit(get_value_by_partial_key(mon_images_dict,mon_group_list[i]), (monster_rects[i].topleft))
    # コマンドウィンドゥ
    if command:
        cursor.set_alpha(alpha)
        alpha += delta
        if alpha <= 120 or alpha >= 255:
            delta = -delta
        pygame.draw.line(screen, white, (20, 358),(236, 358))
        pygame.draw.rect(screen, white, cmd_rect, 3,5)
        text = font.render("ゆうしゃ", True, white)
        screen.blit(text,((cmd_rectx + 14) + 60,(cmd_recty + 14)))
        draw_text_grid(screen, text_list, start_x=51, start_y=365, spacing_x=98, spacing_y=34)  # テキスト描画
        if cmd_cursor:
            screen.blit(cursor, ((cmd_rectx + 5) + 100 * cmd_num2, (cmd_recty + 40) + 34 * cmd_num1))  # カーソル描画
            cmd_index = cmd_num1 + (cmd_num2 * 3)
        # セレクトウィンドゥ
        if select or masic_phase == 2 and masic_list[masic_index]["masic_type"] == "攻撃まほう":
            pygame.draw.rect(screen,white,select_rect,3,5)
            # モンスター名の描画
            if mon_group_list:
                for i, mon in enumerate(mon_group_list):
                    text = font.render(mon, True, white)
                    screen.blit(text, ((select_rectx + 14) + 30, (select_recty + 14) + 24 * i))
                screen.blit(cursor, ((select_rectx + 14), (select_recty + 11) + 24 * select_index))  # カーソル描画
        if masic_phase == 2 and masic_list[masic_index]["masic_type"] == "補助まほう":
            pygame.draw.rect(screen,white,select_rect,3,5)
            # キャラ選択
            # カーソル
            for i, chara in enumerate(chara_list):
                text = font.render(chara, True, white)
                screen.blit(text, ((select_rectx + 14) + 30, (select_recty + 14) + 24 * i))
            screen.blit(cursor, ((select_rectx + 14), (select_recty + 11) + 24 * chara_index))  # カーソル描画
        if masic_select and masic_phase <= 1:
            # まほうがある場合のみ描画
            pygame.draw.rect(screen,white,select_rect,3,5)
            if masic_list:
                for i, masic in enumerate([masic_list[i]["masic_name"] for i in range(len(masic_list))]):
                    text = font.render(masic, True, white)
                    screen.blit(text, ((select_rectx + 14) + 30, (select_recty + 14) + 24 * i))
                screen.blit(cursor, ((select_rectx + 14), (select_recty + 11) + 24 * masic_index))  # カーソル描画
    # メッセージウィンドゥ
    if battle_message:
        pygame.draw.rect(screen, white, message_rect, 3,5)
        # バトルメッセージ
        draw_text(screen,lines,((message_rectx + 24),(message_recty + 24) - up_num),font)
        if text_up and up_num < 27:
            up_num += 1
            if up_num == 27:
                text_up = not text_up
                up_num = 0
                del lines[0]
        

    # プレイヤーのダメージエフェクト
    if status_flash > 0:
        status_visible = pygame.time.get_ticks() // 90 % 2
        status_flash -= 1
        if status_flash == 19:
            sound2.play()
    if status_flash == 1:
        status_flash = 0
        status_visible = 0
        if player_HP <= 0:
            bgm(lose_bgm)
            message = "そのあと\nゆうしゃのすがたをみたものはいない"
            phase = 4
        else:
            phase = 2
    # モンスターのダメージエフェクト
    if mon_flash > 0:
        mon_visible = pygame.time.get_ticks() // 90 % 2
        mon_flash -= 1
        if mon_flash == 19:
            sound2.play()
    if mon_flash == 1:
        mon_flash = 0
        mon_visible = 0
        if status_data[select_index]["HP"] <= 0: 
            defeat = True
        else:
            phase = 2
                
    # まほう(半透明レクト)
    if player_MP >= masic_list[masic_index]["MP"]:
        if masic_frame > 0:
            screen.blit(masic_list[masic_index]["surface"],(0,0))
            masic_frame -= 1

    pygame.display.update()
    
    # キーハンドラー
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_z and mon_flash == 0 and status_flash == 0:
                # あらわれた!を消してコマンドウィンドゥをひらく
                if phase == 0:
                    sound.play()                        
                    battle_message = False # あらわれた!を消す
                    command = True
                    cmd_cursor = True
                    phase = 1
                # コマンドウィンドゥを開いたまま、魔物選択ウィンドゥをひらく
                # 「たたかう」
                elif phase == 1 and cmd_index == 0:
                    sound.play()                        
                    select = True
                    cmd_cursor = False
                    phase = 2
                elif phase == 1 and cmd_index == 1:
                    sound.play()                        
                    masic_select = True
                    cmd_cursor = False
                    masic_phase = min(2,masic_phase + 1)
                    if masic_phase == 2:
                        phase = 2
                # 魔物選択ウィンドゥを消して、メッセージの表示
                elif phase == 2 or phase == 1 and cmd_index == 2 or phase == 1 and cmd_index == 5:
                    command = False
                    select = False
                    masic_phase = 3
                    battle_message = True
                    status_data2 = sorted(status_data, key=lambda dictionary: dictionary["quickness"], reverse=True)
                    if actor_index < len(status_data2):
                        current_actor = status_data2[actor_index]
                        sound.play()
                        if current_actor["name"] == "ゆうしゃ":
                            if cmd_index == 0:
                                lines = [f'{current_actor["name"]}の こうげき!']
                            if cmd_index == 1:
                                    lines = [f'{player_name}は {masic_list[masic_index]["masic_name"]}を となえた!']
                                    masic_effect = masic_list[masic_index]["masic_effect"]
                                    masic_frame = 10
                            if cmd_index == 2:
                                if current_actor["name"] == "ゆうしゃ":
                                    lines = [f"{player_name}は ガードした"]
                                    actor_index += 1
                            if cmd_index == 5:
                                lines = [f"{player_name}は にげだした"]
                        else:
                            lines = [f'{current_actor["name"]}の こうげき!']

                        if cmd_index == 2 and current_actor["name"] == "ゆうしゃ":
                            pass
                        elif cmd_index == 5:
                            phase = 4
                        else:
                            phase = 3
                    # ターン終了したら、コマンドウィンドゥをひらく
                    elif actor_index == len(status_data):
                        actor_index = 0
                        select_index = 0
                        masic_index = 0
                        masic_phase = 0
                        cmd_index, cmd_num1,cmd_num2 = 0, 0, 0
                        sound.play()
                        battle_message, command, masic_select, cmd_cursor = False, True, False, True
                        phase = 1
                        for i in range(len(mon_group_list)):
                            status_data[i]["attack_on"] = 0
                # プレイヤーの攻撃(モンスターに与えるダメージ)
                elif phase == 3 and current_actor["name"] == "ゆうしゃ" and not defeat:
                    if cmd_index == 1 and player_MP < masic_list[masic_index]["MP"]:
                        message = "MPが足りない…"
                        phase = 2
                    elif cmd_index == 0 or cmd_index == 1 and masic_list[masic_index]["masic_type"] == "攻撃まほう":
                        if cmd_index == 0:
                            mon_damage = player_strength // 2 - status_data[select_index]["deffence"] // 4
                        elif cmd_index == 1:
                            mon_damage = masic_list[masic_index]["数値"]
                            player_MP -= masic_list[masic_index]["MP"]
                        mon_damage = random.randint(mon_damage,int(mon_damage * 1.5))
                        if mon_damage <= 0:
                            mon_damage = 1
                        status_data[select_index]["HP"] -= mon_damage
                        lines.append(f'{status_data[select_index]["name"]}に {mon_damage}の ダメージ!' )
                        mon_flash = 20
                        if status_data[select_index]["attack_on"] == 1:
                            actor_index -= 1
                    elif masic_list[masic_index]["masic_type"] == "補助まほう":
                        # まほう(回復)
                        if masic_effect == 2:
                            if player_MP >= masic_list[masic_index]["MP"]:
                                recovery  = random.randint(masic_list[masic_index]["数値"],int(masic_list[masic_index]["数値"] * 1.5))
                                player_HP = min(player_Max_HP,player_HP + recovery)
                                masic_effect = 0 #これがないと先に進まない。
                                lines.append(f'{chara_list[chara_index]}の きずが かいふくした!') 
                        player_MP -= masic_list[masic_index]["MP"]
                        phase = 2
                    actor_index += 1
                elif defeat:
                    lines.append(f'{status_data[select_index]["name"]}をたおした!')
                    del mon_group_list[select_index]
                    del monster_rects[select_index]
                    del status_data[select_index]
                    defeat = False
                    phase = 2
                    # モンスターをすべてたおした時の処理
                    if len(mon_group_list) == 0:
                        bgm(win_bgm)
                        lines = ["モンスターたちをやっつけた!"]
                        phase = 5
                # モンスターの攻撃(プレイヤーに与えるダメージ)
                elif phase == 3:
                    sound.play()
                    for i in range(len(mon_group_list)):
                        if current_actor["name"] == status_data[i]["name"]:
                            player_damage = status_data[i]["strength"] // 2 - player_deffence // 4
                            player_damage = random.randint(player_damage,int(player_damage * 1.5))
                            if cmd_index == 2:
                                player_damage = int(player_damage * 0.6)
                            if player_damage <= 0:
                                player_damage = 1
                            player_HP -= player_damage
                            lines.append(f'{player_name}に {player_damage}のダメージ!')
                            status_flash = 20
                            if player_HP <= 0:
                                player_HP = 0
                    current_actor["attack_on"] = 1
                    actor_index += 1
                # ワンテンポずらす
                elif phase == 4:
                    sound.play()
                    if len(lines) >= 4:
                        text_up = True
                    else:
                        phase = 7
                # 経験値とお金
                elif phase == 5:
                    sound.play()
                    for i in range(len(mon_group_list2)):
                        mon_EXP += get_value_by_partial_key(mon_EXP_dict,mon_group_list2[i])
                        mon_Gold += get_value_by_partial_key(mon_Gold_dict,mon_group_list2[i])
                    lines.append(f"けいけんち {mon_EXP}かくとく")
                    lines.append(f"{mon_Gold}ゴールドを てにいれた")
                    Exp += mon_EXP
                    Gold += mon_Gold
                    mon_EXP = 0
                    mon_Gold = 0
                    if Exp >= level_up_list[level_up_index]:
                        pygame.mixer.music.pause()
                        sound3.play()
                        level_up_index += 1
                        phase = 6
                    else:
                        phase = 7
                elif phase == 6:
                    time.sleep(1)
                    pygame.mixer.music.unpause()
                    sound.play()

                    # 各ステータスの上昇値を決定
                    for _ in range(5):
                        loto = random.choice(hit_and_miss)
                        up_list.append(random.randint(1, 3) if loto == 1 else 0)

                    # ステータスアップのメッセージ作成
                    for i, value in enumerate(up_list):
                        if value != 0:
                            lines.append(f"{status_names[i]}が {value}ポイント 上がった!")
                            status_data[0][status_names_dict[status_names[i]]] += value

                    player_level += 1
                    phase = 4
                elif phase == 7:
                    sound.play()
                    pygame.quit()
                    sys.exit()
                        
            #方向キー
            if event.type == KEYDOWN:
                if command and not select and not masic_select:
                    if event.key == K_DOWN:
                        cmd_num1 = (cmd_num1 + 1) % 3  # 下へ移動
                    if event.key == K_UP:
                        cmd_num1 = (cmd_num1 - 1) % 3  # 上へ移動
                    if event.key == K_RIGHT:
                        cmd_num2 = (cmd_num2 + 1) % 2  # 下へ移動
                    if event.key == K_LEFT:
                        cmd_num2 = (cmd_num2 - 1) % 2  # 下へ移動
                if select or masic_phase == 2 and masic_list[masic_index]["masic_type"] == "攻撃まほう":
                    if event.key == K_DOWN:
                        select_index = (select_index + 1) % len(mon_group_list)  # 下へ移動
                    if event.key == K_UP:
                        select_index = (select_index - 1) % len(mon_group_list)  # 上へ移動
                if masic_phase == 2 and masic_list[masic_index]["masic_type"] == "補助まほう":
                    if event.key == K_DOWN:
                        chara_index = (chara_index + 1) % len(chara_list)  # 下へ移動
                    if event.key == K_UP:
                        chara_index = (chara_index - 1) % len(chara_list)  # 上へ移動
                if masic_select and masic_phase <= 1:
                    if event.key == K_DOWN:
                        masic_index = (masic_index + 1) % len(masic_list)  # 下へ移動
                    if event.key == K_UP:
                        masic_index = (masic_index - 1) % len(masic_list)  # 上へ移動
                    

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?