Posted on

Scratch Coding – Jump the Ball

The goal of this game is to have our character jump the ball.

We will be using two sprites to make our game, we need a sprite that can jump the ball when we press the space key.

We also need a sprite that can be the object that needs to be jumped over.

Jumping Sprite

We are going to use the Glide block to make our character jump up and then jump down.

We want our sprite to start in the same spot each time.

We then want our character to jump when we press the space key.

I’ve made my sprite jump up 140 pixels, and then jump down a little slower 140 pixels. Be sure to use the + and – blocks. We also use the ‘x position’ block to make sure the character doesn’t jump forward or backwards.

Test these numbers out to find a good jumping speed. If you want to jump sideways on the X, give it a try as well.

Now we need to have the ball fly across the screen.

We always want the ball to move across the screen, so we will use a forever block. When the x position gets to -230, it will be very close to the edge of the left hand side of the screen. We then move it back to the right hand side so that it can fly across the screen.

We change the X by a number (I’ve chosen 10), and I’ve also made it look like it’s spinning by using the ‘turn 5 degrees’ block.

Making it Harder

We can add a variable called ‘BallSpeed’ which will store a random number that represents how fast the ball moves across the screen.

Add the Score

We can make the scoring system add one to the user score when the ball is reset to the starting position, this can only happen if the player jumped over the ball successfully.

We then need to add a test to see if we touch the jumping sprite, if we did hit it, we subtract from the score and move the ball back to the right hand side of the screen.

Test out the random speed numbers.

Posted on

Scratch Coding – Fruit Slicer

We are going to make a game called fruit slicer, you might have played a similar fruit cutting game. The idea is that fruit is thrown into the air and you must cut it with your sharp mouse cursor. The player must be very careful because fruit doesn’t always get thrown into the air, we also throw objects that could break our sharp mouse cursor.

Sprites

I have selected a few fruit sprites that we can slice up, I’ve used a lump of rocks as the object to avoid.

We also need to create a sprite costume for the fruit in its sliced state.

To do this I changed the sprite from a vector image to a bitmap by selecting the convert to bitmap button:

Then I could use the select tool to select half of the object and move it away from the other half.

Backdrops

We are going to have two backdrops, one is the normal game play backdrop, the second is the one that is shown when the game is lost:

I put the words you lost on mine.

Flying Fruit

The first part of our game is to get the fruit flying into the air. Let’s select our apple fruit to start with.

We are going to have the apples fly up when our game starts, so we start with our green flag hat block. We also want our apple hidden until it is ready to be shown on the screen.

We want our apple to fly across the screen with some up and down motion. We are going to need the computer to remember the across and up down speeds.

For this we are going to use two variables: UpDownSpeed and AcrossSpeed.

Let’s create the variables:

We need to make sure that the ‘For this sprite only’ checkbox is selected on all of our variables. Otherwise all of our sprites will rise and fall at the same time.

We setup our code so that we set our UpDownSpeed and AcrossSpeed to some random numbers. Experiment later to find some good numbers.

We then create a clone of our sprite which we will animate across the screen.

We find a random location on the screen and then we use the Go to X Y block to put the sprite in its place. We then show the sprite.

We want the sprite to be alive until it hits the bottom of the screen, so we can create a test that will keep our sprite moving until it hits the bottom.

When our sprite hits the Y position of less than -170 we know it has gone close to the bottom of the screen.

To make our sprite move:

We change the UpDownSpeed by -1 each time the loop runs. This number starts as a large positive number and slowly becomes a negative number which makes it fall. We also add a little rotation to make it look like the object is truly flying.

Test this out, we should see some flying apples.

Slice that fruit

Now that our fruit is flying, we can add a test to see if the fruit is touched by our mouse cursor.

We have two options for our game, we can either make it so that the fruit gets sliced when the mouse is down, or just when the mouse moves over the fruit.

To test for the mouse button being pressed:

To test for just the mouse touching the sprite:

What do you think works better?

Keeping Score

We need to create a couple of variables to keep track of how many items have been hit and missed.

My variables are called ‘Hits’ and ‘Misses’, these are created with the ‘for all sprites’ option set.

Now we can add our scoring:

We add a short delay before we remove the sprite from the screen.

The miss needs to be add the end of our code block:

Now we can copy these scripts over to our other sprites, be sure to change the costume references in each sprite, when we copy them over, they won’t be set correctly.

The Hard Rock

The rock needs a few changes to make our game complete:

We need to switch to our ‘you lost’ backdrop when the rock is hit, then we stop the game. We also need to remove the costume changes.

