Discuss Scratch

Axelory
New to Scratch
3 posts

How do i make an enemy move around intelligently?

I'm trying to make a game where you play as a soldier fighting another soldier. It has 5 sprites: enemy, Player, enemy's bullets, player's bullets, and cover. The cover spawns randomly and the player moves. How do i make the enemy both move around, and in cover while simultaniously shooting and dogding the player and the player's bullets?
I'd prefer some quick answers as i plan on using it to teach some younger students about scratch tomorrow.

Last edited by Axelory (Dec. 7, 2017 12:36:00)

ggenije
Scratcher
100+ posts

How do i make an enemy move around intelligently?

Maybe this will help you
TankBattle(with AI) 0.1v
Uniquename1
Scratcher
100+ posts

How do i make an enemy move around intelligently?

depends on a lot of things so it's good to share what you have to decide how they should behave. Like is it top down and how does cover work? can they shoot over it do the opponents bullets get stopped while yours don't somehow? what are the obstacles/ cover like etc?

You can try to think of some different situational situations they might be in and have them check for and react accordingly say if your guy is in a certain area of the screen. My soccer game in my sig has a lot based on where on the field things are and who has the ball. It's a lot there though so don't get confused.

Depending on their rate of fire and how your stuff moves you can have a test shot invisibly and instantly know where bullets are going to and relay this to your enemy and have him get out of the way. He could just always be doing evasive maneuvers if not taking cover or decide when you want him to do one or the other and have them reacting. just be sure not to do anything until done or to completion as he'll likely want to keep his options open for example don't move until or repeat until such and such position. Instead do if position is not such and such move 5 and try to keep it so he can break out of this cycle if anything changes. Too much info and for introducing to kids you'll want to simplify it with basic evasion and randomly taking cover unless they have experience and you're specifically working on AI or advanced AI.

Another thing for bullets instead of an invisible instant test shot for times if they're shooting rapidly or you just want it easier you can have a ghosted sprite centered on an end pointing toward the enemy and be a long (pole)/line thing testing if you're pointing at the enemy and telling him he should move but this would go through walls when bullets should stop at walls. Some thing in the player sprite like if direction equals direction to enemy sprite might do the same thing but again wouldn't account for walls.

I'd maybe come up with a game plan for the AI and try to have some variation. maybe a few cover spots he'll randomly choose to go to. maybe random advance and retreat attempts or based on health or health difference but again getting complicated especially since besides going to places you'll want them navigating around objects and take into account walls and such.

Also a fair amount of greater than and less thans instead of equals as things will rarely be exact. It's kind of a general question so idk. For kids i would suggest to keep it simple. maybe three boxes on his side and he moves up and down on y between them. maybe advancing forward to a second row of two boxes on occasion or if emboldened by your weakness. and maybe keep them both firing across a DMZ they can't cross to avoid having to move to another side of the box if you're flanking them or something.

You could even set it up like American gladiators and the enemy ai has to run this obstacle course and take cover at each station while your main guy stands behind some line and fires at them.

Try my games.
Scratch Sprite Club VI Soccer
Star Fighter Shooting
Space Marines: Boarding Party Strategy
Lion Runner Running/ Platformer
-ShadowOfTheFuture-
Scratcher
1000+ posts

How do i make an enemy move around intelligently?

What you're asking for seems to be pretty complex. These are just some thoughts I have:

You said that the cover spawns randomly; I'm assuming that you'll use clones to do this since that's the most effective way. The enemy AI would need to be able to sense the coordinates of the cover and determine which shelter is the closest to it. One way would be for all the clones to add their x and y positions to a list. Then, the enemy constantly loops through the list and evaluates which of the coordinates are the closest, using the distance formula.

