1

The function start_menu() leads to run_instructions() after a button press. In run_instructions() once a user clicks the mouse again it should go to another function however I think the click from the previous function carries on and automatically triggers click[0] to = 1 despite the fact no one has clicked anything.

def run_instructions():
    clicked = False
    while clicked == False:
        click = pygame.mouse.get_pressed()
        board.blit(instructions,[0,0])
        pygame.display.update() 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        if click[0] == 1:
             create_environment()
             clicked = True

def start_menu():
    global menu
    while menu == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if 125 + 172 > mouse[0] > 150 and 448 + 69 > mouse[1] > 448 and click[0] == 1:
            menu = False
            run_instructions()
            break

Is there anyway to make click[0] update or reset it to 0 when it enters run_instructions(). I've tried using pygame.MOUSEBUTTONDOWN but it gives the same problem.

Thanks.

darkrai
  • 113
  • 3
  • 13

1 Answers1

0

Checking for pygame.MOUSEBUTTONDOWN events in the event loop is the right solution. pygame.mouse.get_pressed is problematic here because it only tells you if a mouse button is currently being held down and not if the button was clicked once.

Here's a working example. I use a pygame.Rect as a very simple button in the first scene on which you have to click to get to the run_instructions function. In the next scene (with the different background color) press the mouse button a second time and it'll print 'create_environment'.

import sys
import pygame


pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
BG_COLOR = pygame.Color('gray12')
BG_COLOR2 = pygame.Color(50, 90, 120)


def run_instructions():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                print('create_environment')

        screen.fill(BG_COLOR2)
        pygame.display.flip()
        clock.tick(30)


def start_menu():
    button_rect = pygame.Rect(40, 100, 80, 50)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # If the button collides with the mouse position.
                if button_rect.collidepoint(event.pos):
                    run_instructions()

        screen.fill(BG_COLOR)
        pygame.draw.rect(screen, (90, 200, 50), button_rect)
        pygame.display.flip()
        clock.tick(30)

start_menu()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • I also recommend implementing the scenes in a [different way](https://stackoverflow.com/a/47460947/6220679). – skrx Feb 20 '18 at 00:42