Pacman Type Game

Started by
9 comments, last by cdxrd 17 years, 10 months ago
I'm thinking that as my next game, I'm going to make a Pacman type game, except probably with a different theme than a pizza shaped thing eating ghosts. Anyways, I just have few questions about how Pacman was made. Well, actually it's one question concerning the entire game. I'm assuming the whole thing is controlled with an array, like certain squares are wall pieces and others are empty or have "food" in them. Then the player can move around and if it collides with a wall, it can't go that way anymore and if it collides with food, that space becomes empty and the player gets Points. Is this true?
Advertisement
Yes, that's correct :)

The array could contain, let's say, "0" for free space, "1" for a wall, "2" for a pill and "3" for a powerpill. Then when Pacman walks over a "2", it changes to a "0", and when he walks over a "3", it gets to "0" as well but his state would change (able to eat ghosts).

One very important thing that you shouldn't forget:

Play the original Pacman and notice, how you can smoothly change your direction into another "hallway" by pressing the arrow key a few seconds before the turn. In fact, if the hallway is horizontal and you walk to the left and press "up", Pacman walks just straight on, and then when he reaches another hallway which is really going upwards, then he also turns to that direction.

In some poor conversions, this kind of movement is not really implemented and it's frustrating, e.g. if you have to be EXACTLY at the right position to move into that hallway.
Oh, I've never noticed that. Maybe I've just never played a bad version, lol.

I'm thinking the most difficult part will be the AI of the ghosts. I've never really worked with AI. My smartest enemies just bounced off of walls and shot randomly, lol. How can I get them to know how to find me while avoiding walls?
Well the original Pacman has 4 different AIs, one for each ghost. I don't know how they are exactly, but they are kind of simple (remember that the original game is from 1981 or 82). You can do a little bit of google recherching and you'll find their descriptions very quickly.

I also programmed a little Pacman clone a while ago, and I just did it like that: Whenever a ghost runs against a wall, he decides randomly which direction to choose - BUT he always takes one that is a 90° turn (if possible), so he won't go just back to where he just came from.

Well that wasn't the best AI to think about ;) but I did that game just for fun. You can take that as a starting point though, and add a few things:

- make turns possible not only when a ghost hits a wall, but also whenever there IS the possibility. So when he's coming to a crossing point (if it's easier for you, you could even put that information right into the array), it gets randomly decided if he should go straight on or take one of the other possible directions.

- you can then also add that he checks where YOU are. If your position is the upper left, and the ghost is placed in the lower left but currently walking to the RIGHT, then when he gets to a crossing he should go more likely upwards, not downwards or continue with his direction. That's pretty easy, just a few "if" checks.

But you should think about that wisely, perhaps you should keep a little bit of randomness to those decisions. Because if every ghost just goes to where you are, the game could get too hard. I also think the idea of every ghost having his own strategy is a pretty good one.

Hope that helps a bit ;) and remember to google about the original AIs!
Also remember the typical Pacman level has no dead ends. If you should USE dead ends, then the AI must be a little bit better than just trying to look for Pacman's direction. For that purpose, you'll need some better algorithms like A* (google: A Star path finding) or something.
Well, if you wanted just a simple follow behaviour (ghosts try to get to you as fast as possible) you could do it this way:
1. Determine in which directions it is possible to move from the current position
2. Determine the player's relative position (up-left/ down /...)
3. Make one of the possible moves that gets you nearer to the player
4. If no such move is possible, just make any of the possible ones

This is of course very basic and simple, and does not result in clever enemies at all, they will get stuck, etc., but it might be enough for a start.
Quote:Original post by ZeHa
In some poor conversions, this kind of movement is not really implemented and it's frustrating, e.g. if you have to be EXACTLY at the right position to move into that hallway.


Yeah, my cellphone version is like that. Getting exacting position in a 1 1/2 inch screen makes me want to kill people. [wink]
This site has a lot of good information on the original pacman game.

http://jongy.tripod.com/GhostPsychology.html
Okay, I have another question. If you look at the image here:

http://www.simcoupe.org/images/screens/pacman.png

It looks like they used lots of different images for the blocks. It's kind of hard to explain my question, but it just seems like there's gotta be a better way than to make different blocks for all the different possible positions it could be in, and the different sides etc.
Quote:I'm assuming the whole thing is controlled with an array, like certain squares are wall pieces and others are empty or have "food" in them. Then the player can move around and if it collides with a wall, it can't go that way anymore and if it collides with food, that space becomes empty and the player gets Points. Is this true?


Quote:Yes, that's correct :)

The array could contain, let's say, "0" for free space, "1" for a wall, "2" for a pill and "3" for a powerpill. Then when Pacman walks over a "2", it changes to a "0", and when he walks over a "3", it gets to "0" as well but his state would change (able to eat ghosts).

Wrong! There are two ways to implement an array-based map. The first one is as you said - a cell can be either a wall or a space. This is not the Pacman way. In Pacman, each cell has four boolean values; can move up, can move down, can move left, can move right. This can be implemented as a four-bit number (eg. 1=up, 2=down, 4=left, 8=right). You will notice this is the case if you play Pacman. This system is a little more difficult to work out, mainly because there will need to be a thin spacing between each cell where a wall can be drawn. But it's not that difficult.

As far as the AI goes - yes, bouncing of walls with preference to 90 degrees is the usual solution. I have almost finished my Pacman clone (abandoned, but I'll be picking it up again soon). I found the ghost AI easy and surprisingly effective. Basically, a ghost makes a decision only when it hits a wall. It can move 90 degrees clockwise, 90 degrees anticlockwise, or 180 degrees. The latter option is always available and at least one of the first two. My ghosts make the decision based on a random number and Pacman's position. If I recall correctly, the options are ranked in order from most agressive to least agressive. Each ghost has an aggressiveness factor. This is combined with a random number to decide the direction to take.

The beauty of this is that it is very effective when you set the four ghosts to have different levels of aggressiveness. When they approach Pacman, they make different decisions and often end up surrounding him. It appears as if they're working in a team - it appears as if no effort was spared on programming the AI, when the truth is that I just got lucky on this one.

In order to implement ghost aggressiveness, at the beginning of each level I precalculate the shortest path from each point to every other point in the map. This may sound like a lot of paths, but the maps are so small that it doesn't even matter.

Send me a private message with your e-mail address and I'll send the unfinished version including source. It uses allegro. I'll include all the binary stuff too.

This topic is closed to new replies.

Advertisement