Posted on

Scratch Coding – Star Catch

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.

Sprites

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.

Extra

Add variables to keep track of how many stars you catch.

Posted on

Scratch Coding – Snake

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.

Sprites

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.

Growing Up

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:

  • XPositions
  • YPositions

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:

Harder Levels

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:

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 – 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 – 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 – 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:

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).