【问题标题】:UnboundLocalError: local variable 'event' referenced before assignment [duplicate]UnboundLocalError:分配之前引用的局部变量“事件”[重复]
【发布时间】:2021-08-19 04:38:56
【问题描述】:

这是错误信息

C:\Users\Me\Documents\Fan game>python testing_file.py
    pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    Traceback (most recent call last):
      File "C:\Users\Me\Documents\Fan game\testing_file.py", line 1685, in <module>
        menu_screen()
      File "C:\Users\Me\Documents\Fan game\testing_file.py", line 464, in menu_screen
        if event.type == MOUSEBUTTONDOWN:
    UnboundLocalError: local variable 'event' referenced before assignment

我的后退按钮有时会起作用,但有时我会收到错误消息。

我的代码如下;

...
while var_menu_screen == 0:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        
        rand = random.randint(1, 8)
        x,y = pygame.mouse.get_pos()        
        if rand == 1:
            if var_stat == 1:
                screen.fill((0,0,0))
                screen.blit(image_menu_background, centered_image_menu_background) 
                if 154 < x < 380 and 221 < y < 272:
                    screen.blit(image_options_hover, (154, 221))
                    var_options = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 1
                else:
                    screen.blit(image_options, (154, 221))
                    var_options = 0
                    
                if 154 < x < 396 and 301 < y < 375:
                    screen.blit(image_new_game_hover, (154, 301))
                    var_new_game = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 2
                else:
                    screen.blit(image_new_game, (154, 301))
                    var_new_game = 0
                    
                if var_night_number > 1:    
                    if 154 < x < 351 and 399 < y < 463:
                        screen.blit(image_continues_hover, (154, 399))
                        var_continues = 1
                        if event.type == MOUSEBUTTONDOWN:
                            var_menu_screen = 3
                    else:
                        screen.blit(image_continues, (154, 399))
                        var_continues = 0
                else:
                    screen.blit(image_continues_disabled, (154, 399))
                    
                if 154 < x < 325 and 489 < y < 557:
                    screen.blit(image_credits_hover, (154, 489))
                    var_credits = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 4
                else: 
                    screen.blit(image_credits, (154, 489))
                    var_credits = 0
                    
                if 154 < x < 270 and 564 < y < 627:
                    screen.blit(image_quit_hover, (154, 564))
                    var_quit = 1
                    if event.type == MOUSEBUTTONDOWN:
                        pygame.quit()
                        sys.exit()
                else:
                    screen.blit(image_quit, (154, 564))
                    var_quit = 0
                screen.blit(image_static_2, centered_image_static_2) 
                pygame.display.update()
                var_stat = 2
            else:
                screen.fill((0,0,0))
                screen.blit(image_menu_background, centered_image_menu_background) 
                if 154 < x < 380 and 221 < y < 272:
                    screen.blit(image_options_hover, (154, 221))
                    var_options = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 1
                else:
                    screen.blit(image_options, (154, 221))
                    var_options = 0
                    
                if 154 < x < 396 and 301 < y < 375:
                    screen.blit(image_new_game_hover, (154, 301))
                    var_new_game = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 2
                else:
                    screen.blit(image_new_game, (154, 301))
                    var_new_game = 0
                    
                if var_night_number > 1:
                    if 154 < x < 351 and 399 < y < 463:
                        screen.blit(image_continues_hover, (154, 399))
                        var_continues = 1
                        if event.type == MOUSEBUTTONDOWN:
                            var_menu_screen = 3
                    else:   
                        screen.blit(image_continues, (154, 399))
                        var_continues = 0
                else:
                    screen.blit(image_continues_disabled, (154, 399))
                    
                if 154 < x < 325 and 489 < y < 557:
                    screen.blit(image_credits_hover, (154, 489))
                    var_credits = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 4
                else: 
                    screen.blit(image_credits, (154, 489))
                    var_credits = 0
                    
                if 154 < x < 270 and 564 < y < 627:
                    screen.blit(image_quit_hover, (154, 564))
                    var_quit = 1
                    if event.type == MOUSEBUTTONDOWN:
                        pygame.quit()
                        sys.exit()
                else:
                    screen.blit(image_quit, (154, 564))
                    var_quit = 0    
                screen.blit(image_static_1, centered_image_static_1)            
                pygame.display.update()
                var_stat = 1
