Posted on

Scratch Coding – Present Drop

Our last game this year is a Santa present dropping game. The code is very similar to the ball catching game, but instead of the balls dropping from random heights, we are going to have the houses and stockings move along the bottom of the screen. Santa must drop off presents.

We already have a number of assets ready to use:

We have to do a few tasks before our game will come to life:

  • Make it snow
  • Make it look like Santa is flying
  • Make Santa drop a present
  • Have the present fall to the ground
  • Check to see if the present hit the target and keep score

Make it Snow

Let’s start with the first task, we have a snowball sprite and we need to make it look like it is snowing.

Because we want a lot of snow on the screen at once, we are going to create a clone of the snow sprite. A clone is just a copy of a sprite. We want the size and positon of our sprite to be random near the top of the background.

Notice how we use the ‘Create clone of myself’ block? We can now use the ‘when I start as a clone’ hat block to make the cloned sprite move. We also make the size of the snowball random, this will make the scene look like it has depth.

We want our snow to fall to just above the bottom of the screen:

We hide the snow once it gets near the bottom of the screen and then finally we use the ‘delete this clone’ block to delete the clone from the screen.

Task: Test this out.

We should be able to see the snow falling.

Make Santa Fly

The next task we need to undertake is to make it look like Santa is flying. We want our houses to move along the bottom of the screen, moving from left to right. Since we have 10 house choices to move across the screen, we can’t start them all moving at once, otherwise there will be too much going on for the player.

We need a way to randomly pick a house and then tell it to move across the screen.

On the backdrop, let’s use a green flag hat block to always be picking a random number for the house to show on the screen.

First up, lets’ create a new variable that will store the random house number that we are going to pick.

This random house number variable will be set with a number between 1 and 10 (the same number of houses that we have).

We then send a message to the sprite that we want to have move. For example, House1 will receive a message if it has been randomly picked.

When the house does get its message, we want to have it move across the screen:

Task: Test this out, do the houses move across the screen?

We have a couple of problems with this. When the house gets send another message, it will restart its movement. This isn’t what we want to have happen, so we need to write a little bit of script to prevent this from happening.

The logic should look like this:

How do we tell if the sprite is moving?

We can check to see if the sprite is still in the starting position. If the sprite is at the same X position as when the game starts, then we know the sprite is not moving. Otherwise we just tell it to keep moving. We need to use an If Then / Else block for this.

The script on the house now looks like:

We need to make sure that we set it back in the starting position when the game finishes and when the game is restarted by clicking on the green flag block.

Test: Does our house move across the screen correctly?

We can now copy this to each house sprite. Be sure to change the message received block so that it matches the house number.

Making the Background Move

To help add to the movement effect, we can make the large background image move slowly across the screen. We want this background to be the last sprite on the backdrop, so we will also set the layer property to make sure it is at the back.

On the background sprite:

We also want Santa to be able to fly higher or lower by using the up and down arrow keys.

On the Santa sprite, we can add the following:

When the up key is pressed, we add more to the Santa sprites Y axis, this will make him move further up the screen.

For the down movement, we don’t want Santa to hit a house, so we will make sure that we only reduce the Y axis if he is higher than 20 pixels on the screen. Which will be just above the house sprites.

Task: Test it out – Does it look like Santa is flying?

Dropping the Presents

Now that Santa is flying high, we can make him drop a present when we press the space key.

On the present sprite:

Again, we use the clone blocks, this will let use have more than one present on the screen. When we start the present as a clone, we pick a random costume, which will pick a different colour for the present. We then move the present up to where Santa is on the screen. We then repeat moving the block downwards on the Y axis until we either get low on the screen (less than -120) or we touch the green drop target. At the end we delete the sprite clone.

Keeping Score

We just need a variable now to keep score.

This variable will be reset to zero when the green flag hat block is selected.

On the backdrop:

We also then need to increase the present count when the present hits the drop zone.

On the present sprite:

Posted on

Scratch Coding – Reaction Timer

Today we are going to be building a reaction timer game. The goal is for our sprite to ask a letter and we keep track of how long it takes the player to press the correct letter.

We are going to be using some of the following scratch features:

  • Use a list to store the letters that the player can be asked
  • Make use of the timer blocks
  • Use variables to store the user’s speed

