# Pac man ghost ai

### » Tue Jan 20, 2009 1:29 pm

Hi i m tryng to make a basic ghost movement for a Pac man style game.i know i m doing wrong with my logic, coould anyone have a look ?

EXCUSE MY ENGLISH

tHX

[url:am1xn6sy]http://files.filefront.com/Pac+mancap/;13035504;/f[/url:am1xn6sy]
B
1
G
3
Posts: 9
Reputation: 843

### » Tue Jan 20, 2009 2:23 pm

It does what you want except for two problems:
1) Sometimes it continues in the same direction.
2) Sometimes it chooses to turn into a block.

To fix the first problem I suggest that you give the ghost a private variable old_angle.
Then:

+ On Collision between Ghost and Block
+ while Ghost.old_angle = Ghost.GetAngle()
- Set Ghost.old_angle to floor(random(4))*90

I hope you can read my pseudo-code. Please let me know if you can't.

To fix the second problem I suggest you try something like David showed in this example:
viewtopic.php?f=16&t=2397
B
3
S
2
G
4
Posts: 271
Reputation: 2,017

### » Tue Jan 20, 2009 3:14 pm

Thank you stainsor, could you show me a cap example because i still don t understand the logic in this system , in fact i dont know where to put these information, is it sub event of collision ? how can i compare a private variable and a global ?

thank you sir
B
1
G
3
Posts: 9
Reputation: 843

### » Tue Jan 20, 2009 3:24 pm

here what i did from your help . i really don t understand how construct works.

[url:3bo9xmqb]http://files.filefront.com/Pac+man+2cap/;13036336;[/url:3bo9xmqb]
B
1
G
3
Posts: 9
Reputation: 843

### » Tue Jan 20, 2009 8:11 pm

[quote="maddle":3cqqvgio]i really don t understand how construct works.[/quote:3cqqvgio]
Have you tried a tutorial?
Scirra Founder
B
414
S
245
G
92
Posts: 25,188
Reputation: 200,287

### » Tue Jan 20, 2009 8:44 pm

Sure i did but i don t understand how the while function works.For me you can pseudo code somethong like this :

while Sprite.x<640 then add 1 to Sprite.X

I hope we will get some easy tutorials like pac man or space invaders to understand the logic.

I will work harder on this Beauty Soft until i understand it .

Thank Ashley and please keep on the good work !!
B
1
G
3
Posts: 9
Reputation: 843

### » Tue Jan 20, 2009 9:10 pm

That's pretty much what it looks like in event form too:

+ While
+ Sprite.X < 640
-> Set Sprite X to .X + 1
Scirra Founder
B
414
S
245
G
92
Posts: 25,188
Reputation: 200,287

### » Wed Jan 21, 2009 5:31 am

Okay, I posted a cap of what I meant here. There are some comments in the cap that explain the details.
B
3
S
2
G
4
Posts: 271
Reputation: 2,017

### » Wed Jan 21, 2009 12:15 pm

thank you Stantor , but your example doesn t work , the Ghost still pass trought the blocks !!

.In fact i understand the idea to test the condition :

While the new randomized Angle equal the one before collision continue to shuffle angle !

In fact : pick any angle but the last one before collision.

Simple !

but still doesn t work !! does it work for you ?

THX
B
1
G
3
Posts: 9
Reputation: 843

### » Wed Jan 21, 2009 2:20 pm

You're correct. This fixes the first problem: Your character will no longer continue in the same direction. But the second problem still remains. Try doing something like David did in the cap posted on this thread.

Now that I think about it if you can fix the second problem you'll probably fix the first problem anyway. So you may not need the code I gave you anyway.

I'm actually having basically the same problem with a Pacman style game right now. But I'm planning to use either Ball movement or grid movement. I suppose I should post about it in a new thread though.

edit: Here is the new thread.
B
3
S
2
G
4
Posts: 271
Reputation: 2,017

Next