Scratch Platformer Tutorial — Part 3

Broken Piano
11 min readOct 17, 2021

--

Welcome back to the Scratch platformer tutorial! This is the final part of the series.

In this part we’ll add a couple new stages, featuring different kinds of enemies which shoot missiles and follow the player! We’ll also add title and end stages using text, and use text to teach people how to play our game.

And of course, before we start, if you haven’t read the first or second parts, you should before reading this.

Fixing Jumping

Before we start, our jumping has a bug: it lets the player jump again while already in midair. While this is sometimes wanted behavior, it’s not in our game, so we should fix it.

The fix is pretty simple: make the input for the up arrow pressed also require the player being on the ground:

The “and” block returns whether or not both hexagon blocks are true. If even one of them is not true, the “and” block will be false.

Another quick thing to do before starting is just to add a broadcast message when the stage changes. This will help a lot in the long run:

The Static Enemy

Our main focus this time is to add some dangerous enemies into our game. We’ve going to add two types of enemies:

  • Static enemies, which stay still and shoot missiles at the player
  • Dynamic enemies, which run and jump towards the player

Both of these enemies will look similar to our player sprite, but be a different color and have a different expression.

First we’ll make our static enemy.

  1. Right click the player in the Sprite Area and click duplicate

2. Click the “costumes” tab and change the color of each of the enemy’s costumes. Also, if you’d like you can select the mouth and click “flip vertically” to make it a frowny face

We’ll have to change some of the blocks inside the enemy:

  • Delete the entire block for x motion
  • Delete the touching red block, and response to danger block
  • Delete the change x by x motion block
  • Delete the checking for next stage block
  • Dismantle the jumping block and gravity block
  • Dismantle the change y by y motion block

Dismantling a block is separating the chain from the event block — therefore it can’t be activated anymore, but we still have it for future use. We’re going to want this because of the dynamic enemy we’re going to build letter.

We want the enemy to always face towards the player, so this chain should do it:

The “x position of Sprite1” block is found in the sensing category, as “backdrop # of Stage” (you’ll have to change the Stage and backdrop # dropdowns).

Now we’ll want to create the missiles. To do that, we’ll need to have a broadcast message that summons a missile coming from the enemy, and a response coming from the missile. Let’s make the message first.

We only want the enemy to summon missiles when it’s showing. Unfortunately, Scratch hasn’t made a hexagon block similar to the round blocks “x position” or “costume name” for whether or not a sprite is showing. We can make our own version of that with variables, though.

Make a variable called “static enemy showing?”. Remember that the question mark means that it’s a true/false value (0 or 1). Set it to 0 when the game starts.

Next, make this block chain, which shows or hides the enemy depending on whether static enemy showing? is true or false:

Now when we update static enemy showing?, it updates whether or not the static enemy is showing. In the future it’ll also update whether or not missiles are sent.

This block chain sets static enemy showing? to 0 or 1 depending on whether or not we’re on the first stage. We’ll change the stage number later, but for testing purposes leave it at 1 for now.

Now that we have that taking care of, we can finally make the block chain for sending out missiles. This will require the “wait seconds” block, which waits the inputted amount of seconds before continuing with the code:

For the entire game, every 3 seconds, if the static enemy is showing, it sends a missile.

It’s time for the actual missile part! Create a new sprite and make it look like a missile in any way you want.

Change the size so that the missile is small enough to dodge (10–25 usually).

All we need to start is this tiny bit of code that hides the missile when the game starts:

To have the missiles work we’ll need to use clones. In Scratch, any sprite can create a clone of themselves. There’s a separate event block that runs whenever any new clone is summoned, but clones also react to normal event blocks too. Clones are separate entities from the original sprite.

We can use clones to make multiple of the same Sprite be on the screen at once. Clone blocks are found in the control category (the only category besides events to contain an event block)

Make this new block chain in the Stage Block Area! Do NOT put it in the Block Area of the missile.

Why not put it in the missile? Well, since each clone responds to any event blocks in the original sprite, every active clone would respond to the missile event block as well as the original sprite. Instead of increasing the clones by 1, the clones would multiply by 2 every time the message was received.

By putting the block in the Stage, it guarantees that each time the message is received only 1 new clone will be summoned.

However, we still need to put the clone event block in the missile, so newly created missiles know how to act.

When a new missile is summoned, it will go to the static enemy, point towards the player, and move 5 steps each frame until it’s touching the edge. After that, it’ll be deleted. Remember that the “move steps” block moves the sprite in the direction that it’s facing.