First up, we need to set the scene up. We need a nice backdrop and a sprite that can ask the player questions.

Task: Add a sprite and a backdrop.

We next need to create a list of keys that we will ask our player to randomly select. To create a list, browse to the data tab and select the ‘Make a List’  button.

I’ve called my list alphabet and I’ve added all of the characters in the alphabet:

The first thing we want to do is hide this list when the player starts our program:

Giving Instructions to the Player

Our player is going to need some instructions on how to play our game, let’s use our sprite to help get the message across:

We can connect these to our green flag hat block because we want the sprite to talk as soon as the player starts the game.

Once the player has the instructions we always want to keep asking the player to press a key. If we always want to do something, we use a forever block. We want to also make the player have to wait a random amount of time before we ask them to type. This makes the game more challenging, they have to be quick!

We also want to pick a different letter to ask each time. We will need a new variable to store the random letter that we have picked out.

I’ve called mine: goalLetter.

I set the value of ‘goalLetter’ to a random letter from the alphabet list. Then I ask the player.

Test: Does it ask the player a different letter each time?

Adding a Timer

Our game is starting to become interesting, but we really need to keep track of how long it takes the player to press the correct key.

To help us keep time, we are going to use the timer blocks:

The first step is to reset the timer, this means that the next time we use the   ‘timer’ block it will have the latest time elapsed values.

We also need a way to check to see if the player has pressed the correct key. For this we are going to create a variable called ‘pressedLetter’.

Now we check to see if the ‘pressedLetter’ is the same as the ‘goalLetter’, if they are we can stop the timer.

How do we set the ‘pressedLetter’ value?

At the moment the ‘pressedLetter’ variable is never set, so our code doesn’t work like we want. We need to add some code for each letter on the backdrop:

We continue this for all of the characters that are in our alphabet list.

Once the player has pressed the correct letter, we need to store the value of the timer.

If we want to store or keep something we need to use a variable. Let’s create another variable called ‘reactionTime’ this variable will store the timer’s value.

Now we have the time it took for the player to press the correct key. We can use our sprite to tell us how long we took:

Posted on

Scratch Coding – Rain Catcher

We need to fill our bucket up with rain water. Your program should move the bucket from left to right to catch the falling rain. Once the bucket is full and the player takes it to the bowl they get a point. But the player has to empty the glass if it touches the bird poo.

Sprites

We need to create two sprites: Bird poo and the dark cloud.

Making the Bucket Move

We need our bucket to move from left to right, so on our bucket sprite we can add the following blocks to test if the arrow keys have been pressed.

Moving to the left means we need to subtract values from the axis.

Make it Rain

On our rain sprite, we want to make sure it is hidden when the game first starts. Then every second we will make a new clone of a rain drop fall down.

When the rain drop starts as a clone it will first go to the cloud and then keep moving down on the Y axis until it touches the edge.

Make the Cloud Move

Since our rain will fall from the cloud, we need to make sure that the cloud keeps moving in the sky. To do this we will pick a random location on the X axis and only move once per second.

On our cloud sprite:

Make our Bird Move

We want to have our bird move smoothly across the screen for a random number of seconds. After it gets to its position it should broadcast a message that we can listen to on the bird poo sprite.

On our parrot sprite:

Make the Bird Poo

Now on our bird poo sprite, we want to listen for the bird poo message that is being sent via the bird.

We use the same type of code as before where we hide the sprite first and then show it once it is created as a clone. We make the falling speed a little faster to make it harder for players.

Add a Score

On our bucket sprite we need to add a few more tests. First we need to make sure the score returns to zero.

Change the bucket when water is caught.

On our bucket sprite, we must test to see if the bucket touches the water. When it touches the water we switch the costume to our full looking one. When we touch the bowl and our costume is number 1 (the full glass), then we give the player a score.

If we touch the bird poo, return the costume back to its empty position.

Posted on

Scratch Coding – Racing Car Game

This week we are going to create a top down car racing game.

My game looks like:

We can use the drawing tools to make the background road and add some grass. Maybe you might add some extra parts to your backdrop to make it more interesting?

Another example:

Notice how both roads have a red line to mark the finish? We are going to use this red line in our code later.

Our Car

Let’s draw a new car to drive around the road. I’ve used the square fill tool and created squares to make it look like a car.

