ZSAInfProject / PozaziemscyZaborcy

Space Invaders clone
3 stars 0 forks source link

Moving paddle blocks sometimes #2

Closed Bravo555 closed 6 years ago

Bravo555 commented 6 years ago

To replicate:

  1. Press D (paddle goes right)
  2. Press A (paddle goes left)
  3. Release D (paddle doesn't move)

Expected: since A is still pressed, I expect paddle to go left.

How to fix: https://github.com/ZSAInfProject/PozaziemscyZaborcy/blob/ce24046aa0da6ba9eca652d9ed62d2465889d0cb/player.py#L7 Here you overwrite velocity with a new velocity. You use it as so: https://github.com/ZSAInfProject/PozaziemscyZaborcy/blob/2e5b6cb57d80b2d67d2120a929f4766f1fbbd642/main.py#L22-L26

So when "A" key is pressed, a new velocity is assigned (-0.2), then when any key is released, the velocity is reset.

What you could be doing instead is to add new velocity to existing velocity:

def move(self, speed):
    self.velocity += speed

This way, when "A" key is pressed, the paddle stays in place (resulting velocity = 0). And when a key is released you can just subtract velocity corresponding with the key that was released:

if event.type == pygame.KEYUP:
    if event.key == pygame.K_a:
        player.move(0.2)
    if event.key == pygame.K_d:
        player.move(-0.2)