Site hosted by Angelfire.com: Build your free website today!

Pacman Applet Tutorial

Day 1

Source code for Pacman

So yesterday's tutorial left us with something that wasn't very exciting: an applet that draws a yellow square. I will be the first to admit that the applet was boring. I hope that if you are following you we able to modify it to draw other shapes. Today we will add something much more fun.

First, we will add four variables to keep track of movement. They are: x, dx, y, and dy. x and y will keep track of our position. dx and dy will keep track of how x and y will change each time the loop in the run() method is iterated.

The keyDown() and keyUp() methods are used to detect key pressed and releases respectively. Let's use the right arrow key as an example. When the key is pressed we change dx to 5 it start moving pacman to the right and change dy to 0 to prevent diagonal movement. Press the arrow keys and try it out!

When we draw our pacman we use the fillArc method. We are passing it x and y instead of fixed values to allow pacman to move.

That is it for today!

Next: Day 2.