# PacMan game field - how do I do?

**URL:** <https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319>\
**Category:** Coding Questions\
**Created:** [January 24, 2020, 6:37pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319 "2020-01-24T18:37:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImDaGr3en](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@ImDaGr3en](https://discourse.processing.org/u/ImDaGr3en)\
**Post date:** [January 24, 2020, 6:37pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/1 "2020-01-24T18:37:54Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [January 24, 2020, 8:59pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/2 "2020-01-24T20:59:33Z")

</div>

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.

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

```

etc.

---

<div class="post-metadata">

**Author:** ![Nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nathan](https://discourse.processing.org/u/Nathan)\
**Post date:** [January 24, 2020, 9:18pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/3 "2020-01-24T21:18:20Z")

</div>

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.

```auto
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:

```auto
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:

> **[Pac-Man | Gameplay](https://en.wikipedia.org/wiki/Pac-Man#Gameplay)**
>
> The player navigates Pac-Man through a maze with no dead ends. The maze is filled with Pac-Dots, and includes four roving multi-colored ghosts: Blinky, Pinky, Inky, and Clyde. There is a passageway from the left side of the screen to the right side, one energizer in each of the four quadrants, and bonus fruits that appear in each level.
> The objective of the game is to accumulate as many points as possible by eating dots, fruits, and blue ghosts. When all of the dots in a stage are eaten, that s...

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [May 14, 2024, 2:58pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/4 "2024-05-14T14:58:18Z")

</div>

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

[edited]

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 16, 2024, 8:24pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/6 "2024-05-16T20:24:29Z")

</div>

> [@LangdonS](#):
>
> Why reinvent the wheel when someone gave you their design?

because that’s how you get better at programming 😄

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [May 24, 2024, 12:35pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/7 "2024-05-24T12:35:43Z")

</div>

Why not improve an existing project?

Improve vs reinvent.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 24, 2024, 3:21pm UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/8 "2024-05-24T15:21:30Z")

</div>

> [@LangdonS](#):
>
> Why not improve an existing project?
> 
> Improve vs reinvent.

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

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.

---

<div class="post-metadata">

**Author:** ![ProcessingOrg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/processingorg/32/18341_2.png) [@ProcessingOrg](https://discourse.processing.org/u/ProcessingOrg)\
**Post date:** [June 26, 2024, 11:53am UTC](https://discourse.processing.org/t/pacman-game-field-how-do-i-do/17319/10 "2024-06-26T11:53:36Z")

</div>


