Posted on

Scratch Coding – Rock Paper Scissors

The goal of this game is to beat the computer in a game of rock paper scissors.

The game should start by displaying the options to the user:

The user should then click on the sprite that represents the option they wish to take.

The options should then hide and the hands should move up and down and revel the winner or loser.

We will need two variables that will store the selected position:

The first task of each clickable option sprite is to reset its visibility when the game is started:

Next is to set the player variable to the matching value of the clickable sprite:

Notice that we are using the ‘when this sprite clicked’ block. Each different sprite will set the Player variable to the value that matches the option chosen (‘P’, ‘R’, ‘S’, for ‘Paper’, ‘Scissor’, ‘Rock’).

We broadcast a message to all sprites when the game starts.

On our backdrop we can first setup the correct starting backdrop, which is the blank template.

When the backdrop receives the ‘StartGame’ message, we can then pick a random number that will represent the computer’s selected position.

The code will pick a random number between 1 and 3 (the number of options that can be selected). It will assign to the computer variable a character that we can later use to compare against the player variable to determine a winner.

We will broadcast a message to the player arm and send a message to the computer arm with:

  • ‘Person Show’
  • ‘Computer Show’

We wait 3 seconds for the arm moving animations to complete.

Then we compare the two variables and set the backdrop to who the winner is.

The first comparison we look at is to see if the player and the computer have the same value, if they do, then we know the game is a tie.

Work through each of the comparisons and try testing your game to see if you have captured them all.

Finally wait 5 seconds and then send a ‘reset’ message that can be used to setup the sprites again for another game.

The completed code:

The stage should have the following code:

The computer arm sprite should have the following code:

It listens for the ‘ComputerShow’ message, which it will use to animate the arm moving and then will switch to a sprite that has the correct animation.

The person arm sprite should have the following code:

The animation is the same, however we change the animation based on the player variable.

Posted on

Scratch Coding – PokeCat

The aim of the game is to throw the Pokeball at the cat and catch it.

Setup the scene with the sprites show above.

The Pokeball will be controlled by the flick of the mouse (while the mouse button is pressed).

We will need to work out the speed of the flick of the mouse, this will determine the position of the pokeball.

Create the following variables:

Poke Cats Caught – The total number of cats we have caught

escaped – Random number to determine if the cat escapes

speed – the speed of the mouse flick.

Build a ‘My Block’ to animate the ball throwing. Using a My Block will allow us to use this animate in more of our code without re-writing it.

Step 1: Move the ball to the correct position:

Step 2: Use the mouse to work out the speed of the ball.

Let’s wait until the mouse is down and the sprite is touching the mouse before we start working out the speed.

Inside our forever block:

When the mouse has been released, point the sprite towards the mouse and then calculate the distance to where the mouse pointer is now (after the button has been released). Make this speed a small number, so we can use it on our backdrop which has a size of 240, 140.

Set the speed variable to this calculation.

Step 3: Animate the ball moving

We want to animate the ball moving, we need to make our ball slow down, to do this we will change it by -0.1 each time the loop runs. If we are higher than 80 pixels high (y position), then we need to move the ball back down on the screen to give a dropping effect.

Otherwise we will move the ball the number of steps that our speed variable currently has in it.

Once the ball has come to a stop, we broadcast the ‘ball thrown’ event, which we can use to animate the capture of the poke cat.

(Don’t forget these 3 steps go into a forever block).

Step 4: PokeCat – Determine if hit.

Use the event ‘ballthrown’ to test if the ball is touching the cat.

Step 5: Reset if we missed the cat

The cat will raise an event called ‘miss’ if we haven’t touched the cat with the pokeball. In this case, we can reset the game:

We can use the My Block in the Pokeball sprite for this.

Step 6: Animate the hit

First we need to switch to the flashing costume of the pokeball and make it get bigger over a small period of time.

We then switch back to the pokeball sprite and make it move backwards and forwards so that it looks like the cat is trying to escape.

Use a random block to determine if the cat escapes from the ball.

Full Code:

Pokeball code:

Cat Code:

Gotcha sprite code:

Broke free sprite code:

Appear sprite code:

Posted on

Scratch Coding – Platformer

Our game today is a classic platformer, where the goal is to get to the door and collect stars along the way.

The sprites that we use for the game are:

The Cat will be moved by the arrow keys, although you can change this to be the classic ‘a-s-d-w’ combination (like Minecraft).

We use a single forever loop and check the key pressed block with an if statement for each of the cases: left arrow, right arrow, up (jumping).

