Task 3.4: Create the move method

  1. Inside your Player class, define a new method called move().

💡 Hint

  • Use the def keyword.
  • Remember to give it just one parameter: self.
  1. 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.

  2. Check if the player is pressing:

KeyActionPygame dictionary key
W keyMove uppygame.K_w
A keyMove leftpygame.K_a
S keyMove downpygame.K_s
D keyMove rightpygame.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]
  1. 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 your Player class.
  • The method checks which keys are pressed.
  • The method updates the player’s position_x and position_y.
  • The player image changes when moving!