hello everyone
this is part four on how to make a tower
defense game in scratch
in this tutorial i'll be adding a lot of
improvements to the turrets
including a firing animation
customizable range
adding a base and making the turret
unable to be placed on the track
or on other turrets if you haven't seen
parts one through three
check them out links are in the
description below anyways
let's get started so first off i want to
add a firing animation to the turret
because right now all the turret does is
point and shoot at the enemy
however i wanted to switch to a firing
costume so i'm going to go to the turret
sprite
and then i'm going to go into costumes
and i'm going to duplicate this costume
and over here i am going to draw
some type of firing thing to show that
the turret shot
at the enemy so something like this
okay i am going to make the outline
orange
maybe a dark orange all right
and for the fill i'm going to have a
sort of yellower orange
so something like this okay
and for the outline size i think a size
of 7 is fine and i will select this
and click the back button right here so
it's going to go under the turret
and that should be fine so
i'm going to make a few final
adjustments
and all right cool
so now we have the turret firing costume
and i'm gonna go back to the code
and inside of the code i am going to go
to the part where the turret shoots the
bullet so over here
and then i'm going to go to looks and
drag a switch costume
to costume 2. so this is the turret
firing costume
so right when the turret fires i'm going
to make it switch costume to costume 2
and then i'm going to make a weight
something like
let's say 0.05 seconds
and then i'm going to make it switch
costume back to
its original costume which was costume
one
all right and if we try it out then if
we place down a turret
then when it shoots as you can see it
does that quick
firing animation like so and yeah
i think that looks a bit better and now
the next thing i want to do
is improve the turret range so right now
i only have
one size for the turret range however i
want to make it so that if i change this
view radius variable right here
then the size of my turret range will
also change
so for example my view radius of 100
works right now for this view radius as
you can see
however if i change this to something
like 50
then i want the size of the view radius
to also decrease
to 50. so to do that it's actually not
too complicated
so first i'm going to set this back to
100
and what we have to do is to drag a set
size to block
and i'm going to put this over here so
inside of the forever loop
and what i'm going to do is that i'm
going to drag
a divided by operator and also a
multiplication operator
and then i'm going to put this divided
by operator on the left side of the
multiplication operator
and then i'm going to go to variables
and drag the view radius variable
onto the left side of the divided by
operator
and then i'm going to drag this all
inside of the set size to block
so now to make the turret range sprite
increase or decrease size
based on the view radius variable we
have to put whatever value of the view
radius that works
inside of here and then the current size
of the turret range
inside of here so in my case a turret
range of size 100
with a view radius of 100 works for me
but it depends on how big you make your
view radius image in your costume
so for mine i'm going to put in a
hundred
in this part right here and this
is the view radius variable and then in
this part
i'm going to also put in 100 because
that's the size of my turret range
so now if i drag this inside of the
forever loop
then the turret range should work as
normal so the turret should shoot
in here as you can see it does however
if i change the view radius to something
like 50
then the size should also change
accordingly
like this and i'll put this in here
as you can see the turret only shoots in
range of this view radius right here and
if i try something like 150
then the turret range should be much
larger and the turret should only shoot
inside of here
and cool so that works
and then if i try something again like
70
then it should also update and work
like so all right cool so again
this number right here you put in
whatever your original
view radius was so in my case it was 100
and then in this number you put in
whatever size your turret range was
that worked with your view radius of
this number so in my case that was 100
also so now it should work as normal
all right cool so now that we're done
with our customizable turret range
i want to now add a base to the turret
so something
like this where it's like a
square at the bottom of the turret
like so
so i sort of want to add some base like
this however i can't do that
and the reason is that when i place down
a turret then the base
also rotates with the turret like this
so i want to add a base to the turret
that does not rotate
and in this case we have to create a new
sprite
so over here paint a new sprite
and then for the base i am going to
create sort of like a rounded rectangle
so i'm going to first shift hold click
and then drag a rectangle and center
this
and then to make it rounded i am going
to click this button right here
and then i'm going to create two more
points let's say
one over here and one over here
and then i'm gonna click the middle one
or the edge point
and then i'm gonna delete it and there
we go
we have a rounded edge and then i'm
going to do the same thing for the other
three sides
something like this all right and i
think
this is a pretty good base for my turret
so now i'm going to click this
and then increase the outline size a bit
to
i think eight is fine yep so a size of
eight
and then i'm going to copy this and
paste this back into the turret costume
to see how this looks okay i think it's
a bit large
so i'm going to go back to my base
sprite
and then decrease the size a bit all
right
and then i'm going to copy it go to my
turret sprite and then paste it
to see how it looks okay
i think maybe still a bit too large so
i'm going to make it smaller
alright and then also for my turret
sprite
i'm going to make this part a bit longer
so something like this all right
and then i'm going to select all of this
ctrl c or copy it and then go to my
firing costume
and then delete these two and paste
all right and
i am going to drag this forward a bit
so over here and that should be fine
all right so now we have my base image
for my turret
and i'm just going to name this space
okay
and then inside of the code i am going
to first go to my turret
so instead of my turret when i place
down the turret
or actually when i click this turret
over here
then i want to create a clone of the
base which follows the turret
so i'm going to go to control and then
drag a
create clone of myself and then i'm
going to change this
to the base and now inside of the base
sprite
i am going to go to events grab a
windfly clicked
and then i'm going to actually first
show
the original bass sprite because i want
this bright
to be shown over here so i'm going to
show it
and then make it go to somewhere around
here
so that's like x212
and y159 alright and then
i'm going to make the base the same size
as the turret
so the turret size is 65 percent so
inside of the base
i am going to set size to 65
all right and now i'm also going to make
the base
go to the back so inside of looks let's
drag a go to front layer
and change this to go to back all right
and now the base is under the turret and
i'm going to change the y position a bit
to maybe like 164
162. okay so now as you can see
in the shop area the base is exactly
under the turret
and now i think the turret looks nicer
now that we have a base
however the base does not follow the
mouse pointer when we
drag the turret so since the turbosprite
created the clone of the base when we
click on the turret
then inside of the base we need to grab
a when i receive clone
or when it starts a clone then
let's grab a set size 2 block first of
all
and then let's see the turret size when
it's selected
so that's 75 percent so let's set the
base size to also
75 percent and then
let's see we also need to create a new
variable
and we have to make the same variable we
have for the turret sprite
which is the mouse action variable and
then we have to select
for the sprite only and then click ok
all right and then right under here
let's
set mouse action to
selected since when the turret creates
the clone
once we click the turret that means
we're selecting the turret and the base
as well and then let's go to control
grab a forever loop and then i'm going
to forever
check if the mouse action
is equal to
selected so if we're currently selecting
the turret
and the base then i'm going to make the
base
go to the mouse pointer
so grab it go to and then change this to
mouse pointer
all right cool so let's try it out once
we click this turret
then as you can see the base also
follows the mouse pointer
however if we place down the turret then
the bass stills follow the mouse pointer
so let's fix that so we have to go
inside of the turret
and then inside of here let's go to
events
and drag a broadcast message one and
then
i'm going to change this to something
like broadcast
let's call it place turret all right
and then let's drag this under the wait
until
not mouse down so once we click again
then we are going to place the turret
and then inside of the basebrite
we are going to receive the place turret
message
so essentially the turret is telling the
base that we have placed the turret
and then in this case we want to set
the mouse action to none
all right cool and since the mouse
action is none
then the bass won't follow the mouse
pointer anymore
and now if we try it once we click it
follows
however if you click again then the base
stops moving
along with the turret all right cool and
now as you can see
the base does not rotate however only
the turret head
does so let's try it again as you can
see
the base does not rotate and only the
turret does
and cool i think that looks a lot better
um i think it looks like an actual
turret now
but i think i'm going to make the sizes
of the turrets a bit smaller
so i might change this to maybe 50
and 40 for these two values and then
instead of the turret i'm also going to
change these so
40 and 50
and if we try it again then
the turret is smaller and replace it all
right cool
i think this is a more appropriate size
and yeah
all right cool so now that we have the
base down one more problem
is that we can place this turret
anywhere on the map
and also we can stack these turrets so
we have to fix these problems
i am going to duplicate the base sprite
to create a turret glider sprite
and this sprite is going to essentially
check whether it's touching the track
or the other turrets so i'm going to
call this
something like turret collider
all right and then inside of the
costumes
i am going to select the rectangle tool
and then draw a rectangle over the base
and i think i'm going to make it a bit
smaller than the base
so something like this all right
and then i'm going to take out the
outline so outline 0
and then for the color i think i'm going
to make it a super
obvious color so maybe a red
all right and then i'm going to delete
the base
okay so now inside of the code for the
turret collider
um let's see so under the wind fly
clicked
i'm going to first go to looks and then
set
the ghost effect to 100
so we want the turret glider to be
invisible and i'm also going to actually
hide the turret collider
so i'm going to hide this
and in this case we don't actually need
the set ghost effect
and we also want the turret glider to
follow the mouse pointer when we touch
the turret
because we want it to detect whether
it's touching a track or another turret
so in this case let's actually go to the
turret
and then when we click on the turret we
want to also
create clone of the turret collider
okay and then inside of the turret
glider sprite
once the user is dragging the turret we
want the collider to actually
show so let's track a show under the
when i saw as a clone
and then let's see um
let's keep all of this mouse action
stuff and i'm also going to drag a go to
front layer block
and put this inside of the forever loop
because i want the collider to show on
top
of everything and then inside of this
mouse action equals selected part
i'm going to check let's grab an if else
if the turret lighter is touching the
track
so if touching and oh yeah we actually
can't detect
if it's touching the track sprite
because the track is part of the
backdrop
so inside of the stage let's actually
select this track
so i'm going to actually do control x or
cut
and then i'm going to create a new
sprite
and inside of the costumes i'm going to
paste
so now we have our track sprite by
itself and
i'm also going to quickly drag a go to
xy block
and set it to zero zero alright so now
we have our track
and i'm also going to make it go to the
back layer
so everything goes in front of it let's
go to back
and now we should be fine we have our
track sprite as normal
okay and now inside of our turret
collider sprite
we can detect if touching the track
so i'm going to name this track
okay then i'm going to make the collider
slightly visible
so i'm going to drag a set and then
change this to ghost
to something like 20 so make it sort of
transparent
else if it's not touching the track that
means we're okay
so i'm going to set the ghost to 100
so that makes it invisible and now if we
try it out
then if we click on the turret and go
over the track
then as you can see the collider turns
red however once we leave it
then it hides alright cool however one
thing
is that we can still place the turret so
to avoid that
let's go to variables and then i'm going
to create a new variable
and call this something like um can
place
tower okay and then i'm going to select
for this sprite only all right
and then i'm gonna check if it's
touching the track
then of course we don't want to place
the tower so let's set
can place tower to no else
we can place a tower so i'm going to set
it to yes
however the code that is used to place
the turret is inside of a turret sprite
so we want to move it inside of the
turret collider sprite
so inside of the turbosprite let's
find the part where we place the turret
so it's over here so let's drag this
entire block
and then move it inside of our turret
collider
so like this all right so now we have
the turret placing code
inside of the turret collider and now
inside of the turret
we can take this part out and
back inside of the collider
let's also add an if statement and check
if can place tower
is equal to yes so
pretty much since we set can place tower
to no
if the clyder is touching the track that
means that we can only place down the
turret
once the glider is not touching the
track so once we place down the turret
we want to of course set the mouse
action to none
and also we can take out the set cancho
radius circle to no
because that's part of the turret which
we will change later
but let's also set the ken place tower
to no and now since we took this block
out of the turret sprite
the turret still needs to set the mouse
action and the can show radius circle
variables
so inside of the turret sprite let's go
to events
and drag a when i receive place turret
that means the turret is placed down
then
we want to set
can show radius circle to no
and also set the mouse action
to none all right cool
and everything should work so if we try
it out
then if we try placing it down on the
track
then we can not however if we place it
normally
then we can all right cool
so that works however the next thing we
have to do
is to make it so that it cannot be
placed on top of other
turrets so let's do that so let's see
um so i'm going to right click and
duplicate my base sprite
and then i'm going to name this let's
call this a
placed turret lighter
so as the name suggests this collider is
only going to show
when the turret is placed so i'm going
to go inside of the costumes
and for the place turret glider it's
going to be the exact same size as my
normal turret glider
so i'm going to select this and then
control c
and then inside of the place turret
lighter i'm going to delete this one
and then ctrl v all right and then for
the color
just to um know which one's which i'm
going to make this
a different color so i might make this a
let's say purple all right
and now inside of the code i am going to
hide the place turret glider
so hide when the flag is clicked
and everything else should be okay
um all right so we actually don't need
the go to x or set size or go to back
layer
so i'm going to take these out and then
inside of the windows of clone i'm going
to grab also a hide
just in case and i'm going to set mouse
action to selected okay
and also all of this and i'm also going
to drag a go to front layer
inside of the forever loop just to make
sure that this clutter
goes on top of everything else and
under the when i receive place turret
which means that the turret is placed
then i want to show the place turret
glider
and also set the mouse action to none
which we already have here and now one
last thing i want to do
is that inside of the normal turret
collider
after it places a turret i want to
delete its clone so delete it
all right now finally in the turret
glider
we also want to detect if it's touching
the track
or touching the placed turret collider
then of course we cannot place down the
turret
so let's drag the place turret glider
and the ore
and drag this back and oh yeah one last
thing i forgot to do
is that inside of the turret sprite i
have to also
create a clone of the placed turret
collider
so i'm going to go to control and then
create clone
of placed turret collider and now let's
try it out
so if we place the turret
all right here is the clyder and now
as you can see we cannot place the
turrets on top of each other
so it works all right cool
so now of course i want to hide the
colliders once they're placed
sew inside of the place turret glider
sprite
i'm going to go to looks and then drag a
set
ghost effect to 100 when the flag is
clicked
right so let's try it again now the
place turret gliders do not show
however you cannot place turrets on top
of each other
or on the map
alright cool so everything seems to work
and yeah now we have better turret
placement
and one last thing i want to do is that
i want to create a layering system
because for example um sometimes for
some reason the base
might actually be on top of the turret
or something like that
like this for example so i just want to
make sure that everything
is ordered correctly and to do that i am
going to go through all of these sprites
and set the correct layer so first off
in the turret
um i am going to grab another
when i start as a clone and a forever
loop
and then i'm going to forever make it
go to the front layer and i'm going to
make it go
backwards one layer
okay and then of course we want the base
directly
under this turret so inside of the base
in the forever loop i am going to make
it go to front layer
and then make it go backwards something
like five layers
okay just to make sure that the base is
behind the actual
turret and then for the turret range
we want to make sure that it's behind
the turret and the turret base
so in the range i am going to make it
go to front layer and make it go
backwards
let's say like 10 layers all right just
to make sure
and then for the turret and place turret
sprites let's make them go to front
layer
all right we have go to front layer and
for this one
also go to front layer okay and lastly
for the enemy sprite
we actually always want them to appear
under
the turret range so inside of the enemy
inside of the forever loop i am going to
grab
a go to back layer because we wanted to
make sure that it goes under everything
else
and then i'm going to drag a go forward
one layer
and that's because we want to make sure
that the enemy is of course
above the track and for the track
i'm going to drag a windfly clicked
and forever loop then always go to back
layer
so the track is always on the bottom and
now everything should be layered
correctly
all right cool and now i'm going to hide
these variables
so i'm going to hide the base variable
and also the turret collider variable
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 in the next video
i'll make it so that you can view a
specific turret's range
and i'll also start creating new enemies
and a wave system
by the way this project is shared on my
scratch profile
link is in the description below anyways
that's it for this tutorial
see ya