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 withinGame.java
to receive keyboard input and act accordingly. Check out the lines that containimplements KeyListener
andkeyPressed()
function inGame.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 correspondingDirection
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, usepacmanleft.jpg
as the image rendered to the screen.
- Member variable for the direction Pacman is facing. This should use the
- Add the following to the
Game
class- Function
drawGameBackground()
. This should be called from thepaint()
function and be given theGraphics
object. Use theGraphics.drawRect()
andGraphics.fillRect()
functions to ensure the background of the window is black. The size of the window is 500 by 500.
- Function
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, thepaint()
is going to be called numerous times per second. - Images from the
img
folder