When I’ve drawn the car I’ve made it face the right hand side.

If we brainstorm some of the parts of the code that we need to write, we might come up with things like:

  • Make the car move
  • Test if we hit the grass
  • Test if we are at the finish line
  • Keep track of the time the user took to drive through the track.

Make The Car Move

We want to use the arrow keys to make our car move. Let’s lay out the key press hat blocks so they match the movement we would like to code:

Our Up arrow, will just move the car forward a certain number of steps. Whereas the left and right arrow will turn the car.

We might find that our car doesn’t move straight. If this is the case then we can add the following block and attach it to the green flag hat block.

Test out the movement of our car.

We really need our car to start in the same spot each time. That will make it a little fairer when we start to time how long it takes a player to get through the maze.

Place your car at the starting point and then drag the go to x and y block under the green flag hat block.

Now every time we press the green flag button, the car will be reset.

Off Road Check

If our car goes off the road we want to reset the car back to the beginning of the course. We always want to be checking to see if the car is touching the grass, so we will be using a forever block.

We use the If block to check to see if we touch the green grass. If we find that we are touching the grass, we move the car back to the start and tell the use that its bad luck for them.

Check For The Win

Now that we can move the player back to the starting position when they hit the green grass, we need to check to see if we are touching the red finish line. Let’s add another check to the forever loop to see if we are touching the finish line colour.

Keeping Track of Time

We need to be able to see how long it takes our player to get to the red finish line. We need to add a variable to keep track of the time.

Now we need to do a few things:

  • Reset the time when the game is reset
  • Keep adding to the time until the car hits the red line
  • Wait 1 second, add 1 to the time variable.

Now when we play the game we will find that the score will continue to increase until the player reaches the red line.

We can also make the timer a little more precise, by changing the amount that the time variable is increased. Try changing to 0.1.

High Score List

Now that we know when the player hits the red line and we also know how long it took them, we can start to create a high score list.

Lists are like variables, except they can store more than one value. We create them just the same as a variable:

The list will be shown on the screen, it’s going to get in our way as we try to drive around the screen. Let’s add a block that will hide this list until we need it later on.

Once we get to the finish point we can ask the user for their name and then add it to the high score list.

Here we ask the user for their name and then build up a string that looks like:  

<user name> : <score>

  We do this by using two join blocks:

This lets us add the name and score to the list all in one go.

Test your game out and see if you can get the best high score.

Posted on

Scratch Coding – Penguin Feeder

The goal of this game is to keep the Penguin eating the fish. Too many fish will make him sick and not enough fish will see him go hungry.

Sprites

These are the two sprites I picked; a hungry penguin pal, and some yummy fish. Feel free to be creative when choosing your sprites!

The Penguin’s Name

We really need to name our Penguin, we will first ask the player to give our penguin a nice name.

We are going to select the tick box on the answer variable. If we double click on the variable in our main scene (the left view), we can make the variable look like a label.

Ask the player for a penguin name:

Setting the Hunger Level

We want to make our penguin tell us when he is hungry, we will need to use a variable to store how many fish he has eaten.

If the penguin has a hunger level of…

  • 10 or more: he is very full
  • 5 is the perfect amount
  • 0 is very hungry

Create a variable called hunger and set it to start at 5.

Fish Spawner

On our fish sprite, lets add some code to spawn new fish as clones:

When the sprite starts as a clone:

Our Penguin’s Health

On our penguin sprite, we are going to build a check to see if the penguins health changes. We are going to use a forever loop with some if statements that check the value of the health variable.

Posted on

Scratch Coding – Pacman

Today we are going to build a Pacman game. If you have never seen Pacman before, well you’re in for a real treat. The goal of the game is to move the yellow Pacman around the screen and eat all of the food pellets. We must also make sure that we avoid the ghosts!

We already have a nice backdrop with a Pacman style maze:

We also have a few sprites that we can use to develop our game:

You might notice that the Pacman has a little black dot in front of him, we are going to use that dot to detect if a maze part is in front of us.

Pacman

Let’s start by moving Pacman around the maze.

We want to always be checking to see if the player has pressed an arrow key, if we always want to be doing something, then we should consider using a forever block.

The next item to check is if the player has pressed an arrow key:

Repeat this for the up / down & right keys.

