hello everyone
this is part two on how to make a tower
defense game in scratch
in this tutorial i will be setting up
the enemy lists which will be used for
tower aiming and shooting
and i will also begin making our first
tower that we will be able to place on
the map
if you haven't seen part one check it
out link is in the description below
and before we start we will be doing
some code which involves lists
but i won't be going into too much
detail about them right now
because they'll be used in the next
video anyways
let's get started so first off before we
start creating the turrets
we have to first add some lists to the
enemies so let's go to variables
and then i'm going to make a list
and i'll call the first one um enemyx
all right and then keep it for all
sprites
and i'll create another list and then i
will call this enemy y
okay and then i'll create another list
and call it enemy progress
and i'll create one more and call this
enemy ids okay
so the reason i'm creating these lists
is because i need a way to track
all of the enemies on the screen and
this is so that the towers that i will
create
will know which enemy to target so of
course the enemy x and enemy y lists
will track the enemy x and y positions
and then the enemy progress list will
track the distance each clone has
traveled on the track
and the enemy id list will just have all
of the clone numbers
so i'm going to hide all of these lists
okay and then under the windfly clicked
i am going to drag a delete all and
change this to delete all
of enemy x and then also delete all of
enemy y
delete all of enemy progress and delete
all of enemy ids
and then i'm going to put this right
after the hide
okay so this just makes sure that all of
the lists are empty when the game starts
so now let's create a new variable and
then i will call this um
something like clone number okay
and then for this one make sure to
select for the sprite only
and this is because each clone will have
a separate value for this clone number
so select for the sprite only and then
click ok
all right and this clone number variable
will track
which clone is which inside of all of
these lists
so when the flag is clicked let's set
clone number to zero
and then inside of this repeat loop
let's change clone number by one
and then i'm going to put this right
before the create clone myself
and now each clone that is created
should have a different value for this
clone number variable
from 1 to 10. so now when a clone is
created
i'm going to add a item to the four list
that we created
so inside of the lists let's drag a
add and then i'm going to change this to
add
um nothing to enemy x
and this just adds an empty new item
into the enemy x list
and then i'm going to duplicate this
and then also add an empty item to the
enemy y list
and then for the enemy progress i am
going to add
0 for that and lastly for the enemy ids
i am going to add the clone number
alright so each enemy that is created
will have their own enemy x
enemy y and enemy progress which they
will change
as well as their own enemy id alright
so i'm going to drag this under the
winnestar as a clone
and now each enemy will update these
three lists
and by the way this might seem a bit
confusing but it'll make more sense once
i create the turrets
which will use these lists so inside of
the enemy movement block
i'm going to grab a replace item 1 of
block
okay and then i'm going to drag this
under the enemy movement custom block
and then for this i'm going to replace
the clone number
so replace item clone number of enemy
x with go to motion
the enemy x position okay
so and then i'm going to right click
duplicate this block
and instead of enemy x i'm going to
change this to enemy y
and of course i am going to replace that
with the y position
and then lastly for the enemy progress i
am going to duplicate this again
and change this to enemy progress and
then for this
sense this is going to show how far the
enemy has moved
i am going to actually go to variables
and then go to the lists
and then i am going to grab a item one
of enemy id's block and then i'm going
to change this to enemy progress
and inside of this right here i'm going
to drag
a clone number variable and then i'm
going to go to operators drag a plus
operator
all right and then drag this on the left
side
and then i'm going to add the move speed
on the right side alright so pretty much
what this does is that when the enemy
moves five steps
then the enemy progress will increase by
five
okay so now i am going to put this
inside of here
and then i'm going to drag this back
all right so lastly we have to add a few
more things
for when the enemy leaves the screen so
inside of the if touching edge
i am going to go to the lists again
and then drag a replace item of block
and then in here drag a clone number
variable
and change this to enemy x
and then inside of the value i think i'm
going to do
a blank okay and then
i am going to duplicate this change this
to enemy y
and also leave it blank and then lastly
i am going to go to the lists again
and this time actually um grab a delete
one of block and then i am going to grab
a
item number of block and then put the
clone number variable in here
and then this is going to be for the
enemy ids list so we can leave it like
that
and i will drag this back inside of the
value
all right so i know i'm sort of speeding
along with this but pretty much
now we know all of the clones x and y
positions
as well as their progress and their
clone numbers
so i'm going to put this inside of the
if touching edge
and then instead of hiding i'm going to
actually
delete the clone all right
and now i believe that should be fine
for the enemies
so if i just show all of these lists for
now
then when the enemies appear then they
should
add their values to these four lists as
you can see
and then once they disappear then this
enemy ids list
should decrease in size
like this and then the values should
turn blank
alright cool so of course there are no
changes to the game
other than these four lists but now we
can start creating the towers
so i'm going to hide these
and then i'm going to create a new
sprite
and for now i will make a simple tower
so i will make this a gray color
okay
and i think a black outline should be
fine
maybe a dark gray all right
and then for the tower i think i'll make
a simple
square maybe a lighter gray
with a larger outline all right
might make it a bit smaller
okay
and i'll make the outline a bit smaller
as well
okay and then i am going to create one
more rectangle
this is going to be how the turret
shoots
all right and i think
this should be fine
okay so this is my simple turret and
make sure that this square right here is
centered in the middle
and not this entire thing because
otherwise the turret won't rotate
correctly
so only center this square in the middle
okay so now let's go inside of the code
and i'm going to drag a wind flag
clicked
and then i'm going to go into looks drag
a set
size to a hundred percent drag a switch
costume to costume one
and oh yeah actually i'm going to create
a really simple shop
where you can buy and place turrets so
actually i am going to
set the size to something like 25
okay that's a bit too small so maybe 50
percent
60 65
okay so 65 and then
i'm gonna put this turret right over
here somewhere
so on the top right corner all right
and then i'm going to drag this go to xy
block
and pretty much what i'm planning to do
is that i'm gonna make it so
if i click this image right here then it
will create a clone of the turret for me
to place on the map
so to do that i am going to go to
variables and then create two new
variables
the first one is going to be called um
let's call it mouse action
all right and then click ok and then for
the second one
i'm going to create let's call this can
show radius circle
and then click ok alright so i want to
make it so that if i click this right
here
then it will create a clone of a turret
which will follow my mouse pointer
until i click again and then the turret
stays in place
so to do that we are going to use the
mouse action variable
so i'm going to first set mouse action
to none when the flag is clicked
and then i'm going to go to control drag
a forever loop
and then forever i'm going to check if
go to sensing if the turret is touching
the mouse pointer
and the mouse is down
so sensing again mouse down
then i'm going to go to variables
and then set mouse action to
something like let's say selected
okay so this is going to be the first
click which means that this sprite right
here
creates a clone of a new turret and then
the turret clone
should follow our mouse pointer so let's
go to control
and then let's drag a 1xr as a clone
and then inside of it i'm going to drag
a forever loop
and i'm going to drag an if else
statement
and check if go to variables
if the mouse action is equal to selected
which means that the clone should follow
the mouse pointer
then of course we are going to go to
motion and drag a go to
and change it to mouse pointer alright
and also make sure to create a clone
of myself once the original sprite is
clicked
and i'm also going to add a wait until
block
so wait until and then i'm going to
right click duplicate this
and put it in here and i'm going to say
wait until touching mouse pointer and
not mouse down
so i'm going to go to operators drag a
knot
and put the mouse down on there and put
it back
all right cool and now i'm going to put
this
right before the create clone of myself
alright so now if we try it then we
should have a clone which appears under
it
once we click the sprite right here
alright cool
so now of course we want to place the
turret once we click again
so i'm going to go to control and then
drag another when i start as a clone
and in this one i'm going to check
forever
if mouse down so sensing
if mouse down then i'm going to wait
until
not mouse down so this ensures that
the player does one full click so wait
until not mouse down
then i'm going to go to variables
and set the mouse action
to none okay
and for the clone i am also going to set
the size
to its normal size which was 100 percent
okay so now if you try it then
if i click then we have a clone and then
once i click again
then it stays on the track alright cool
so of course we all know that towers in
tower defense games
have ranges so now i'm going to create a
new sprite
and this is going to be the range for
this tower
and for the range i am going to create a
simple
circle so i will press shift
and then hold for a perfect circle
all right and i will make the fill a
white color
and i think the outline for this is fine
maybe a bit smaller
um okay i think an outline of four is
okay
and now inside of the code i'm going to
drag a when fly clicked
and then i'm going to first hide the
range sprite
so drag and hide and i think i'm also
going to make it
sort of transparent or sort of
see-through
so i'm going to change this to set ghost
effect
to 50 so let's try it out
oops okay it's hidden so show
all right i think that looks pretty good
and now
i'm going to create one more variable so
let's call this um
something like view radius
so this is going to determine how large
this circle will be
and now we should be good so let's go
back to our turrets right
and by the way i'm going to name this
turret
okay and also i'm going to name this uh
radius circle i guess
or turret range circle
all right and inside of the turret
sprite
i am going to first set the can show
radius circle
variable to no
all right and this means that the range
circle will be hidden
however once we are actually dragging
the turret
we want to show the range circle so
inside of the if mouse action is equal
to selected
i'm going to set the can show ready
circle
to yes and then
up over here once we actually place the
tower
then i'm going to hide it again so i'm
going to drag the cancho radius circle
to no all right cool so now inside of
the turret range sprite
we want to check forever and then i'm
going to drag in if
else so we want to check if
the can show radius circle variable is
equal to yes
then of course we want to show the
radius circle
so drag a show and we also want to make
it go to the mouse pointer
so let's drag a go to
and change this to mouse pointer all
right cool
and then inside of the else we want to
hide
and now if we try it out then if we
click
on the image then as you can see we have
the turret
along with the radius circle and if i
place it down
then the circle disappears all right
cool
and i think i'm going to make the turret
a bit smaller
so maybe set size to something like 75
percent
let's try it out and okay i think that's
fine
and now we have our radius
all right cool alright so let's try it
out again
and let's see if we can place multiple
turrets
um okay so let's see um
oh yeah one thing to change is that the
mouse action variable
should actually be for this sprite only
so
um sorry about that so let's create a
new mouse action variable
and then i'll actually add a space in
between it so mouse action
and then select for the spy only all
right
and then click ok and now i'm going to
replace
all of the previous mouse action
variables with the new one
so the one with the space
okay change this to the one with the
space
and this one all right and then i will
delete
my old mouse action variable
all right and now it should work with
multiple turrets
so if i try it out then all right cool
so now i can place multiple turrets and
the range always follows the one that is
following my mouse
and now i'm going to hide all of these
variables
and by the way the view radius variable
will be used
in the next video so i'll just have it
created for now
but i won't touch it and let's go inside
of the enemy as well
and hide the variables all right so now
we have our four
enemy lists that will be used for the
turret
as well as a simple turret shop where we
can place these down
on the map all right cool
anyways that's it for this tutorial if
you enjoyed it then give it a thumbs up
and subscribe too if you haven't already
again in this video
we mostly just completed the setup for
the turret aiming and shooting
which we will do in the next video
anyways that's it for this tutorial
see ya