hello everyone this is part one on how
to make a tower defense game and scratch
in this tutorial I'll be creating the
enemies as well as the paths for the
enemies to follow now before we get
started I just want to say that this
tutorial series is a bit more on the
advanced side because there will be
cloning and lists involved but I will
try to make it as simple as possible so
that is not too difficult to follow and
hopefully by the end of the series
you'll better understand how this in
cloning works and of course have a tower
defense game anyways let's get started
so first of course we need a path for
the enemies to follow so I'm going to
create one by first going to the stage
and then going to backdrops and then to
make the path I'm gonna first select the
line tool and then drag it across the
drawing window over here and I think I'm
gonna make the path start from down over
here and then end somewhere over here
alright and then now I'm gonna use the
reshape tool to click anywhere on the
line to create more points and then that
way I can create a curvy path so if I
just drag the point then I can move it
and then if I drag the handles then I
can change the steepness of the curve
[Applause]
[Music]
all right so now I have my path down and
now I'm gonna select my path and then
change this outline number right here to
something like let's say 80 okay maybe
90 all right cool so now the path is
much wider and I think I'm going to also
change the fill color of the path to
something like a brown oops sorry I
meant the outline color so I'm gonna
change the outline color to a brown
color so something like this okay a bit
darker all right and then I think I also
want to add a green background so I'm
gonna select this rectangle tool and
then draw it over my entire drawing area
and then I'm gonna first take out the
outline so I'm gonna click this and then
for the fill I'm gonna make it a green
color so it's gonna sort of look like
grass all right and then I'm gonna
select a rectangle and then click the
backwards button right here and now we
have sort of like a grass field and a
dirt path and I think I'm going to
change the colors a bit more so and now
that we're done with the path let's
begin creating our enemy so first off
let's go to our cats bright and then I'm
gonna go to costumes and delete all of
the current costumes and now for the
enemy I think I'm just gonna start with
a simple circle so I'm gonna select a
circle tool and then I am going to hold
shift while pressing down to create a
perfect circle and then I'm going to
Center it all right and I think for the
enemy I'll make it a orange color so
something like this and for the outline
I will have a brown outline so something
like this all right cool and now we have
our enemy so I'm going to first show the
enemy alright and then I'm also going to
name it enemy and then I'm gonna
position it somewhere down here okay and
they also think it's a bit too large so
I'm gonna make a size something like 60
and okay I think that looks fine so now
of course the next thing I want to do is
to make the enemy move and to make the
enemy follow this curved path we have we
are going to need an invisible line to
guide the enemy along this path so to
start let's go to the stage and copy our
current path so I'm gonna select this
and then ctrl C and then I'm going to
create a new sprite so paint alright and
then paste so ctrl V and now we have our
second path so this is gonna be the line
which the enemies follow so I'm going to
select this and then I'm going to take
out all the outline or actually I'll
leave some in so I'll make the outline
something small like six alright and
then I'll also make the outline color a
white color just to make it clear that
this is the path the enemy follows and
then I'm also going to go to the code
and then go to motion and then drag a go
to X Y block and I'm gonna change this
to go to X 0 and y 0 all right
and now your line should be directly on
top of your path this is gonna be the
line that guides the enemy I'm going to
keep the line shown for now but we can
hide it later once we're done coding the
enemy movement now let's go to our enemy
sprite and go to costumes and then I'm
gonna create two detection boxes for the
enemy so let's duplicate the enemy
costume twice so right-click duplicate
and then inside of the first costume I
am going to create a rectangle so I'm
gonna first choose a fill I'm gonna make
a rectangle blue alright and for this
rectangle I'm gonna have no outline so
I'm gonna click this okay and now I'll
draw a small rectangle like
this and I am going to position this
rectangle so that it's in front of the
enemy so somewhere around here and in
this case I am imagining that the enemy
is facing towards the right and then I'm
also going to move this rectangle
slightly above the middle of the enemy
so as you can see it's not here but it's
slightly above the middle part so
somewhere around here all right and then
I'm going to delete the enemy image so
now for the second detection box I'm
going to copy the first rectangle and
then go to costume two and then paste it
and then I'm gonna make this a red color
so go to fill and then change it to red
and by the way these colors are not
necessary but I'm just choosing two
different colors to know which detection
box is which anyways I'm now going to
move it so that it's slightly below the
middle of the enemy so right around here
alright and then I'm going to delete the
enemy image all right so now we have our
two detection boxes for with the enemy
like this and these are going to check
if they're touching the white line and
if they are then they are going to
rotate the enemy back into place so that
it's always following the white line and
this way the enemy will follow the path
and turn if necessary by the way I'm
also going to copy and paste these
detection boxes in the real enemy
costume so that we can see how the line
detection works alright cool so this is
our enemy and these two are the line
detection boxes so now let's go inside
oakley code and then let's drag a win
flag clicked then I'm going to show the
sprite and then I'm gonna of course make
the enemy start over here so that is
negative 133 and Y negative 172 all
right and then I'm also going to make
the enemy point in the direction of the
white line so as you can see right now
the two detection boxes are not facing
towards the white line so I have to drag
a point in Direction block and then
change this to point in direction zero
alright and then I also am going to move
the enemy a bit to the right
so maybe negative 127 okay so now as you
can see the white line is now perfectly
in between d2 detection boxes and now
let's go back and then when the flag is
clicked I'm also going to switch costume
to costume 3 so that is the enemy
costume alright so now for the enemy
movement I'm going to create a new block
and then I'm gonna call this something
like enemy movement alright and then
click OK and now we are going to do all
of the line detection code in here so
first off we have to go to variables and
then I'm going to create two new
variables the first one is going to be
movespeed so this is how fast the
enemies will move and I will select for
despite only so click this and then
click OK
alright and then for the next one I am
going to create a variable called turn
speed and then I will select for despite
only and then I'll click OK alright so
first off inside of the enemy movement
block I am going to go to motion and
then drag a move ten steps and then I'm
gonna go to variables and drag a move
move speed steps so under the wind I
clicked I can set the move speed
variables and turn speed variables to
whatever I want so let's say if I have a
move speed of like four and turn speed
of eight then the enemy will move four
steps and it will also turn eight
degrees when we code in d rotation so
now back inside of the enemy movement
block I am going to first go to looks
and then drag a switch costume to block
and then I'm gonna change this to switch
costume to costume one so this is the
blue detection box so for this one we
are going to check if the enemy is
currently and they go to sensing
touching the white line or sprite one
then the enemy is going to
turn to the left or counterclockwise
turn speed degrees all right and then
now for the other detection box we can
just right click duplicate and then
switch costume to costume two which is
the red detection box and then it will
also check if it's touching sprite one
and if it is then the enemy will turn to
the right or clockwise turn speed
degrees all right and then at the very
end
let's switch costume to costume three so
what happens is that all of these blocks
should run immediately after one another
so the detection box costumes are never
going to show because the enemy always
switches to the real costume at the end
however the detection boxes will still
do their job and the blue one will check
when it turned left or counterclockwise
and the red one will check when to turn
right or clockwise so I think that
should work and one last thing let's go
to control drag a further loop put it
after the wind flag clicked and then
let's go to my blocks and drag the enemy
movement block inside of the for loop
all right cool so let's try it out if I
click the green flag so as you can see
the enemy is following the white line
and the red and the blue detection boxes
are preventing the enemy from going
outside of the white line like so and
also one thing to keep in mind is that
this doesn't always work the first time
for example like if I set the move speed
to eight instead of four then the enemy
is gonna move too fast and then the line
detection might not always work as you
can see it did not properly detect right
there and then the enemy went out of the
line so if your enemy movement does not
work like if it goes out of the track or
something there are several ways for you
to check and see what's wrong so first
off make sure the enemy is pointing in
the right direction so your detection
boxes over here should be on the two
sides of the white line and if they
aren't then you should rotate your enemy
so that they are and secondly try
creasing your enemy move speed or
increasing your enemy turn speed
and I would say having a turn speed of
around double the amount of your move
speed is a good number to have but you
can play around with it so for example
if I have a move speed of six I'm gonna
try a turn speed of 12 and then if I try
the enemy then as you can see it should
work all right cool
looks good all right nice so it worked
let me try a move speed of 10 and turn
speed of 20 it's not going to look as
good but alright so that seemed to also
work but I would suggest having a slower
move speed and that way the enemy turns
would look more smooth and if everything
still does not work try making sure your
line is smooth and try decreasing how
much the track turns so make sure to not
have any sharp edges or anything like
that anyways now that we're done with
the enemy movement we can now hide the
white line so inside of these looks we
can grab a set color effect 2-0 block
and then I'm gonna change this to set
ghost effect to 100 and pretty much this
just makes the line invisible but it
still allows the enemy to touch the line
so I'm going to drag a 1 Phi clicked and
then put it after everything all right
and then I'm also going to go to looks
and drag a show block just in case and
now that should be fine and then for the
enemies bright I am going to go to the
costumes and take out the red and blue
colliders in the third costume so the
first costume is gonna have the blue
Clyde err second one's gonna have the
red one and then third one is going to
have your actual enemy sprite all right
so now if you try it again then
everything should work as normal okay
we have the enemy and then it's touching
the white line but the line is not shown
so alright and then it should complete
the track okay cool so now of course we
also want the enemy to hide once it
touches the edge of the track so let's
go to the code
and then I'm going to drag another wind
fly clicked and then I'm gonna go to
control drag a forever loop and then
before that four of a loop I am going to
drag a weight until block and I'm going
to check wait until not and then sensing
touching and change this to edge alright
and then inside of the Farva loop I am
going to always check if and then
duplicate this touching the edge then
hide so go to looks and then hide all
right cool
so of course since the enemy is touching
the edge of the screen when it spawns we
are going to ignore that until it stops
touching the edge and then once it
touches the edge again that means the
enemy left the track so we're going to
hide it so let's try it out and now as
you can see it should work so once it
touches the edge over here then it
should hide so almost there
and it is hidden all right cool
so now that we have one enemy down let's
now create multiple enemies and this is
actually pretty simple all we have to do
is go to control and then drag a repeat
loop and then in psychotherapy loop
let's drag a create clone of myself and
also a wait one seconds
and then I'll change this to something
like wait 0.2 seconds and then I will
take away everything under this one flag
clicked and then drag this under the
wind I clicked and I am NOT going to
show the original enemy sprite so I'm
going to go to looks and then drag a
hide block and put it right before they
repeat ten all right and then now for
this part we are going to put this under
a when I start as a clone so let's go to
control and then I'm going to drag a
when a star is a clone and then I'm also
going to replace this one if I clicked
with a when I star is a clone
and now everything should be fine so
let's try it out
all right here are our enemies and as
you can see we have 10 enemies going
around the track and they should
disappear once they reach the end and
all right cool
and they do all right so I think I am
going to increase the move speed of the
enemies a bit so maybe from 3 to 4 and
let me try the same turn speed of 6 cell
let's try it out ok here are our enemies
and I think a turn speed of 6 is working
fine so I'll leave it at that and there
we go alright cool so now we have a
simple and very customizable enemy
movement system along with our track
anyways that's it for this tutorial if
you enjoyed it then give it a thumbs up
and subscribe to if you haven't already
and the next tutorial I will begin
creating some turrets to shoot the
enemies by the way this project is
shared on my scratcher file link is in
the description below
anyways that's for this video see ya
[Music]