Task 3.4: Create the move method
- Inside your
Player
class, define a new method calledmove()
.
💡 Hint
- Use the
def
keyword. - 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
W
key is pressed, decrease the y position. - If the
S
key is pressed, increase the y position. - If the
A
key is pressed, decrease the x position. - If the
D
key 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 yourPlayer
class. - The method checks which keys are pressed.
- The method updates the player’s
position_x
andposition_y
. - The player image changes when moving!