首页 > 解决方案 > 嗨,我正在寻找解决 Pygame 中多个按钮的问题

问题描述

对于上下文,我刚刚进入 Pygame,我正在尝试学习如何使用按钮。当然,还有制作类或函数并同样调用它们来工作。但是,当我打开一个新屏幕时,我希望有一个新按钮,它只能在那里工作。

我的问题出现在第二个场景之后,我不确定如何调用第二个按钮或第三个按钮等。如果我在按钮所在的函数中放置 return,并检查该函数是否与点发生碰撞,整个屏幕变成了一个不理想的按钮。所以我正在寻找解决方法。

这是导致问题的块,我喜欢任何关于优化它的建议。我的猜测是 HeroCreation()[1] 引起了问题,但我不完全确定如何让多个按钮同时工作。

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()

标签: python-3.xbuttonpygame

解决方案


对于每个屏幕,创建新按钮,然后在屏幕退出时将其删除。在您的循环中,在碰撞检查之前检查按钮是否在屏幕上。

试试这个代码:

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()

推荐阅读