Page 1 of 2

Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 7:29 am
by GuybrushThreepwood
I'm making a pac-man game, and I need a lives counter where the lives are represented by little pac-men. Now I can't figure out how to make one of these images disappear when the appropriate amount of lives are depleted. IE: two lives are lost, two images disappear.
Anyone's help would be appreciated :mrgreen:

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 8:24 am
by skydereign
You could create an animation, much like how you would do an hp bar (since that is all it is). Something like this. That way you can set animpos equal to hp.

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 8:37 am
by GuybrushThreepwood
Didn't think about that. That works. :mrgreen: Also, I'm having real difficulties with the ghosts AI. How could I make it so they ghosts go around the walls, in a mostly random pattern, but if they come closish to pac-man, they start chasing him?

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 9:01 am
by skydereign
Well, do you know how to make them move around randomly and also move towards the player? If so then all you need to do is use an if statement to check if the player is near.
Code: Select all
if(distance(x, y, player.x, player.y)<200)
{
    // move to player
}
else
{
    // move randomly
}

Or do you also need help with either/or?

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 9:16 am
by GuybrushThreepwood
I have no clue how to do either of those things :oops:

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 9:25 am
by skydereign
Well, there are several ways to do it, but none of them I would say are particularly easy. Here is an example I made for another user. It uses shortest path computations via a grid. The code I think was pretty well documented, but it uses some slightly advanced stuff. Another approach is to use a node based decision system (where when a ghost reaches a node it picks the direction closest to the player, or one at random). That might be slightly easier for you. Either way, here's the file.

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 10:12 am
by GuybrushThreepwood
Ya, that demo made little to no sense. What's this node thing you mentioned before?

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 10:27 am
by skydereign
The idea is that you have your map, and at every intersection, you have an actor of some type (most likely some invisible actor). If I were to use this system, I would give them animations to show what type of paths are available, as in from that position can the player move right/left/up/down. Then when the actor collides with it, it sets the xvelocity and yvelocity appropriately (you can use random setting or directed).

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 10:52 am
by GuybrushThreepwood
I will try this out in the morning. But, before I go to bed, how would I make it random?

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 10:58 am
by skydereign
Well, each node would have a certain number of possible paths. The most choices would be a 4-way intersection. But the ghost is limited to only 3 choices, as it can't move backwards (same direction it came from). So you need to have a variable to determine which direction it is going, and then find a random number (each number represents a direction) that works.
Code: Select all
int previous_dir=dir;
do
{
    dir=rand(4);
}while(dir==(previous_dir+2)%4);

The (previous_dir+2)%4 just gets the opposite of the previous direction, so that it won't go backwards (since the loop will continue).

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 4:10 pm
by phyzix5761
Just a note. In the original pac-man 1 ghost is chasing the player, 1 ghost is chasing 20 pixels in front of the player, 1 ghost is chasing one of the other ghosts and the 4th ghost is chasing a random point in the map.

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 7:29 pm
by GuybrushThreepwood
So could I make them move relative to the player? Also, do I put that code into the ghosts, or the nodes? Or where? Is it a collision event? I'm really confused :?

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 8:18 pm
by skydereign
It would be in the ghost's collision event with the node. So I'm guessing you don't get the idea behind it? If so do tell, and I can explain it further. But, here's what it should generally look like.

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 10:35 pm
by GuybrushThreepwood
Oooohhhhh ok. This makes perfect sense. So, could I have maybe like a region relative to my pac-man where if the ghost is in it, would pick the path closest to the player? If so, how would I go about doing that? Or is that already scripted into your demo?

Re: Pac-man like Lives System

PostPosted: Sat Dec 24, 2011 10:46 pm
by skydereign
I didn't make the move to the player part, but you would put that code into the decision making (collision event).
Code: Select all
if(distance(x,y,player.x,player.y)<200)
{
    // code that determines direction to move in
}
else
{
    // code that is already in the collision event (random choice)
}

This won't technically be a perfect way of chasing the player since your level could have a wall divide, but generally you can use if the angle to the player as a way of determining which direction to go. Of course you'll need to use the same idea as the random selection, namely preventing directions that it is not allowed to move at.