Posted on

Scratch Coding – Flap a Bat

We are going to build a game that is very similar to something popular, we are calling it: Flap a Bat.

The goal of the game is to keep the bat flying and not hit the walls that close in. The bird will fall from the sky unless you hit the space key to give it some extra boost.

Sprites

We will need a few sprites to get our game working:

Making the Bat Fly

Our bat should always be falling from the sky, we know that the Y axis controls the up / down movement. If we always want something to happen, then we should be using the forever block.

I’ve moved our bat to the position I want it to start flying with. Test this out and we will see that our bat slowly falls from the sky. We can animate the bat flying by adding in a costume change.

Giving the bat some extra boost

At the moment our bat falls from the sky with no way to keep it in the air. We need the user to tell us when to make the bat fly. We want our user to press the space key. To make our bat fall we used a negative number, so to make it fly we can use a positive number:

Make the building move

We want our building to be able to move across the screen, let’s select the bottom building sprite first. Drag it to where you want to start the movement.

I moved my building all the way to the right hand side of the screen. This will set the correct position on the X – Y movement blocks which we can use.

We can then use the glide block to set the position of the building that we want it to move towards:

I changed the X position to be -240 which is the lowest X number that our game area has.

If we run this we see that the building moves across the screen, but only once. We need to change this to happen all the time.

The next problem we have is that the buildings are always the same height from the bottom of the screen. We can add a little bit of randomness to this.

We used negative numbers because the bottom half of the screen on the Y direction is between 0 and -180. If we test this out, we find that the buildings move diagonally. This is because we set a Y position in the glide block. We want this Y position to be the same number that we randomly moved, if the glide block has the same Y position at the start and end, then it will move straight.

Drag the Y position block into the glide block:

Now we have a building moving across the screen at the bottom.

Top Building

The process for moving the top building is the same, but this time we use positive numbers for the random part. Be sure to place the building in the starting position first!

My code looked like:

Hitting Buildings

We need a test to see if the bat hits the building. To test if the bat is touching the buildings we can use an “If” block. We need to see if the bat hits either the top building or the bottom building.

Select the bat sprite and modify the script to include the “If” test:

I’ve made my game stop at this point, but later we will change this.

We can also add a test to see if the bat hits the edge of the game screen:

Scoring

We need a way to score our game, if we count each time the one of the buildings gets to the end we can use this as a chance to give the player a point.

Add a variable called ‘Score’ and select one of the buildings to use as the scoring reference.

We only need to do this to one building.

Increasing the difficulty

Our game is good, but we can make it get harder the longer the player plays it. We can make the buildings speed up.

Create a new variable called ‘Speed’, we are going to use this to decrease the amount of time that it takes for the building to move across the screen.

Use the same building sprite that we added our scoring code into, the other building will just need the speed block added to the glide:

Our game gets a little faster, but at some point the speed number is going to get really fast and it will look like the buildings aren’t moving at all. We need a way to set the max speed of the game:

We add a test to see if the speed is less than 2 seconds and then set it back to ½ a second. You can experiment to find a good number.

Extra

Give the bat lives and decrease the lives each time it hits a building. We can add an extra variable that will keep track of how many lives the bat has.

We still make it so that the game is over if the player hits the edge, but we take away a life each time the building is hit. We need to make sure that we move the bat to the middle of the screen to give the player a chance to keep going.

We also need a check to see if all of the lives are used up:

Where should this test go?

Posted on

Scratch Coding – Fall and Catch

Today we are going to make a game where we must catch something falling from the sky.

My game has a basketball falling and a bowl to catch the ball. We are going to first use the keys to control the position of the bowl. The balls are going to drop from a random position and fall down. We want to keep a count of how many times we catch the ball.

If we break down our game into parts, we find that we can look at a few small tasks which will make up the whole game:

  • Move the bowl backwards and forwards
  • Make the ball fall
  • Keep count of how many balls are caught
  • Reset the game and make sure everything is in the correct position

Task 1: Move the bowl backwards and forwards

Rather than using a green flag hat block to start our game, we are going to use the Key Press hat blocks to run some script when we press the left or right arrow keys.

On our Bowl Sprite:

Now we only want our bowl to move left or right, not up and down. Moving left and right is on the X axis:

Use the ‘change x by’ block to make it move. Since moving left on the X axis is moving closer to zero, we need to make this a negative number.

Test it: Test to see if you bowl moves correctly.

Task 2: Make the Ball Fall

