メニュー画面

[記事全体の目次へ移動](https://qiita.com/hijpa/items/056acaa13e935590fdc2)
 
# メニュー画面

Xキー：　メニュー画面。取り消し。
Zキー：　決定。
方向キー：　キャラの移動。カーソルの移動。

<details><summary>ここをクリックしてください</summary>

```python:
import pygame
from pygame.locals import *
import sys


def split_image(image):
    """32x128のキャラクターイメージを32x32の4枚のイメージに分割
    分割したイメージを格納したリストを返す"""
    imageList = []
    GS = 32
    for i in range(0, 128, GS):
        for j in range(0, 128, GS):
            surface = pygame.Surface((GS, GS))
            surface.blit(image, (0, 0), (j, i, GS, GS))
            surface.set_colorkey(surface.get_at((0, 0)), RLEACCEL)
            surface.convert()
            imageList.append(surface)
    return imageList

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('menu')
menu = False
item = False
magic = False
status = False
equip = False
right = False
left = False
head = False
body  = False
right_c = False
left_c = False
head_c = False
body_c = False

#初期ステータス
Max_HP = 8
player_HP = 8

Max_MP = 0
player_MP = 0
power = 3
quick = 1
arms = 0
attack_power = power + arms
defense = 0

Gold = 200
Level = 1
Exp = 0

item_list = ["やくそう1","やくそう2","やくそう3","やくそう4","やくそう5","やくそう6","やくそう7","やくそう８","やくそう９"]
magic_list = ["ホイムン","メーラ","ラリホット","キアリリ","ヒャドイ","マフウジ","タイマツ","リレミィ","ルンラ",]
right_list = ["銀の剣","黄金剣"]
left_list = ["銀の盾","金の盾"]
head_list = ["銀の兜","金の兜"]
body_list = ["銀の鎧","金の鎧"]
right_dict = {"銀の剣": 3,"黄金剣": 5}
left_dict = {"銀の盾": 3,"金の盾": 5}
head_dict = {"銀の兜": 3,"金の兜": 5}
body_dict = {"銀の鎧": 3,"金の鎧": 5}


x, y = 2, 2
GS = 32
DOWN, LEFT, RIGHT, UP = 0, 1, 2, 3
direction = DOWN
animcycle = 24  
frame = 0
clock = pygame.time.Clock()
#ここにキャラクターのパスを貼り付ける
playerImgList = split_image(pygame.image.load(パス))
#ここにフォントのパスを貼り付ける
font = pygame.font.Font(パス, 20)
#ここにカーソル画像のパスを貼り付ける
cursor = pygame.image.load(パス)
cursor_x = 465
cursor_y = 56
black = (0, 0, 0)
white = (255, 255, 255)
alpha = 220
delta = 5
a = 0
cmd = 0
num = 0
equip_num = 0

item_index1 = 0
item_index2 = 1
item_index3 = 2
item_index4 = 3
item_index5 = 4
item_index6 = 5
item_index7 = 6

magic_index1 = 0
magic_index2 = 1
magic_index3 = 2
magic_index4 = 3
magic_index5 = 4
magic_index6 = 5
magic_index7 = 6

right_index = 0
left_index = 0
head_index = 0
body_index = 0
text_right = ""
text_left = ""
text_head = ""
text_body = ""
on_right_index = 0
on_right_name = ""
on_right = False
on_left_index = 0
on_left_name = ""
on_left = False
on_head_index = 0
on_head_name = ""
on_head = False
on_body_index = 0
on_body_name = ""
on_body = False

while True:
    clock.tick(60)
    frame += 1
    screen.fill((128, 128, 128))
    
    # 経過フレーム数に応じて表示する画像を変える
    player = playerImgList[int(direction * 4 + frame / animcycle % 4)]
    screen.blit(player, (x * GS, y * GS))
    attack_power = power + arms

    if menu:
        pygame.draw.rect(screen, black, (460, 48, 150, 165))
        pygame.draw.rect(screen, white, (460, 48, 150, 165), 3)
        text1 = font.render("アイテム", True, white)
        text2 = font.render("まほう", True, white)
        text3 = font.render("ステータス", True, white)
        text4 = font.render("そうび", True, white)
        text5 = font.render("セーブ", True, white)
        screen.blit(text1, (485, 58))
        screen.blit(text2, (485, 88))
        screen.blit(text3, (485, 118))
        screen.blit(text4, (485, 148))
        screen.blit(text5, (485, 178))
        if item:
            pygame.draw.rect(screen, black, (190, 120, 260, 240))
            pygame.draw.rect(screen, white, (190, 120, 260, 240),3)
            if len(item_list) >= 1:
                text = font.render(item_list[item_index1], True, white)      
                screen.blit(text, (210, 140))
            if len(item_list) >= 2:
                text = font.render(item_list[item_index2], True, white)      
                screen.blit(text, (210, 170))
            if len(item_list) >= 3:
                text = font.render(item_list[item_index3], True, white)      
                screen.blit(text, (210, 200))
            if len(item_list) >= 4:
                text = font.render(item_list[item_index4], True, white)      
                screen.blit(text, (210, 230))
            if len(item_list) >= 5:
                text = font.render(item_list[item_index5], True, white)      
                screen.blit(text, (210, 260))
            if len(item_list) >= 6:
                text = font.render(item_list[item_index6], True, white)      
                screen.blit(text, (210, 290))
            if len(item_list) >= 7:
                text = font.render(item_list[item_index7], True, white)      
                screen.blit(text, (210, 320))
        if magic:
            pygame.draw.rect(screen, black, (320, 120, 130, 240))
            pygame.draw.rect(screen, white, (320, 120, 130, 240),3)
            if len(magic_list) >= 1:
                text = font.render(magic_list[magic_index1], True, white)      
                screen.blit(text, (340, 140))
            if len(magic_list) >= 1:
                text = font.render(magic_list[magic_index2], True, white)      
                screen.blit(text, (340, 170))
            if len(magic_list) >= 1:
                text = font.render(magic_list[magic_index3], True, white)      
                screen.blit(text, (340, 200))
            if len(magic_list) >= 1:
                text = font.render(magic_list[magic_index4], True, white)      
                screen.blit(text, (340, 230))
            if len(magic_list) >= 1:
                text = font.render(magic_list[magic_index5], True, white)      
                screen.blit(text, (340, 260))
            if len(magic_list) >= 1:
                text = font.render(magic_list[magic_index6], True, white)      
                screen.blit(text, (340, 290))
            if len(magic_list) >= 1:
                text = font.render(magic_list[magic_index7], True, white)      
                screen.blit(text, (340, 320))
        if status:
            pygame.draw.rect(screen, black, (190, 60, 260, 376))
            pygame.draw.rect(screen, white, (190, 60, 260, 376),3)
            name_text = font.render("　ゆうしゃ", True, white)
            screen.blit(name_text, (213, 73))
            lebel_text = font.render("レベル　" + str(Level), True, white)
            screen.blit(lebel_text, (213, 103))
            HP_text = font.render("HP 　 "+str(player_HP), True, white)
            screen.blit(HP_text, (213, 133))
            MP_text = font.render("MP　 " + str(player_MP), True, white)
            screen.blit(MP_text, (213, 163))
            Gold_text = font.render("　G　"+str(Gold), True, white)
            screen.blit(Gold_text, (213, 193))
            Exp_text = font.render(" Exp　" + str(Exp), True, white)
            screen.blit(Exp_text, (213, 223))
            power_text = font.render("　　　　ちから：" + str(power), True, white)
            screen.blit(power_text, (213, 253))
            quick_text = font.render("　　　すばやさ：" + str(quick), True, white)
            screen.blit(quick_text, (213, 283))
            Max_HP_text = font.render("　 さいだいHP ：" + str(Max_HP), True, white)
            screen.blit(Max_HP_text, (213, 313))
            Max_MP_text = font.render("　 さいだいMP ：" + str(Max_MP), True, white)
            screen.blit(Max_MP_text, (213, 343))
            attack_power_text = font.render("こうげきりょく：" + str(attack_power), True, white)
            screen.blit(attack_power_text, (213, 373))
            defense_text = font.render("　しゅびりょく：" + str(defense), True, white)
            screen.blit(defense_text, (213, 403))
            _text = font.render("", True, white)
            screen.blit(_text, (28, 318))
        if equip:
            pygame.draw.rect(screen, black, (190, 120, 260, 140))
            pygame.draw.rect(screen, white, (190, 120, 260, 140),3)
            text = font.render("　みぎて:　" + text_right, True, white)      
            screen.blit(text, (210, 140))
            text = font.render("ひだりて:　" + text_left, True, white)      
            screen.blit(text, (210, 170))
            text = font.render("　あたま:　" + text_head, True, white)      
            screen.blit(text, (210, 200))
            text = font.render("　からだ:　" + text_body, True, white)      
            screen.blit(text, (210, 230))
            if right:
                pygame.draw.rect(screen, black, (250, 260, 260, 60))
                pygame.draw.rect(screen, white, (250, 260, 260, 60),3)
                text = font.render(right_list[right_index], True, white)      
                screen.blit(text, (270, 280))
            if left:
                pygame.draw.rect(screen, black, (250, 260, 260, 60))
                pygame.draw.rect(screen, white, (250, 260, 260, 60),3)
                text = font.render(left_list[left_index], True, white)      
                screen.blit(text, (270, 280))
            if head:
                pygame.draw.rect(screen, black, (250, 260, 260, 60))
                pygame.draw.rect(screen, white, (250, 260, 260, 60),3)
                text = font.render(head_list[head_index], True, white)      
                screen.blit(text, (270, 280))
            if body:
                pygame.draw.rect(screen, black, (250, 260, 260, 60))
                pygame.draw.rect(screen, white, (250, 260, 260, 60),3)
                text = font.render(body_list[body_index], True, white)      
                screen.blit(text, (270, 280))
        screen.blit(cursor, (cursor_x, cursor_y))
        cursor.set_alpha(alpha)
        alpha += delta
        if alpha <= 120 or alpha >= 255:
            delta = -delta
        
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if not menu:
                if event.key == K_DOWN:
                    direction = DOWN
                    y += 1
                elif event.key == K_UP:
                    direction = UP
                    y -= 1
                elif event.key == K_RIGHT:
                    direction = RIGHT
                    x += 1
                elif event.key == K_LEFT:
                    direction = LEFT
                    x -= 1
                elif event.key == K_x:
                    menu = True
            elif menu:
                if event.key == K_x:
                    if menu and equip and right:
                        right = False
                        cursor_x = 190
                        cursor_y = 138
                        right_index = 0
                        a = 2
                    elif menu and equip and left:
                        left = False
                        cursor_x = 190
                        cursor_y = 168
                        left_index = 0
                        a = 2
                    elif menu and equip and head:
                        head = False
                        cursor_x = 190
                        cursor_y = 198
                        head_index = 0
                        a = 2
                    elif menu and equip and body:
                        body = False
                        cursor_x = 190
                        cursor_y = 228
                        body_index = 0
                        a = 2
                    elif menu and item:
                        item = False
                        num = 0
                        cursor_x = 465
                        cursor_y = 56
                        item_index1 = 0
                        item_index2 = 1
                        item_index3 = 2
                        item_index4 = 3
                        item_index5 = 4
                        item_index6 = 5
                        item_index7 = 6
                    elif menu and magic:
                        magic = False
                        num = 0
                        cursor_x = 465
                        cursor_y = 86
                        magic_index1 = 0
                        magic_index2 = 1
                        magic_index3 = 2
                        magic_index4 = 3
                        magic_index5 = 4
                        magic_index6 = 5
                        magic_index7 = 6
                    elif menu and status:
                        status = False
                    elif menu and equip:
                        equip = False
                        cursor_x = 465
                        cursor_y = 146
                        equip_num = 0
                        a = 0
                    elif menu:
                        menu = False
                        cmd = 0
                        cursor_x = 465
                        cursor_y = 56
                if event.key == K_z:
                #アイテムウィンドゥをひらく
                    if cmd == 0:
                        item = True
                        cursor_x = 190
                        cursor_y = 138
                #まほうウィンドゥをひらく
                    if cmd == 1:
                        magic = True
                        cursor_x = 320
                        cursor_y = 138
                #ステータスウィンドゥをひらく
                    if cmd == 2:
                        status = True
                #そうびウィンドゥをひらく
                    if cmd == 3:
                        if a == 1:
                            a += 1
                        if equip == False:
                            cursor_x = 190
                            cursor_y = 138
                            a += 1
                        equip = True
                    if equip_num == 0:
                        if a == 2 or a == 3 or a == 4:
                            if a == 3:
                                a += 1
                            if right == False:
                                cursor_x = 250
                                cursor_y = 278
                                a += 1
                            right = True
                                #未装備
                            if a == 4:
                                if right_list[right_index] != "(装備を外す)" and on_right == False:
                                    text_right = right_list[right_index]
                                    arms += right_dict[right_list[right_index]]
                                    on_right_index = right_index
                                    on_right_name = right_list[right_index]
                                    right_list[right_index] = "(装備を外す)"
                                    on_right = True
                                elif right_list[right_index] != "(装備を外す)" and on_right == True:
                                    right_list[on_right_index] = on_right_name
                                    arms -= right_dict[on_right_name]
                                    text_right = right_list[right_index]
                                    arms += right_dict[right_list[right_index]]
                                    on_right_index = right_index
                                    on_right_name = right_list[right_index]
                                    right_list[right_index] = "(装備を外す)"
                                    on_right = True
                                    
                                elif right_list[right_index] == "(装備を外す)":
                                    right_list[right_index] = text_right
                                    arms -= right_dict[right_list[right_index]]
                                    text_right = ""
                                    on_right = False
                         
                            
                            
                    elif equip_num == 1:
                        if a == 2 or a == 3 or a == 4:
                            if a == 3:
                                a += 1
                                
                            if left == False:
                                cursor_x = 250
                                cursor_y = 278
                                a += 1
                            left = True
                            
                                #未装備
                            if a == 4:
                                if left_list[left_index] != "(装備を外す)" and on_left == False:
                                    text_left = left_list[left_index]
                                    defense += left_dict[left_list[left_index]]
                                    on_left_index = left_index
                                    on_left_name = left_list[left_index]
                                    left_list[left_index] = "(装備を外す)"
                                    on_left = True
                                elif left_list[left_index] != "(装備を外す)" and on_left == True:
                                    left_list[on_left_index] = on_left_name
                                    defense -= left_dict[on_left_name]
                                    text_left = left_list[left_index]
                                    defense += left_dict[left_list[left_index]]
                                    on_left_index = left_index
                                    on_left_name = left_list[left_index]
                                    left_list[left_index] = "(装備を外す)"
                                    on_left = True
                                    
                                elif left_list[left_index] == "(装備を外す)":
                                    left_list[left_index] = text_left
                                    defense -= left_dict[left_list[left_index]]
                                    text_left = ""
                                    on_left = False
                         
                    elif equip_num == 2:
                        if a == 2 or a == 3 or a == 4:
                            if a == 3:
                                a += 1
                            if head == False:
                                cursor_x = 250
                                cursor_y = 278
                                a += 1
                            head = True
                                #未装備
                            if a == 4:
                                if head_list[head_index] != "(装備を外す)" and on_head == False:
                                    text_head = head_list[head_index]
                                    defense += head_dict[head_list[head_index]]
                                    on_head_index = head_index
                                    on_head_name = head_list[head_index]
                                    head_list[head_index] = "(装備を外す)"
                                    on_head = True
                                elif head_list[head_index] != "(装備を外す)" and on_head == True:
                                    head_list[on_head_index] = on_head_name
                                    defense -= head_dict[on_head_name]
                                    text_head = head_list[head_index]
                                    defense += head_dict[head_list[head_index]]
                                    on_head_index = head_index
                                    on_head_name = head_list[head_index]
                                    head_list[head_index] = "(装備を外す)"
                                    on_head = True
                                    
                                elif head_list[head_index] == "(装備を外す)":
                                    head_list[head_index] = text_head
                                    defense -= head_dict[head_list[head_index]]
                                    text_head = ""
                                    on_head = False
                    elif equip_num == 3:
                        if a == 2 or a == 3 or a == 4:
                            if a == 3:
                                a += 1
                            if body == False:
                                cursor_x = 250
                                cursor_y = 278
                                a += 1
                            body = True
                                #未装備
                            if a == 4:
                                if body_list[body_index] != "(装備を外す)" and on_body == False:
                                    text_body = body_list[body_index]
                                    defense += body_dict[body_list[body_index]]
                                    on_body_index = body_index
                                    on_body_name = body_list[body_index]
                                    body_list[body_index] = "(装備を外す)"
                                    on_body = True
                                elif body_list[body_index] != "(装備を外す)" and on_body == True:
                                    body_list[on_body_index] = on_body_name
                                    defense -= body_dict[on_body_name]
                                    text_body = body_list[body_index]
                                    defense += body_dict[body_list[body_index]]
                                    on_body_index = body_index
                                    on_body_name = body_list[body_index]
                                    body_list[body_index] = "(装備を外す)"
                                    on_body = True
                                    
                                elif body_list[body_index] == "(装備を外す)":
                                    body_list[body_index] = text_body
                                    defense -= body_dict[body_list[body_index]]
                                    text_body = ""
                                    on_body = False
                if event.key == K_DOWN:
                #メニュー
                    if item == False and  magic == False and status == False and equip == False and right == False and left == False and head == False and body == False:
                        if cursor_y != 176:
                            cmd += 1
                            cursor_y += 30
                #アイテム
                    if item == True:
                        if cursor_y != 318 and num < len(item_list) - 1:
                            num += 1
                            cursor_y += 30
                        elif cursor_y == 318 and num < len(item_list) - 1:
                            num += 1
                            item_index1 += 1
                            item_index2 += 1
                            item_index3 += 1
                            item_index4 += 1
                            item_index5 += 1
                            item_index6 += 1
                            item_index7 += 1
                #まほう
                    if magic == True:
                        if cursor_y != 318 and num < len(magic_list) - 1:
                            num += 1
                            cursor_y += 30
                        elif cursor_y == 318 and num < len(magic_list) - 1:
                            num += 1
                            magic_index1 += 1
                            magic_index2 += 1
                            magic_index3 += 1
                            magic_index4 += 1
                            magic_index5 += 1
                            magic_index6 += 1
                            magic_index7 += 1
                #そうび
                    #全部位
                    elif equip == True:
                        if cursor_y != 228 and right == False and left == False and head == False and body == False:
                            equip_num += 1
                            cursor_y += 30
                    #みぎて
                        elif right and right_index < len(right_list) - 1:
                            right_index += 1
                    #ひだりて
                        elif left and left_index < len(left_list) - 1:
                            left_index += 1
                    #あたま
                        elif head and head_index < len(head_list) - 1:
                            head_index += 1
                    #からだ
                        elif body and body_index < len(body_list) - 1:
                            body_index += 1
                elif event.key == K_UP:
                #メニュー
                    if item == False and magic == False and status == False and equip == False and right == False and left == False and head == False and body == False:
                        if cursor_y != 56:
                            cmd -= 1
                            cursor_y -= 30
                #アイテム
                    if item == True:
                        if cursor_y != 138:
                            num -= 1
                            cursor_y -= 30
                        elif cursor_y == 138 and num != 0:
                            num -= 1
                            item_index1 -= 1
                            item_index2 -= 1
                            item_index3 -= 1
                            item_index4 -= 1
                            item_index5 -= 1
                            item_index6 -= 1
                            item_index7 -= 1
               #まほう
                    if magic == True:
                        if cursor_y != 138:
                            num -= 1
                            cursor_y -= 30
                        elif cursor_y == 138 and num != 0:
                            num -= 1
                            magic_index1 -= 1
                            magic_index2 -= 1
                            magic_index3 -= 1
                            magic_index4 -= 1
                            magic_index5 -= 1
                            magic_index6 -= 1
                            magic_index7 -= 1
 

                #そうび
                    #全部位
                    elif equip == True:
                        if cursor_y != 138 and right == False and left == False and head == False and body == False:
                            equip_num -= 1
                            cursor_y -= 30
                    #みぎて
                        elif right and right_index != 0:
                            right_index -= 1
                    #みぎて
                        elif left and left_index != 0:
                            left_index -= 1
                    #みぎて
                        elif head and head_index != 0:
                            head_index -= 1
                    #みぎて
                        elif body and body_index != 0:
                            body_index -= 1


```
</details>
 
#

<details><summary>ここをクリックしてください</summary>

```python:


```
</details>