-
Notifications
You must be signed in to change notification settings - Fork 0
Part 2
Altan Gulen edited this page Nov 6, 2020
·
6 revisions
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
KeyListenerclass withinGame.javato receive keyboard input and act accordingly. Check out the lines that containimplements KeyListenerandkeyPressed()function inGame.javafor some starter code. - Create a
Directionenum in a new file to represent the four directions Pacman can travel (left, right, up, down) - Add the following to the
Pacmanclass- Member variable for the direction Pacman is facing. This should use the
Directionenum you create. - Function
setDirection()to update the direction Pacman faces based on the keyboard input. Based on the key pressed, pass in the correspondingDirectionenum 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.jpgas the image rendered to the screen.
- Member variable for the direction Pacman is facing. This should use the
- Add the following to the
Gameclass- Function
drawGameBackground(). This should be called from thepaint()function and be given theGraphicsobject. 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
KeyListenercode, 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
imgfolder