Never forget to delete your clones! Having too many clones will cause the Scratch engine to reach it’s clone limit and stop making clones.

The final thing we must do to make this enemy complete is to make the missiles harm the player and disappear when the stage is switched. Harming the player just takes a minor tweak in the missile’s code:

Now the missile stops if it is touching the player as well as if it’s touching the edge. So when the repeat block ends, it must be touching one of those two things. If it’s touching the player, the danger message is broadcasted.

To make them disappear when the stage is switched, just add a very small block chain to the missile:

Note: when the original sprite reacts to this, it won’t do anything, since it is not a clone. However, if there are active clones at the time, they will be deleted.

The Dynamic Enemy

It’s time to start the dynamic enemy! This one will move around and chase the player while jumping up and down.

To start, let’s change the stage that the static enemy appears in to 4. This is to keep room for the dynamic enemy we’ll be testing.

Duplicate the static enemy sprite, and change it’s appearance.

Back to the Block Area, add a new variable called “dynamic enemy showing?”. Replace all the instances of “static enemy showing?” with this variable, as well as change the stage that it appears in to 1 (for testing). Finally, add three new variables: “enemy jump?”, “enemy x motion”, and “enemy y motion”.

Also, don’t forget to get rid of the block that summons missiles!

Set all three of them to 0 when the green flag is clicked:

To control where the enemy goes, update the block that changes the costumes to change the x motion too. Let’s also make this only happen when the dynamic enemy is showing.

And of course update the blocks that change the position depending on the variables:

We want the dynamic enemy to jump every time it hits the ground. First, let’s change the existing jump block to change the enemy y motion (instead of just y motion) and respond to a message instead of the up arrow key being pressed.

Now let’s do the same for gravity:

Whenever the player dies and respawns, we want the enemy to go back to their original position in the stage. First, let’s make a message called “reset” which resets the dynamic enemy:

When we broadcast the reset message, if the stage is 1 the dynamic enemy goes back to their original position and all the variables get reset as well.

Now we can call it at different times:

The final block chain we need to add is the one to repeatedly make the enemy jump. Every time they touch the ground they should jump again, so we just need to add on to our gravity:

To make these enemies actually dangerous, we need to update the block chain in the player sprite that broadcasts danger. Not only will it check if the player’s touching the color red, but it will also check if they’re touching any of the enemy sprites:

Note: remember that Sprite3 is the missile and isn’t an enemy! It’s also dangerous, but we check that in the missile sprite instead of the player sprite.

New Stages

We made our new enemies, but we need stages to put them in! Let’s make new stages 4, 5, and 6 for our enemies.

Remember, you can change them up in any way you’d like!

I want the static enemy to appear in stages 4 & 6, and the dynamic one in stage 5, but you can change them depending on your stage layouts or which stages you want the enemies in.

Static enemy:

Dynamic enemy:

We finally finished the stages of our game!

Extra Stages

To finish up, we’re going to use text to make a title and end stage.

  1. Select the text tool

2. Type in text in the first stage

3. Duplicate the last stage and add a back wall

4. Add text to the end stage

And that’s it! We’ve finished with our game!

Publishing

Scratch is an world-wide platform. You can publish any games you make, and play others’ games for inspiration (it’s highly recommended that you do — you’ll be surprised to see what people can make).

If you’ve verified your email, you can publish any games you make! If you don’t know, here’s how:

  1. Give your project a good name (not like this)

2. Click the “See project page” button on the top bar

3. Type in Instructions

4. If you have any, type in Notes and Credits

5. Click share!

And we did it! Our platformer game is finally complete!

Let’s review what we did this time. We:

  • Fixed a bug for jumping
  • Made a static enemy
  • Used cloning to make multiple missiles
  • Duplicated the static enemy to make a dynamic enemy
  • Designed several new stages implementing the enemies
  • Added text to the intro and ending stages

Remember that this is just the basis for what you could do in Scratch. Here are some ideas for things to add:

  • More stages with harder platforming challenges
  • Moving platforms
  • Spikes that hurt you but don’t kill you
  • Animations with costumes
  • Third type of enemy that patrols and only attacks you if they see you
  • Boss fight at the end

These are just some of the ideas that you could add to your game.

Thanks for reading this series! If you want to learn more, check out some of our more complex series when they release (such as tower defense or sandbox).

--

--

Broken Piano
0 Followers

The official Medium account for Broken Piano!