PacMan game field - how do I do?

As you see in the title, I’ve some problems in preparing the field for my PacMan.
I had the idea of basing it all on an int matrix, which is going to be filled with numbers: as it is -1, I have the walls; as it is 1, it’s gonna be the player coords; from 2 to 5 we have the ghosts coords.

Is there any algorithm that I can add to my program to help myself on setting the walls? Because I thought about a function that adds manually every single wall block; but it’s soooo long and time consuming, moreover it makes the readability of the code very difficult…

PS if you think that having a matrix is a bad idea plz suggest me something else because I’ve no better ones…

You could have an array of the vertical wall coords(walls up & down) and the horizontal wall cords(walls left to right), and checking if the player bumps into one. EX.

int[][] horizontal = {
{wallOneX1, wallOneX2}, {wallTwoX1, wallTwoX2} };
int[][] vertical = {
{wallThreeY1, wallThreeY2}, {wallFourY1, wallFourY2} };

etc.

An integer array is an acceptable way of achieving this for fairly basic cases. It does not need to be multi-dimensional as you can simply calculate the size based on the dimensions.

int[] field = new int[x*y];

It would be fairly easy, if tedious to build up fixed fields by adding static array fields to your code.

For example:

int[] test = new int[] {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0, -1
    -1,  0, -1, -1,  0,  0,  0,  0,  0,  0,  -1, -1,  0, -1
    -1,  0, -1, -1,  0,  0,  0,  0,  0,  0, -1, -1,  0, -1
    -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,
    -1,  0,  0,  0, -1, -1,  0,  0, -1, -1,  0,  0,  0, -1,
    -1,  0, -1,  0, -1,  0,  0,  0,  0, -1,  0, -1,  0, -1,
    -1,  0, -1,  0, -1,  2,  3,  4,  5, -1,  0, -1,  0, -1,
    -1,  0, -1,  0, -1,  0,  0,  0,  0, -1,  0, -1,  0, -1,
    -1,  0, -1,  0, -1, -1, -1, -1, -1, -1,  0,  0,  0, -1,
    -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,
    -1,  0, -1, -1,  0,  0,  1,  0,  0,  0, -1, -1,  0, -1,
    -1,  0, -1, -1,  0,  0,  0,  0,  0,  0, -1, -1,  0, -1,
    -1,  0,  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
}

If you actually want to generate such a field without anything being predefined it’s going to be rather more complicated. And thereason I say that is that’s the game pac-mac requires a maze without any dead-ends.

So while you could place walls haphazardly (random gen) you would have a decent chance of generating an invalid map. So I would advise going with predefined map ‘blocks’ of even dimensions (2x2, 4x4, 8x8) you can place against each other. You can use single grid space ‘blocks’ too but that will require a little bit more computational time.

I would recommend that you consider keeping wall data separate from where the player and ghosts are, the former shouldn’t change over the course of the game/level and the latter are constantly changing.

P.S.

Depending on how much of a clone you’re going for, you may want to read this section of the wiki page:

I wrote a pac-man game in processing/Java.
Consider viewing it.
Why reinvent the wheel when someone gave you their design?

[edited]

because that’s how you get better at programming :smile:

2 Likes

Why not improve an existing project?

Improve vs reinvent.

This is a very interesting question and a full answer could fill several volumes. :grinning:

There is no doubt that a programmer can learn a lot from both activities. The choice is down to the individual based on their existing programming experience, skill and personal preference.