我的 pygame window 颜色没有填满屏幕

My pygame window color is not filling over the screen

好吧,这可能很荒谬,但我正在创建一个 pygame 游戏,目前我正在制作开场 window。现在我只向 window 添加了 2 个按钮;播放和帮助。 “播放”按钮完全可以正常工作,但“帮助”按钮功能却不行。我并不是说“帮助”按钮不起作用。函数内的代码不起作用。这是我的代码:


class Button:
    # I did not include this because it works completely fine. The problem is in the help window function.
def help_window():
    win.fill((0,0,0)) # Doesn't fill the screen completely. The buttons are still shown.

def main():
   # The main function

def main_menu():
    title_font = pygame.font.SysFont("comicsans", 70)
    run = True
    win.fill((90, 255, 180))
    while run:
        play_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - 30, 200, 70, "Play")
        play_button.draw(win, (0,0,0))

        help_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - play_button.height - 120, 200, 70, "Help")
        help_button.draw(win, (0,0,0))

        pygame.display.update()
        pos = pygame.mouse.get_pos()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                if play_button.isOver(pos):
                    main()
                elif help_button.isOver(pos):
                    help_window() # The help window function
    pygame.quit()
main_menu()

我该怎么办?

如果我对你的问题的理解正确,玩家点击帮助按钮后按钮不应再出现。

问题是,当玩家点击按钮时,这会调用 win.fill((0,0,0)),但在主循环中,您仍然绘制按钮(play_button.draw(win, (0,0,0))help_button.draw(win, (0,0,0)))。

您可能需要使用布尔变量来了解您的按钮是否必须 抽与不抽:

draw_button = True  # enable buttons drawing
while run:
    if draw_button:
        play_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - 30, 200, 70, "Play")
        play_button.draw(win, (0,0,0))
        help_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - play_button.height - 120, 200, 70, "Help")
        help_button.draw(win, (0,0,0))
    pygame.display.update()
    pos = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if play_button.isOver(pos):
                main()
            elif help_button.isOver(pos):
                help_window()
                draw_button = False  # stop drawing buttons