The enemy AI would also need to be able to “see” incoming bullets and dodge them accordingly. I'm not entirely sure how this mechanic would work. Maybe you could use the directions of the bullets and trig functions to determine whether or not they'll hit the enemy, and on which side of the enemy they'll fall. The enemy would be able to “see” the bullets as they are coming towards it, and would evaluate how many bullets are being shot on either side of it, then move away from the bullet towards the side with less bullets. That would also need to balance the location of cover somehow…how, I don't know (Hey, I'm just an 8th grader!).

These are just some thoughts I have, and they may or may not work. Best of luck on creating the AI

Isn't it so fun to click the images in people's signatures?

Axelory
New to Scratch
3 posts

How do i make an enemy move around intelligently?

ggenije
Scratcher
100+ posts

How do i make an enemy move around intelligently?

First set center of enemy sprite properly

Then make new sprite “enemyTarget” which is invisible(you can add sprite for testing)
then make brodcast ChangeTarget and on “enemyTarget” sprite add "when I receive ChangeTarget go to random position(you have block for that)
when I receive [ChangeTarget v]
go to [random position v]

then make variable shooting that will countdown time when will enemy is shoot
(shooting)




And add next blocks to enemy

When flag clicked
go to x: (0) y: (130)
set [shooting v] to [5]//or how many sec you want to be delay between enemy shots
forever
if <(shooting) < [0]> then
set [shooting v] to [5]//or how many sec you want to be delay between enemy shots
point towards [Hero v]
create clone of [Projectile baddie v]

else
if <<distance to [EnemyTarget v] < [10]> or <touching [Cover v] ?>> then
broadcast [ChangeTarget v]
move (-5) steps//or negative value of speed of enemy
end


end
point towards [EnemyTarget v]
move (5) steps//or enemy speed
change [shooting v] by (-0.1)
end


Last edited by ggenije (Dec. 14, 2017 15:31:28)

Uniquename1
Scratcher
100+ posts

How do i make an enemy move around intelligently?

Pathfinding scripts can be pretty involved by itself. Here is a crude one. I havn't decided on a good run away or avoid option. This one is pretty aggressive all the time so far. I made some basic changes to the player script as well. Still needs work. https://scratch.mit.edu/projects/193645727/#editor

Try my games.
Scratch Sprite Club VI Soccer
Star Fighter Shooting
Space Marines: Boarding Party Strategy
Lion Runner Running/ Platformer
Axelory
New to Scratch
3 posts

How do i make an enemy move around intelligently?

Thanks. I now consider the game to be good enough. There's still one problem though. I decided to use ggenije's solution because it was the more similar to what i wanted from the game, the problem is that because the
point towards [ Hero]
block is nested inside the
point towards [ enemyTarget]
block it takes a lesser priority making the enemy continuously shoot at the target and not the player. It shouldn't be that difficult to fix though.
Uniquename1
Scratcher
100+ posts

How do i make an enemy move around intelligently?

Not sure what you're asking. Since the baddie is a circle his bullet does not need to point at target or in the direction he is going. I assume the target is where he is going to. he can go there and produce bullets along the way and when bullets are created they can just point to hero (not point in direction of hero and not point in direction of baddie). kind of like strafing. Then you can figure a way you want to have the baddie go towards the hero sometimes and toward his target destination other times and shoot towards you the whole time. Also you don't need to broadcast and then create clone you can just have the baddie or hero create clone of their respective bullets directly. Just makes it more streamlined. Also stopping other scripts in sprite stops them permanently and they can't be restarted unless you change them from a green flag hat to a broadcast hat and have another sprite rebroadcast whatever to begin those scripts again but I'm not sure why you're stopping the scripts to begin with. Here he is aiming like that but i'm afraid he's an unstoppable machine now. And player is at a disadvantage because they can only shoot up/ down, left/ right while the baddie has 360 degrees. You bullets too are cloning so fast that much is wasted and unseen. Not using a broadcast will allow you to slow it down which will increase the rate of fire or at least steady it and make it smoother.

https://scratch.mit.edu/projects/193798098/

Try my games.
Scratch Sprite Club VI Soccer
Star Fighter Shooting
Space Marines: Boarding Party Strategy
Lion Runner Running/ Platformer
ggenije
Scratcher
100+ posts

How do i make an enemy move around intelligently?

first way
You should create clone of projectile in baddie(and players also) sprite because when you receive broadcast shotByBaddie it make clones for every clone so you get much lag(for every clone it make clone)


////////////way below is more complicated and i don't recommend it(works same)
second way
but if you must to place broadcast make local var isClone and set it at “is flag pressed”=0 and in when start as clone set it at 1 and in broadcast check if clone=0

Powered by DjangoBB

Standard | Mobile