Legacy GM GM8.1 Lite- How to make a "pacman AI"? (In code)

A

Ann

Guest
Hi! This is my first post as I am new to these forums and also pretty new to GM. In fact I am quite clueless.
Feel free to delete this if its in the wrong place :)

I am making a prototype where you're a thief running from the police, which is why I need an artificial intelligence.
Imagine an AI like the ghosts in pacman :) That's what I need.
It has to stand still to begin with though - and only begin to follow if you get too close.
AND, one more thing: You are able to hide in secret walls by standing close to the secret wall and hitting space. This throws the police of and they return to their base position.
I have NO IDEA where to start or what to do.

Helpfull info:
Room and charactes snaps to an 8 by 8 grid. Sprites are 16(h) by 32(w). They move up, down, left and right.
I think the AI needs to avoid solid objects?
Room size is 560(w) by 192 (h).
Framerate is 30.

Too difficult? I am open to suggestions! Any ideas or advice? Fire loose! :D
Any help is most appreciated. Thank you & have a good day! :D
 

Attachments

Tthecreator

Your Creator!
The "walking" part is the hardest to do.
If you want something like the ghosts in packman, maybe you could google "game maker packman tutorial".

Otherwise, you would have make the agent move in a random direction. I believe there is a drop n' drag function for this, but you can also use code:
Code:
//create event
speed=5
Code:
//setting direction
direction=irandom(3)*90//irandom(3) creates a random value from 0 to 3, then multiply it by 90 to get a scale from 0-270
Now, A thing to consider is when to make him go another direction. to test the code, you can put the "//setting direction" inside a space event.
But in your game you might want to put this code inside a collision event.
Also you should put this code inside your police object.
 
  • Like
Reactions: Ann
A

Ann

Guest
The "walking" part is the hardest to do.
If you want something like the ghosts in packman, maybe you could google "game maker packman tutorial".

Otherwise, you would have make the agent move in a random direction. I believe there is a drop n' drag function for this, but you can also use code:
Code:
//create event
speed=5
Code:
//setting direction
direction=irandom(3)*90//irandom(3) creates a random value from 0 to 3, then multiply it by 90 to get a scale from 0-270
Now, A thing to consider is when to make him go another direction. to test the code, you can put the "//setting direction" inside a space event.
But in your game you might want to put this code inside a collision event.
Also you should put this code inside your police object.
---------------------------------------------
Thanks! Not sure If I understood it all though. What do you mean when you said : "you can put the "//setting direction" inside a space event"?
 

Tthecreator

Your Creator!
OWW that was a little vague, i agree. What I mean is a if "space is pressed" event, whenever you press space bar on your keyboard. This is just for debugging purposes and is naturally not desired in your end product.
Also, I only gave you some very basic code for what you want. Just get this first small mechanic down, and I'll help you afterward if you need it.
 
  • Like
Reactions: Ann

Bingdom

Googledom
I think i know what you are looking for, try this.
Put this in the step event for the obj_enemy.
Code:
if distance_to_point(obj_player.x,obj_player.y) <= 300 {
    mp_potential_step();
}
If you want to have line of sight, then you may want to look at collision_line.
 
  • Like
Reactions: Ann
A

Ann

Guest
Why are you using GM 8.1 lite though? studio is free and filled with more content
I got a weird installer thingy from school. Got my GM from that. I totally agree with you :eek:
I am planning on downloading the other version from Steam :D :)
 
A

Ann

Guest
OWW that was a little vague, i agree. What I mean is a if "space is pressed" event, whenever you press space bar on your keyboard. This is just for debugging purposes and is naturally not desired in your end product.
Also, I only gave you some very basic code for what you want. Just get this first small mechanic down, and I'll help you afterward if you need it.
Thank you so much! :D I have one more question though hehe :)
Ok, so if i understood you correctly, I could do it like this?
Within my obj_enemy I need to have a create event and a step event. In the create I write the speed- and in the step event I can set the direction and stuff? :)
 
A

Ann

Guest
I think i know what you are looking for, try this.
Put this in the step event for the obj_enemy.
Code:
if distance_to_point(obj_player.x,obj_player.y) <= 300 {
    mp_potential_step();
}
If you want to have line of sight, then you may want to look at collision_line.
Wah! This is perfect! :D Thank you so much.
Yeah. The enemy needs to avoid the walls/solid objects and such since its a maze game so this would fit quite well. And I can use the line of sight for when the obj_player hides or gets out of sight?
What else could I use the line of sight for? For example:
Lets say the enemy stands still but is turning around every few seconds; if he turns your way you will be in the line of sight - and he will then start to follow you? Is that something I could use the line of sight for? :)
 
V

VansiusProductions

Guest
Wah! This is perfect! :D Thank you so much.
Yeah. The enemy needs to avoid the walls/solid objects and such since its a maze game so this would fit quite well. And I can use the line of sight for when the obj_player hides or gets out of sight?
What else could I use the line of sight for? For example:
Lets say the enemy stands still but is turning around every few seconds; if he turns your way you will be in the line of sight - and he will then start to follow you? Is that something I could use the line of sight for? :)
use collision_line for the line of sight, very straightforward but can be laggy. You can set x1 and y1 the coordinates of the enemy, and x2 and y2 for the player. Then use an if statement to check if its noone (meaning no one is touching the collision line (it's arbitrary))
 

Tthecreator

Your Creator!
Thank you so much! :D I have one more question though hehe :)
Ok, so if i understood you correctly, I could do it like this?
Within my obj_enemy I need to have a create event and a step event. In the create I write the speed- and in the step event I can set the direction and stuff? :)
Yes, you can write the speed inside your create event, but if you want your police agent to stop, you'll have to set speed to 0, and speed to whatever when you want him to move agian.
 
Top