Before we can make the ball fall down; we need to make sure it is in the correct position for it to begin falling.

Let’s use a Green Flag Hat Block to help us find a nice random position.

On our ball sprite:

I’ve used the random-position block (this is the go to ‘mouse position’, but with the option changed).

Test: What happens, when we press the green flag button a few times?

We find that the ball will move to any height, this is going to make it hard for us to catch the ball. We need to make sure that the ball stays at a nice height so that we have a chance of catching it.

Let’s make sure the ball stays at a nice height:

Now we set the Y position to 180, so that no matter where the random position is, we set the height to 180 regardless.

Test: Does the ball position look good?

Now it’s time to make the ball fall to the ground. We always want the ball to be moving down, so we are going to use a ‘forever’ block.

We also want to move down, which is on the Y axis, but using a negative number:

Test: Does it work?

We now find that the ball sticks to the bottom of the screen. We need to be able to test to see if we hit the bottom and restart the ball falling again.

To test something out, we use the ‘If’ Block. For our ball we want to test to see what the y position is. We know that -180 is the very bottom of the screen, so we should be safe testing this at -170.

Test: Does our ball reset now?

Task 3: Keep count of how many balls we have caught

Before we can count how many balls we have caught, we need to be able to test to see if the bowl has touched any balls.

On our ball sprite, we need to always be testing to see if we touch the bowl. If we always want to be testing something, we use a forever block and an if block together:

I’ve added a nice pop sound. You should add a sound to your project and play it when the ball is caught.

Sounds can be found on the sound tab. A new sound can be found by pressing the ‘Choose a sound’  button.

Test: Does the ball make a sound each time it is caught?

Now that we have our ball and bowl testing to see if they touch, we can easily add a score to our game.

Let’s add a new variable called ‘Score’.

Since the score is used over the whole game, we can set it ‘For all sprites’.

We need to change the score when the ball touches the bowl. We already have some code to play a sound when this happens, this will also be a good spot to change our score.

We add the score to the very end of the If block.

Test: What happens to the score if we restart our game a few times?

Task 4: Reset the game and make sure everything is in position

We should change the ball sprite, so that the score is set to zero when the green flag button is pressed.

Now that our game is working properly, we should also make sure that the bowl is put in the correct position also.

On the bowl sprite:

We added a green flag hat block and then set the Y position to -125 (your number might be different).

Extras

Loose points when the ball hits the bottom of the screen:

We can remove points from the player when the ball hits the bottom of the screen. Remember that we reset the ball position if it got to -170 on the Y axis?

This is also a good spot to change the score by -1:

Adding extra balls to the game:

Catching one ball at a time is cool, but can we make it a little harder?

Use the duplicate option to create more game balls.

Change the colours of the sprite when the player gets a certain score:

We should test the score. To do this, let’s add to the existing forever block on the ball sprite:

Now we want to make sure that the ball changes colour:

Posted on

Scratch Coding – Drive by Avoider

The goal of our game this week is to avoid the traffic obstacles on the road. We will control our vehicle by using the arrow keys.

Backdrop

Start by creating a backdrop that looks like a road. I used the rectangle with fill mode turned on for the road base, then the paint bucket tool for the green sides.

Sprites

Feel free to create as many sprites as you would like, be sure to pick one to be the main character.

Our Main Sprite

We want our main character to move left and right.

Hitting the Green and Resetting

We want to add a cool feature where the character will bounce off the green sides. When we test to see if the sprite hits the side green we want to automatically slide the sprite back to the middle of the road. The player will not be able to control this movement, so hopefully no other objects are falling.

Detecting our Objects

We need to test to see if the main player sprite hits an object, we can setup our forever loop to include some tests to see if we are touching the other sprites.

The Falling Sprites

To make our sprites fall, we are going to use the glide block. We must first set the starting location using the go to x: y: block, we use some numbers at the top of the screen. Then we glide to a position at the bottom of the screen. We then hide and wait for a random amount of time before putting the sprite back on the screen.

Setting the Score

We should add some scoring to the game, each time the player gets past an object we change the score by 1. Be sure to set the score to 0 to start with.

Game Over

When our player hits one of our objects, they should show a game over backdrop and stop the game.

Extra

Each new falling object should have a random delay on the time it takes to fall for the first time.

Notice how the first instruction of one of our obstacles is a wait a random amount of time?

More Ideas:

  • Add at least 5 objects that fall at different times
  • Make the player sprite move forward and backwards using the up and down arrow keys
  • Make a countdown timer that only gives the player a certain amount of time:
Posted on

