1

I have been following a tutorial from Sentdex to create a button and make it functional. I tried to change it as per my requirement. When I click on the button, I want the function(another screen) to execute. I placed a button in the function(another screen) where I can go back to the main page. But when I click on the button, it goes to the other function only when I clicked the mouse and the output is displayed just until I click the mouse. It does not go to another screen and keeps on staying at initial screen.

import pygame
window = pygame.display.set_mode((1500, 800), pygame.RESIZABLE)

def text_objects(text, font):
    textSurface = font.render(text, True, (0,0,0))
    return textSurface, textSurface.get_rect()

def button(msg, x, y, w, h, ic, ac, action):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(window, ac, (x, y, w, h))
        if click[0] == 1:
            action()
    else:
        pygame.draw.rect(window, ic, (x, y, w, h))
    smallText = pygame.font.SysFont(None, 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ((x+(w/2)), (y+(h/2)))
    window.blit(textSurf, textRect)

def home_intro():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                 
        window.blit(image, (0,0))
        pygame.display.set_caption("PROGRAM")
        button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
        button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)
        pygame.display.flip()


home_intro()
pygame.quit()
quit()

I have followed everything as the tutorial. But I don't understand why it does not work. How can I fix this ?

1 Answer 1

2

You have to add a variable that stores the current state of the game (game_state ). Change the variable when a button is clicked and draw different scenes depending on the state of the variable:

game_state = "stop"

def start():
    global game_state
    game_state = "start" 

def stop():
    global game_state
    game_state = "stop"
def home_intro():
    global game_state

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                 
        window.blit(image, (0,0))
        button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
        button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)

        if game_state == "start":
            # [...]

        else:
            # [...]

        pygame.display.flip()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.