Discuss Scratch

aaronsacks2006
Scratcher
65 posts

Need help making NPC

I am trying to make an update to my tank game that allows you to play against a computer, but I am having trouble making the computer. PLS help. this is the project: https://scratch.mit.edu/projects/306578680/ look at the sprite called “computer tank”.
captaindrewboy
Scratcher
14 posts

Need help making NPC

Well, you want them to point at the player… and maybe move towards them… coding them a separate bullet type may also be required!

Well, this is a signature!
aaronsacks2006
Scratcher
65 posts

Need help making NPC

captaindrewboy wrote:

Well, you want them to point at the player… and maybe move towards them… coding them a separate bullet type may also be required!


did you look at the code?
ResExsention
New to Scratch
1000+ posts

Need help making NPC

Try:

when I receive [start_game]
point towards [player_tank]
wait (pick random (1) to (10)) secs
broadcast [AI_tank_fire]

That would be a script useful for a static turret. To add movement, you can try:

when I receive [start_game]
point towards [player_tank]
wait (pick random (1) to (10)) secs
broadcast [AI_tank_fire]
if <(pick random (1) to (3)) = [1]> then
if <(pick random (1) to (5)) = [1]> then
turn cw (pick random (45) to (135)) degrees
move (pick random (1) to (100)) steps
end
if <(pick random (1) to (5)) = [1]> then
turn ccw (pick random (45) to (135)) degrees
move (pick random (1) to (100)) steps
end
if <(pick random (1) to (5)) = [1]> then
move (pick random (1) to (100)) steps
end
if <(pick random (1) to (5)) = [1]> then
move (pick random (-1) to (-100)) steps
end
end
wait (1) secs

What that does is it points the tank in the direction of the player, and fires within the next 10 seconds. Then, by luck, the AI will either stay put and reload for another salvo, face another direction and move 1 to 100 steps that way, go forward 1 to 100 steps, or back up 1 to 100 steps. If there is anything else you need please let me know!

Last edited by ResExsention (May 3, 2019 17:28:41)


Infrequently active.

It feels weird to see how far we've come. I hope you're well, wherever you are!
ResExsention
New to Scratch
1000+ posts

Need help making NPC

Sorry, mistake, but my New Scratcher status won't let me edit anymore. If you want to let the AI continue to attack the player, you can put all that code within a forever loop, or a repeat until player dead block.

Also, in your bullet code you'll need to add:

when I receive [AI_tank_fire]

And then use the appropriate code to send the bullet flying in the player's direction.

If you'd like to add variable direction (like sometimes the AI will point the tank wrong on purpose) let me know as well.

Last edited by ResExsention (May 3, 2019 17:35:00)


Infrequently active.

It feels weird to see how far we've come. I hope you're well, wherever you are!
captaindrewboy
Scratcher
14 posts

Need help making NPC

I was just giving general ideas, sorry!

Well, this is a signature!
ResExsention
New to Scratch
1000+ posts

Need help making NPC

captaindrewboy wrote:

I was just giving general ideas, sorry!

Welp, seems like those ideas just got turned into a solid script.

Infrequently active.

It feels weird to see how far we've come. I hope you're well, wherever you are!
aaronsacks2006
Scratcher
65 posts

Need help making NPC

ResExsention wrote:

Try:

when I receive [start_game]
point towards [player_tank]
wait (pick random (1) to (10)) secs
broadcast [AI_tank_fire]

That would be a script useful for a static turret. To add movement, you can try:

when I receive [start_game]
point towards [player_tank]
wait (pick random (1) to (10)) secs
broadcast [AI_tank_fire]
if <(pick random (1) to (3)) = [1]> then
if <(pick random (1) to (5)) = [1]> then
turn cw (pick random (45) to (135)) degrees
move (pick random (1) to (100)) steps
end
if <(pick random (1) to (5)) = [1]> then
turn ccw (pick random (45) to (135)) degrees
move (pick random (1) to (100)) steps
end
if <(pick random (1) to (5)) = [1]> then
move (pick random (1) to (100)) steps
end
if <(pick random (1) to (5)) = [1]> then
move (pick random (-1) to (-100)) steps
end
end
wait (1) secs

What that does is it points the tank in the direction of the player, and fires within the next 10 seconds. Then, by luck, the AI will either stay put and reload for another salvo, face another direction and move 1 to 100 steps that way, go forward 1 to 100 steps, or back up 1 to 100 steps. If there is anything else you need please let me know!

Sorry I haven't been getting notifications. what about the walls?
Wainggan
Scratcher
500+ posts

Need help making NPC

change x by (speed)
if <touching [ wall] ?> then
change x by ((-1) * (speed))
end

~ parchii
aaronsacks2006
Scratcher
65 posts

Need help making NPC

Wainggan wrote:

change x by (speed)
if <touching [ wall] ?> then
change x by ((-1) * (speed))
end

yeah but he used the
 move () steps
block
Kraken_Games
Scratcher
100+ posts

Need help making NPC

aaronsacks2006 wrote:

Wainggan wrote:

change x by (speed)
if <touching [ wall] ?> then
change x by ((-1) * (speed))
end

yeah but he used the
 move () steps
block
move (speed) steps
if <touching [ wall] ?> then
move ((-1) * (speed)) steps
end

KRAKEN_GAMES


I live in the depths


.)
aaronsacks2006
Scratcher
65 posts

Need help making NPC

Kraken_Games wrote:

aaronsacks2006 wrote:

Wainggan wrote:

change x by (speed)
if <touching [ wall] ?> then
change x by ((-1) * (speed))
end

yeah but he used the
 move () steps
block
move (speed) steps
if <touching [ wall] ?> then
move ((-1) * (speed)) steps
end
yeah but if they hit a wall then they will be stuck there forever
StrangeMagic32
Scratcher
1000+ posts

Need help making NPC

aaronsacks2006 wrote:

Kraken_Games wrote:

aaronsacks2006 wrote:

Wainggan wrote:

change x by (speed)
if <touching [ wall] ?> then
change x by ((-1) * (speed))
end

yeah but he used the
 move () steps
block
move (speed) steps
if <touching [ wall] ?> then
move ((-1) * (speed)) steps
end
yeah but if they hit a wall then they will be stuck there forever
the “move ((-1) * (speed)) steps” would stop that, because once they hit the wall they go backwards


“Remember the worth of souls is great in the sight of God;”
- Doctrine and Covenants 18:10


I have since moved to @JollyWinter
aaronsacks2006
Scratcher
65 posts

Need help making NPC

StrangeMagic32 wrote:

aaronsacks2006 wrote:

Kraken_Games wrote:

aaronsacks2006 wrote:

Wainggan wrote:

change x by (speed)
if <touching [ wall] ?> then
change x by ((-1) * (speed))
end

yeah but he used the
 move () steps
block
move (speed) steps
if <touching [ wall] ?> then
move ((-1) * (speed)) steps
end
yeah but if they hit a wall then they will be stuck there forever
the “move ((-1) * (speed)) steps” would stop that, because once they hit the wall they go backwards
ok I will try it.

Edit: It might be a wile though.

Last edited by aaronsacks2006 (May 22, 2019 23:55:42)

EliosTheGameCreator
Scratcher
15 posts

Need help making NPC

Pls help i want to make the enemy npc follow the player when the player is a good small distance away, and when it touches the player it will broadcast to make the health go down. pls help. https://scratch.mit.edu/projects/638665998/editor/

Powered by DjangoBB