python - Pygame 按钮 getRect Collidepoint 不起作用?

标签 python button menu pygame

我已经完成了游戏的主要代码,并开始制作菜单屏幕。我可以很好地在屏幕上显示按钮,但是当我单击某处时,我得到这个 Error :

我该如何解决这个问题?如果我在这个问题上没有说清楚,请告诉我,以便我澄清。谢谢!

这是我的菜单屏幕代码:

import pygame
import random
import time

pygame.init()

#colours
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
blue = (50,50,155)


display_width = 800  
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('Numeracy Ninjas')

clock = pygame.time.Clock()

#Fonts

smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 75)

#Sprites

img_button_start = pygame.image.load('Sprites/Buttons/button_start.png')
img_button_options = pygame.image.load('Sprites/Buttons/button_options.png')

gameDisplay.fill(white)
pygame.display.update()


class Button(pygame.sprite.Sprite):
    def __init__(self, image, buttonX, buttonY):
        super().__init__()

        gameDisplay.blit(image, (buttonX, buttonY))

        pygame.display.update()

        selfrect = image.get_rect()

    def wasClicked(event):
        if selfrect.collidepoint(event.pos):
             return True

def gameIntro():
    buttons = pygame.sprite.Group()
    button_start = Button(img_button_start, 27, 0)
    button_options = Button(img_button_options, 27, 500)
    buttons.add(button_start)
    buttons.add(button_options)

    print(buttons)


    #main game loop
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                print(event.pos)
                #check for every button whether it was clicked
                for btn in buttons:
                    print('forbtninbuttons')
                    if btn.wasClicked():
                        print('clicked!')

            if event.type == pygame.QUIT:
                pygame.quit()

最佳答案

您还没有为您的类声明任何属性,只是局部变量。尝试在初始化程序和 wasClicked(event) 方法中执行 self.selfrect = image.get_rect() 操作:

def wasClicked(self, event):
    if self.selfrect.collidepoint(event.pos):
         return True

通常的约定是将矩形变量命名为 rect

class Button(pygame.sprite.Sprite):
    def __init__(self, image, buttonX, buttonY):
        super().__init__()
        #  This code doesn't make sense here. It should be inside your game loop.
        #  gameDisplay.blit(image, (buttonX, buttonY))
        #  pygame.display.update()

        self.image = image  # It's usually good to have a reference to your image.
        self.rect = image.get_rect()

    def wasClicked(self, event):
        if self.rect.collidepoint(event.pos):
             return True
        else:
            return False

关于python - Pygame 按钮 getRect Collidepoint 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38839401/

相关文章:

python - 创建 python 生成器后更新它

python - 基于 Pandas 条件的行的最大值和最小值(取决于列名)

php - php中的href html按钮

button - JavaFX:自定义网格 Pane 中按钮的宽度

jquery指定css框

python - 在类中定义 __iter__ 的多个方差

HTML:让按钮自动调整大小

batch-file - 如何使用 Batch 在可滚动菜单中制作一系列切换开关?

html - 找不到风格的来源

python - Pygame - 我可以让音乐有一个介绍然后是一个循环点吗?