Part 3 - agulen/PacmanJava GitHub Wiki

Goal: Pacman should be able to move by updating its x,y coordinates. Pacman should change directions based on keyboard input.

Steps

Add a updateCoordinates() function within the Pacman class. This should be called from the draw() function within Game so that it is executed many times per second.

  • Java's coordinate system starts at the top-left of the window, which is (0,0). It increases as you go right in the x direction and increases as you go down in the y direction. Since we initialized our window to be 500 by 500, the bottom-right corner of the window is (500,500). Based on this knowledge, update the x,y coordinates of Pacman depending on the direction it is facing! You can update Pacman's position by one pixel on each call to updateCoordinates().
  • Ensure you handle the cases where Pacman runs into the boundaries of our Java window. The key coordinate boundaries for these are:
    • (0,0) - top left
    • (0, 500) - bottom left
    • (500, 500) - bottom right
    • (500, 0) - top right
  • Once you have boundary checking in place, you may notice that Pacman may still go off-screen when traveling to the bottom edge or to right-most edge, but will not travel forever. This is because when Java draws an image to the screen, like the Pacman image, it draws the image from the top-left corner of the image. If your Pacman coordinates are set to (10,10), those coordinates corresponds to the top-left point of the image. Since the Pacman image has a height and width of 20 pixels, Pacman would be drawn in a rectangle spanning from (10,10) to (30,30). What extra checks might you need to add to your boundary checking to handle this special case? NOTE: depending on how your operating system renders windows, you may need to account for some extra space on the bottom of the window. This is due to the taskbar that goes along the top of the window.

Tips

  • It's alright to run into some challenges! That's part of programming. To clarify things for yourself, put a System.out.println() statement in the Pacman draw() function and output Pacman's current x,y coordinates. As Pacman moves around the screen, you will see these values update in your IDE's console and gain a better idea of what your code is doing.