戦闘画面
Zキーで決定。
パスとモンスターの名前とモンスターのグループを入力します。
ここをクリックしてください
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 draw_text(surface, message, pos, font, color=(255, 255, 255)):
lines = message.split('\n')
x, y = pos
for line in lines:
text_surface = font.render(line, True, color)
surface.blit(text_surface, (x, y))
y += text_surface.get_height() * 2
# 部分一致で値を取得する関数
def get_value_by_partial_key(dictionary, partial_key):
for key in dictionary:
if key in partial_key:
return dictionary[key]
return None
# ここのpathはそのままにしておく(パスを貼り付けるところではない)
def bgm(path):
pygame.mixer.music.load(path)
pygame.mixer.music.play(-1)
def bright_image(image):
# 画像をnumpy配列に変換
image_array = pygame.surfarray.array3d(image)
# 明るさを調整する関数
def adjust_brightness(img_array, brightness_factor):
img_array = img_array * brightness_factor
img_array = np.clip(img_array, 0, 255) # 値を0-255に制限
return img_array
# 明るさを上げる(例:1.2倍にする)
brightness_factor = 1.6
bright_image_array = adjust_brightness(image_array, brightness_factor)
# 調整後の画像をサーフェスに戻す
bright_image_surface = pygame.surfarray.make_surface(bright_image_array)
return bright_image_surface
def damage_effect(screen, bright_image, monster_image, black_image, monster_rect, sound):
screen.blit(bright_image, monster_rect.topleft)
pygame.display.update()
time.sleep(0.1)
screen.blit(monster_image, monster_rect.topleft)
pygame.display.update()
time.sleep(0.1)
screen.blit(black_image, monster_rect.topleft)
pygame.display.update()
time.sleep(0.1)
screen.blit(monster_image, monster_rect.topleft)
pygame.display.update()
sound2.play()
def monster_damage(message,damage,monster_HP,monster_deffence,monster_name):
if action_number == 0:
damage = attack_power // 2 - monster_deffence // 4
if random_value < Decimal("1.00") - rate:
damage = random.randint(damage,int(damage * 1.5))
if damage <= 0:
damage = 1
monster_HP -= damage
message = f"{player_name}のこうげき\n{monster_name}に {str(damage)}の ダメージ"
if random_value >= Decimal("1.00") - rate and random_value <= Decimal("1.00") - rate2:
damage = random.randint(damage,int(damage * 1.5)) * 2
if damage <= 0:
damage = 1
monster_HP -= damage
message = f"{player_name}のこうげき\n強烈な一撃\n{monster_name}に{str(damage)}の ダメージ"
time.sleep(0.5)
if action_number == 1:
if masic_effect == 1:
damage = masic_parameter_dict[masic_list[arrow_index]]
damage = random.randint(damage,int(damage * 1.5))
if damage <= 0:
damage = 1
monster_HP -= damage
message = f"{player_name}は、{masic_list[arrow_index]}をつかった\n{monster_name}に {str(damage)}の ダメージ"
return monster_HP, message
width, height = 640, 480
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('battle')
# ここにフォントのパスを貼り付ける
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 =
# モンスターのグループ
# 全角スペースで区切ります
# ex) "スライム ウルフ"
# ひとグループ3体までです
groupA = ""
groupB = ""
groupC = ""
# クォーテーションの中にモンスターの名前を入れてください
nameA = ""
nameB = ""
nameC = ""
bgm(battle_bgm)
# カーソル
cursor_x = 30
cursor_y = 368
alpha = 220
delta = 5
battle_message = True
command = False
select = False
effect = 0
# 攻撃、ミス、大ダメージの確立に使用
rate = Decimal("0.25")#0.75
rate2 = Decimal("0.15")
player_name = "ゆうしゃ"
random_value = 0
monster_attack = 0
motion = 0
attack = 0
encount = 0
sub_motion = 0# monster2以降の攻撃のため
defeat1 = 0
defeat2 = 0
defeat3 = 0
defense_cmd = 0
# アイテムリスト
item_list = ["やくそう", "せいすい", "げんきのかけら", "まほうのしずく", "ひかりのたま"]
arrow_index = 0 # 選択カーソルの位置
item_cursor = 0
item_effect_dict = {"やくそう":2}
item_parameter_dict = {"やくそう":30}# ダメージや回復量
item_effect = 0
recovery = 0
# まほう
masic_list = ["ダメラ","ホイムン"]
masic_effect_dict = {"ダメラ":1,"ホイムン":2}
masic_cost_dict = {"ダメラ":2,"ホイムン":3}# 消費MP
masic_parameter_dict = {"ダメラ":10,"ホイムン":30}# ダメージや回復量
masic_effect = 0
damera = pygame.Surface((width,height),SRCALPHA)
damera.fill((255,201,14,128))
hoimun = pygame.Surface((width,height),SRCALPHA)
hoimun.fill((255,255,255,128))
masic_on = 0
masic_action = 0
arrow_index2 = 0
# キャラクター
chara_list = ["ゆうしゃ"]
chara_sele = False
chara_cursor = 0
# 勇者の初期ステータス
Max_HP = 53
player_HP = 53
Max_MP = 10
player_MP = 5
power = 23#23
player_quick = 8
arms = 0
attack_power = power + arms
deffence = 0
Gold = 200
Level = 1
Exp = 0
damage = 0
# モンスター
monster_list = [groupC]#[groupA,groupB,groupC]
monster_dict = {nameA: monsterA_image,nameB: monsterB_image,nameC: monsterC_image}
# モンスターの数値
mon_HP_dict = {nameA: 4,nameB: 6,nameC: 8}
mon_power_dict = {nameA: 16,nameB: 10,nameC: 6}
mon_deffence_dict = {nameA: 4,nameB: 4,nameC: 8}
mon_quickness_dict = {nameA:3,nameB:4,nameC:7}
mon_EXP_dict = {nameA: 1,nameB: 2,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()
monster_num = len(mon_group_list2)
if len(mon_group_list2) == 1:
mon_index_list = [0]
mon_index = 0
elif len(mon_group_list2) == 2:
mon_index_list = [0,1]
mon_index = 0
elif len(mon_group_list2) == 3:
mon_index_list = [1,0,2]
mon_index = 0
elif len(mon_group_list2) == 4:
mon_index_list = [2,0,1,3]
mon_index = 0
elif len(mon_group_list2) == 5:
mon_index_list = [3,1,0,2,4]
mon_index = 0
num = 0
select_mon = mon_index_list[0]
# "ウルフA"や"ウルフB"を"ウルフ"をキーとして辞書から画像を取得
if len(mon_group_list) >= 1:
monster_image1 = get_value_by_partial_key(monster_dict,mon_group_list[0])
if len(mon_group_list) >= 2:
monster_image2 = get_value_by_partial_key(monster_dict,mon_group_list[1])
if len(mon_group_list) >= 3:
monster_image3 = get_value_by_partial_key(monster_dict,mon_group_list[2])
if len(mon_group_list) >= 4:
monster_image4 = get_value_by_partial_key(monster_dict,mon_group_list[3])
if len(mon_group_list) >= 5:
monster_image5 = get_value_by_partial_key(monster_dict,mon_group_list[4])
message = "モンスターが あらわれた!"
# リストから選ばれたモンスターに数値を設定
if len(mon_group_list) >= 1:
monster1_name = mon_group_list[0]
monster1_HP = get_value_by_partial_key(mon_HP_dict,mon_group_list[0])
monster1_power = get_value_by_partial_key(mon_power_dict,mon_group_list[0])
monster1_deffence = get_value_by_partial_key(mon_deffence_dict,mon_group_list[0])
monster1_quickness = get_value_by_partial_key(mon_quickness_dict,mon_group_list[0])
if len(mon_group_list) >= 2:
monster2_name = mon_group_list[1]
monster2_HP = get_value_by_partial_key(mon_HP_dict,mon_group_list[1])
monster2_power = get_value_by_partial_key(mon_power_dict,mon_group_list[1])
monster2_deffence = get_value_by_partial_key(mon_deffence_dict,mon_group_list[1])
monster2_quickness = get_value_by_partial_key(mon_quickness_dict,mon_group_list[1])
if len(mon_group_list) >= 3:
monster3_name = mon_group_list[2]
monster3_HP = get_value_by_partial_key(mon_HP_dict,mon_group_list[2])
monster3_power = get_value_by_partial_key(mon_power_dict,mon_group_list[2])
monster3_deffence = get_value_by_partial_key(mon_deffence_dict,mon_group_list[2])
monster3_quickness = get_value_by_partial_key(mon_quickness_dict,mon_group_list[2])
if len(mon_group_list) >= 4:
monster4_name = mon_group_list[3]
monster4_HP = get_value_by_partial_key(mon_HP_dict,mon_group_list[3])
monster4_power = get_value_by_partial_key(mon_power_dict,mon_group_list[3])
monster4_deffence = get_value_by_partial_key(mon_deffence_dict,mon_group_list[3])
monster4_quickness = get_value_by_partial_key(mon_quickness_dict,mon_group_list[3])
if len(mon_group_list) >= 5:
monster5_name = mon_group_list[4]
monster5_HP = get_value_by_partial_key(mon_HP_dict,mon_group_list[4])
monster5_power = get_value_by_partial_key(mon_power_dict,mon_group_list[4])
monster5_deffence = get_value_by_partial_key(mon_deffence_dict,mon_group_list[4])
monster5_quickness = get_value_by_partial_key(mon_quickness_dict,mon_group_list[4])
if len(mon_group_list) >= 1:
process_data = [
{"type": "player", "quickness": player_quick},
{"type": "monster1", "quickness": monster1_quickness},
]
if len(mon_group_list) >= 2:
process_data = [
{"type": "player", "quickness": player_quick},
{"type": "monster1", "quickness": monster1_quickness},
{"type": "monster2", "quickness": monster2_quickness},
]
if len(mon_group_list) >= 3:
process_data = [
{"type": "player", "quickness": player_quick},
{"type": "monster1", "quickness": monster1_quickness},
{"type": "monster2", "quickness": monster2_quickness},
{"type": "monster3", "quickness": monster3_quickness},
]
"""process_data = sorted([
{"type": "player", "quickness": 5},
{"type": "monster1", "quickness": monster1_quickness},
{"type": "monster2", "quickness": monster2_quickness},
{"type": "monster3", "quickness": monster3_quickness},
], key=lambda item: item["quickness"], reverse=True)"""
execute_index = 0
# モンスター画像を明るくする(ダメージエフェクトのため)
if len(mon_group_list) >= 1:
bright_image1 = bright_image(monster_image1)
if len(mon_group_list) >= 2:
bright_image2 = bright_image(monster_image2)
if len(mon_group_list) >= 3:
bright_image3 = bright_image(monster_image3)
if len(mon_group_list) >= 4:
bright_image4 = bright_image(monster_image4)
if len(mon_group_list) >= 5:
bright_image5 = bright_image(monster_image5)
# 黒い画像(ダメージエフェクトのため)
m_width, m_height = monster_image1.get_size()
black_image = pygame.Surface((m_width, m_height))#106,107
# モンスターの位置
# 奇数
if len(mon_group_list) == 1 or len(mon_group_list) == 3 or len(mon_group_list) == 5:
if len(mon_group_list) >= 1:
monster_rect1 = monster_image1.get_rect(center = (width * 0.5, height * 0.5))
if len(mon_group_list) >= 2:
monster_rect2 = monster_image2.get_rect(center = (width * 0.3, height * 0.5))
if len(mon_group_list) >= 3:
monster_rect3 = monster_image3.get_rect(center = (width * 0.7, height * 0.5))
if len(mon_group_list) >= 4:
monster_rect4 = monster_image4.get_rect(center = (width * 0.1, height * 0.5))
if len(mon_group_list) >= 5:
monster_rect5 = monster_image5.get_rect(center = (width * 0.9, height * 0.5))
# 偶数
if len(mon_group_list) == 2 or len(mon_group_list) == 4:
if len(mon_group_list) >= 1:
monster_rect10 = monster_image1.get_rect(center = (width * 0.4, height * 0.5))
if len(mon_group_list) >= 2:
monster_rect11 = monster_image2.get_rect(center = (width * 0.6, height * 0.5))
if len(mon_group_list) >= 3:
monster_rect12 = monster_image3.get_rect(center = (width * 0.2, height * 0.5))
if len(mon_group_list) >= 4:
monster_rect13 = monster_image4.get_rect(center = (width * 0.8, height * 0.5))
# 枠点滅
damage_flash = True
flash = 0
# 色
white = (255, 255, 255)
action_number = 0
player_damage = 0
damage = 0
motion_next = 0 # 次のターンに移す
mon1_on = 0
mon2_on = 0
mon3_on = 0
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
# 画面を黒色に塗りつぶし
screen.fill((0, 0, 0))
# モンスターの画像表示
if len(mon_group_list2) == 1 or len(mon_group_list2) == 3 or len(mon_group_list2) == 5:
if len(mon_group_list2) == 1:
if monster1_HP > 0:
screen.blit(monster_image1, (monster_rect1.topleft))
if len(mon_group_list2) == 3:
if monster1_HP > 0:
screen.blit(monster_image1, (monster_rect2.topleft))
if monster2_HP > 0:
screen.blit(monster_image2, (monster_rect1.topleft))
if monster3_HP > 0:
screen.blit(monster_image3, (monster_rect3.topleft))
if len(mon_group_list2) == 5:
if monster1_HP > 0:
screen.blit(monster_image1, (monster_rect4.topleft))
if monster2_HP > 0:
screen.blit(monster_image2, (monster_rect2.topleft))
if monster3_HP > 0:
screen.blit(monster_image3, (monster_rect1.topleft))
if monster4_HP > 0:
screen.blit(monster_image4, (monster_rect3.topleft))
if monster5_HP > 0:
screen.blit(monster_image5, (monster_rect5.topleft))
if len(mon_group_list2) == 2 or len(mon_group_list2) == 4:
if len(mon_group_list2) >= 1:
if monster1_HP > 0:
screen.blit(monster_image1, (monster_rect10.topleft))
if len(mon_group_list2) >= 2:
if monster2_HP > 0:
screen.blit(monster_image2, (monster_rect11.topleft))
if len(mon_group_list2) >= 3:
if monster3_HP > 0:
screen.blit(monster_image3, (monster_rect12.topleft))
if len(mon_group_list2) >= 4:
if monster4_HP > 0:
screen.blit(monster_image4, (monster_rect13.topleft))
# メッセージウィンドゥ
if battle_message:
pygame.draw.rect(screen, white, Rect(20, 320, 600, 140), 3)
# バトルメッセージ
draw_text(screen,message,(40,350),font)
# ステータスウィンドゥ
if damage_flash == True:
pygame.draw.line(screen, white, (20, 58),(118, 58))
pygame.draw.rect(screen, white, (20, 20, 102, 134), 3)
text = font.render("ゆうしゃ", True, white)
screen.blit(text,(35, 35))
text = font.render("H", True, white)
screen.blit(text,(35, 65))
text = font.render(str(player_HP), True, white)
screen.blit(text,(85, 65))
text = font.render("M", True, white)
screen.blit(text,(35, 95))
text = font.render(str(player_MP), True, white)
screen.blit(text,(85, 95))
text = font.render("ゆ: ", True, white)
screen.blit(text,(35, 125))
text = font.render(str(Level), True, white)
screen.blit(text,(85, 125))
# コマンドウィンドゥ
if command:
pygame.draw.line(screen, white, (20, 358),(236, 358))
pygame.draw.rect(screen, white, (20, 320, 220, 140), 3)
text = font.render("ゆうしゃ", True, white)
screen.blit(text,(80, 335))
text = font.render("こうげき", True, white)
screen.blit(text,(50, 370))
text = font.render("まほう", True, white)
screen.blit(text,(50, 400))
text = font.render("ガード", True, white)
screen.blit(text,(50, 430))
text = font.render("アイテム", True, white)
screen.blit(text,(160, 370))
text = font.render("そうび", True, white)
screen.blit(text,(160, 400))
text = font.render("にげる", True, white)
screen.blit(text,(160, 430))
# カーソル
if not chara_sele:
screen.blit(cursor, (cursor_x, cursor_y + item_cursor * arrow_index))
cursor.set_alpha(alpha)
alpha += delta
if alpha <= 120 or alpha >= 255:
delta = -delta
# セレクトウィンドゥ
if select:
pygame.draw.rect(screen, white, (260, 320, 220, 140), 3)
if action_number == 0 or masic_action == 2 and masic_list[arrow_index] == "ダメラ":
if len(mon_group_list) >= 1:
text = font.render(mon_group_list[0], True, white)
screen.blit(text,(290, 330))
if len(mon_group_list) >= 2:
text = font.render(mon_group_list[1], True, white)
screen.blit(text,(290, 355))
if len(mon_group_list) >= 3:
text = font.render(mon_group_list[2], True, white)
screen.blit(text,(290, 380))
if len(mon_group_list) >= 4:
text = font.render(mon_group_list[3], True, white)
screen.blit(text,(290, 405))
if len(mon_group_list) >= 5:
text = font.render(mon_group_list[4], True, white)
screen.blit(text,(290, 430))
if masic_action == 2 and masic_list[arrow_index] == "ホイムン":
# キャラ選択
# カーソル
screen.blit(cursor, (cursor_x, cursor_y + chara_cursor * arrow_index2))
cursor.set_alpha(alpha)
alpha += delta
if alpha <= 120 or alpha >= 255:
delta = -delta
if chara_list:
for i, chara in enumerate(chara_list):
text = font.render(chara, True, (255, 255, 255))
screen.blit(text, (290, 330 + 25 * i))
if action_number == 1 and masic_action <= 1:
# まほうがある場合のみ描画
if masic_list:
for i, masic in enumerate(masic_list):
text = font.render(masic, True, (255, 255, 255))
screen.blit(text, (290, 330 + 25 * i))
else:
text = font.render("まほうなし", True, (255, 255, 255))
screen.blit(text, (90, 150)) # 画面中央に「アイテムなし」を表示
if action_number == 3:
# アイテムがある場合のみ描画
if item_list:
for i, item in enumerate(item_list):
text = font.render(item, True, (255, 255, 255))
screen.blit(text, (290, 330 + 25 * i))
else:
text = font.render("アイテムなし", True, (255, 255, 255))
screen.blit(text, (90, 150)) # 画面中央に「アイテムなし」を表示
# ターン終了したら、コマンドウィンドゥをひらく
if motion == 12:
message = ""
battle_message = False
command = True
select = False
chara_sele = False
motion = 1
defense_cmd = 0
mon_index = 0
select_mon = mon_index_list[0]
mon1_on = 0
mon2_on = 0
mon3_on = 0
num = 0
action_number = 0
masic_on = 0
masic_effect = 0
masic_action = 0
item_cursor = 0
arrow_index = 0
arrow_index2 = 0
if monster1_HP <= 0 and monster2_HP <= 0:
num = 2
elif monster1_HP <= 0:
num = 1
# player_HPがゼロになったら閉じる
if player_HP == 0 and motion == 14:
pygame.display.update()# player_HPがゼロのときにテキスト表示するのに必要
time.sleep(2)
pygame.quit()
sys.exit()
if motion == 13:
message = "そのあと\nゆうしゃのすがたをみたものはいない"
motion = 14
pygame.display.update()
# アイテムエフェクト
if item_effect == 1:
item_effect = 0
message = "しかし、なにもおこらなかった…"
motion = 2
if item_effect == 2 or masic_effect == 2 and masic_on == 1:
motion = 2
if item_effect == 2:
recovery = random.randint(item_parameter_dict[item_list[arrow_index]],int(item_parameter_dict[item_list[arrow_index]] * 1.5))
if player_HP >= Max_HP - recovery:
player_HP = Max_HP
elif player_HP < Max_HP - recovery:
player_HP += recovery
item_effect = 0
del item_list[arrow_index]
if masic_effect == 2:
if player_MP < masic_cost_dict[masic_list[arrow_index]]:
pass
else:
recovery = random.randint(masic_parameter_dict[masic_list[arrow_index]],int(masic_parameter_dict[masic_list[arrow_index]] * 1.5))
if player_HP >= Max_HP - recovery:
player_HP = Max_HP
elif player_HP < Max_HP - recovery:
player_HP += recovery
player_MP -= masic_cost_dict[masic_list[arrow_index]]
execute_index += 1
masic_effect = 0 #これがないと先に進まない。
# まほう(半透明レクト)
if masic_action > 0:
if player_MP >= masic_cost_dict[masic_list[arrow_index]]:
if masic_effect == 1 and masic_on == 0:
screen.blit(damera,(0,0))
pygame.display.update()
time.sleep(0.2)
masic_on = 1
elif masic_effect == 2 and masic_on == 0:
screen.blit(hoimun,(0,0))
pygame.display.update()
time.sleep(0.2)
masic_on = 1
# プレイヤーのダメージエフェクト
if flash > 0:
damage_flash = not damage_flash
flash -= 1
time.sleep(0.15)
if flash == 1:
sound2.play()
motion = 2
# モンスターのダメージエフェクト
if action_number == 0 and motion == 3 and effect == 1 or \
action_number == 1 and motion == 3 and effect == 1 and masic_on == 1:
if len(mon_group_list2) == 1 or len(mon_group_list2) == 3 or len(mon_group_list2) == 5:
if select_mon == 0:
damage_effect(screen, bright_image1, monster_image1, black_image, monster_rect1, sound2)
elif select_mon == 1:
damage_effect(screen, bright_image2, monster_image2, black_image, monster_rect2, sound2)
elif select_mon == 2:
damage_effect(screen, bright_image3, monster_image3, black_image, monster_rect3, sound2)
elif select_mon == 3:
damage_effect(screen, bright_image4, monster_image4, black_image, monster_rect4, sound2)
elif select_mon == 4:
damage_effect(screen, bright_image5, monster_image5, black_image, monster_rect5, sound2)
elif len(mon_group_list2) == 2 or len(mon_group_list2) == 4:
if select_mon == 0:
damage_effect(screen, bright_image1, monster_image1, black_image, monster_rect10, sound2)
elif select_mon == 1:
damage_effect(screen, bright_image2, monster_image2, black_image, monster_rect11, sound2)
elif select_mon == 2:
damage_effect(screen, bright_image3, monster_image3, black_image, monster_rect12, sound2)
elif select_mon == 3:
damage_effect(screen, bright_image4, monster_image4, black_image, monster_rect13, sound2)
effect = 0
execute_index += 1
motion = 2
if len(mon_group_list2) >= 1:
if monster1_HP <= 0 and defeat1 == 0:
motion = 15
if len(mon_group_list2) >= 2:
if monster1_HP <= 0 and defeat1 == 0 or monster2_HP <= 0 and defeat2 == 0:
motion = 15
if len(mon_group_list2) >= 3:
if monster1_HP <= 0 and defeat1 == 0 or monster2_HP <= 0 and defeat2 == 0 or monster3_HP <= 0 and defeat3 == 0:
motion = 15
# モンスターを一匹たおした時の処理
if motion == 11:
if monster_num == 1 and defeat1 == 0:
message = monster1_name + "をたおした!"
elif monster_num == 2:
if monster1_HP <= 0 and defeat1 == 0:
message = monster1_name + "をたおした!"
motion = 2
defeat1 = 1
if sub_motion == 0:
sub_motion = 1
motion = 2
del mon_group_list[0]
del mon_index_list[0]
del process_data[1] # 0はプレイヤー
if mon1_on == 1:
execute_index -= 1
elif monster2_HP <= 0 and defeat2 == 0:
message = monster2_name + "をたおした!"
motion = 2
defeat2 = 1
if monster1_HP > 0:
del mon_group_list[1]
del mon_index_list[1]
del process_data[2]
if mon2_on == 1:
execute_index -= 1
elif monster1_HP <= 0:
del mon_group_list[0]
del mon_index_list[0]
del process_data[1]
if mon2_on == 1:
execute_index -= 1
elif monster_num == 3:
if monster1_HP <= 0 and defeat1 == 0:
message = monster1_name + "をたおした!"
motion = 2
defeat1 = 1
if sub_motion == 0:
if monster2_HP > 0:
sub_motion = 1
elif monster2_HP <= 0:
sub_motion = 2
motion = 2
del mon_group_list[0]
del mon_index_list[0]
del process_data[1]
if mon1_on == 1:
execute_index -= 1
elif monster2_HP <= 0 and defeat2 == 0:
message = monster2_name + "をたおした!"
motion = 2
defeat2 = 1
if sub_motion == 1:
sub_motion = 2
motion = 2
if monster1_HP > 0:
del mon_group_list[1]
del mon_index_list[1]
del process_data[2]
if mon2_on == 1:
execute_index -= 1
elif monster1_HP <= 0:
del mon_group_list[0]
del mon_index_list[0]
del process_data[1]
if mon2_on == 1:
execute_index -= 1
elif monster3_HP <= 0 and defeat3 == 0:
message = monster3_name + "をたおした!"
motion = 2
defeat3 = 1
if monster1_HP > 0 and monster2_HP > 0:
del mon_group_list[2]
del mon_index_list[2]
del process_data[3]
if mon3_on == 1:
execute_index -= 1
elif monster1_HP <= 0 and monster2_HP <= 0:
del mon_group_list[0]
del mon_index_list[0]
del process_data[1]
if mon3_on == 1:
execute_index -= 1
elif monster1_HP <= 0 or monster2_HP <= 0:
del mon_group_list[1]
del mon_index_list[1]
del process_data[2]
if mon3_on == 1:
execute_index -= 1
# モンスターをすべてたおした時の処理
if monster_num == 1:
if monster1_HP <= 0:
bgm(win_bgm)
motion = 5
elif monster_num == 2:
if monster1_HP <= 0 and monster2_HP <= 0:
bgm(win_bgm)
motion = 5
elif monster_num == 3:
if monster1_HP <= 0 and monster2_HP <= 0 and monster3_HP <= 0:
bgm(win_bgm)
motion = 5
# キーハンドラー
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_z:
# ダメージ前のメッセージ
if motion == 2 and masic_action != 1:
process_data2 = sorted(process_data, key=lambda item: item["quickness"], reverse=True)
if player_HP <= 0:
bgm(lose_bgm)
motion = 13
elif execute_index < len(process_data2):
item = process_data2[execute_index]
if item["type"] == "player":
sound.play()
motion = 3
if action_number == 0:
message = f"{player_name}のこうげき"
if action_number == 1:
message = f"{player_name}は、{masic_list[arrow_index]}をつかった"
masic_effect = masic_effect_dict[masic_list[arrow_index]]
if action_number == 2:
message = f"{player_name}は ガードした"
if action_number == 3:
message = f"{player_name}は、{item_list[arrow_index]}をつかった"
if action_number == 5:
message = f"{player_name}は にげだした"
# モンスター1
if item["type"] == "monster1":
if monster1_HP > 0:
sound.play()
message = f"{monster1_name}のこうげき"
motion = 3
mon1_on = 1
# モンスター2
if item["type"] == "monster2":
if monster2_HP > 0:
sound.play()
message = f"{monster2_name}のこうげき"
motion = 3
mon2_on = 1
# モンスター3
if item["type"] == "monster3":
if monster3_HP > 0:
sound.play()
message = f"{monster3_name}のこうげき"
motion = 3
mon3_on = 1
if execute_index == len(process_data2):
execute_index = 0
motion = 4
if motion == 4:
sound.play()
motion = 12
# プレイヤーの攻撃(モンスターに与えるダメージ)
elif motion == 3 and item["type"] == "player":
random_value = random.random()# 1.0~0.0をランダムで生成
if action_number == 0:
if random_value < Decimal("1.00") - rate2:# 0.74以下0.0以上
if len(mon_group_list2) >= 1:
if monster1_HP > 0 and num == 0:
monster1_HP, message = monster_damage(message,damage,monster1_HP,monster1_deffence,monster1_name)
if len(mon_group_list2) >= 2:
if monster2_HP > 0 and num == 1:
monster2_HP, message = monster_damage(message,damage,monster2_HP,monster2_deffence,monster2_name)
if len(mon_group_list2) >= 3:
if monster3_HP > 0 and num == 2:
monster3_HP, message = monster_damage(message,damage,monster3_HP,monster3_deffence,monster3_name)
if len(mon_group_list2) >= 4:
if monster4_HP > 0 and num == 3:
monster4_HP, message = monster_damage(message,damage,monster4_HP,monster4_deffence,monster4_name)
if len(mon_group_list2) >= 5:
if monster5_HP > 0 and num == 4:
monster5_HP, message = monster_damage(message,damage,monster5_HP,monster5_deffence,monster5_name)
effect = 1
else :
message = f"{player_name}のこうげき\nミス\nモンスターに ダメージを与えられない"
motion = 2
execute_index += 1
if action_number == 1:
if masic_effect == 1:
if player_MP < masic_cost_dict[masic_list[arrow_index]]:
message = "MPが足りない…"
motion = 2
execute_index += 1
else:
if len(mon_group_list2) >= 1:
if monster1_HP > 0 and num == 0:
monster1_HP, message = monster_damage(message,masic_parameter_dict[masic_list[arrow_index]],monster1_HP,monster1_deffence,monster1_name)
if len(mon_group_list2) >= 2:
if monster2_HP > 0 and num == 1:
monster2_HP, message = monster_damage(message,masic_parameter_dict[masic_list[arrow_index]],monster2_HP,monster2_deffence,monster2_name)
if len(mon_group_list2) >= 3:
if monster3_HP > 0 and num == 2:
monster3_HP, message = monster_damage(message,masic_parameter_dict[masic_list[arrow_index]],monster3_HP,monster3_deffence,monster3_name)
if len(mon_group_list2) >= 4:
if monster4_HP > 0 and num == 3:
monster4_HP, message = monster_damage(message,masic_parameter_dict[masic_list[arrow_index]],monster4_HP,monster4_deffence,monster4_name)
if len(mon_group_list2) >= 5:
if monster5_HP > 0 and num == 4:
monster5_HP, message = monster_damage(message,masic_parameter_dict[masic_list[arrow_index]],monster5_HP,monster5_deffence,monster5_name)
effect = 1
player_MP -= masic_cost_dict[masic_list[arrow_index]]
if masic_effect == 2:
if player_MP < masic_cost_dict[masic_list[arrow_index]]:
message = "MPが足りない…"
motion = 2
execute_index += 1
if action_number == 2:
motion = 2
execute_index += 1
if action_number == 3:
none_key = item_effect_dict.get(item_list[arrow_index]) # 存在しない場合は None
if none_key is not None:
item_effect = item_effect_dict[item_list[arrow_index]]
if none_key == None:
item_effect = 1
execute_index += 1
if action_number == 5:
sound.play()
pygame.quit()
sys.exit()
# モンスター1の攻撃(プレイヤーに与えるダメージ)
elif motion == 3 and item["type"] == "monster1":
if monster1_HP > 0:
random_value = random.random()# 1.0~0.0をランダムで生成
if random_value < Decimal("1.00") - rate2:# 0.74以下0.0以上
if random_value < Decimal("1.00") - rate:
player_damage = monster1_power // 2 - deffence // 4
player_damage = random.randint(player_damage,int(player_damage * 1.5))
player_damage = int(player_damage)
if defense_cmd == 1:
player_damage = int(player_damage * 0.6)
if player_damage <= 0:
player_damage = 1
player_HP -= player_damage
message = f"{monster1_name}のこうげき\n{player_name}に {str(player_damage)}のダメージ"
damage_flash = False
flash = 3
time.sleep(0.5)
if player_HP <= 0:
player_HP = 0
execute_index += 1
if random_value >= Decimal("1.00") - rate and random_value <= Decimal("1.00") - rate2:
player_damage = monster1_power // 2 - deffence // 4
player_damage = random.randint(player_damage,int(player_damage * 1.5)) * 2
player_damage = int(player_damage)
if defense_cmd == 1:
player_damage = int(player_damage * 0.6)
if player_damage <= 0:
player_damage = 1
player_HP -= player_damage
message = f"{monster1_name}のこうげき\n強烈な一撃\n{player_name}に {str(player_damage)}のダメージ"
damage_flash = False
flash = 3
time.sleep(0.5)
if player_HP <= 0:
player_HP = 0
execute_index += 1
else :
message = f"{monster1_name}のこうげき\nミス\n{player_name}に ダメージを与えられない"
motion = 2
execute_index += 1
# モンスター2の攻撃(プレイヤーに与えるダメージ)
elif motion == 3 and item["type"] == "monster2":
if monster2_HP > 0:
random_value = random.random()# 1.0~0.0をランダムで生成
if random_value < Decimal("1.00") - rate2:# 0.74以下0.0以上
if random_value < Decimal("1.00") - rate:
player_damage = monster1_power // 2 - deffence // 4
player_damage = random.randint(player_damage,int(player_damage * 1.5))
player_damage = int(player_damage)
if defense_cmd == 1:
player_damage = int(player_damage * 0.6)
if player_damage <= 0:
player_damage = 1
player_HP -= player_damage
message = f"{monster2_name}のこうげき\n{player_name}に {str(player_damage)}のダメージ"
damage_flash = False
flash = 3
time.sleep(0.5)
if player_HP <= 0:
player_HP = 0
execute_index += 1
if random_value >= Decimal("1.00") - rate and random_value <= Decimal("1.00") - rate2:
player_damage = monster1_power // 2 - deffence // 4
player_damage = random.randint(player_damage,int(player_damage * 1.5)) * 2
player_damage = int(player_damage)
if defense_cmd == 1:
player_damage = int(player_damage * 0.6)
if player_damage <= 0:
player_damage = 1
player_HP -= player_damage
message = f"{monster2_name}のこうげき\n強烈な一撃\n{player_name}に {str(player_damage)}のダメージ"
damage_flash = False
flash = 3
time.sleep(0.5)
if player_HP <= 0:
player_HP = 0
execute_index += 1
else :
message = f"{monster2_name}のこうげき\nミス\n{player_name}に ダメージを与えられない"
motion = 2
execute_index += 1
# モンスター3の攻撃(プレイヤーに与えるダメージ)
elif motion == 3 and item["type"] == "monster3":
if monster3_HP > 0:
random_value = random.random()# 1.0~0.0をランダムで生成
if random_value < Decimal("1.00") - rate2:# 0.74以下0.0以上
if random_value < Decimal("1.00") - rate:
player_damage = monster1_power // 2 - deffence // 4
player_damage = random.randint(player_damage,int(player_damage * 1.5))
player_damage = int(player_damage)
if defense_cmd == 1:
player_damage = int(player_damage * 0.6)
if player_damage <= 0:
player_damage = 1
player_HP -= player_damage
message = f"{monster3_name}のこうげき\n{player_name}に {str(player_damage)}のダメージ"
damage_flash = False
flash = 3
time.sleep(0.5)
if player_HP <= 0:
player_HP = 0
execute_index += 1
if random_value >= Decimal("1.00") - rate and random_value <= Decimal("1.00") - rate2:
player_damage = monster1_power // 2 - deffence // 4
player_damage = random.randint(player_damage,int(player_damage * 1.5)) * 2
player_damage = int(player_damage)
if defense_cmd == 1:
player_damage = int(player_damage * 0.6)
if player_damage <= 0:
player_damage = 1
player_HP -= player_damage
message = f"{monster3_name}のこうげき\n強烈な一撃\n{player_name}に {str(player_damage)}のダメージ"
damage_flash = False
flash = 3
time.sleep(0.5)
if player_HP <= 0:
player_HP = 0
execute_index += 1
else :
message = f"{monster3_name}のこうげき\nミス\n{player_name}に ダメージを与えられない"
motion = 2
execute_index += 1
# セレクトウィンドゥをひらく
elif motion == 1 and select == False and action_number == 0 or \
motion == 1 and select == False and action_number == 1 or \
motion == 1 and select == False and action_number == 3 or \
masic_action == 1:
sound.play()
select = True
cursor_x = 270
cursor_y = 328
if action_number == 1:
masic_action = min(2,masic_action + 1)
if masic_action == 2 and masic_list[arrow_index] == "ホイムン":
chara_sele = True
#「たたかう」
elif motion == 1 and action_number == 0:
sound.play()
command = False
battle_message = True
cursor_x = 30
cursor_y = 368
motion = 2
#「まほう」
elif motion == 1 and action_number == 1:
sound.play()
command = False
battle_message = True
cursor_x = 30
cursor_y = 368
motion = 2
#「ガード」
elif motion == 1 and action_number == 2:
sound.play()
defense_cmd = 1
command = False
battle_message = True
cursor_x = 30
cursor_y = 368
motion = 2
#「アイテム」
elif motion == 1 and action_number == 3:
sound.play()
command = False
battle_message = True
cursor_x = 30
cursor_y = 368
motion = 2
#「にげる」
elif action_number == 5:
sound.play()
motion = 2
command = False
battle_message = True
# あらわれた!を消してコマンドウィンドゥをひらく
elif motion == 0:
sound.play()
motion = 1
message = ""
battle_message = False
command = True
# 経験値とお金
elif motion == 5:
sound.play()
if monster_num == 1:
mon_EXP = get_value_by_partial_key(mon_EXP_dict,mon_group_list2[0])
mon_Gold = get_value_by_partial_key(mon_Gold_dict,mon_group_list2[0])
message = f"けいけんち {mon_EXP}かくとく\n{mon_Gold}ゴールドを てにいれた"
Exp += mon_EXP
Gold += mon_Gold
elif monster_num == 2:
mon_EXP = get_value_by_partial_key(mon_EXP_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[1])
mon_Gold = get_value_by_partial_key(mon_Gold_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[1])
message = f"けいけんち {mon_EXP}かくとく\n{mon_Gold}ゴールドを てにいれた"
Exp += mon_EXP
Gold += mon_Gold
elif monster_num == 3:
mon_EXP = get_value_by_partial_key(mon_EXP_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[1]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[2])
mon_Gold = get_value_by_partial_key(mon_Gold_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[1]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[2])
message = f"けいけんち {mon_EXP}かくとく\n{mon_Gold}ゴールドを てにいれた"
Exp += mon_EXP
Gold += mon_Gold
elif monster_num == 4:
mon_EXP = get_value_by_partial_key(mon_EXP_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[1]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[2]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[3])
mon_Gold = get_value_by_partial_key(mon_Gold_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[1]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[2]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[3])
message = f"けいけんち {mon_EXP}かくとく\n{mon_Gold}ゴールドを てにいれた"
Exp += mon_EXP
Gold += mon_Gold
elif monster_num == 5:
mon_EXP = get_value_by_partial_key(mon_EXP_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[1]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[2]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[3]) + get_value_by_partial_key(mon_EXP_dict,mon_group_list2[4])
mon_Gold = get_value_by_partial_key(mon_Gold_dict,mon_group_list2[0]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[1]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[2]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[3]) + get_value_by_partial_key(mon_Gold_dict,mon_group_list2[4])
message = f"けいけんち {mon_EXP}かくとく\n{mon_Gold}ゴールドを てにいれた"
Exp += mon_EXP
Gold += mon_Gold
if Level == 1 and Exp >= 4 or Level == 2 and Exp >= 8:
motion = 8
else:
motion = 6
# 戦闘に勝利したら画面を閉じる
elif motion == 6:
sound.play()
motion = 7
pygame.quit()
sys.exit()
# レベルアップ
elif motion == 8:
pygame.mixer.music.pause()
sound3.play()
message = "レベルが上がった"
if Level == 1:
motion = 9
elif Level == 2:
motion = 10
elif motion == 9:
time.sleep(1)
pygame.mixer.music.unpause()
sound.play()
message = "さいだいHPが 4ポイント上がった!\nちからが 3ポイント 上がった!\nすばやさが 2ポイント 上がった!"
Max_HP += 4
power += 3
player_quick += 2
Level += 1
motion = 6
attack_power = power + arms
elif motion == 10:
time.sleep(1)
pygame.mixer.music.unpause()
sound.play()
message = "さいだいHPが 6ポイント上がった!\nさいだいMPが 5ポイント上がった!\nちからが 1ポイント上がった!\nすばやさが 1ポイント上がった!\nホイムンをおぼえた"
Max_HP += 4
power += 3
player_quick += 2
Level += 1
motion = 6
attack_power = power + arms
# モンスターをたおした時(ワンテンポずらす)
elif motion == 15:
sound.play()
motion = 11
#キーハンドラー(矢印)
if command:
if event.key == K_DOWN:
# コマンド
if cursor_y != 428 and select == False and motion != 0:
cursor_y += 30
action_number += 1
sound.play()
# モンスター
elif len(mon_group_list) - 1 != mon_index and select == True and action_number == 0 or \
len(mon_group_list) - 1 != mon_index and masic_action == 2 and not chara_sele:
cursor_y += 25
sound.play()
if monster2_HP > 0:
num += 1
if monster2_HP <= 0:
num += 2
mon_index += 1
select_mon = mon_index_list[mon_index]
# キャラ
elif chara_list and action_number == 1 and masic_action == 2 and chara_sele:
chara_cursor = 25
arrow_index2 = (arrow_index2 + 1) % len(chara_list) # 上へ移動
# まほう
elif masic_list and action_number == 1 and masic_action != 2:
item_cursor = 25
arrow_index = (arrow_index + 1) % len(masic_list) # 上へ移動
# アイテム
elif item_list and action_number == 3:
item_cursor = 25
arrow_index = (arrow_index + 1) % len(item_list) # 上へ移動
elif event.key == K_UP:
# コマンド
if cursor_y != 368 and select == False and motion != 0:
cursor_y -= 30
action_number -= 1
sound.play()
# モンスター
elif cursor_y != 328 and select == True or cursor_y != 328 and masic_action == 2:
if masic_list[arrow_index] != "ホイムン":
cursor_y -= 25
sound.play()
if monster2_HP > 0:
num -= 1
if monster2_HP <= 0:
num -= 2
mon_index -= 1
select_mon = mon_index_list[mon_index]
# キャラ
elif chara_list and action_number == 1 and masic_action == 2:
item_cursor = 25
arrow_index2 = (arrow_index2 - 1) % len(chara_list) # 上へ移動
# まほう
elif masic_list and action_number == 1 and masic_action != 2:
item_cursor = 25
arrow_index = (arrow_index - 1) % len(masic_list) # 下へ移動
# アイテム
elif item_list and action_number == 3:
item_cursor = 25
arrow_index = (arrow_index - 1) % len(item_list) # 下へ移動
elif event.key == K_LEFT:
# コマンド
if cursor_x != 30 and select == False and motion != 0:
cursor_x -= 110
action_number -= 3
sound.play()
elif event.key == K_RIGHT:
# コマンド
if cursor_x != 140 and select == False and motion != 0:
cursor_x += 110
action_number += 3
sound.play()