Python scope using Pygame

Viewed 72

I am a bit confused why when my redraw_game_window() function is called the right variable isn't being carried over properly and its returning False when I press my right arrow key. Under, if keys[pygame.K_RIGHT]: it is returning True however. Do I need to call my redraw_game_window higher in the code. I figured it needs to be the last thing in the code. I left out the beginning of the code for easier reading.

So it is in fact activating when I press the right arrow key but not carried over even though I'm providing the global flag. What is happening here?

# 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()
1 Answers

If LEFT is not pressed, right and left are set to False:

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

You have to use an 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

However, you can simplify the code:

right = False
true = False

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

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

else:
    # [...]
Related