...

这对每个静态图像重复 8 次。

我的后退按钮的工作原理如下;

...

    while var_menu_screen == 4:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        rand = random.randint(1, 8)
        x, y = pygame.mouse.get_pos()
        if rand == 1:
            if var_stat == 1:           
                screen.fill((0, 0, 0))
                screen.blit(image_credits_screen, centered_image_credits_screen)
                if 154 < x < 258 and 564 < y < 621:
                    screen.blit(image_back_hover, (154, 564))
                    var_back = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 0
                else:
                    screen.blit(image_back, (154, 564))
                    var_back = 0
                screen.blit(image_static_2, centered_image_static_2)
                pygame.display.update()
                var_stat = 2
            else:
                screen.fill((0, 0, 0))
                screen.blit(image_credits_screen, centered_image_credits_screen)
                if 154 < x < 258 and 564 < y < 621:
                    screen.blit(image_back_hover, (154, 564))
                    var_back = 1
                    if event.type == MOUSEBUTTONDOWN:
                        var_menu_screen = 0
                else:
                    screen.blit(image_back, (154, 564))
                    var_back = 0
                screen.blit(image_static_1, centered_image_static_1)
                pygame.display.update()
                var_stat = 1
...

这对 8 个静态图像重复 8 次。

主程序;

while True:
    if var_menu_screen == 0:
        menu_screen()
    if var_menu_screen == 1:
        options_screen()
    if var_menu_screen == 2:
        new_game_boyz()
    if var_menu_screen == 3:
        pygame.quit()
        sys.quit()
    if var_menu_screen == 4:
        credits_screen()
    if var_start_game == True:
        break

不知道为什么这只在某些时候有效。帮助

【问题讨论】:

  • 我认为rand = random.randint(1, 8) 和以下行也应该在你的循环中,或者不是?
  • 它在我的循环中
  • 我认为他的意思是在for event in pygame... 循环内。不是while循环。因为从代码中我们至少可以看到它不在循环内
  • 所以它修复了它。现在每当我移动鼠标时它都会运行,但一旦我停止它就会停止运行,直到我再次移动鼠标
  • 有没有什么方法可以让鼠标不动也能静态运行?

标签: python pygame


【解决方案1】:

您应该将事件处理代码移到从 pygame 轮询事件的 for 循环中,因为在该循环中定义了 event。您的第一个代码变为:

