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?

バトル画面

Last updated at Posted at 2024-11-03

記事全体の目次へ移動

バトル画面

ここをクリックしてください
import pygame
from pygame import *
import sys
import time
import random
import numpy as np


pygame.init()
screen=pygame.display.set_mode((640,480))
pygame.display.set_caption('battle')


#初期ステータス
Max_HP = 8
player_HP = 8
Max_MP = 0
player_MP = 0
power = 13
quick = 1
arms = 0
attack_power = power + arms
deffence = 0

Gold = 200
Level = 1
Exp = 0


#ここにフォントのパスを貼り付ける
font = pygame.font.Font(, 18)
#ここにカーソル画像のパスを貼り付ける
cursor = pygame.image.load()
#ここにモンスター画像のパスを貼り付ける
slyme_image = pygame.image.load()
#ここにモンスター画像のパスを貼り付ける
wolf_image = pygame.image.load()
#ここにモンスター画像のパスを貼り付ける
chikin_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 = 


# テキストを改行して描画する関数
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()

#カーソル
cursor_x = 30
cursor_y = 368
alpha = 220
delta = 5
clock = pygame.time.Clock()

battle_message = True
command = False
effect = 0

monster_attack = 0
motion = 0
attack = 0 
encount = 0

#モンスター
monster_list = [slyme_image, wolf_image ,chikin_image]
random.shuffle(monster_list)
monster_image = monster_list[0]
monster_name = ""
if monster_list[0] == slyme_image:
    message = "スライムが あらわれた!"
    monster_name = "スライム"
    monster_HP = 4
    monster_power = 6
    monster_deffence = 4
elif monster_list[0] == wolf_image:
    message = "ウルフがあらわれた!"
    monster_name = "ウルフ"
    monster_HP = 6
    monster_power = 10
    monster_deffence = 4
elif monster_list[0] == chikin_image:
    message = "バードがあらわれた!"
    monster_name = "バード"
    monster_HP = 8
    monster_power = 6
    monster_deffence = 8

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


# 画像を読み込む
if monster_list[0] == slyme_image:
    image = slyme_image
elif monster_list[0] == wolf_image:
    image = wolf_image
elif monster_list[0] == chikin_image:
    image = chikin_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)

m_width, m_height = monster_image.get_size()
black_image = pygame.Surface((m_width, m_height))
    
#ダメージエフェクト
def damage_effect():
    screen.blit(bright_image_surface, (220, 174))
    pygame.display.update()
    time.sleep(0.1)
    screen.blit(monster_image, (220, 174))
    pygame.display.update()
    time.sleep(0.1)
    screen.blit(black_image, (220, 174))
    pygame.display.update()
    time.sleep(0.1)
    screen.blit(monster_image, (220, 174))
    pygame.display.update()
    sound2.play()
         
#枠点滅
damage_flash = True
flash = 0

# 色
white = (255, 255, 255)

action_number = 0 
player_damage = 0

damage = 0

running = True

while running:
    clock.tick(60)
    # 画面を黒色に塗りつぶし
    screen.fill((0, 0, 0))
    #モンスターの画像表示
    if monster_HP > 0: 
        screen.blit(monster_image, (220, 174))
    draw_text(screen,message,(40,350),font)
    #メッセージウィンドゥ
    if battle_message:
        pygame.draw.rect(screen, white, Rect(20, 320, 600, 140), 3)
    #ステータスウィンドゥ
    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))
        #カーソル
        screen.blit(cursor, (cursor_x, cursor_y))
        cursor.set_alpha(alpha)
        alpha += delta
        if alpha <= 120 or alpha >= 255:
            delta = -delta

    if motion == 12:
        message = ""
        battle_message = False
        command = True
        motion = 1
    if player_HP == 0 and motion == 14:
        #pygame.display.update()
        time.sleep(2)
        pygame.quit()
        sys.exit()
    #HPゼロになったら閉じる
    if motion == 13:
        message = "そのあと\nゆうしゃのすがたをみたものはいない"
        motion = 14

    pygame.display.update()
    if flash > 0:
        damage_flash = not damage_flash
        flash -= 1
        time.sleep(0.15)
        if flash == 1:
            sound2.play()
    elif action_number == 0 and motion == 2 and attack == 1 and effect == 1:
        damage_effect()
        effect = 0
    elif motion == 11:
        damage_effect()
        monster_HP = 0
        sound2.play()
        message = monster_name + "をたおした!"
        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 action_number == 5:
                    sound.play()
                    pygame.quit()
                    sys.exit()

                
                elif motion == 2 and attack != 1:
                    sound.play()
                    damage = attack_power // 2 - monster_deffence // 4
                    if damage <= 0:
                        damage = 1
                    
                    monster_HP -= damage
                    damageY = 200
                    if monster_HP <= 0:
                        motion = 11
                        
                    else:
                        message = monster_name + "に " + str(damage) + "の ダメージ"
                        time.sleep(0.5)
                        attack = 1
                        effect = 1
                # 「たたかう」
                elif motion == 1 and action_number == 0:
                    sound.play()
                    command = False
                    battle_message = True
                    message = "プレイヤーのこうげき"
                    motion = 2
                elif motion == 0:
                    sound.play()
                    motion = 1
                    message = ""
                    battle_message = False
                    command = True

                elif attack == 1 and motion == 2:
                    sound.play()                        
                    message = monster_name + "のこうげき"
                    motion = 3

                elif attack == 1 and motion == 3:
                    if monster_HP > 0:                            
                     player_damage = monster_power // 2 - deffence // 4
                     if player_damage <= 0:
                         player_damage = 1
                     player_HP -= player_damage
                                                  
                    message = "プレイヤーに " +str(player_damage) + "のダメージ"
                    motion = 4
                    monster_attack = 1
                    damage_flash = False
                    flash = 3
                    time.sleep(0.5)
                    if player_HP <= 0:
                     player_HP = 0
                
                elif attack == 1 and motion == 4:
                    sound.play()
                    if player_HP <= 0:
                        bgm(lose_bgm)
                        motion = 13
                    else:
                        attack = 0
                        motion = 12
                elif motion == 5:
                    sound.play()
                    if monster_image == slyme_image:
                        message = "けいけんち 1かくとく\n4ゴールドを てにいれた"
                        Exp += 1
                        Gold += 4
                    elif monster_image == wolf_image:
                        message = "けいけんち 2かくとく\n5ゴールドを てにいれた"
                        Exp += 2
                        Gold += 5
                    elif monster_image == chikin_image:
                        message = "けいけんち 4かくとく\n10ゴールドを てにいれた"
                        Exp += 4
                        Gold += 10
                    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
                    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
                    quick += 2
                    Level += 1
                    motion = 6
                    attack_power = power + arms
                    
                      

            
            if event.key == K_DOWN:
                if cursor_y != 428:
                    cursor_y += 30
                    action_number += 1
                    sound.play()
            elif event.key == K_UP:
                if cursor_y != 368:
                    cursor_y -= 30
                    action_number -= 1
                    sound.play()
            elif event.key == K_LEFT:
                if cursor_x != 30:
                    cursor_x -= 110
                    action_number -= 3
                    sound.play()
            elif event.key == K_RIGHT:
                if cursor_x != 140:
                    cursor_x += 110
                    action_number += 3
                    sound.play()


#
ここをクリックしてください


#
ここをクリックしてください


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?