Scratch Tower Defense Tutorial — Part 1

Broken Piano
7 min readOct 23, 2021

--

There are a variety of different game genres, from platformers to simulations to puzzle and shooter games. One of the most fun genres in my opinion is tower defense. Enemies follow a specific path to your base. You can place down cannons that automatically shoot missiles at those enemies to stop them from reaching your base.

This genre has a lot of room for innovation and new ideas. We’re going to be making a top-down tower defense game.

In this part, we’ll make the layout and path, and make enemies that follow the path to the base.

Before we start — this tutorial is more complex than the previous ones. If you want something easier, check out the Scratch platformer tutorial series. If you know nothing about Scratch, see the Scratch intro tutorial.

Making the Path

As soon as you create the project, delete Scratch Cat and make the background a solid green color.

We can use rectangles to make the path. Make sure it looks neat. Also make sure that the color of the path is noticeably different from the color of the ground.

The start is the top left and end is the bottom right. This will be the basic path that the enemies follow.

Enemies

Since we want multiple enemies to be travelling across the path at a time, we’re going to have to use clones! See part 3 of the Scratch platformer tutorial to learn how clones work.

Create an enemy sprite and design it’s appearance:

Make sure to decrease the size if you need to to make the enemy fit on the path!

All of our enemy types will be in this one enemy sprite. This means we’ll need variables to keep track of which enemies is spawning, which type of enemy the clone is, the enemy’s speed, base damage, and health.

Scratch has a special feature — if you make a variable be limited only to one sprite, if that sprite has a clone the variable is different for each individual clone. This will be extremely helpful later on.

For now, create those 5 new variables, but for all of them except the first one make them limited to the enemy sprite:

Inside enemy sprite:

Inside Stage or any other sprite:

The last 4 variables are limited to only the enemy sprite.

Now we’ll add these two block chains:

The left one hides the main enemy sprite when the game is started. The “start” message will be broadcasted at the start of the game, similar to the “when green flag clicked” event block.

The right one runs when a clone is made and only for that clone. It moves to the position (-195, 180) and becomes visible.

We can add on to the clone block chain:

The two new blocks (1) set the individual enemy’s type to the variable spawning. Since the type variable is local to the enemy sprite, it can be different for each clone. And (2) it switches the clone’s costume to it’s type.

When we make a clone, we’ll want to set the spawning variable to the type of enemy we want to make first.

And adding on it even more:

Now the enemy follows the path from the beginning to the end. The “glide” blocks move the enemy at a pace to one spot to another.

The next step is going to the Stage and adding these block chains:

The left one broadcasts the “start” message at the start of the game. This may seem like a waste right now, but it will be helpful later on.

The other one uses a new category — custom blocks! Custom blocks are blocks that you can make to do specific actions. Custom blocks are always limited to the sprite they’re made in.

How to create a block:

  1. Go to the custom blocks category

2. Click “make a block” and design the block

Just like normal blocks, oval inputs are any letters or numbers and hexagon inputs are true/false values. You can also add text labels for both the block and any inputs.

3. Under the custom blocks event, connect the blocks that you want to activate when the custom block is used

Custom blocks are similar to broadcasting messages, but have the ability to use parameters, which are inputs that you insert when using the block. For example, in the “change x by” block, the parameter is the change.

Add this block to the Stage now:

This also means that we need to rename the costume of our enemy to “weak”:

To review:

  1. The Stage activates the “spawn” block
  2. The spawn block sets the “spawning” variable to the value entered in the spawn block and creates a clone of enemy
  3. When the enemy is cloned it takes the value of the “spawning” variable and sets it’s costume to that

Add a new block chain and see the enemies spawn!

This is fine and all, but each enemy should have a specific speed and damage.

We could do this:

But as we’d add more types of enemies it’d become really long and annoying. A better way would be to use a new type of variable: a list.

Lists and Stats

A list is a collection of values. Here’s a comparison:

Variable

  • Has one label used to access it
  • Has one value that it holds
  • Can set or change it

List

  • Has one label used to access it
  • Has a collection of values
  • Can add, change, and delete individual values
  • Can change the list as a whole

Lists are much more useful for holding a collection of data all having one purpose, while variables hold one specific piece of data that’s very important.

Click “make a list” under the variables category but above the custom blocks category. Name it “stats”.

Lists, like variables, don’t reset when the game starts. That means we’ll need to reset the list each time when the green flag is clicked.

There are 2 types of lists I like to use in my games:

  1. Constant lists: all items get defined at the start of the game, the list is used but never changed
  2. Dynamic lists: little or no items get defined at the start of the game, items get added/deleted

We’re going to use a static list to keep track of enemies’ stats:

The first item is the name of the enemy, the second one is the enemy’s health, the third one is the enemy’s speed, and the last one is the enemy’s base damage.

Note: the higher the speed is, the slower the enemy will move. This is because it measures how long the enemy takes to get from one point to the next.

Keep repeating this process for all the enemies you want to add. These are enough for now.

We’ll have to update the clone block chain to match:

This is a bit confusing. Here is the oval block on it’s own:

The “item of” oval block returns the item in the list of the index. So if you had the list (apple, orange, pear), and you used the block on the index 2, you’d get the item “orange”.

The “item # of in” oval block returns the index of the item specified. Using the same example, if you used the block on the item “pear”, you’d get the index 3.

All together, this block gets the index of the item of the variable type, but adds 1. So if you used it on the same list as the previous examples, and inputted the item “orange”, it would return the item “pear”.

Don’t forget to update the rest!

Part 1 — Complete!

That’s it for part 1!

Let’s review. We:

  • Designed a path using the backdrop editor
  • Made an enemy sprite
  • Created both global and local variables
  • Used clones to have multiple enemies on the screen at once
  • Constructed a custom block for spawning enemies
  • Learned about lists and used them to control enemy stats

Thanks for reading, and see you in part 2!

--

--

Broken Piano
0 Followers

The official Medium account for Broken Piano!