Pygame - 菜单屏幕按钮不能正常工作

时间:2016-07-20 03:32:26

标签: python button menu pygame

我正在尝试为我的游戏创建一个菜单屏幕。目前我使用精灵按钮,一切运行良好,我可以创建无限量的按钮(目前我只有开始和选项),但只有我调用的第一个按钮出现在屏幕上。我认为这与按钮类中的while循环有关,但我不确定如何修复它。我可能在这里毫无意义,所以如果你需要我澄清任何我想做的事情。谢谢!

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()

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)

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

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

        pygame.display.update()

        running = True
        while (running):
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    # Set the x, y postions of the mouse click
                    x, y = event.pos
                    print(x, y)
                    if x <= (150 + buttonX) and x >=(0 + buttonX) and y <= (75 + buttonY) and y >= (0 + buttonY):
                        print('clicked on button')

def gameIntro():                   
    button_start = Button(img_button_start, 27, 0)
    button_options = Button(img_button_options, 27, 500)

gameIntro()

1 个答案:

答案 0 :(得分:0)

在Button类的构造函数中,您有一个无限循环。这意味着你永远不会到达你制作第二个按钮的代码部分。

def gameIntro():                   
    button_start = Button(img_button_start, 27, 0) #stuck in infinite loop
    print('This print statement is never reached')
    button_options = Button(img_button_options, 27, 500)

相反,你要做的是初始化两个按钮,然后在gameIntro()方法中有一个主游戏循环来检查事件。如果发生mousebuttondown事件,您希望将事件(或者甚至只是事件位置,如果您不关心单击哪个鼠标按钮)传递给按钮功能,该按钮检查是否单击了此按钮实例,然后处理输入(可能通过返回你在主游戏循环中处理的东西)。

请注意,我没有运行以下代码,我只是想让您了解它应该如何构建:

class Button(pygame.sprite.Sprite):
    def __init__(self, image, buttonX, buttonY):
        super().__init__()
        self.image = image
        self.rect = image.getRect()
        self.rect.x = buttonX
        self.rect.y = buttonY

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

def gameIntro():
    #initialize buttons
    buttons = pygame.sprite.Group() #make a group to make drawing easier
    button_start = Button(img_button_start, 27, 0)
    button_options = Button(img_button_options, 27, 500)
    #draw buttons to display
    buttons.draw(gameDisplay)
    pygame.display.update()

    #main game loop
    running = True
    while (running):
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                #check for every button whether it was clicked
                for btn in buttons:
                    if btn.wasClicked():
                        #do something appropriate
            if event.type == pygame.QUIT:
                pygame.quit()