Test our game. Does Pacman move around the Maze? Does he stop at the maze walls?

Moving Animation

One of the cool things that Pacman does as he moves, is he opens and closes his mouth, we can use another green flag hat block and check for the arrow key presses.

If either the left / right or up / down are pressed we can switch to the next costume in the Pacman sprite. We can add a small delay so that it looks like Pacman is eating at a normal rate.

Crossing the Screen

If we move to the edge of the screen, we expect Pacman to cross over to the other side of the screen. Let’s add some code to always check where Pacman is.

This code checks to see if Pacman has gone past the boundary of our backdrop, which is located at 165, and -165. (not the normal 240 and -240)

Ghost Movement

Our ghosts should move around the screen, we are going to make them move in two different directions:

Up/Down

Moving the Y position up and down depending on if the ghost hits a maze colour. After each touch we add a little bit of movement in the opposite direction, this prevents the ghost getting stuck on the maze wall. Only add one of these to each type of ghost, so pick a ghost that moves up and one that moves down.

You will be able to duplicate the ghosts later to make your game harder.

Left/Right

Moving the Ghost left to right has us changing the X position. We follow the same pattern as moving up and down.

Food Pellets

Finally, we can add food pellets to the game and have them hide when they are touched by Pacman. We can duplicate more of these items and place them in other locations on our screen.

Extra Challenges

  • Add a scoring system
  • Add code to make Pacman die if he touches a ghost
Posted on

Scratch Coding – Number Guessing Game

Today we are building a number guessing game in scratch. The aim of the game is to guess the number that the computer picks.

We will need a sprite and a nice backdrop to make our scene look great. I’ve chosen a nice-looking fish, sitting in front of a brick wall, a regular fish out of water story.

The first part of our game involves us telling the user what the game is about. We need to have our sprite say a few words when the ‘Green Flag’ is pressed.

We first drag over the ‘Green Flag Hat Block’.

Let’s connect some ‘Say’ blocks from the ‘Looks’ tab.

My fish is going to say ‘Hello’ and then ‘Let’s play a game’.

Let’s test to make sure it’s work the way we think it should.

Before we ask the user for their guess, we need the computer to pick a number and remember it. To get the computer to remember our number, we need to use a variable. A variable is just a spot where the computer can store something.

We add a variable from the data tab:

Give this variable a name like ‘NumberToGuess’, this will be the number that the computer has picked for the player to guess.

We are going to set the variable to a random number, you can pick how hard your game is going to be by changing the range of the random numbers. I’ve picked 1 – 20, meaning that the computer could pick a number between 1 and 20.

After we have stored our number in a variable, it’s time to ask the user for their first guess.

We use the blue ‘Ask’ block for this (under the sensing tab). The block will ask the user a question and wait for the user to respond by typing something into the text box.

Now that the user has given us their guess, we need to compare it with our stored number to see if it’s higher or lower.

We can use the blue ‘Answer’ block to provide us with the number given by the user.

Since we want to test the number, we are going to use the If block:

Anytime we want to test something, we should be thinking about using the If Then block.

To see if the number is greater than our variable, we can use the green operators tab and select the greater than sign (>).

We can place the blue answer block in the left hand side of the green > block and the orange variable in the right hand side.

Any code that is inside the “if” block will be run if the user’s answer is greater than the picked out number.

Test this out, use the checkbox to show the number on the screen. This will show what number the computer picked, this will help you see if your code runs properly. Make sure you type a number larger than what the computer picks.

Now that we have the greater than part handled, now is the time to add the case when the user picks a number that is less than the number picked by the computer.

Use the less than green block:

Test it out again. Make sure it works how it should.

Now the only other possible case is if the player picks the same number that the computer picked. In this case, we are going to use the equals sign.

Test this out. What is missing from our game at this point?

We only give the player a single chance to guess the number. We really need to give the player more chances.

We want the player to always be guessing a number until they pick the correct number. If we always want to be doing something, then we should be using the forever block.

Test this out.

Now our game doesn’t end, even if the user selects the correct number. Let’s add a little bit more to make the game interesting for our player.

Let’s keep count of how many guesses they take before they reach the correct number. Add a new variable called ‘Guesses’, so we now have two variables:

We need to make sure that our game resets the number of Guesses to zero when it first runs. Under the hat block, add the Set block:

