This week we are building a game that will help our character wear some clothes.
The idea is that we will click on a piece of clothing and have it move to the correct spot on our character. If we select two types of the same piece of clothing we will have to move one back in place, for example if I select the pants and then the shorts, the pants move back to the wardrobe.
Let’s start with using the Dani sprite, this has costumes for both Male and Female characters.
Then let’s add a number of clothing sprites:
When the green flag is pressed, let’s have the player move to a known location on the screen and say ‘What should I wear?’
Each piece of clothing must also start in a spot just a little to the right of the character. We can click on each sprite and move it to the location we want and then look at the X, Y position on the go to block and use that as a starting point.
We also call the show block – we will be hiding some of our clothes later, so this helps us make sure they can be seen.
When the sprite is clicked on, we want to move the clothing to the character:
We can use the glide block to animate our clothes to a certain spot.
Test the game.
What we find is that we can pile on more clothes and make our character look crazy. We really need to remove our clothes if another one is already in place.
We need to add to the code that runs when our sprite is clicked:
For a scarf:
The scarf will always be looking for the other scarf and if it finds it, it will move the scarf back in place.
If I had three shirts that we could try on, my code might look like:
We can add a camera sprite, when it is clicked we will change the backdrop and make sure the other clothes are hidden, this gives us a chance to take a nice little photo.
On each of our clothing sprites we need to make sure that we hide the clothes:
We only want to hide if we are not touching the character.
This is a game that will have the player use video motion to keep the balls from hitting the moving paddle.
We need to create two sprites, we are going to have some falling balls and a moving paddle.
We would like our balls to drop from the top of the screen, before we start the ball dropping, we are going to set the background as the video feed.
We then need to hide the ball, so that when we start as a clone we can show it again. We need to create clones that will start falling every few seconds. We always want our balls to be created so we are going to use a forever loop.
When our ball starts as a clone, the first thing we want to do is show the sprite. The next thing we do is move the cloned sprite to a random position where it can fall. We make the ball start on the Y position to 160 so that it doesn’t start by touching the edge.
The next thing we are going to do is make the balls fall from the top.
If we touch the edge of the of the screen, we need to delete the clone.
If we test this, we will see that the balls fall from the sky and then are deleted when they hit the edge.
The green paddle is going to move across the screen, when it hits the edge we will get it to move back the other way.
Back on our ball sprite, we need to test to see if the video has some fast moving parts to it. We have a special block that we can use to test the speed of the video.
This makes the ball bounce back up and will touch the top of the screen which will delete the cloned ball.
We need to add a new variable called ‘lives’, on our ball sprite, we want to set this to 5 lives to start with. Our code will look like:
We then also test to see if the lives are zero, if we have run out of lives we end the program.
We also need to test to see if the ball hits the paddle and if it does then we subtract a life. The full code looks like:
In this game, the player must keep the teeth clean by scrubbing away the slime that forms on the teeth. If the player isn’t quick enough cracks will start to form on the teeth and they will become rotten.
Make our background a colour other than white, we will be using white as our teeth colour.
The slime sprite needs to have a second costume, this is going to be the crack sprite. This can be added by selecting the file icon in the costumes tab.
We want the toothbrush to follow the players mouse around the screen, to make this happen we are going to use the following script:
We need to keep track of how much time has passed and how many cracks have formed on the players teeth. Create two new variables; Cracks and Time
We want some slime to appear randomly on our teeth, we are going to use a clone of the slime so that we can have many patches of slime on the screen at the same time.
First, we make sure the number of cracks starts at zero when our game is starting up. Next, we make sure that the sprite costume is set to slime, we will change it to a crack later if it doesn’t get cleaned up.
Now we create a new slime clone every second. You can speed this up for more slime!
When our clone starts, we want to make sure it’s positioned in the correct spot.
I’ve picked numbers that match the left and right sides of my mouth sprite (X position) and the top and bottom heights of the mouth sprite (Y position).
I used the x and y positions below the game to find the correct positions.
We want to make sure that our slime only lands on the teeth sprite, so let’s test to see if it is touching white.
Now if we test our game we find that we have spots of slime getting added to our teeth.
We need a way to get rid of them when the toothbrush is over them.
We repeat 100 times a check to see if the mouse down and if the clone is touching the toothbrush. If it is, then we delete the clone as it has been cleaned up.
Try changing the repeat number to make the slime last longer or shorter. After the repeat block is done, we add one to the cracks variable and then switch to the crack costume to show a different image on the screen.
On our teeth sprite, we check to see if the number of cracks is large enough to stop our game. We also have a timer that advances every second, this can be used to show the player how long they kept their teeth in good condition for.
The goal of this game is to get our character to catch the star without touching the edge of the stage.
This game is a little like a platformer. The more stars we catch the more levels we go through.
I’ve made two custom sprites, the first is the ‘You Lost’ sprite and the second is the Level. I’ve made two costumes that will form the levels:
Make Our Cat Stand on the Level
To make our cat stand on our level, we are going to need to add some special colour to the cat costumes, look at this zoom-in view of our cat’s feet:
I’ve added some red dots so that we can use some code to make our code easier.
Now we want to make sure that our cat starts in the correct location:
I also made sure the backdrop is correctly set and that my cat will rotate correctly.
Now we need a way to make our cat fall from the sky always, we are going to create a new variable called ‘YSpeed’.
We always want to make this number decrease, this will act like gravity.
We also make sure that Scratch the cat is at the front of the screen.
If scratch is touching our level blocks, then we want to make sure that the gravity doesn’t have any effect.
Be sure to use the colour of the dots we added to Scratch the cat to the first part of the colour test, then use the colour of the level blocks. We will set our variable to 1 if we are standing on the level.
Next we can add some left and right movement checks for our keypress events.
Finally we can add the jumping code and the code that will change our y position.
The full code for our character sprite is above.
Losing If We Touch The Edge
If we touch the edge of the screen we want to automatically loose.
On our Scratch the cat sprite, add another check to our forever loop:
We created a new broadcast message called ‘Lost’. This will tell our sprite that it is time to be seen.
On our ‘You Lost’ Sprite we can have the following code:
Moving to the Next Level
If our character touches the star sprite, then we want to change the level sprite to the next costume, On the start sprite:
We pick a random location and broadcast a ‘NewLevel’ event.
The Level sprite will respond by changing the level.
Add variables to keep track of how many stars you catch.
We are going to be building a spaceship landing game.
The program should have the following goals:
The player will use the arrow keys to apply power to the rocket
Up will move the rocket down
Down will move the rocket Up
Left will move the rocket Right
The spaceship should be travelling less than 0.5 on the X and Y to successfully land
Prepare the rocket sprite costumes
Use the drawing tools to create a number of costumes that we can use to make our rocket look like it is thrusting in the correct direction.
We will need a way to keep track of the rocket speed, to do this we will need a way to store the X (across) and Y (up / down) speed of the rocket.
Create two new variables called X and Y.
We need a way for our rocket to be able to move along, we always want to be checking for movement while the game is running, so we should be using a forever block for this. We need a loop for the X axis and a loop for the Y axis.
Using Keys to Control Movement
Each arrow key press will need to add some speed to the rocket.
Task: Add the Down / Left / Right arrow code.
The left arrow should change the X axis by -0.5 and use the right costume
The right arrow should change the X axis by 0.5 and use the left costume
The down arrow should change the Y axis by -0.25 (make it harder to slow down) and use the up costume.
Add a take-off sprite and a landing sprite to your game. I’ve used planets to work with the theme.
Create two new backdrops:
A great landing backdrop for when the rocket lands
A crashed backdrop for when the rocket hits the planet too quickly
Ensure that both planet sprites start in the correct location:
We want to make sure that our landing site is always waiting for the rocket to touch it. Once it does we need to test the speed of the rocket and if the rocket is travelling slow, we will show the landed backdrop. Otherwise we will show the crashed backdrop.
Today we are building a game that features a witch flying on her broom across the sky. She can shoot falling objects like apples.
The game should have the following properties:
The user can move the witch up and down with the arrow keys.
The user can shoot a fireball by pressing the space key
When a fireball hits the falling object, the user gets a point
The background should look like the witch is flying.
We will need to create our own sprite for the moving clouds. Draw a new sprite and use the circle fill tool to make some join circles that will look like a cloud.
We can add our witch, fireball and the object that falls from the sky.
Make the Clouds Move
We are going to use the glide block to make the clouds move across the screen. We always want the clouds to be moving, if we always want something to happen, we will use a forever loop.
We first move the clouds to the point on the screen where we want them to fly from. Move the ‘go to x and y’ block into our forever loop:
Then we move the clouds to the other side of the screen and drag up our glide block:
We also want the clouds to be a little transparent, in scratch this is the ghost effect:
We can change the speed of the clouds by changing the number of seconds the glide block takes to move across the screen.
Making the Witch Move
We want our witch to move up and down using the arrow keys. We know that the Y axis is for up and down, this means that we can add some change Y blocks onto our keyboard event hat blocks:
We use a negative number to make the witch go down.
We might notice that the clouds are in front of the witch, we can also bring her to the front of the screen when our game starts.
We are going drag the apple to the top of the screen on the right hand side where we want it to fall from. Since the Y axis is for up and down we will use some change Y blocks to make the apple fall.
We always want our apple to be falling, so we need to use a forever block:
Use a negative number for the y position to make it move down.
We can have our witch shoot a fireball by cloning the fireball sprite and moving it towards the mouse position.
We must first move the sprite to where the witch is on the screen, then create a clone of the fireball sprite:
When the clone starts up, we want to point it towards the mouse position and move it until it hits the edge of the screen at that point we want to delete the clone.
Make the fireball hidden at the start of the game.
To keep score we are going to need to use a variable, let’s call it score.
On our fireball, we are going to set this to zero when the game starts:
We need to test our apple to see if it is touching a fireball object, we can use an if block for a test.
Our forever block now looks like:
Make our Apple Fall from Different Places
At the moment our apple always falls from the same position in the sky, we can add a random block into the ‘go to X Y’ block. I only want my apples to fall from different X positions on the screen (across ways), rather than from different heights. So I added a random block to the X position.
We are going to be making a classic game today: Snake. The idea is that the snake starts off really small as one block and then every time eats a piece of food the snake grows one block. The player must look for more food but must keep the snake off the side walls and from touching itself.
We are going to make a custom snake using the rectangle fill tool. Our costume should look like:
Be sure to make our rectangle in the centre of the drawing area.
We also use a ball as the food.
Make the Snake Move
The first step is to make our snake move around the screen. We want the player to be able to use the arrow keys to control the snake.
We start the snake in the centre of the window at 0, 0.
We want the snake to always be moving, if we want something to always happen we use the forever block:
Next we can test for some user input and point the snake in the correct direction:
Test our game.
We can see that our snake moves around, it’s a little big and doesn’t look much like a snake, so let’s make it a lot smaller:
Place the set size block just before the forever loop.
As our snake eats more food we want to be able to make our snake look longer. The way we are going to do this is to use a number of snake part clones and have them follow the head sprite. To make it so they can follow the lead sprite, we need to store the position that the lead sprite steps into. For this we are going to create two lists:
We should end up with:
We need to make sure that our list is cleared when our game first starts:
Change our starting script to include the removal of all items from our position lists.
Now we need to keep track of each position the snake sprite lands in:
This will fill our list up quickly, so make sure you have the lists hidden on the screen.
In order to have our clone sprites know which position they need to sit in, we need to keep track of which clone number they are. Each clone will look further down the position list to work out which spot they need to sit on.
Let’s create two variables:
Now to create a clone:
The clone will find which number it is based on the score, then it will forever look in the position list to see when it should move to. If it touches itself (the green) then the game is over.
Now we need to make it such that our snake creates a clone when it touches a food pellet, this block must be placed in the main loop, just under the instructions for testing the movement.
We broadcast a message that the food pellet will listen for.
Touching the Edge
We want the game to end if the snake touches the edge of the screen, so we will place this test under the other movement tests:
Moving the Food
The food pellet should move into a random position:
When we receive a new message:
Try creating a harder level that our snake must move around:
We want our snake to die if it touches the walls, we can add a check to see if it touches the wall colour.
Now we can change the level when we get to a certain score:
The goal of this game is to catch as many fish as we can. We would like to be able to control the fishing boat with our left and right arrow keys which is sitting on top of the water.
Our boat will have a fishing hook that can move up and down with the up / down arrow keys. The fish will swim from left to right, if they touch our fishing hook, they will become hooked and caught.
We should keep score of how many fish we catch.
We can use the underwater backdrop, but use the drawing tool to clear some space at the top of the backdrop for our boat to sit on.
I’ve used the sailboat as the main fishing boat. But feel free to draw up your own boat.
For the fishing hook sprite, I’ve drawn a nice letter J, but I added a tiny little bit of red bait. I’m going to use the red bait later in my code to check to see if the fish is touching this.
We should also add a fish that we are going to catch.
One more thing, we are going to add a pesky crab to our game. If we catch him, then we will lose our hook.
Making Our Boat Move
We can make our boat move from left to right by pointing the boat in the correct position (either left or right) when the keys are pressed. Then we can move to the boat with the ‘Move steps’ blocks.
We want our fishing hook to follow the boat around until the player decides to drop the line into the water, we can use the wait until block for this.
We can control the depth of the hook by moving the hook either up or down, depending on which keys the user presses. First we point the hook in the direction we want to move, then we move it a number of steps:
Test: Our game should let us move both the boat and the hook. If we move the boat after the hook has been dropped, the boat should still move, but the hook doesn’t move.
It’s not much of a fishing game if we don’t have some fish to catch. Since we want to have a lot of fish swimming around, we are going to use the ‘clone’ command. This just allows us to create lots of fish that are the same, but our code will have some random numbers to control its location. So the end effect should look like lots of random fish are swimming around.
We first use the green flag block, which will create a new cloned fish every 1 to 4 seconds. When the clone is created we need to do the following:
Move to a random location to the left of the screen (-240 X)
Point towards the right hand side of the screen
Move across the screen both fast and slowly, but random speeds.
If we are further than 240 (X), remove the fish from the screen.
The code for the should achieve the points above.
Test: Does the fish swim from left to right?
Our fish now needs to get hooked so that we can catch it. Let’s add a check to our code to see if the fish is touching the hook. If it does touch, let’s change the position so that it is facing the boat. While we are on the hook and not yet at the boat, we want the fish to follow the hook.
Ensure the Hook Moves Correctly
We really want the hook to continue following the boat once it has caught a fish. We always want the hook following the boat until the down arrow is pressed. Then the next time it touches the boat, it must follow the boat until it has been dropped again.
Hooking the crab should stop the game immediately. No second chances.
Add a variable to keep count of how many fish have been caught.
The goal of this game is to move the diver to the balloon as many times as we can. We must avoid the falling monsters at all costs. Our diver will have a special skill of shrinking and growing when the player pressed the up and down arrows.
Every time we touch the balloon we will get an extra point.
We should add the above sprites to our game. We use the drawing tools to build a nice sea water line.
Use the shrink tool to make the sizes a nice fit for the game.
Let’s start by making our diver move, make sure the diver sprite is selected.
We want our diver to shrink and grow:
Next we want to move across the screen:
We would like to randomly place our monsters and have them fall-down to the bottom of the screen. Be sure to select the monster sprite.
We want to have lots of these monsters falling down, so we are going to use clones to achieve this.
To start with:
This code will create a monster clone every 1 – 10 seconds.
Now once our clone has been created, we must add the code to make it fall until it touches the bottom of the screen.
First we make sure that we select a random position along the X (across) axis.
Next we point the monster looking downward.
We then repeat until we touch the edge of the game, which is the perfect time to delete the clone.
We use a random block to make sure that our monster is moving randomly, this makes our game a little more unpredictable.
Add a new variable called ‘score’, this will add some new blocks that we can use:
Select our diver sprite again, we are going to be adding some code to test when our diver touches either the monster or the balloon.
We have two test in our code, the first checks to see if the monster has been touched:
This check will stop the game and show a backdrop that I created that has ‘Game Over’ written on it.
The code also includes a nice colour change effect that hits the player.
The second test will move the player to the other side of the screen when they hit the balloon and give them a point.
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:
Let’s make him move around after he has come to life.