This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-07-24 12:06:32

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Enemy AI?

How do I make a enemy have AI?
I was thinking of haveing a set path like from A to B but I also want them to shot?
I saw the Dogfight 3D game so I know its possible.

Offline

 

#2 2007-07-24 12:16:40

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

You can use the "distance to" sensor to tell how far away a sprite is from an enemy sprite, to trigger an attack.  And the "direction" sensor to point the attack in the right direction.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#3 2007-07-24 12:19:58

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

Ok but how do I get it to move like lets say I want a gaurd paceing up and down the hall if he sees you he stops and shots you. Also how do I make walls?

Last edited by Slacker101 (2007-07-24 12:20:58)

Offline

 

#4 2007-07-24 14:06:40

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

Step 1 - lets get the patrol route sorted out.

There are a number of different ways of doing this, depending on how sophisticated you want to get.

You want your guard to be able to stop walking part way through his patrol, so you want his movement to be carried out in a number of small steps.  These steps will be repeated till he reaches the end of his patrol, then he turns around and it starts again. 

How to detect when he needs to turn around?  It could be by touching a sprite or colour (perhaps representing the wall) or it could be by location.

**********

By Sprite:
Patrolling between Waypoint1 and Waypoint2.  Your guard will walk in one direction till he touches Wall1, then turn around and walk towards Wall2

When green flag clicked
Set direction to (direction to Waypoint2)
Forever
- If touching Waypoint1
- - Set direction to (direction to Waypoint2)
- If touching Waypoint2
- - Set direction to (direction to Waypoint1)
move 10 steps

*******

Alternatively, you might want him to go left till he hits a wall, then turn around and walk right until he hits a wall.  Assumign the walls are drawn in black on the stage:

When green flag clicked
Set direction to -90
Forever
- If touching colour black
- - change direction by 180
- - move 10 steps      <-- this isto get your sprite clear of so it doesn't turn again)
- move 10 steps

*******
Finally, walking between 2 points:  Say you want him to patrol between X:0, Y:0 and X:100, Y:0.

When green flag clicked
Set direction to -90
Forever
- If X<0
- - Set direction to 90
- If X>100
- - Set direction to -90
move 10 steps

*****
The last is good if you want a route that has no visible boundies, and the guard isn't walking all the way up to walls before turning.

The second is nice and easy if you are using definate walls.

The first is particularly good if your path is not exactly horizontal or vertical - the two destination sprites can be anywhere on the screen, and if they are the same colour as the background the player won't be able to see them.  Its easy to add more waypoints, giving your guard a complicated route. 

EG:  A triangular route:
Forever
- If touching Waypoint1
- - Set direction to (direction to Waypoint2)
- If touching Waypoint2
- - Set direction to (direction to Waypoint3)
- If touching Waypoint3
- - Set direction to (direction to Waypoint1)
move 10 steps

******

Try those out and see which works best for you, then we can look at shooting, and walls.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#5 2007-07-24 14:18:59

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

This maybe really dumb to ask but how do I make a wall? I've made walls that teleport you somewhere else but not solid regular walls. Also I need to stop and shoot is there any way to do that?

Last edited by Slacker101 (2007-07-24 14:54:48)

Offline

 

#6 2007-07-24 15:00:36

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

Making a wall is as simple as drawing it on the background, or creating it as a sprite.

The tricky part is then telling your sprite to not walk though it.  Basically, whatever you are using to tell the sprite to move forwards needs to be ammended to say "but don't move if I'm touching the wall".

Look at the pacman game that comes with scratch for one example of how to do it.

That uses a small coloured dot, and constantly checks to see if that dot is in empty space.  If it is, it lets the pacman move forward, but if it isn't, it doesn't let it move forward.

Say your sprite has a red dot in front of it, and your clear paths are white, your script might look like this.

Forever
- if <<Key Up arrow pressed> AND <colour red over colour white>>
- - Move forwards 10


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#7 2007-07-24 15:01:38

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

ah I see great thx

Offline

 

#8 2007-07-24 15:01:43

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

