Hi, I'm looking for a fix to having multiple buttons in Pygame

Viewed 66

For context, I've just gotten into Pygame, and I'm trying to learn how to use buttons. Of course, there's making the classes or functions and likewise calling them to work. However, when I open a new screen, I'd like there to be a new button, which then only works there.

My issue comes in after the second scene, where I'm unsure how to call a second button, or a third etc. If I put a return in a function where my button is, and check whether that function has collided with a point, the whole screen becomes a button which isn't ideal. So I'm looking for a fix.

Here's the chunk causing issues, and I'd love any advice on optimising it. My guess is that HeroCreation()[1] is causing issues, but I'm not entirely sure how else to make multiple buttons work simultaenously.

def TextCreation(font, text, coords, screen):
    label = font.render(text, True, BLACK)
    labelRect = label.get_rect()
    labelRect.topright = coords
    window.blit(label, labelRect)
    return(label, labelRect)

StartButton = TextCreation(largeFont, 'Start Game', (rect_centerx+140, rect_centery - 250), window)
QuitButton = TextCreation(largeFont, 'Quit Game', (rect_centerx+140, rect_centery + 150), window)

def HeroCreation():
    window.fill(WHITE)
    Greeting = TextCreation(largeFont, 'Welcome, Hero!', (rect_centerx+210, rect_centery-200), window)
    Intro1 = TextCreation(paraFont, "I presume you're caught up, but just in case:", (rect_centerx+250, rect_centery + 50), window)
    Intro2 = TextCreation(paraFont, "The Rat King's wrecking our kingdom, and you're the only one who can find the Orb of Power.", (rect_centerx+500, rect_centery + 100), window)
    Intro3 = TextCreation(paraFont, "Deal with him once and for all! Do us proud, hero.",(rect_centerx+260, rect_centery + 150), window)
    pygame.draw.rect(window, LIGHT_GREEN, (rect_centerx - 170, rect_centery-80, 400, 70), 0)
    ProceedtoMapButton = TextCreation(paraFont, "Are you ready?", (rect_centerx+110, rect_centery-70), window)
    return (ProceedtoMapButton)

    
pygame.display.flip()

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            mouse = pygame.mouse.get_pos()
            if QuitButton[1].collidepoint(mouse):
                pygame.quit()
                sys.exit()
            if StartButton[1].collidepoint(mouse):
                HeroCreation()
            if HeroCreation()[1].collidepoint(mouse):
                drawGrid(width, height, window)
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
1 Answers

For each screen, create new buttons, then remove them when the screen exits. In your loop, check if the button is on the screen before the collision check.

Try this code:

HeroButton = None
while run:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            mouse = pygame.mouse.get_pos()
            if QuitButton[1].collidepoint(mouse): # quit button on every screen ?
                pygame.quit()
                sys.exit()
            if StartButton != None and StartButton[1].collidepoint(mouse):
                StartButton = None  # done with this button
                HeroButton = HeroCreation()
            elif HeroButton != None and HeroButton[1].collidepoint(mouse):
                HeroButton = None  # done with this button
                drawGrid(width, height, window) # start game?
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
Related