We made it 1, because it will take at least one guess to pick the correct number.

Now we want to stop the game if the player picks the correct number, so add the ‘Stop All’ block inside the equals if block section.

Next, we need to tell the player that they need to pick another number, so ask them to try again. We also need to make sure that the first ‘Ask’ block is removed from the ‘forever’ block, we only want to ask that question once, after the first attempt we will say ‘try again’.

Finally, we need to give our players some reward for picking the correct number. This could be a cheering sound or maybe it could be some really cool visual trick.

I’ve done something cool to my fish:

We need to make sure this is just above the ‘Stop All’ block. My final code is shown below, but try to make it interesting.

Posted on

Scratch Coding – Banana Throw

This week we are going to be building a fun game where we pick up and throw Bananas at a flying object.

We first need to setup the scene with our sprites. I’ve added a Bat, Banana and the Monkey.

My backdrop is a nice plain brick wall.

The first part of our game that we are going to add some script to is moving the monkey.

We really want our Monkey to move left when we press the left arrow key and right when we press the right arrow.

We always want our Monkey to be ready to move, if we want to always be doing something, then we need to use a forever block. On our Monkey sprite, let’s add the script:

Now, it doesn’t do a lot, we really need to check to see if the arrow buttons have been pressed. If we want to test something, then we need to use the if -> then block.

We can find the blue key pressed block under the sensing tab. It will look like:

Notice that the shape of the block is the same as the if -> then shaded hole block?

Make the Monkey Move

Now that we can test if the arrow keys have been pressed, we need to make our monkey move. First up we get him to point in the correct direction. Use the point in direction block for this:

We need to make sure that our monkey points left inside the ‘left arrow’ if -> then block and that our monkey points right inside the ‘right arrow’ if-> then block.

Now is a great time to test our script.

Did we find that our Monkey turns upside down when we change direction?

Scratch thinks that it can rotate the sprite when it changes direction, we need to tell it that it should mirror the sprite. We do this by setting the rotation style to left-right:

Our Monkey now moves correctly! But it doesn’t look like he is running anywhere. To animate him to make it look like he is running, we need to switch costumes. We can use the ‘next costume’ block for this:

Our code now looks like:

We added another block to make sure that our monkey starts in the same spot each time.

Flying Bat

We would like our bat to fly from side to side across the screen, this is going to give our monkey something to aim at. Switch over to the bat sprite and be sure to add the green flag hat block.

We always want our bat to be flying, so we will need to use a forever block.

Test this and observe what happens.

Notice that our bat hits the wall and stays there. Let’s find the special block that will help our sprite bounce back off the wall.

Now our code looks like:

We want to be able to fly back and forth only until the bat hits a banana.

To wait until the bat hits the banana we need to add another new block:

We can combine this with the touching sense block:

Now our bat will continue to fly backwards and forwards until it touches the banana. At the moment we don’t have any script after the repeat-until block that makes the bat fall out of the sky.

We need to add a few other little blocks to make the movement of the bat look better when it hits the banana.

After the ‘repeat until’ blocks, we can move the bat downwards, so that it makes him look like he is falling from the sky. Then we move a number of steps, which creates the falling effect.

This gets added to form:

Test it out: Try moving the banana over the bat when its flying. This will tell us that our logic is working.

Notice how we need to reset the bat’s position?

Add a go to block above the ‘repeat until’ block.

Now we just need to pick up and throw our bananas!

Bananas

The last part that we need to add is the ability for our monkey to pick up and throw the bananas.

Let’s start with the banana pick up. We always want the banana to follow the Monkey as he moves around. We want the banana to follow the monkey until we press the ‘space’ key.

Test: Our banana should follow the monkey.

We also set the banana pointing up, so that when we press the space button and start throwing, it will be in the correct position to start moving.

After the space key has been pressed, we want to move the banana up the screen so that it looks like it has been thrown.

Next, we want the banana to appear either on the left or right hand side of the screen, ready for the monkey to pick it up again. Let’s use a “random number” to choose which side to place the bananas on once they appear again.

Finally add the wait “until touching Monkey” block. This will cause the bananas to sit in the corner until the monkey touches them. Once this happens it will cause the bananas to follow the monkey along, until the space button is pressed.

Posted on

Scratch Coding – Monkey Platformer

