简体   繁体   中英

Pygame: The screen flickers when I hold down the mouse, but only a specific button

I have multiple buttons residing in a menu class. The ones I want to point out specifically are the play button and the back button. The play button appears in the "main_menu" state, and when clicked, changes the state to "difficulty_menu". The back button does just the opposite; difficulty menu to main menu. It should be noted that the play button and back button are in two different locations on the screen.

When I hold down the play button, everything works as intended. The button underneath is not clicked and the screen does not flicker. However, when I hold down the back button, the screen flickers rapidly. I either end up in the main menu or the difficulty menu depending on when I release the mouse.

I'm probably overlooking something minor but I just can't figure out what it is. Below is all the code and method calls that are related to the problem. I'm sure other methods aren't an issue because the code for the play and back buttons there are identical.

game.py

if self.menu.state == "main_menu":
    self.menu.check_hover(mouse)
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        self.menu.check_click(event.pos)
    elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
        self.menu.play_button.l_click = False
elif self.menu.state == "difficulty_menu":
    self.menu.check_hover(mouse)
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        self.menu.check_click(event.pos)
    elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
        self.menu.easy_button.l_click = False
        self.menu.medium_button.l_click = False
        self.menu.hard_button.l_click = False
        self.menu.back_button.l_click = False

menu.py

def check_hover(self, mouse):
    """
    Buttons are checked to see if they are being hovered over.
    If so, the button color changes.
    """
    if self.state == "main_menu":
        self.play_button.check_hover(mouse)
        self.about_button.check_hover(mouse)
        self.credits_button.check_hover(mouse)
        self.quit_button.check_hover(mouse)
    elif self.state == "difficulty_menu":
        self.easy_button.check_hover(mouse)
        self.medium_button.check_hover(mouse)
        self.hard_button.check_hover(mouse)
        self.back_button.check_hover(mouse)

def check_click(self, pos):
    """
    Buttons are checked to see if they have been clicked.
    """
    if self.state == "main_menu":
        self.play_button.check_left_click(pos)
        self.about_button.check_left_click(pos)
        self.credits_button.check_left_click(pos)
        self.quit_button.check_left_click(pos)
    elif self.state == "difficulty_menu":
        self.easy_button.check_left_click(pos)
        self.medium_button.check_left_click(pos)
        self.hard_button.check_left_click(pos)
        self.back_button.check_left_click(pos)

def update(self):
    """
    The state changes depending on if a button has been clicked.
    """
    if self.state == "main_menu":
        if self.play_button.l_click:
            self.state = "difficulty_menu"
        elif self.about_button.l_click:
            self.state = "about_menu"
        elif self.credits_button.l_click:
            self.state = "credits_menu"
        elif self.quit_button.l_click:
            pygame.quit()
            sys.exit()
    elif self.state == "difficulty_menu":
        if self.easy_button.l_click:
            self.difficulty = "easy"
            self.state = "hidden"
        elif self.medium_button.l_click:
            self.difficulty = "medium"
            self.state = "hidden"
        elif self.hard_button.l_click:
            self.difficulty = "hard"
            self.state = "hidden"
        elif self.back_button.l_click:
            self.state = "main_menu"

I realized what I did wrong. In menu.update(), if l_click is true for the play or back button, I changed the state. Then, in game.py, I'm checking state conditions. The state has already changed so the program never was able to set play_button.l_click or stop_button.l_click to False. I fixed the problem by creating a new method in menu called no_click() that set all button.l_clicks to False. I called this method in every state condition in game.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM