为什么play_button.rect.collidepoint(mouse_x,mouse_y)不起作用?

时间:2020-10-01 06:28:00

标签: pygame

这是Python中的打字游戏。点击“播放”按钮,游戏就应该开始了。按下相应的键时,随机字母会掉落,每个字母都会消失。但是,当我单击按钮时,它无法开始。我认为问题必须与game_functions.py方法check_play_button()中的if语句(.*)有关,因为如果if语句被删除,则stats.game_active会更改为True。另一方面,如果删除了if语句,则单击屏幕上的任何位置都会将stats.game_active的值更改为True。我对if语句的故障感到很困惑。请说明一下。 这是我的代码(有点长,但是很困难:P):

  1. alphabet_zoo.py
if play_button.rect.collidepoint(mouse_x , mouse_y):
  1. button.py
import pygame
import time
from pygame.locals import *
from settings import Settings
import game_functions as gf
from game_stats import GameStats
from button import Button

def run_game():
    pygame.init()
    az_settings =Settings()
    screen = pygame.display.set_mode((0,0), RESIZABLE)
    pygame.display.set_caption("Alphabet Zoo")
    play_button = Button(screen, "Play")
    stats = GameStats(az_settings)
    letters = pygame.sprite.Group()
    start = time.time()
    sleepTime = 3


    while True:
        now = time.time()
        gf.check_events(letters, stats, play_button)
        if stats.game_active:
            gf.update_screen(az_settings, stats, screen, letters, play_button)
            if now - start >sleepTime:
                gf.letter_generator(az_settings ,screen, letters)
                start = now
        else:
            gf.update_screen(az_settings, stats, screen, letters, play_button)



run_game()

  1. settings.py
import pygame.font


class Button():


    def __init__(self, screen, msg):
        self.screen = screen
        self.screen_rect = screen.get_rect()

        self.width, self.height = 200, 50
        self.button_color = (0, 0, 255)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont(None, 48)

        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        self.prep_msg(msg)

    def prep_msg(self, msg):
        self.msg_image = self.font.render(msg, True, self.text_color,
                                          self.button_color)
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image_rect.center = self.rect.center

    def draw_button(self):
        self.screen.fill(self.button_color, self.rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)
  1. game_stats.py
class Settings():
    def __init__(self):
        self.bg_color = (0, 0, 0)
        self.letter_speed_factor = 10
        self.lives_limit = 10
  1. letter.py
class GameStats():
    def __init__(self, az_settings):
        self.az_settings = az_settings
        self.reset_stats()
        self.game_active = False

    def reset_stats(self):
        self.lives_left = self.az_settings.lives_limit
  1. game_functions.py
import pygame
import random
from pygame.sprite import Sprite


class Letter(Sprite):

    def __init__(self, az_settings, screen):
        super().__init__()
        self.screen = screen
        self.az_settings = az_settings
        a = random.randint(97, 122)
        c = chr(a)
        self.image = pygame.image.load('images/' + c.upper() + '.png')
        self.ascii = a

        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        self.rect.centerx = random.randint(0, self.screen_rect.right)
        self.rect.top = self.screen_rect.top
        self.center = float(self.rect.centerx)



    def update(self):
        if self.rect.bottom < self.screen_rect.bottom:
            self.rect.centery += self.az_settings.letter_speed_factor

1 个答案:

答案 0 :(得分:3)

很显然,我无法对此进行测试,但是我认为您的问题是您没有为play_button.rect分配职位。默认情况下,rect的头寸值为(0, 0)。不久前,我遇到了同样的问题,因此,您在阅读this问题时会读到更多详细信息。解决该问题的一种方法-在按钮类中创建一个getRect方法:

class Button:
    def getRect(self):
         return pygame.Rect(x, y, width, height) # Since you will 
          #be using this rect for collision, enter the arguments accordingly

然后

if play_button.getRect().collidepoint(mouse_x , mouse_y):

其他选择是直接将位置分配给按钮rect,但是我更喜欢第一种解决方案,它只是个人喜好:

class Button():
    def __init__(self, screen, msg):
        self.rect.topleft = #your button position