Make A Jumping Cat Game With Microbit And Scratch
Scratch is set up really well to integrate with a range of hardware, such as webcams, microphones, and Lego products such as Mindstorms. It also integrates with microbit, which is what Liam has been trying out recently. Microbit contains accelerometers, so can act as a motion sensor. We used this feature to create a game with microbit and Scratch.
This game was the first time Liam integrated microbit with Scratch, so it’s a very simple game, great for getting started.
We found this game on the microbit website, where they have a range of projects designed for different ages, skills and experience levels.
Getting started
The game involves making a cat on the screen jump when you throw a soft toy into the air. For an 8-year-old, it was instantly appealing. Straight away, Liam was off to find a suitable soft toy, and returned from his room with this epic little monster. He’s well suited to the task, because the microbit gets tucked inside him and will be well-protected when he falls on the floor.
The monster has an internal pocket, which closes underneath with a velcro strip. It previously had a speaker embedded, which we removed to make space for the microbit. The pocket was not quite deep enough, so we did have to open the end of the pocket. So that the stuffing didn’t damage the microbit, we put it inside a small plastic bag before putting it inside the monster.
Next, we needed the microbit to pair with Scratch, and get the microbit extension for the right code blocks. We’ve written more about how to do both of these here.
Writing the code for the game
The microbit website shows all the code needed, but we try not to use it as a copy & paste exercise. We just skim the example at the beginning to get a feel for what’s needed. Liam enjoys working it out and problem solving himself, and he learns so much more in the process.
The key things Liam wanted the cat on the screen to do were:
- Jump
- Meow
- Change colour
Using the input from the microbit
The goal for this game is to make the cat jump when the monster is tossed into the air. So, we don’t want the actions to occur when the green flag is clicked. The microbit code menu has alternatives that use the various input options of the microbit to trigger the code sequence.
In this case, Liam used ‘when jumped’ as the start of the sequence, but changed the drop-down option to ‘when shaken’. There are three options in total, and any of them will work, but it affects the sensitivity. For example, when we tried using ‘when moved’, the cat on the screen meowed if the monster even twitched the tiniest bit.
Make the cat jump
There are a few different ways to do this. Liam used the ‘glide’ command, with two commands back-to-back. The first tells the cat to glide upwards for half a second to fixed coordinates, and the second line sends the cat back down. Note that the x-coordinate stays the same at -22, which means the cat doesn’t move sideways at all. The y-coordinate, which controls the vertical movement, is the only one that changes.
Liam also tested out different durations for the glide effect. The cat looks pretty funny when the duration is reduced to 0.2 seconds – he goes shooting across the screen at high speed!
The second method is to introduce a variable called ‘jump’, and this method took a bit more work to get right. We’ve written about how to use this method in another post dedicated to variables.
Add sounds to the game
Sound effects make everything cooler, and Scratch has a great selection of sounds ready to go. For the cat, a ‘meow’ sound was the obvious choice. Just like the glide action, it’s set to activate when the microbit is shaken.
Change the colour of the cat every time he jumps
Changing the colour of the cat was good fun, and can be changed with a couple of different methods. Like the volcanic eruption project, multiple different costumes could be created for the cat, and an instruction added to the code to change to the next costume each time. In this case though, Liam tried something new.
He used a block called ‘change colour effect by…’ which directly controls the sprite. The amount of change can be controlled with the number – enter a smaller number for a more subtle change. Liam likes 25 because it gives a bright and obvious change each time.
Putting it all together
Putting it together and testing the game with microbit threw up some curveballs that Liam wasn’t expecting. For a start, the order of the blocks is important. At first, the glide blocks were placed first in the sequence. It worked, but the cat had to finish the jump and land before he changed colour or meowed. Liam really wanted the cat to meow at the start of the jump, so there was some rearranging needed. Ultimately, he got it working the way he wanted. This is what the code looked like:
Modifying the game to add a rainbow trail to the cat
Check out the video at the top of this post to see the modification Liam made to the jumping cat game once he’d got it working. You’ll see in the second half of the video, the cat has a rainbow trail that follows him.
This modification involved a couple more new blocks for Liam to get the hang of – variables and clones. Rather than making this post really long, we’ve put together a post dedicated to the modification.