Erdem Gunseli
Erdem Gunseli

Reputation: 33

Pygame Buttons In Different Menus All Activate

Here is the problem: Say in my game, I have 2 menus, implemented as different game loops. Say one is the red menu and the other is the green menu.

In each menu, there is a button that, when pressed, launches the other menu. If the buttons are in the same position in the screen, pressing one results in the other being pressed as soon as the new menu loads, taking the user back to the old menu.

Here is a code example - how can I fix this?

import pygame

RED = (255, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


class Game:

    def __init__(self):

        # When true, the game will end:
        self.done = False
        self.screen = pygame.display.set_mode((600, 400))
        self.clock = pygame.time.Clock()

        # List storing all events:
        self.all_events = []

        self.show_red_page()


    def update(self):
        self.all_events = []

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.done = True
            else:
                self.all_events.append(event)


    def show_red_page(self):

        button = Button(self, pygame.rect.Rect((100, 100), (50, 50)), BLACK)

        while not self.done:
            self.screen.fill(RED)
            self.update()

            # If the button is clicked, launch the other page:
            if button.clicked():
                self.show_green_page()

            # Draw button:
            button.draw()
            self.update()
            pygame.display.flip()
            self.clock.tick(60)

    def show_green_page(self):
        # Same as red page, but different colors:
        button = Button(self, pygame.rect.Rect((100, 100), (50, 50)), WHITE)

        while not self.done:
            self.screen.fill(GREEN)

            # If the button is clicked, launch the other page:
            if button.clicked():
                self.show_red_page()

            # Draw button:
            button.draw()
            self.update()
            pygame.display.flip()
            self.clock.tick(60)


class Button:

    def __init__(self,game,  rect, color):
        self.game = game
        self.rect = rect

        self.image = pygame.Surface(self.rect.size)
        self.image.fill(color)

    def draw(self):
        pygame.display.get_surface().blit(self.image, self.rect)

    def hovering(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def clicked(self):
        return self.hovering() and pygame.MOUSEBUTTONUP in [event.type for event in self.game.all_events]


Game()

Upvotes: 1

Views: 190

Answers (1)

bix
bix

Reputation: 47

Check for clicks in the update function's for loop. This registers as one click, unlike checking for clicks outside of the while loop, with a different function. Try:

python
#check if left clicked:
if event.type == pygame.MOUSEBUTTONDOWN:
    if event.button = 1:
        #check if mouse is hovered over the button, if so:
            #function here

Hope this helps Do so for each menu, and good luck with your game!

Upvotes: 1

Related Questions