So back in the spring, I was in a GUI class at my university that made me program a simple java based version of Pacman™. After screwing around with it for a bit more, I figured I might as well post it up on my site for others to enjoy or learn from (aka improve on my mistakes).
Technical Highlights
All of this code is made with Swing, which makes the animation on slower computers (and even some faster computers) a bit jaggy. The animations themselves are timed with java.awt.Timer’s, then the panel is repainted every 1/4 of a second at least, making that jagged effect happen. Yeah, I could have optimized the repaint function a little by making some buffers with the terrain and whatnot, but I was lazy.
There are 3 LinkedList objects being repainted: the terrain classes (Wall, Floor), the food classes (currently only “Pill”), and the Creature classes (Player and Monsters).
From PacCanvas.java
/**
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
public void paintComponent(Graphics gfx)
{
//super.paintComponent(gfx) ;
Graphics2D g = (Graphics2D)gfx;
int statusFood = 0;
ListIterator titerator = tstorage.listIterator();
while ( titerator.hasNext() ){
Terrain t = titerator.next();
t.draw(g);
}
ListIterator fiterator = fstorage.listIterator();
while ( fiterator.hasNext() ){
Food f = fiterator.next();
statusFood++;
f.draw(g);
}
ListIterator citerator = cstorage.listIterator();
while ( citerator.hasNext() ){
Creature c = citerator.next();
c.draw(g);
}
frame.setStatus("Level: "+curLevel+". Current Points: "+statusPoints+". Number of pills left: "+statusFood+". Number of lives left: "+lives);
if (statusFood == 0 && !end)
newLevel();
}
Each creature has its own timer that is called for animation and movement purposes.
The levels are basically preset by MazeGrid.java at 20×20 with declarations made in PacCanvas.java. An example is made below. 0 initializes a floor, 1 a wall, 2 the player, and 3 and above are reserved for monsters (these are all viewable from a switch statement made in the initial terrain linked list building function (wherever that is)).
From PacCanvas.java
transient final int lvl[][] = {
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,3,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,0,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,3,0,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,3,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,0,0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
1,2,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
}, ...
}
Everything was pretty much made public in terms of global variable declaration (actually more like default…), just so I didn’t have to worry about get and set statements and not having proper permissions, etc. If you want to do a better job, go right ahead
. Oh yeah, I also gave direct access to frames from the canvas and menus below it (or in it, so to say), again for ease of access.
For the final version, I disabled the music from playing at startup, but this can easily be enabled from withing the main “main” function. The music is played with the tritonus_share and zoom libraries. The RelativeLayout library is used for the dialogue boxes (I had to go to awt.dialogue boxes because the swing ones (JDialogue) didn’t work properly once the game was started for some weird reason).
The gameplay specifics can be found in the readme document in the zip file attached to this post. The main program itself is licensed under the GPL v3, with the source code being in the main jar file.
Pacmon Screenshot

In unrelated news, I had to use two alt codes in the title of this post.
PacMon Download