Task 3.4: Create the move method
- Inside your
Playerclass, define a new method calledmove().
💡 Hint
- Use the
defkeyword. - Remember to give it just one parameter:
self.
-
Use
pygame.key.get_pressed()to check which keys are currently being held down and save the result into a variable. This function will return a dictionary which can be accessed with the variables in the table below. -
Check if the player is pressing:
| Key | Action | Pygame dictionary key |
|---|---|---|
W key | Move up | pygame.K_w |
A key | Move left | pygame.K_a |
S key | Move down | pygame.K_s |
D key | Move right | pygame.K_d |
💡 Hint
- If the
Wkey is pressed, decrease the y position. - If the
Skey is pressed, increase the y position. - If the
Akey is pressed, decrease the x position. - If the
Dkey is pressed, increase the x position. - You can access a dictionary like so:
dictionary[dictionary_key]
- Change the player's image depending on the direction they are moving
💡 Hint
- When moving up, change the image to
poco_up.png. - When moving down, change the image to
poco_down.png. - When moving left, change the image to
poco_left.png. - When moving right, change the image to
poco_right.png.
🚩 Checkpoint
- A
move()method inside yourPlayerclass. - The method checks which keys are pressed.
- The method updates the player’s
position_xandposition_y. - The player image changes when moving!