Project of the Week – How To Code Your Own Game
Liam has been inspired recently to start creating his own games. To get started, we thought it would be good to try something fairly simple, and work upwards from there. The games where objects fall from the top of the screen and have to be caught before they fall off the bottom seemed pretty straightforward. As it turned out, this game is a bit more complex than you would expect. But don’t worry, we’ll walk you through each component and you won’t get left behind. In no time at all, you’ll be able to code your own game in Scratch!
About the Catching Strawberries game
When Liam is keen to try a new type of project, we typically head to YouTube or Pinterest as a starting point. We found this project on YouTube by Maria Gettel a few weeks back. It’s a step-by-step tutorial for a Year 9 class (13 year olds), put together by their school teacher. It’s very well presented, runs at a good pace, and Liam found it very easy to follow along. Maria has a few tutorials uploaded that cover different elements of how to code your game, so you can easily focus in on the parts you need more detail on.
The game contains four levels, and at each level a fixed number of strawberries or other foods fall from the top of the screen at random positions and intervals. Move your bowl left and right at the bottom of the screen to catch as many as you can. A scoreboard keeps track of how well you did, so you can compete with your friends. At each level, the game gets a bit harder, so you’ve got to be quick to keep up!
The Basic Elements
There are two key elements to this project. The backgrounds, and the sprites.
The backgrounds
For this game, the background needs to be very neutral so that it isn’t a distraction. Scratch has got a lot of pre-made backgrounds that would be great options. For each level of gameplay, Liam used a plain solid colour. That’s four different backgrounds as a starting point, and they’re named as level 1, level 2 etc.
For the ‘Level Complete’ screen, Liam chose a pre-made background called Rays, and added the text overlay. The final screen, ‘Game Complete’ is also pre-made, called Party. Using pre-made backgrounds is often a great option, because they look professional and you can move into the fun part of coding your own game instantly.
The sprites
For this game, Liam chose sprites from the Scratch library. Each level of the game has a different food, so he chose a strawberry, jar, doughnut and watermelon. The fifth sprite is the bowl to catch the food in. The majority of the game features are controlled by the bowl. That’s because it’s consistent through all levels.
How to code the bowl
Set the scene
The first thing the bowl does is set the scene for the game. The score variable is set to zero, so the game starts fresh every time. The size of the bowl is changed to 90%, to suit the size of the strawberries. The backdrop is set to the Level 1 backdrop, and the bowl is placed in the centre of the screen at the bottom. All of this happens before the bowl appears on-screen.
Code the movement of the bowl
Once you ‘show’ the bowl, add a forever loop. This is where you build the game controllers. Liam used the left and right arrow keys on the laptop, so that the game could be played anywhere, but this could be customised. You could use the Lego Wedo 2.0 tilt sensor like in Liam’s racing car game. Or if you have a microbit, you can make a remote control.
The bowl moves along the x-axis (horizontally). In Liam’s version, the bowl moves by 15 pixels with every arrow press. Test it out in your game to see what works best for you. A smaller number will make the bowl move slower, while a bigger number will speed it up.
How to move up the levels
To move up the levels, the bowl needs more instructions. Four pieces of code are used, and they activate when they receive a broadcast message. The Broadcast Message block was something new for this project, so there’s more about it below.
When the bowl receives the message, it activates the sequence to move up to the next level. In the sequence below you can see:
- The bowl hides
- The ‘Level Complete’ backdrop appears (called Rays)
- Level Complete stays on the screen for 5 seconds
- The backdrop changes to the colour for Level 2
- Coordinates for the bowl place it back in the centre of the screen at the bottom
- The bowl reappears, ready to play Level 2
The code for the end of Level 2 is exactly the same as Level 1, it just changes to the backdrop colour for Level 3. At the end of Level 3, there is only one extra line in the code. After the 5-second pause, the bowl size reduces. It’s completely optional, and makes the final level a bit harder.
The game finishes at the end of level 4. That’s what this final piece of code is. The backdrop changes to Party instead of Rays, which is the ‘Game Complete’ screen. The 5-second pause is the same as the other levels, and then the game finishes with a ‘stop all’ command.
How to code the strawberries for your game
The strawberries are the first fruit to appear, so this is where most of our explanation is. To code your own strawberries, you’ll have two sections of code. The first part generates clones, and the second part tells the clones what to do.
Generating strawberry clones
The strawberries that fall down the screen aren’t sprites – they’re clones of a single sprite. A strawberry placed at the top of the screen is hidden at the start of the code sequence. It doesn’t do anything except hold the code. The repeat loop dictates the number of clones you want to create. For the first level, Liam chose 20.
For a good effect, you want the clones to generate at random times, and not evenly spaced. That’s what the green ‘pick random’ does. Then, the clone will generate. When all 20 clones have appeared, the ‘wait 5 seconds’ instruction gives them time to reach the bottom of the screen before finishing the level.
Modifying your strawberry clones
For the second part, the clone appearance is controlled, followed by the behaviour.
The purple lines control the appearance of each clone, using random ranges again. Using these in combination means you’ll see a variety of colours, sizes and transparency levels, which makes the game interesting. Once the appearance is set, the blue line sets up the clone to appear somewhere random across the top of the screen. Then your clone can appear.
The orange section of the code controls the movement of the strawberries down the screen. In this loop, the instructions keep repeating until the clone reaches y-coordinate -170 (the bottom of the screen). The clone moves down by 3 pixels, and then checks the criteria in the if statement. If the strawberry touches the bowl, it will run the instructions within the statement. If not, it will continue down for another 3 pixels and try again.
When the strawberry touches the bowl, the if statement is triggered. The score will increase by 1, and the clone is deleted. If the clone reaches the bottom without touching the bowl, it will also be deleted, but your score won’t increase.
Code the food for levels 2, 3 & 4
Make the code for the honey jar, doughnuts and watermelons in the same way as the strawberries. The easiest way to do this is to create your sprite, and then copy and paste the strawberry code across to the new sprite. Then edit the new code to suit the level:
- Change the starting event for the honey jar to be when the ‘level 1 complete’ broadcast message is received. That will keep the jars hidden until the start of level 2. Then do the same for the doughnuts and watermelons, so that they start at the end of level 2 and level 3.
- Next, change how many clones you want for each level. If they are moving faster, you might want more of them.
- To make the clones appear closer together, reduce the range of the wait times that are written in the green ‘pick random’ block.
- And to trigger the end of each level, edit the broadcast message to signal which level is complete.
The honey jars, doughnuts and watermelons will move down the screen in the same way as the strawberries, so this is great for copy & paste too. To edit the speed of the clones, change the blue line inside the repeat loop.
The Broadcast Command
You can find the Broadcast blocks in the Events code menu, down the bottom. There is a starting block, which acts in the same way as when the green flag is clicked. Then there are a couple of standard blocks to embed in your code – one with a wait command, and one without.
To set up a broadcast message, here’s some easy steps:
- Pull a standard broadcast block into your code
- Click the oval, and select ‘new message’
- Type in some logical text for your message, like ‘level 1 complete’ and click ok
- Pull the starting block into the coding worksheet to start a new sequence
- Click the oval on the starting block, and select your message from the list
- Add the code for the sequence you want to trigger beneath the starting block
Add a new message Type your own text Start new sequence when your message is received
Give this project a try!
Are you ready to code your own game? Give it a go, and let us know how you got on. If there’s anything you aren’t sure of, get in touch so we can help you out.
Happy coding!