我正在制作一个开始屏幕按钮,当您按play时,游戏循环开始,但是它说game_loop没有定义,即使我以为是。我似乎无法弄清楚问题所在,所以有人知道如何解决此问题吗?这会很有帮助! :)

def button(x, y, width, height, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    # print(mouse)

    if x + width > mouse[0] > x and y + height > mouse[1] > y:
        pygame.draw.rect(screen, ac, (x, y, width, height), 0)
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and action is not None:
                if action == 'play':
                    game_loop()
                elif action == 'quit':
                    pygame.quit()
                    quit()
    else:
        pygame.draw.rect(screen, ic, (x, y, width, height), 0)

def start_screen():

    start = True

    while start:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYUP:
                start = False

        screen.blit(background, background_rect)

        button(180, 285, 120, 60, BLUE, LIGHT_BLUE, 'play')
        button(202, HEIGHT - 150, 80, 40, WHITE, GRAY, 'quit')
        button(310, 45, 140, 30, WHITE, GRAY)
        button(20, 45, 140, 30, WHITE, GRAY)

        draw_text(screen, "Shoot 'Em Up", 80, WIDTH / 2, HEIGHT / 4, WHITE)
        draw_text(screen, "Instructions", 30, 90, 53, BLACK)
        draw_text(screen, "Credits", 35, 378, 50, BLACK)
        draw_text(screen, "Play", 55, WIDTH / 2, HEIGHT / 2, WHITE)
        draw_text(screen, "Quit", 30, WIDTH / 2, HEIGHT - 137, BLUE)

        pygame.display.flip()

start_screen()
running = True

def game_loop():
    while running:
        *game_loop code*
game_loop()

最佳答案

正确设置代码格式,它应该可以工作。


 def button(x, y, width, height, ic, ac, action=None):

     mouse = pygame.mouse.get_pos()
     # print(mouse)

     if x + width > mouse[0] > x and y + height > mouse[1] > y:
         pygame.draw.rect(screen, ac, (x, y, width, height), 0)
         for event in pygame.event.get():
             if event.type == pygame.MOUSEBUTTONDOWN and action is not None:
                 if action == "play":
                     game_loop()
                 elif action == "quit":
                     pygame.quit()
                     quit()
     else:
         pygame.draw.rect(screen, ic, (x, y, width, height), 0)

 def game_loop():
     print('game loop')
 game_loop()

关于python - 为什么我的game_loop没有定义?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57343478/

10-12 17:03