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.
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