Scratch Coding – Burger Maker

We are going to make a game where the player can choose the ingredients that make up a burger and find out how much our burger will cost.

Sprites

We have a lot of sprites for this game, they can all be found on your computer and can be added by clicking on the folder icon.

Make your sprites smaller and place them to the left so that the player will be able to click on them.

Burger Base

Let’s setup the burger base first, we want this sprite to sit in a location that will can use to stack the other parts of our burger on top of.

We setup a nice spot for our burger base.

Variables

We need the computer to do a few things:

  • Add up and remember the total cost of the burger ingredients
  • Remember the position of the last ingredient so that we can add items on top
  • Only let the player add one ingredient at a time

To help us achieve these goals, we will create three variables:

Now, we can set these to some nice numbers on our game start:

Ingredients

All of our ingredients are going to share the same code, with the exception of the cost of the ingredient.

We want our player to click on the ingredient and have it fall to the burger base. We are going to have our sprite create a clone of the ingredient so that we don’t have to move it away from the left position.

Since we are going to be using a variable called ‘Y Stop Position’ which will remember the Y position of the last ingredient. We only want the player adding a single ingredient at a time.

We also have a variable called ‘CanAdd’ which we can use to see if our player can add a new ingredient. Our game starts with this set to 1, when a player clicks an ingredient we change it to ‘0’, then when the ingredient is in place, we then set it to ‘1’ again.

So our click will look like:

Now when our ingredient starts as a clone:

We have the sprite go to the front, we setup a position at the top of the burger base, then we have the ingredient move down to the value of our ‘Y Stop Position’ variable. Since we add to this every time an ingredient is added, this position will get larger and larger, so the ingredients stop higher on the screen.

We can now copy this code to our other ingredients and change the cost of the ingredient.

Burger Top

The burger top is the last item that makes up our burger, we want to be able to tell the player how much their burger will cost.

The code for this is very similar to the other ingredients:

However, we tell the user the total cost at the very end of the of the script.

Drink and Fries

Do we want to let our player select a drink and fries with that?

When the game starts, we move the sprite to the left and make it a little smaller. Once clicked we move it to the position next to our burger and make it a little larger. We also add to the ‘Total Cost’ so that we make sure our player gets charged correctly.

Posted on

Scratch Coding – Goal Kicker

The aim is to create a game where you attempt to score a number of penalty goals.

The scene should look something like this:

The idea is that you use the cross to aim by using the arrow keys (up, down, left, right). Use the space key to shoot the ball into the net.

The goalie should use a random number to determine if they dive left or right and use a random number to determine how far from the centre they should move.

We should add our sprites first:

You will need to draw the nets and posts separately. We don’t want to draw them together because it’s easier to determine if the ball hit the posts if they are separate sprites.

Connect your aim sprite up to the left, right, up and down arrow keys. We use the ‘change X by’ and ‘change Y by’ blocks for this.

When the space button is pressed we want to move the ball towards the Aim sprite.

We can use the following block to help us point the ball in the correct direction (towards our aiming sprite):

We can use a loop (repeat block) to move the ball along, we can use a calculation like:

Inside the repeat we can move 5 steps (or change this to be faster, but both the repeat condition and the move steps should be the same).

Inside this loop we can also use the change size block to make it look like the ball is moving away from us.

This will look like:

To determine which way the goalie should move, we should use a random number to determine the left or right direction.

To show the line that follows the ball, we can use the Pen, think of this pen like a real pen. We need to put the pen down before we can write and we lift the pen off the surface to stop drawing. The pen moves with the sprite and leaves a mark.

For a 50% chance:

To determine if the ball hits the net we need to test both the net and the goalie. If the ball is touching the net and not touching the goalie, then it is a goal. We can construct this type of operation by combining operator blocks.

We have learnt about the green flag hat block before and how it is run when we start our program, but we can also create custom messages that we can broadcast to all of our sprites.

Create broadcast messages for the following events:

  • Reset
  • Win
  • Lost

Reset will be raised after the goal kick has taken place and we need to reset for the second kick.

Win will be raised once the number of goals reaches our desired win value.

Lost will be raised once the number of misses reaches our allowed misses.

Answers:

Code for the Aim Sprite:

Code for the net sprite:

Code for the post sprite:

Code for the lost sprite:

Code for the win sprite:

Code for the Goalie:

Code for the soccer ball:

Posted on

Scratch Coding – Bird Bombs

In this game we are going to be flying a bird above a person walking below. Our bird will drop some ‘bombs’ on the pedestrian to score points.

