微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Pygame 的 Python 范围

如何解决使用 Pygame 的 Python 范围

我有点困惑,为什么当我的 redraw_game_window() 函数调用时,正确的变量没有被正确地传递,当我按下右箭头键时它返回 False。在下面,如果 keys[pygame.K_RIGHT]: 它返回 True 但是。我需要在代码调用更高的 redraw_game_window 吗?我认为它需要是代码中的最后一件事。为了便于阅读,我省略了代码的开头。

所以当我按下右箭头键时它实际上是激活的,但即使我提供了全局标志,它也不会继续。这里发生了什么?

# Moving Variables
    moving_x = 100
    moving_y = 350
    walk = 1
    vertical = 1
    jump = False
    jump_count = 10
    
    left = False
    right = False
    walkCount = 10
    
    # Christmas Background scaled to fit the size of the full display
    
    christmas = pygame.image.load('winter.jpg')
    christmas = pygame.transform.scale(christmas,(650,600))
    
    
    def redraw_game_window():
        global right
        screen.blit(christmas,(0,0))
        print(right)
    
    # ///////////////////////////////////////////////////
    # MAIN LOOP
    # ///////////////////////////////////////////////////
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
            moving_x += walk
            right = True
            left = False
            print(right)
            if moving_x >= 615:
                first_screen = False
                second_screen = True
                moving_x = 0
        if keys[pygame.K_LEFT]:
            moving_x -= walk
            left = True
            right = False
            if moving_x == -50:
                moving_x = -10
        else:
            right = False
            true = False
            walkCount = 0
    
        if not jump:
            if keys[pygame.K_SPACE]:
                jump = True
                right = False
                left = False
                walkCount = 0
        else:
            if jump_count >= -10:
                neg = 1
                if jump_count < 0:
                    neg = -1
                moving_y -= (jump_count ** 2) * 0.5 * neg
                jump_count -= 1
            else:
                pygame.time.delay(200)
                jump = False
                jump_count = 10
    
        redraw_game_window()
    
    # ////////////////////////////////////////////////
    # PYGAME CLOSING UTILITIES
    # ////////////////////////////////////////////////
    
        clock.tick(120)
        pygame.display.flip()
    
    pygame.quit()

解决方法

如果没有按下 LEFT,rightleft 被设置为 False

if keys[pygame.K_LEFT]:
   # [...]
else:
   right = False
   true = False

您必须使用 if-elif-else 语句:

if keys[pygame.K_RIGHT]:
    right = True
    left = False
    # [...]

elif keys[pygame.K_LEFT]:
    left = True
    right = False
    # [...]

else:
    right = False
    true = False

但是,您可以简化代码:

right = False
true = False

if keys[pygame.K_RIGHT]:
    right = True
    # [...]

elif keys[pygame.K_LEFT]:
    left = True
    # [...]

else:
    # [...]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。