Skip to main content

PYGAME: When i realeseI release two directional keys together the player keep moving

I did the code this way to avoid the player to stop if a press two directional keys together making the movimentmovement more smooth. It workworks well but when i realeaseI release two keys or more together the player keep moving in one of the directions of the key. I can't figure out how to solve this issue.

PYGAME: When i realese two directional keys together the player keep moving

I did the code this way to avoid the player to stop if a press two directional keys together making the moviment more smooth. It work well but when i realease two keys or more together the player keep moving in one of the directions of the key. I can't figure out how to solve this issue.

PYGAME: When I release two directional keys together the player keep moving

I did the code this way to avoid the player to stop if a press two directional keys together making the movement more smooth. It works well but when I release two keys or more together the player keep moving in one of the directions of the key. I can't figure out how to solve this issue.

Bumped by Community user
Bumped by Community user
Source Link
Otavio
  • 9
  • 1
  • 4

PYGAME: When i realese two directional keys together the player keep moving

I did the code this way to avoid the player to stop if a press two directional keys together making the moviment more smooth. It work well but when i realease two keys or more together the player keep moving in one of the directions of the key. I can't figure out how to solve this issue.

Here is my code:

def input(self, event, pygame):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            self.left_is_down = True
            self.speed_x = -self.velocity
        if event.key == pygame.K_RIGHT:
            self.right_is_down = True
            self.speed_x = self.velocity
        if event.key == pygame.K_UP:
            self.up_is_down = True
            self.speed_y = -self.velocity
        if event.key == pygame.K_DOWN:
            self.down_is_down = True
            self.speed_y = self.velocity

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT:
            self.left_is_down = False
            if self.right_is_down:
                self.speed_x = self.velocity
            else:
                self.speed_x = 0
        if event.key == pygame.K_RIGHT:
            self.right_is_down = False
            if self.left_is_down:
                self.speed_x = -self.velocity
            else:
                self.speed_x = 0
        if event.key == pygame.K_UP:
            self.up_is_down = False
            if self.down_is_down:
                self.speed_y = self.velocity
            else:
                self.speed_y = 0
        if event.key == pygame.K_DOWN:
            self.down_is_down = False
            if self.up_is_down:
                self.speed_y = -self.velocity
            else:
                self.speed_y = 0


def update(self):

    self.x += self.speed_x
    self.y += self.speed_y