Sprites:

We can use the bird sprite and make our own cool bomb with two costumes.

I created two simple costumes that will make it look like the bomb is falling from the sky and a second circle that will make it look like it splattered on the ground.

Bird Movement:

The bird will fly from left and right, we can use the keyboard pressed events to make this work. Since we are looking at making the bird move across the screen, we will use the change X block.

Left uses a negative number and right uses a positive number.

Pedestrian Movement:

We want our pedestrian to walk along the bottom, so we’ll need to be able to change costumes. Since we want to change costumes, we can’t use the Glide block like we may have in the past. Instead we will have to check to see if she gets to the end of the screen and restart her walking.

We want to make sure that her costume changes to make it look like she is walking:

Next, we can use the same change X block that we used to move our bird, this time it will move the pedestrian across the screen.

We first move the pedestrian into the place we want them to start walking by using the go to X and Y blocks.

We can then test to see if the pedestrian touches the edge of the game, if they do, then we can reset them to the starting position.

We might introduce a bug into our game if we move the pedestrian back to left and touching the left edge of the screen. If we really want to avoid this, we can check to see if the pedestrian is near the right hand side by checking the position.

(different approach – same outcome)

Next we can make the pedestrian walk different speeds by introducing some random blocks.

Run and test this out, notice that the pedestrian stands still before being reset?

We can change this by adding some hide / show blocks:

Now our pedestrian is moving very smoothly.

Bird Bombs:

We want our bird bomb to drop from the bird, so we need to make sure the bomb is always following the bird until we press the space key.

We also want our bomb to be hidden until it’s time to start dropping.

We will create a clone of our bomb that we will send to the ground. We can use the ‘when I start as a clone’ hat block to set this up.

Now we can make the bomb fall to the ground until it touches the edge of the screen, then we switch to the fall image and make it look like the bomb went splat. After a second we delete the clone to clear the screen.

Pedestrian hit by a bird bomb:

We can add a test to see if the pedestrian is hit by a bomb and increment a hit score. I’ve made my pedestrian change colour when they are hit.

Posted on

Scratch Coding – Whack a Cat

Aim of the game is to hit the cat with a hammer. Just like the classic Whack-A-Mole game.

The cat will pop up from a random hole in the ground and your job as the game player is to hit the cat when it is fully out of the hole.

We will try to trick the player by having some of our cats only poke their heads up part of the way.

We will be learning how to use the following blocks with this code:

  • Random – We will be using random numbers to determine which cat should come out of the hole.
  • Broadcast messages – We will continue to use the broadcast messages to show the cat.

The variables we will need to use:

CatToShow is used to store a random number that will determine which cat should be shown.

PretendCatToShow is used to store a random number that will determine which cat will push his head half way to trick the player.

Score will store the number of times the cat has been hit with the hammer.

Step 1: Animate the hammer when the mouse is clicked

We want to move the hammer around where ever the mouse is, to do this we must use always be moving the sprites position to where the mouse cursor is.

When the hammer sprite is clicked on, we want the hammer to rotate in one direction, wait a small amount of time and then rotate back to the starting position. This will look like we have tried to hit something.

Step 2: Setup the background to send messages to the Cats

We should firstly reset our score to zero every time the program has been started.

Setup a broadcast message called ‘Play’. On the same sprite, use the hat block that will respond to the Play message.

The backdrop Play message will pick a random number that will tell us which cat has been chosen to appear, store this in our CatToShow variable. We will use an if block to compare the CatToShow variable with a number that we assign to each cat. We then broadcast a message inside the if block to the cat.

For example, cat 1 will receive the message ‘Cat1’, cat 2 will receive the message ‘Cat 2’.

Step 3: Cat Script

Our cat has 3 costumes:

  • A blank hole
  • A cat which is half way out of the hole
  • A cat that is fully out of the hole

We need to respond to our Cat message and change costumes. The first costume should switch to costume 1 (blank hole), then wait a small amount of time and switch to costume 2, wait a small amount of time again and show costume 3 (the cat is out of the hole). Wait about half a second and show costume 1 again.

Once the cat is back in the hole (we just showed costume 1), then we can broadcast the ‘Play’ message again which will be picked up by the backdrop and the game will restart again.

Our cat also needs to be checking to see if they have been hit by the hammer.

This check should include:

  • Check that the costume number is equal to 1
  • Make sure that the mouse button is down
  • We might need to include a colour on the hammer to be more precise

The completed code:

The code for the cat is:

The code for the hammer is:

Code for the backdrop: