GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

PacMan

The PacMan can be moved with the cursor keys inside the grid. The goal of the game is to eat all the yellow dots (pills). It is chased by two ghosts, pinky and blinky . Both ghosts, as well as the PacMan itself, can only move inside the gray cells. Each ghost has its own pursuit strategy. The pink ghost is less aggressive. It moves around randomly and only attacks the PacMan when it is in its way. The blue ghost always knows the current position of the PacMan and recalculates the shortest way to it after each move. As soon as the PacMan is caught by a ghost, the game is over.

The original PacMan game was developed in 1980 in Japan and is still one of the most favorited computer and arcade game. Our example only shows a simple variation of the original. Some PacMan variations are played against four ghosts (Pinky, Blinky, Inky and Clyde) and move through different worlds.

Run this example

Edit this example in the Online-Editor



Download the program code (pacman.zip)

 

Explaining the program code:

The implementation consists of four classes: the application class PacMan.java, the actor classes PacActor.java and Ghost.java and the class PacManGrid.java, which is needed the layout the background.

The class PacManGrid builds a labyrinth in the backgorund of the game grid. Its grid has 30 horizontal and 33 vertical cells which are either walls, paths or paths with pills. Layouting the background is hard work. The less symmetric the labyrinth, the more difficult the layout prozess.

The class PacActor implements a GGKeyListener which registers the players key events. The method canMove() makes sure that the PacMan only moves on the gray cells. The method eatPill() hides the eaten pills and the method act() lets the PacMan's mouth open and close (changes the Sprites).

Both ghosts are instances of the class Ghost. They only differ in their pursuit strategies, which are defined in the method moveStrategy() and depend on the ghost type.