发布pygame.USEREVENT,但程序未向前移动

时间:2019-05-03 08:44:53

标签: python pygame

我正在使用OOP制作自上而下的游戏。我发布了pygame.USEREVENT + x,其中x是一些数字,用于通过按钮集移动程序(即播放和退出按钮到诸如easy和hard之类的难度按钮上)。但是,程序不会返回菜单或不重试该级别。我将如何修改程序以解决此问题?

您需要的游戏:https://github.com/Jaffa609/Stealth-Assassin/upload

主程序包含菜单的按钮类和游戏循环。还有两个附加文件,Character和Wall,它们是制作和移动精灵并绘制墙的类。我到达游戏结束的地步,并且在屏幕上出现了Menu(菜单)按钮和retry(重试)按钮,并且我使用了打印语句来显示即使单击按钮也可以注册。但是什么也没发生。

class GameState(enum.Enum): #A class for all of the game states the menu/game can be in
    MENU_PLAYQUIT       = 1 #Each of the following is defining a unique value for the state of the game/menu to each of the enumerations
    MENU_DIFFICULTY     = 2 #^
    MENU_LEVELSELECT    = 3 #^
    GAME_PLAYING        = 4 #^
    GAME_OVER           = 5 #^
class ButtonEvent(enum.IntEnum):  # IntEnum so we can convert back to an int for event posting
    QUIT     = pygame.USEREVENT +  1  #Each of the following is defining a unique value for event codes to which the buttons will send back for each of the enumerations
    PLAY     = pygame.USEREVENT +  2  #^
    EASY     = pygame.USEREVENT +  3  #^
    MEDIUM   = pygame.USEREVENT +  4  #^
    HARD     = pygame.USEREVENT +  5  #^
    VETERAN  = pygame.USEREVENT +  6  #^
    LEVEL1   = pygame.USEREVENT +  7  #^
    LEVEL2   = pygame.USEREVENT +  8  #^
    LEVEL3   = pygame.USEREVENT +  9  #^
    LEVEL4   = pygame.USEREVENT + 10  #^
    CALLMENU = pygame.USEREVENT + 11  #^
    RETRY    = pygame.USEREVENT + 12  #^

在下面的类中,摆脱了一半的构造方法实例和诸如draw之类的其他方法,因为它们是直接且不必要的。

class Button: #This class contains methods for buttons including display and functionality

    def __init__(self, buttonname, event_code, buttonx, buttony, buttonwidth, buttonheight, textfile, textx, texty): #Constructor method used to allow classes to intialise attributes
        self.event_code = event_code                                                    #The event code to enable game/menu state changes

    def checkClick(self, mouse_position):                                                                   #Method which checks if the button has ben pressed within the rectangle's area
        result = False                                                                                      #Assumes the mouse click is not within the given dimensions unless proven otherwise. If the result is false in the end then the program and UI remain unaffected
        if self.rect.collidepoint(mouse_position):                                                          #If the mouse-click is inside our rectangle, post a message to the queue
            pygame.event.post(pygame.event.Event(int(self.event_code), {"button_name" : self.buttonname}))  #Posts the enumerated value depending on which game state it is in along with the button name
            result = True                                                                                   #Result is true therfore action can be taken within the program to progress state
        return result                                                                                       #Returns result so its value is known outside this method

另一个名为ButtonSet的类,它将接收程序那部分的所有按钮并进行检查

elif game_state == GameState.GAME_OVER:             #If the game has been finished
                gameOver_buttons.anyClicked(click_location)

上方和下方位于游戏主循环中

elif event.type in [ButtonEvent.CALLMENU, ButtonEvent.RETRY]:
            if event.type == ButtonEvent.CALLMENU:
                import menu
            if event.type == ButtonEvent.RETRY:
                game_state = GameState.GAME_PLAYING 


0 个答案:

没有答案