Posted on

Scratch Coding – Dragon Hatch

Today we are going to build a game where we need to click on an egg to hatch a dragon.

We are going to start with a nice egg:

Change: Feel free to change the backdrop to something new.

We already have the sprites ready for this game. They are:

How does our game work?

The idea is that we click on our egg a number of times to make it hatch. As we click on the egg we start to see it move as if it is coming to life. We also see some of the sparkles start to come off the egg. Then finally the dragon emerges from the egg. We must of course give this dragon a name!

Hatching the Egg

We are going to need to keep a count of how many times we click on the egg. In order for the computer to remember how many times we have clicked, we will need to use a variable.

Select the orange data tab on the egg sprite -> Make a variable.

Call the variable: clicks.

Now when our game starts we can reset the number of clicks to 0.

We need to increase this number when the player clicks on the egg. Let’s use the hat block ‘when this sprite is clicked’.

Test: Make sure the number of clicks increases.

Now that we have a number that tells us how many times the player has clicked on the egg, we can start to add some special effects to the egg as it starts to hatch. We want to always be checking to see how many times the egg has been clicked on.

If we always want to do something, then we need a forever block:

Let’s add it to the bottom of the green flag hat block.

We want to be able to test to see how many clicks have been pressed on the egg, if we need to test something, then we need to use an if block.

I’m going to look for 5 clicks:

I’ve used the green operators tab to find the ‘equals’ sign and compared it to 5 clicks.

I would like my egg to move around, just a little at five clicks. I use a repeat block to make it move side to side 5 times. I use a little wait block to help slow it down.

Test: Does your egg move after your number of clicks?

I’m going to add another little test to our script. This time I’m going to add some sparkles to the egg. We have to do this a little differently, because the sparkles are on another sprite, we can’t use any code in the egg sprite to make it move or show.

We are going to send the stars sprite a message to tell it that it’s time to turn on.

On our stars sprite, we can listen for that message with a hat block:

The first thing we do is to show the star sprite, then we repeat ten times and change the sprite costume. At the end we hide the sprite again.

The next part of the hatching process is to change the egg costume so that it looks like its hatching.

On the Egg sprite:

Test: Check to see if the egg looks like it is cracking open. Do we need to add some costume reset code to the green flag hat block, so that our egg doesn’t start cracked next time?

We can now add a few more points where the egg will sparkle and wobble as we get to the hatching point, we just need to use the same code blocks for broadcasting an event and moving the egg, but at different click points.

Here be a Dragon

At some point, the dragon eyes are going to be shown. Because this is a sprite that is different to the egg sprite, we are going to need to send it a message as well:

On the eyes sprite:

First up we make sure the eyes are hidden at the start of the program. Then when we are told to show the eyes via the eye message, we show the eyes and always switch the costumes with a small delay.

Back on our egg sprite again:

This is the hatch message that we are going to send to the dragon. We switch to the costume16 which is a clear sprite. We also stop this script, just so that the broadcast message only gets sent once.

On the dragon: Let’s ask the player what the dragon’s name is going to be.

Test: Let’s make sure the egg disappears and the player is asked the dragon name.

Did we notice that our eyes are still showing as well? Let’s fix that by adding a hide block to the hatch message on the eyes sprite.

A Living, Breathing Dragon

Our dragon now has a name, let’s introduce him to the world:

Test it.

Let’s make him move around after he has come to life.