To give us the jumping effect and the side jumping effect we need to amplify movement across the X (across) and Y (up / down) plane. If we just used change x by <> and change y by <> with we would only get slow movement.

Instead we create two variables called velocityX and velocityY. We increase velocityY when we want to jump and we slowly decrease velocityX on each run of the loop, this gives our cat a speedy effect.

Try changing the 0.9 value and see this for yourself.

We then use the calculated values for velocityX and velocityY and put these into the change x by <> and change y by <> blocks.

We use a number of costumes on the Level sprite which show the bricks that our cat can jump on. When a door is hit by our cat, we broadcast an event to the level sprite indicating that it can switch costumes to the next one.

For our cat to stand on our bricks we need to colour his shoes in, we then use this colour and test it against our brick color.

Try swapping the above block with the touching ‘ground’ block. You will find that our cat’s head will stick to the blocks if you touch it from above.

Completed Code:

Code for Cat:

Try experimenting with the ‘touching ground’ block, switch this out with the two colour conditions Or’d together.

Code for Level:

Code for the Star:

Cat Costume:

Notice the coloured in shoes.

Level Costumes:

Bricks:

Extra Activities:

  • Add in some enemies like a moving crab, if you hit him the score or lives should decrease.
  • Make special blocks that are hidden that give you extra lives.
  • Create a start splash screen like a real game.
  • Make the game scroll left to right when our cat hits the left hand edge or right hand edge (use the limits of the screen to detect this).
Posted on

Scratch Coding – Maze Challenge

The goal of this game is to move the sprite to the end of a maze, but beware there are obstacles all around.

Step 1: Setup

Start by adding a sprite, I’m going to make mine a bug:

I’ve duplicated my bug’s costume and added a red cross, this will be the sprite that I switch to when we hit the side of the maze.

We need a backdrop that will contain the walls or our maze:

Step 2: Sprite Movement

We want to have our sprite follow the direction of the mouse pointer, this way we can glide our character with our mouse.

We can use the following block to help us:

Since we always want to be moving towards the mouse pointer while the game is on, we will need to place this in a forever block.

Question: What happens if we have the following code?

To fix this, we need to make sure that the mouse pointer is at least 5 pixels away from the sprite. (we can use an if statement).

If we have hit the side of the maze, we need to show our crashed costume for 1 second and move the character back to the starting spot.

Step 3: Food

We need to add our food to the game:

The scripts for this food has to be always looking to see if the bug has touched it.

We will need a forever block and an if block that uses the sensing blocks.

If we touch the food, we can stop the background timer by calling the stop all block:

Step 4: Background

We should add a background timer to keep track of how long it takes to complete the maze.

We need to add a variable called ‘timer’.

In our forever loop we will change this value by 0.5, we will also add a wait block for the same amount of time. If we double click on the variable on the screen, we can make it smaller:

Extra:

Add new sprites that act to slow or speed up your sprite.

Since we can’t change the movement of the bug sprite from these new obstacles, we must broadcast a message to our bug sprite:

Then our sprite will listen for these events:

Code Solution:

Code for bug:

Code for the food:

Code for the backdrop:

Posted on

Scratch Coding – Life Raft Challenge

The goal of this game is to move the life raft to the main boat, but beware there are obstacles all around.

Step 1: Setup

Start by adding a new life raft sprite:

We need a backdrop that will contain the walls and rocks:

Step 2: Boat Movement

We want to have our boat follow the direction of the mouse pointer, this way we can glide our boat with our mouse.

We can use the following block to help us:

Since we always want to be moving towards the mouse pointer while the game is on, we will need to place this in a forever block.

Question: What happens if we have the following code?

To fix this, we need to make sure that the mouse pointer is at least 5 pixels away from the boat. (we can use an if statement).

In our main loop we can also test to see if the boat has hit the rocks. We can use the colour detection blocks to determine this:

If we have hit the rocks, we need to show our crashed costume for 1 second and move the boat back to the starting spot.

Step 3: Sailboat Rescue

We need to add our rescue boat to the game:

The scripts for this boat has to be always looking to see if the life raft has touched it.

We will need a forever block and an if block that uses the sensing blocks.

If we touch the boat, we can stop the background timer by calling the stop all block:

Step 4: Background

We should add a background timer to keep track of how long it takes to complete the maze.

We need to add a variable called ‘timer’.

In our forever loop we will change this value by 0.5, we will also add a wait block for the same amount of time.

If we double click on the variable on the screen, we can make it smaller:

Extras:

