Skip to content

Learning Python Day: 57

Allowing Continuous Movement

  • want the ship to move right, with the right arrow key
  • have the game detect a pygame.KEYUP event so we’ll know when the right arrow key is released, then we’ll use the KEYDOWN and KEYUP events together with a flag called moving_right
  • when the moving_right flag is False, the ship will not move
  • when pressing the right arrow key, we’ll set the flag to True, releasing the key will set the flag back to False
  • Ship class controls all attributes of the ship, we’ll give it an attribute called moving_right and an update() method to check the status of the moving_right flag
  • update() method will change the position of the ship if the flag is set to True
  • call this method once on each pass through the while loop to update the position of the ship
  • changes to Ship.py
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *