为什么我的game_loop没有定义?

时间:2019-08-04 03:01:22

标签: python python-3.x pygame

我正在制作一个开始屏幕按钮,当您按下play时,game_loop开始,但是它说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()

2 个答案:

答案 0 :(得分:1)

  

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

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

答案 1 :(得分:1)

我将[dictwriter][1]的正文替换为button()。奏效了。

pass

您应该真正在代码中用空格替换制表符。或者,从SO复制并粘贴到您的环境中。