Add new sprites that act to slow or speed up your boat.

Since we can’t change the movement of the boat sprite from these new obstacles, we must broadcast a message to our boat sprite:

Then our boat will listen for these events:

Create sprites and broadcast messages for:

  • Pause (this wastes time)
  • Current Left (move the boat to the left)
  • Current Right (move the boat to the right)
  • Speed up (move the boat forward, with the move steps block set to a larger number).

More:

  • Use a list to store the high scores.
  • Use a pen to create a line behind the boat just like the wash from a boat motor.
Posted on

Scratch Coding – Race Car

Your goal is to create a race track that you can race your car around. Mine looks like this:

Notice how I have a nice red line for the end and my road is all one colour.

You will need to create your own road and car.

The very basic start to your game will use the following code:

We now need to add some code to check to see if the car has moved off the road.

Posted on

Scratch Coding – Flappy Bird

The starter project already has the art assets for flappy bird loaded.

Flappy bird is a game of skill; you must try to fly your bird through the moving pipes. We use the space bar to add some height to our bird’s flight, otherwise he’ll fall from the sky.

Our game will be setup so that the bird stays in the same X (across) position, we will move the upper and lower pipes across the screen.

The first task is to get our bird to always flap his wings.

We can do this by adding a forever block to our bird sprite using a forever block. With a small pause after each costume change it will look like our bird is flapping around:

The next step is to make our bird fly and fall from the sky. To do this we are going to need to create a variable that we can use to store the Y position of the bird.

Create a variable called ‘FallSpeed’:

The idea is that we will set this value to 0 every time the space bar is pressed, otherwise we will reduce it by a small amount and apply to the Y value of the bird’s position.

When we use the space bar, we want to set the falling speed back to zero and add some height to the bird:

Give that a test, find some nice values for moving the bird up on the space press and for the falling speed.

The next step is to move the pipes along the screen.

Coding the pipes:

We start the pipe on the right-hand side and move it to the left. Once we get very close to the left hand side, we move the pipe back into position.

Use this same logic for both pipe sprites.

We made our pipes move horizontally, but the game can also have us move our pipes vertically as well. We add some randomness to the Y position, so that each time the game resets the pipe’s position, we give it a different height.

What happens if we don’t reset the Y position on each reset?

We now need to add some logic to make game end if we hit a pipe or the ground.

On our bird sprite:

We broadcast a Game Over event which we can listen for on our Game Over sprite:

We also stop all the scripts from running once the game has ended.

Extras:

  • Add scoring, so that each time the pipes are reset you get a point
  • Make the game gradually get faster. (hint: you’ll need to use a pipe speed variable that can be changed on each reset).
  • Add another sprite that if the bird touches will make you hold your position for a few seconds.
Posted on

Scratch Coding – Fish Music

We are going to create a school of sing fish today.

When we click on the fish we are going to animate them and have them make some sounds.

If I pick one of my fish, I can use the following blocks:

I’ve used the ‘play note’ block to make a sound. The ‘next costume’ has also been added to make the fish come to life.

We can add a number of different fish with different notes and timings, then we can click through each fish and put together some cool tunes.

Instructions for the player

It might not be clear to users of our program that they need to click on each fish to make them sing, so we are going to add a message to the screen. To do this we are going to create a new sprite and add some text to it with the text tool:

We only want to show this to the player for the first three seconds after the green flag is pressed:

Be sure to use some loops on some fish:

Explore the sound library

Scratch has a number of cool sounds that can be added to our fish, check out some of these:

Now we can use both the basic notes and the more complex music that we added to our sprites.

Posted on

Scratch Coding – Beginner Extra Activities

Make Scratch the Cat walk on the spot:

Notice how Scratch the Cat has two costumes, if we swap them very fast it will look like he is walking.

The script for changing costumes is as follows:

Can you speed up his walking?

Changing Scratch the Cat’s Colour

The script for changing a sprite’s colour is as follows:

There are 200 colours so by changing the colour effect by 25 8 times the character finishes at the colour that it started as. Try changing the 25 to 1 and the 8 to 200 to see all 200 colours.

Fisheye with Scratch the Cat

The fisheye effect makes the character look like it would through a wide angle lens.

The code for the fisheye effect is as follows:

The ‘set fisheye effect to 0’ at the end is used to reset the character back to normal.

Pixel Art Scratch the Cat

We can use the pixelate effect to change the appearance of Scratch the Cat.

The code for the pixelate effect is as follows:

Can you combine the fisheye, colour change and pixelate commands into one program?