For stopping and shooting - get your movement and walls sorted out first then we can look at the rest.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#9 2007-07-24 15:26:40

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

Ok all done the sentry is moveing and the good guy isnt going through walls. Many thanks. Also made a title screen with difficulty settings.

Last edited by Slacker101 (2007-07-24 22:14:21)

Offline

 

#10 2007-07-25 11:41:26

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

I still need to know how to get baddies to stop and shot at you. Plus I would like to know were people get the old Atari and NES sounds from.

Last edited by Slacker101 (2007-07-25 16:30:55)

Offline

 

#11 2007-07-25 16:37:03

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

Bump

Offline

 

#12 2007-07-25 16:53:47

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

OK, you need to choose a detection method.  How is your guard going to see you? 

You can do it with distance, or a combination of distance and direction, usign the "distance to " and "direction to" sensor blocks.

You could do it by comparing sprite X and Y values (probably more complicated)

Or you could do it by having a "searchlight", a cone-shaped sprite emanating from the guard like a torch-beam, that triggers teh shooting if its touching you.

I think the latter has a certain elegance to it, especially since the "torchbeam" doesn't have to be visible to the player.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#13 2007-07-25 17:01:32

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

I have actually thought about that searchlight concept but I'm haveing trouble and I have also thoguht of a way of haveing something fire someting else but it must be standing still(the shooter) but it seems the sprite I have assigned to be the projectile isn't recieving the broadcast that is supposed to be being sent.I do have an idea on how to fix that though. And it worked.

Last edited by Slacker101 (2007-07-25 17:14:23)

Offline

 

#14 2007-07-27 18:02:32

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

Can't figure out how to make a moveing sprite stop to fire abd then fire.

Offline

 

#15 2007-07-28 15:25:39

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

How are you going to decide its time for the guard to shoot?  Is he shooting at random interavals, or have you chosen a method for him to detect the player-sprite?


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#16 2007-08-02 15:36:18

Slacker101
Scratcher
Registered: 2007-07-23
Posts: 60

Re: Enemy AI?

I was thinking of if distance to sprite is under 200 then show (want to make it wait) and shoot.

Offline

 

#17 2007-08-02 16:13:25

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

OK - inside your movement loop you need a check to see if the player is close enough.

So if using the "waypoint method" I posted above:

Forever
- If touching Waypoint1
- - Set direction to (direction to Waypoint2)
- If touching Waypoint2
- - Set direction to (direction to Waypoint3)
- If touching Waypoint3
- - Set direction to (direction to Waypoint1)
- move 10 steps
[b[- If <(distance to playersprite)<200)
- - Broadcast "Guard1shoots" and wait[/b]

Note that needs to be inside the "forever" loop, immediately after the move 10 steps, *not* after the end of the forever block.

That will make the guard stop until the actions attached to a "When I recieve [guard1shoots]" block are completed.

Then you need a shooting method.
This can be a costume change on the guard sprite with a shooting "beam" - laser or stream of bullets on it (crude but effective)
Or a new sprite whose scripts cause it to launch at the player sprite and travel in that direction until they hit it or an obstacle or the edge of the screen.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#18 2007-08-03 04:41:54

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

A shooting script can be found here, btw:

http://scratch.mit.edu/projects/Mayhem/7672


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#19 2007-08-03 14:05:19

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Enemy AI?

Example patroling/shooting guard here:

http://scratch.mit.edu/projects/Mayhem/26727


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#20 2008-10-21 23:16:19

shockwave
Scratcher
Registered: 2007-12-29
Posts: 64

Re: Enemy AI?

Slacker101 wrote:

How do I make a enemy have AI?
I was thinking of haveing a set path like from A to B but I also want them to shot?
I saw the Dogfight 3D game so I know its possible.

ur interested in AI? visit my friends gallery, AI tournament. http://scratch.mit.edu/galleries/view/18028


=^..^= meow. Come look at Fact blaster or else.
http://scratch.mit.edu/projects/shockwave/2005372 • Also, my latest "educational" game, Cola Crisis:http://scratch.mit.edu/projects/shockwave/1702364

Offline

 

Board footer