|Top Previous Next|
I remember myself playing Pacman on a Sinclair Spectrum computer. These were the days! If you were a programmer, you had to make sure that your game has less than 48kb - or it wouldn't fit in the memory! Pacmen uses a "little" more memory - it is a Pacmen (several men :) not Pacman.
Function main has nothing special in it; let's take a look at the action that drives our hero:
Pacman is sensitive to other entities; if it collides / is run over by enemies, its kill_me event is triggered. The camera can pass through walls, looks down, moves with the player and is placed 1000 quants above the player.
Pacman and its enemy are using invisible "antennas" (markers) to detect their environment and act like smart creatures. I have used this technique in my first AI demo in AUM. In this picture you can see the pink antennas (run the game with pacmen -d markers if you want to see them too):
We are using four markers for pacman and four markers for the enemy, like below:
while (player != null)
As long as the player is alive (player != null) we reset motion_type (more on that later). We are using rotated coords for all the markers because the player and enemy are rotated models, too.
player_left.x = my.x;
We check the content of the player_left marker and if it is in solid, we add 1 to motion_type. If player_right == content_solid -> motion_type += 2, player_down == content_solid -> motion_type += 4,
It is clear that motion_type = 15 can never happen, but I wanted to show you all the possibilities.
So we have four markers, we check their content and depending on the result, we allow them (pacman and enemy) to move only if they choose a direction where the road is "clear".
if (motion_type == 0)
if (motion_type == 1)
if (motion_type == 2)
if (motion_type == 3)
if (motion_type == 4)
if (motion_type == 5)
if (motion_type == 6)
if (motion_type == 7)
if (motion_type == 8)
if (motion_type == 9)
if (motion_type == 10)
if (motion_type == 11)
if (motion_type == 12)
if (motion_type == 13)
if (motion_type == 14)
Now if the following line won't give you a headache then nothing else will:
if ((my.pan == 0 && motion_type < 8) || ((my.pan == 90) && (motion_type % 2 != 1)) ||
The action attached to the enemy is similar; the only big difference is that we generate a random number and depending on its value we set the direction for the enemy.
If you look at the first example, the enemy is coming from right to the left; the available ("free") directions are up and right. The enemy came from the right so it wouldn't look that clever if it would bounce off the wall and return where it came from so this possibility has been disabled in enemy's code. In this case, the enemy will go up. In the second example, the enemy is coming going up and it won't return down, but move to the left because I set this limit in its code for this situation.
The enemy can pass through dots so I am using this line in its action (the dots are passable):
move_mode = ignore_passents;
Here's the action attached to every dot:
The dots are aligned to player's z; this way I don't need to make sure that I place them at the proper height in Wed. If the player comes closer than 30 quants, the dot will be eaten and the score will be increased by 10.
The function that kills the player is simple; player's scale is lowered 10 times and then the player is removed.
If you want to move to the next level, check the score in a loop; score = 600 can take you to the next level if you have 60 dots in the level.
What about "real" AI? This enemy of yours does nothing but moves correctly in the level! This part is easy and is left as an exercise for you. Pacmen has four different enemies - I've coded the dumb one. Don't worry - I'll explain how to code the most Pac-thirsty enemy right here:
The enemy must make sure that its x and y coordinates are equal with player's x, y coords (this means that the enemy has "got" the player). When the enemy reaches the 1st red point, it can't come closer to the player - it must move to the right, towards the 2nd point. When it arrives there its motion type has changed; the enemy knows that by moving upwards the distance between it and the player is getting smaller (on y) so it will move towards the 3rd point (we aren't using any random value here). By choosing to move towards the 4th point (again, not a "random" decision) the enemy knows that the distance between it and the player (on x) will get smaller. The 5th point can be a random decision or not, depending on player's position.
And you thought that creating a Pacman clone is easy!
Shooting switches / statues
This piece of code will show you how you can move to the next level only if you have destroyed a certain number of idols / statues / switches / whatever you want.
Every statue is sensitive at shooting; their number is counted at startup. If the player comes close to a statue (or shoots it) a sound is played, the number of statues is decreased, the statue breaks in pieces and disappears.
What about shooting? Here's the event that is triggered when the statue is shot:
It doesn't look like having something to do with the statues, isn't it? If you look at action statue above you'll see that the statue is destroyed if the player comes close to it OR if skill10 = 1 - that's what the event does.
We must have another entity that takes us to the next level on impact:
When we impact with this entity, its change_level event will be triggered:
You can see that we can't move to the next level if we haven't destroyed all the statues. You will have to remove the comment for the load_level line and change the name to your_next_level.wmb