...
while var_menu_screen == 0:
    mouse_down = False
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN:
            mouse_down = True

    rand = random.randint(1, 8)
    x, y = pygame.mouse.get_pos()
    if rand == 1:
        if var_stat == 1:
            screen.fill((0, 0, 0))
            screen.blit(image_menu_background,
                        centered_image_menu_background)
            if 154 < x < 380 and 221 < y < 272:
                screen.blit(image_options_hover, (154, 221))
                var_options = 1
                if mouse_down:
                    var_menu_screen = 1
            else:
                screen.blit(image_options, (154, 221))
                var_options = 0

            if 154 < x < 396 and 301 < y < 375:
                screen.blit(image_new_game_hover, (154, 301))
                var_new_game = 1
                if mouse_down:
                    var_menu_screen = 2
            else:
                screen.blit(image_new_game, (154, 301))
                var_new_game = 0

            if var_night_number > 1:
                if 154 < x < 351 and 399 < y < 463:
                    screen.blit(image_continues_hover, (154, 399))
                    var_continues = 1
                    if mouse_down:
                        var_menu_screen = 3
                else:
                    screen.blit(image_continues, (154, 399))
                    var_continues = 0
            else:
                screen.blit(image_continues_disabled, (154, 399))

            if 154 < x < 325 and 489 < y < 557:
                screen.blit(image_credits_hover, (154, 489))
                var_credits = 1
                if mouse_down:
                    var_menu_screen = 4
            else:
                screen.blit(image_credits, (154, 489))
                var_credits = 0

            if 154 < x < 270 and 564 < y < 627:
                screen.blit(image_quit_hover, (154, 564))
                var_quit = 1
                if mouse_down:
                    pygame.quit()
                    sys.exit()
            else:
                screen.blit(image_quit, (154, 564))
                var_quit = 0
            screen.blit(image_static_2, centered_image_static_2)
            pygame.display.update()
            var_stat = 2
        else:
            screen.fill((0, 0, 0))
            screen.blit(image_menu_background,
                        centered_image_menu_background)
            if 154 < x < 380 and 221 < y < 272:
                screen.blit(image_options_hover, (154, 221))
                var_options = 1
                if mouse_down:
                    var_menu_screen = 1
            else:
                screen.blit(image_options, (154, 221))
                var_options = 0

            if 154 < x < 396 and 301 < y < 375:
                screen.blit(image_new_game_hover, (154, 301))
                var_new_game = 1
                if mouse_down:
                    var_menu_screen = 2
            else:
                screen.blit(image_new_game, (154, 301))
                var_new_game = 0

            if var_night_number > 1:
                if 154 < x < 351 and 399 < y < 463:
                    screen.blit(image_continues_hover, (154, 399))
                    var_continues = 1
                    if mouse_down:
                        var_menu_screen = 3
                else:
                    screen.blit(image_continues, (154, 399))
                    var_continues = 0
            else:
                screen.blit(image_continues_disabled, (154, 399))

            if 154 < x < 325 and 489 < y < 557:
                screen.blit(image_credits_hover, (154, 489))
                var_credits = 1
                if mouse_down:
                    var_menu_screen = 4
            else:
                screen.blit(image_credits, (154, 489))
                var_credits = 0

            if 154 < x < 270 and 564 < y < 627:
                screen.blit(image_quit_hover, (154, 564))
                var_quit = 1
                if mouse_down:
                    pygame.quit()
                    sys.exit()
            else:
                screen.blit(image_quit, (154, 564))
                var_quit = 0
            screen.blit(image_static_1, centered_image_static_1)
            pygame.display.update()
            var_stat = 1
...

您的第二个代码变为:

...

    while var_menu_screen == 4:
        mouse_down = False
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                mouse_down = True
        rand = random.randint(1, 8)
        x, y = pygame.mouse.get_pos()
        if rand == 1:
            if var_stat == 1:           
                screen.fill((0, 0, 0))
                screen.blit(image_credits_screen, centered_image_credits_screen)
                if 154 < x < 258 and 564 < y < 621:
                    screen.blit(image_back_hover, (154, 564))
                    var_back = 1
                    if mouse_down:
                        var_menu_screen = 0
                else:
                    screen.blit(image_back, (154, 564))
                    var_back = 0
                screen.blit(image_static_2, centered_image_static_2)
                pygame.display.update()
                var_stat = 2
            else:
                screen.fill((0, 0, 0))
                screen.blit(image_credits_screen, centered_image_credits_screen)
                if 154 < x < 258 and 564 < y < 621:
                    screen.blit(image_back_hover, (154, 564))
                    var_back = 1
                    if mouse_down:
                        var_menu_screen = 0
                else:
                    screen.blit(image_back, (154, 564))
                    var_back = 0
                screen.blit(image_static_1, centered_image_static_1)
                pygame.display.update()
                var_stat = 1
...

, 第三个代码就可以了,你不需要改变它。 编辑:我添加了一个 mouse_down 变量,以便您可以在事件循环之外检查鼠标按下。

【讨论】:

  • 所以它修复了它。现在每当我移动鼠标时它都会运行,但一旦我停止它就会停止运行,直到我再次移动鼠标
  • 有没有什么方法可以让鼠标不动也能静态运行?
  • @WolfmannGames 什么静态?
  • 所以基本上我只是展示了一张静态图片的代码,实际上它显示了 8 张静态图片,具体取决于 rand 等于多少。
  • @WolfmannGames 我更新了代码,现在应该可以了
猜你喜欢
  • 1970-01-01
  • 2013-02-28
  • 2021-12-16
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
相关资源
最近更新 更多