Discuss Scratch

StuffIsStrange
Scratcher
6 posts

coding for an enemy AI, gun shooting and killing enemy simply

Just thought you guys should know that im new to scratch, i suck. I get that im doing something a bit complicated but its a project i wanna work on.
Link to project is here to see what i've already done: https://scratch.mit.edu/projects/839386593/
Also im a total scratch noob apart from sprite creating i think.

What Im looking for is help really cause i dont know what im doing really.
Also all of the sprites Im working with their on my alt (which i gave up on cause the names too long and confusing) : https://scratch.mit.edu/projects/753663517/

GUN_PART


first of all how to code a gun properly that follows my cursor and shoots when i click. well just how to make it like a solid object and not pass through objects and disappear when it hits the enemies and kill enemies. Also have the bullets follow the cursor too. Also weapon swapping




EnemyAI_Part

How to make the enemies ai move around and shoot at the player.






Movement:

How to make the players sprites walking animations play with the players movements and changing of direction
top100000000
Scratcher
82 posts

coding for an enemy AI, gun shooting and killing enemy simply

GUN_PART:
To code a gun that follows your cursor and shoots when you click, you can use the following code:

1. Create a gun sprite and add the following code to it:

when green flag clicked
go to [mouse-pointer v]
point towards [mouse-pointer v]

when this sprite clicked
create clone of [bullet v]

This code will make the gun sprite follow your mouse pointer and create a clone of the bullet sprite when you click on it.

2. Create a bullet sprite and add the following code to it:

when green flag clicked
hide

when I start as a clone
show
go to [gun v]
point towards [mouse-pointer v]
repeat until <touching [enemy v]> or <not <touching [stage v]>>
move (10) steps
if <touching [enemy v]> then
broadcast [enemy hit v]
end
end
hide
delete this clone

This code will make the bullet sprite follow the gun sprite, move forward until it either hits an enemy or goes off the stage, and then delete itself.

3. To make the bullets not pass through objects and disappear when they hit enemies, you can add the following code to the enemy sprite:

when I receive [enemy hit v]
change [health v] by (-1)
if <(health) = (0)> then
hide
delete this clone
end

This code will reduce the enemy's health when it receives a “enemy hit” broadcast, and hide and delete itself if its health reaches 0.

4. To implement weapon swapping, you can create multiple gun sprites with different costumes and add the following code to the stage sprite:

when green flag clicked
set [current gun v] to [1]

when key [1 v] pressed
set [current gun v] to [1]

when key [2 v] pressed
set [current gun v] to [2]

when key [3 v] pressed
set [current gun v] to [3]

when I receive [swap gun v]
switch costume to [current gun v]

This code will switch the current gun sprite and costume when you press the corresponding number keys or receive a “swap gun” broadcast.

EnemyAI_Part:
To make the enemy AI move around and shoot at the player, you can use the following code:

1. Create an enemy sprite and add the following code to it:

when green flag clicked
forever
if <(distance to [player v]) < (100)> then
point towards [player v]
create clone of [bullet v]
end
move (5) steps
if <touching [player v]> then
broadcast [player hit v]
end
end

This code will make the enemy sprite follow the player sprite and create a clone of the bullet sprite when it gets close enough, and reduce the player's health when it touches it.

2. Create a bullet sprite (similar to the code in GUN_PART) and add it to the enemy sprite's code as well.

Movement:

To make the player sprite's walking animations play with the player's movements and changing of direction, you can use the following code:

1. Create a player sprite with multiple costumes for different directions and add the following code to it:

when green flag clicked
set [direction v] to [right v]
set [walking v] to [false]

forever
if <key [right arrow v] pressed?> then
set [direction v] to [right]



Sorry if this is a lot, but this should definatly help with making your game.
StuffIsStrange
Scratcher
6 posts

coding for an enemy AI, gun shooting and killing enemy simply

Holy Cow

Thank you this will definitely help a lot thank you very much .
StuffIsStrange
Scratcher
6 posts

coding for an enemy AI, gun shooting and killing enemy simply

Also what variables do i need just write down the names of them please. that would be amazing. is there a way a can make the gun shoot without having to click the gun but just clicking in general
StuffIsStrange
Scratcher
6 posts

coding for an enemy AI, gun shooting and killing enemy simply

Umm which is this block i cant seem to find the block

top100000000 wrote:

GUN_PART:

To code a gun that follows your cursor and shoots when you click, you can use the following code:

1. Create a gun sprite and add the following code to it:

