pythonpython-3.xpygamepygame2

Issue in pygame.key.get_pressed() for movement function


I'm quite new with python and coding. Trying to code a simple game using pygame I have found an issue that I can't solve in my movement function.

I have a function that uses the pygame method get_pressed() in order to set the direction of the player velocity vector (see below). With this code if in the game I hold down a and then press d the player surface changes direction even if i'm still holding down the a key, while if I do the opposite the player continues moving to the right as if I don't press the a key. I understand that this is due to the order of the if statements in the function and my question is: is there a way to make the player change direction in both cases?

I really hope that the question is clear and thanks in advance if someone will help me.

The function that sets the direction of the velocity:

def get_keys(self):
        self.vel = vec(0, 0)
        keys = pg.key.get_pressed()
        if keys[pg.K_a]:
            self.vel.x = -PLAYER_SPEED
        if keys[pg.K_d]:
            self.vel.x = PLAYER_SPEED
        if keys[pg.K_w]:
            self.vel.y = -PLAYER_SPEED
        if keys[pg.K_s]:
            self.vel.y = PLAYER_SPEED
        if self.vel.x != 0 and self.vel.y != 0:
            self.vel *= 0.7071

The update function:

def update(self)
    self.get_keys()
        self.pos += self.vel * self.game.dt
        self.rect.centerx = self.pos.x
        self.rect.centery = self.pos.y

Solution

  • For what you want to do you have to use the keyboard events. Note that you want to change direction as soon as a key is pressed. This is and event. e.g.:

    import pygame
    
    pygame.init()
    window = pygame.display.set_mode((400, 400))
    clock = pygame.time.Clock()
    
    pos = pygame.Vector2(window.get_rect().center)
    dx, dy = 0, 0
    speed = 5
    
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False 
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    dx = -1
                if event.key == pygame.K_d:
                    dx = 1
                if event.key == pygame.K_w:
                    dy = -1
                if event.key == pygame.K_s:
                    dy = 1
            if event.type == pygame.KEYUP:
                if (event.key == pygame.K_a and dx < 0) or (event.key == pygame.K_d and dx > 0):
                    dx = 0
                if (event.key == pygame.K_w and dy < 0) or (event.key == pygame.K_s and dy > 0):
                    dy = 0
    
        vel = pygame.Vector2(dx, dy)
        if vel.x != 0 or vel.y != 0:
            vel.scale_to_length(speed)
    
        pos += vel
        pos.x %= window.get_width()
        pos.y %= window.get_height()
    
        window.fill(0)
        pygame.draw.circle(window, (255, 0, 0), pos, 20)
        pygame.display.flip()
        clock.tick(60)
    
    pygame.quit()
    exit()