We are going to build a game where a monkey must collect some moving objects. This monkey will have the ability to jump across the platforms in order to get the height needed to collect the balls.

The monkey should move in the following way:

  • Left arrow makes the monkey run to the left
  • Right arrow makes the monkey run to the right
  • Up arrow makes the monkey jump in the air

Sprites

We can start with the following sprites:

Monkey Jump

We can make our monkey jump with the up arrow, but we need to change the monkey movement so that the monkey falls back to the ground.

Since we selected a nice backdrop with some brown ground, we are going to always be changing the Y position of the monkey when it is not touching the brown colour.

If we test this, we see that our monkey doesn’t do much, if we start our game with the monkey in the air, we see that the monkey will fall to the ground.

To make the monkey now jump we just have to add a little push upwards:

I’ve also added a check here to make sure the monkey is standing on the ground when he does a jump.

Monkey Move

At the moment all our monkey can do is jump, we can add some code to make this monkey move!

We check to see if the keys are pressed and if they are, we then move the monkey on the X axis. We also switch to the next costume to make it look like the monkey is running.

Make the Ball Move

The goal of the game is to have balls moving across the screen for the monkey to catch. Let’s switch to the ball and get it moving.

Since we want a number of balls to be flying at the monkey at the same time, we are going to use a clone. To set this up we add:

This means a new ball will be created every 1 second. You might like to make this larger if too many balls are coming at the player.

Now when the clone is created we move it to the left hand side of the screen and start its movement.

We ask for a random spot and then set the X to the right most position. We repeat until we move to the left hand side of the screen. If we touch the monkey along the way, then we delete the clone. Also if we get to the left hand side of the screen, we also delete the clone.

Keeping Score

We can add a new variable and use that to keep track of how many balls the monkey has caught. Call this variable ‘Score’, we should have some new blocks:

Now on our monkey sprite, we need to add a check for our scoring:

Once we touch the ball the score is changed by 1.

Running Out of Time

Let’s add a timer so that we can give our player just a small amount of time to collect the balls.

On the backdrop, add a new variable called ‘timer’.

We then add some code that will count the timer down to zero. First I set the timer to 30 seconds, then I repeat until the timer is zero, changing the value of timer down by one each second. After the timer is zero, we stop the game.

Now we can draw some more platforms to make our game easier.

Posted on

Scratch Coding – Lightning Knight

The goal of this game is to collect power from the lightning strikes. Our game scene will have a Knight dressed in his metal armour, this protective suit will help him collect electricity to charge up the ball sprite.

Of course the rain will reduce his electric charge by one.

Our player will control the knight by using the arrow keys.

Sprites

We need to use a number of sprites for this game. I’ve used Button 2 to be our rain drops, I used the shrink tool to make them the perfect size.

Knight Movement

The knight can move only from left to right, to achieve this we will use the keyboard hat blocks and move the knight only along the X axis.

Power Score

We will create a variable called ‘Power’ that we will use to keep track of how much power we have collected.

On our Knight Sprite we will add a check to see if we are touching the lightning and change our Power by one if we are touching:

We wait for one second after each time we are touching the lightning before checking again. This delay will mean that our score only goes up by a maximum of 1 per second.

Lightning Our lighting will fall from the sky quickly. We first want our player to be able to see where it is going to form, then we have it fall to the bottom of the screen quickly before being removed from the scene.

We are going to use sprite clones to make the lightning work nicely. When the game starts we are going to use a forever loop that creates a clone every second:

Now when the lightning starts as a clone, we want to make it move from top to bottom.

Clouds

Our clouds will move randomly across the screen only from left to right.

We set the X between -220 and 220 so that it’s not quite off the screen. Remember the screen size is -240 to 240. The clouds can move randomly between 1 and 5 seconds.

Rain

Our rain sprite is also going to use clones. We are going to use very similar code to our cloud sprite, we only want it to rain randomly.

Now when the rain sprite is created it needs to start directly on the cloud. Then we need to make it fall to the bottom of the screen.

If it touches the Knight on the way down, we need to remove some power. If it touches the edge of the screen the clone should be deleted.

Power Orb

The power orb will change colours depending on how much power the Knight adds to it.

Extra

Add more If statements to change the Orb colour as more power is added to it. There are 5 costume colours to choose from.