@117kd

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

pythonのpygameで画面切り替え

解決したいこと

pygameでの画面切り替えができません。
「easy_button_imgをクリックしたらgamescreen=2になる」という状態にしたいのですが、クリックしても何も反応がありません。
rect_easybutton.collidepoint((mx, my))
のところが間違っていると思うのですが...どう変えれば良いのか助言をいただきたいです。

(神経衰弱のゲームを作っていて、実はプレイ画面のところもちょっとおかしいのですが、動作には支障がないのでカードらへんの処理は無視してください)

該当するソースコード

class Card:

    def __init__(self, position, number, back_img, front_img, sound):

        self.position = position
        self.number = number
        self.back_img = back_img
        self.front_img = front_img
        self.sound = sound

        #描画用画像
        self.image = self.back_img
        self.rect = self.image.get_rect(topleft = self.position)
import pygame
from card import Card
import random

pygame.init()

#ウィンドウ
gamescene = 0
screen = pygame.display.set_mode((960, 720))

#フレームレート
FPS = 60
clock = pygame.time.Clock()

#画面読み込み
###難易度選択
easybutton_img = pygame.image.load('easy_button.png')
rect_easybutton = easybutton_img.get_rect()
hardbutton_img = pygame.image.load('hard_button.png')
rect_hardbutton = hardbutton_img.get_rect()
###カード
back_img = pygame.image.load('card_easy.png')

#位置のリスト
position_list = []
x = 130
y = 100
for i in range(2):
    for j in range(4):
        position_list.append([x, y])
        x += 150
    y += 150
    x = 130
random.shuffle(position_list)

#カードの画像のリスト
number_list = []
front_list = []
sound_list = []

for i in range(1, 9):
    number_list.append(i)
    front_img = pygame.image.load(f'{i}_shadow.png')
    front_list.append(front_img)
    sound = pygame.mixer.Sound(f'{1}_animalsound.mp3')
    sound_list.append(sound)
    sound.set_volume(0.5)

#カードの作成
card_list = []
for i in range(8):
    card = Card(position_list[i], number_list[i], back_img, front_list[i], sound_list[i])
    card_list.append(card)

#文字
font_start = pygame.font.SysFont('meiryo', 70)
text_start = font_start.render('画面を押してスタート!', True, (0,0,0))
font_levelselect = pygame.font.SysFont('meiryo', 70)
text_levelselect = font_levelselect.render('難易度を選択してください', True, (0,0,0))

#メインループ==================================
run = True
while run:

    #背景色
    screen.fill((255, 255, 255))

    if gamescene == 0:
        screen.blit(text_start, (80,300))
    elif gamescene == 1:
        screen.blit(easybutton_img, (250, 230))
        screen.blit(hardbutton_img, (250, 450))
        screen.blit(text_levelselect, (65, 50))
    elif gamescene == 2:
        for card in card_list:
            screen.blit(card.image, card.position)

    #マウスの位置の取得
    mx, my = pygame.mouse.get_pos()

    #イベント取得
    for event in pygame.event.get():
        #バツボタン押したら閉じる
        if event.type == pygame.QUIT:
            run = False
        #Escキー押したら閉じる
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
        
        #画面切り替え
        if event.type == pygame.MOUSEBUTTONDOWN:
            #タイトル画面から難易度選択
            if gamescene == 0:
                gamescene = 1
            #難易度選択からプレイ画面
            elif gamescene == 1 and rect_easybutton.collidepoint((mx, my)):
                gamescene = 2

    #クリックイベント
    if event.type == pygame.MOUSEBUTTONDOWN:
        for index, card in enumerate(card_list):
            if card.rect.collidepoint((mx, my)) and card.image == card.back_img and gamescene == 2:
                card.image = card.front_img
                #表面のとき音が鳴る
                if card.image == card.front_img:
                        card.sound.play()

    #更新
    pygame.display.update()
    clock.tick(FPS)
# ============================================

pygame.quit()
0 likes

1Answer

rect_easybutton.collidepoint((mx, my))
のところが間違っていると思うのですが...どう変えれば良いのか助言をいただきたいです。

rect_easybuttonの中身を確認しましたか?
おそらく、(0, 0, width, height)になっていると思います。

71行目で(250, 230)に置いているので、(250, 230, width, height)になっていなければ正しい判定ができないです。

0Like

Comments

  1. @117kd

    Questioner

    たしかに(0, 0, width, height)となっていました…
    ありがとうございます!

  2. ✌️

    解決ならば、当Q&Aをクローズしてくださいね。

Your answer might help someone💌