when green flag clicked
go to [mouse-pointer v]
point towards [mouse-pointer v]

when this sprite clicked
create clone of [bullet v]

This code will make the gun sprite follow your mouse pointer and create a clone of the bullet sprite when you click on it.

2. Create a bullet sprite and add the following code to it:

when green flag clicked
hide

when I start as a clone
show
go to [gun v]
point towards [mouse-pointer v]
[u]repeat until <touching [enemy v]> or <not <touching [stage v]>>[/u]
move (10) steps
if <touching [enemy v]> then
broadcast [enemy hit v]
end
end
hide
delete this clone

This code will make the bullet sprite follow the gun sprite, move forward until it either hits an enemy or goes off the stage, and then delete itself.

3. To make the bullets not pass through objects and disappear when they hit enemies, you can add the following code to the enemy sprite:

when I receive [enemy hit v]
change [health v] by (-1)
if <(health) = (0)> then
hide
delete this clone
end

This code will reduce the enemy's health when it receives a “enemy hit” broadcast, and hide and delete itself if its health reaches 0.

4. To implement weapon swapping, you can create multiple gun sprites with different costumes and add the following code to the stage sprite:

when green flag clicked
set [current gun v] to [1]

when key [1 v] pressed
set [current gun v] to [1]

when key [2 v] pressed
set [current gun v] to [2]

when key [3 v] pressed
set [current gun v] to [3]

when I receive [swap gun v]
switch costume to [current gun v]

This code will switch the current gun sprite and costume when you press the corresponding number keys or receive a “swap gun” broadcast.

EnemyAI_Part:
To make the enemy AI move around and shoot at the player, you can use the following code:

1. Create an enemy sprite and add the following code to it:

when green flag clicked
forever
if <(distance to [player v]) < (100)> then
point towards [player v]
create clone of [bullet v]
end
move (5) steps
if <touching [player v]> then
broadcast [player hit v]
end
end

This code will make the enemy sprite follow the player sprite and create a clone of the bullet sprite when it gets close enough, and reduce the player's health when it touches it.

2. Create a bullet sprite (similar to the code in GUN_PART) and add it to the enemy sprite's code as well.

Movement:

To make the player sprite's walking animations play with the player's movements and changing of direction, you can use the following code:

1. Create a player sprite with multiple costumes for different directions and add the following code to it:

when green flag clicked
set [direction v] to [right v]
set [walking v] to [false]

forever
if <key [right arrow v] pressed?> then
set [direction v] to [right]



Sorry if this is a lot, but this should definatly help with making your game.
HundenBasse
Scratcher
100+ posts

coding for an enemy AI, gun shooting and killing enemy simply

Which of the blocks cant you find
StuffIsStrange
Scratcher
6 posts

coding for an enemy AI, gun shooting and killing enemy simply

red ones
CallenFlynn
Scratcher
0 posts

coding for an enemy AI, gun shooting and killing enemy simply

HundenBasse wrote:

REMOVE THE STRAY BLOCKS (why are there so many random blocks that aren't used just floating around in every project… Am i missing out on something here, maybe a secret bonus for most cluttered and unreadable project)

Those can be used for testing. in my current fps project i have a block that sets health to 0. when i click it in the editor my character dies and it runs all of the death scripts.
play sound [fail v]

Last edited by CallenFlynn (Nov. 16, 2023 15:50:00)

greatgamebro2
Scratcher
37 posts

coding for an enemy AI, gun shooting and killing enemy simply

erm wat da sigma
7lottowinner
Scratcher
6 posts

coding for an enemy AI, gun shooting and killing enemy simply

so I'm making a scratch game where this guy shoots something at a clone but when I coded it it didn't go to the clone it went to the original source, so how do I make the bullet go to the clone
Oisthebestletter
Scratcher
100+ posts

coding for an enemy AI, gun shooting and killing enemy simply

7lottowinner wrote:

so I'm making a scratch game where this guy shoots something at a clone but when I coded it it didn't go to the clone it went to the original source, so how do I make the bullet go to the clone
Please do not necropost AKA posting on an old topic. Create your own topic instead
Oisthebestletter
Scratcher
100+ posts

coding for an enemy AI, gun shooting and killing enemy simply

7lottowinner wrote:

so I'm making a scratch game where this guy shoots something at a clone but when I coded it it didn't go to the clone it went to the original source, so how do I make the bullet go to the clone
And yes, I do know how.

Powered by DjangoBB