【问题标题】:Python scope using Pygame使用 Pygame 的 Python 作用域
【发布时间】:2021-01-17 22:50:14
【问题描述】:

我有点困惑,为什么当我的 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()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    如果不按 LEFT,rightleft 将设置为 False

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

    你必须使用if-elif-elsestatement:

    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:
        # [...]
    

    【讨论】:

    • 是的,就是这样!谢谢兔子!没看左边的选项。
    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多