Part 2 - agulen/PacmanJava GitHub Wiki

Goal: The player should be able to change the direction Pacman is facing by pressing the arrow keys on the keyboard.

Steps

  • Use Java's built-in KeyListener class within Game.java to receive keyboard input and act accordingly. Check out the lines that contain implements KeyListener and keyPressed() function in Game.java for some starter code.
  • Create a Direction enum in a new file to represent the four directions Pacman can travel (left, right, up, down)
  • Add the following to the Pacman class
    • Member variable for the direction Pacman is facing. This should use the Direction enum you create.
    • Function setDirection() to update the direction Pacman faces based on the keyboard input. Based on the key pressed, pass in the corresponding Direction enum to update Pacman's direction.
    • Within setDirection(), you should also update the image used to render Pacman based on the key pressed. If the left arrow was pressed, use pacmanleft.jpg as the image rendered to the screen.
  • Add the following to the Game class
    • Function drawGameBackground(). This should be called from the paint() function and be given the Graphics object. Use the Graphics.drawRect() and Graphics.fillRect() functions to ensure the background of the window is black. The size of the window is 500 by 500.

You are given:

  • Some of the KeyListener code, referenced above
  • The initializeAutoPaint() function, which will set up the process to constantly draw images to the screen. Once this function is called, the paint() is going to be called numerous times per second.
  • Images from the img folder