#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Build a Pacman Game

edited December 2016

Hi everyone. I am a complete beginner with processing. I have tried to build a pacman game. I assume i will need to build it with a grid and let the pacman follow the grid. However, I'm unsure about how I would create the little bits of food around the grid and the ghosts to move around it. Any help would be great. The only bit of code I have so far is creating the pacman and making the mouth move. Also is there a way to create walls in the game for the pacman to move around or would I need to use the grid and draw lines. Thanks.

``````int radius = 12;
int direction = 1;
int direction2 = 0;

float x = 250;
float y = 250;

void setup() {
size(500, 500);
}

void draw() {
background(255);
fill (0, 175, 255);
smooth ();
noStroke();
render();

}

void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
x = x - 10;
direction = -1;
direction2 = 0;
}
else if (keyCode == RIGHT) {
x = x + 10;
direction = 1;
direction2 = 0;
}
else if (keyCode == UP) {
y = y - 10;
direction = 0;
direction2 = -1;
}
else if (keyCode == DOWN) {
y = y + 10;
direction = 0;
direction2 = 1;
}
}
}

void render() {
for ( int i=-1; i < 2; i++) {
for ( int j=-1; j < 2; j++) {
pushMatrix();
translate(x + (i * width), y + (j*height));
if ( direction == -1) {
rotate(PI);
}
if ( direction2 == 1) {
rotate(HALF_PI);
}
if ( direction2 == -1) {
rotate( PI + HALF_PI );
}
arc(0, 0, radius, radius, map((millis() % 500), 0, 500, 0, 0.52), map((millis() % 500), 0, 500, TWO_PI, 5.76) );
popMatrix();
// mouth movement //
}
}
}
``````
Tagged:

• Are you familiar with the concept of classes and OOP? You should consider having a look at the object tutorial in Processing's reference page.
For the actual anymation of the ghosts and pacman, you should consider using sprites. Check a recent previous discussion where @quark presented a good example (Focus in the first page of comments of that post). With sprites you can create the effect of the ghost's eye movement and the effect of packman chewing while they are moving.

Kf

• Did you do the tutorial on objects? Do it now

For the grid define a class Cell

cell can either be pacman, wall food or way

Write a textfile like LwLpffffff etc. to read the grid data from

• @TfGuy44 i did see you go through it when researching and my program didnt like when you started using world.dat and i couldnt find anywhere what exactly it was.

• world.dat is a 900 byte file that describes the pacman level.

You can generate a blank one if you run this simple two-line sketch:

``````byte[] temp = new byte[900];
saveBytes("world.dat", temp);
``````

It should appear in the sketch's data folder (press ctrl + k to view).

Just copy that world.dat file over to your pacman sketch and it will load a blank world that you can edit.

• It seems to me that Pacman isn't the "complete beginner" kind of game. I have seen a few people attempt it in this forum and I'm guessing there are many more that have tried and given up. I myself have no intention to do it, and only have a vague idea of how I would solve it.

It's not necessarily a simple game to code just because it's a simple game to play. But, having said that, you will learn some essential programming tools if you manage to go through with it.

• That's very true. To do Pacman properly you'll need to be comfortable with all the basics, plus a lot of the things new coders struggle with (arrays, Objects, files, etc). Even at 450 lines of code, my Pacman sketch is still missing things (score, bonus fruit, multiple levels, increasing difficulty, lives, menus, game overs, ghost house doors, kill screens, etc). You can easily make something beautiful with far less effort.

• Yeah im very surprised how difficult the pacman game is I did of course think because it was an easy game that I would be able to make it without any hassle. I also thought that about Mario but soon discovered horrible platforms. I have finally got that working, however the code doesn't like the line `return (data[j][i] == 1);` It gives back an error saying ArrayIndexOutOfBoundsException:30

• This Array index out of bounds exception only happens once the ghosts or pacmn leave the screen. I would need to add boundaries to each side or is there something else i could do so they dont leave?

• Just because Pacman is an old game and runs on rather simplistic, decades old computers doesn't mean that it is easy to code in any way....
Rather, it is just not that CPU or graphics intensive a game as today's games.
Snake is a little easier, so try that first if you don't understand how to make Pacman.
This advice holds to all people new to programming who wish to code Pacman.

Back in the day, the graphics part of the program was even more difficult to code.

• Im doing this as part of a college assignment and someone has already made a snake game so Im doing something different. I'm almost finished the pacman game I just have to add food and not let him go past the walls at the screen edges.

• Ok, but the advice will still come of use to other newbies.

• Absolutely. I now advise any other newbies to stop while they're ahead and try something far simpler

• too late shaunafitzgoo.. HELP