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