About this project
This project guides pupils through the creation of a simple game played out on the BBC micro:bit's LED array. Players can earn points by using the A and B buttons to avoid falling hazards, similar to classic arcade games like Space Invaders.
What you'll need
1 x BBC micro:bit
1 x Micro USB
1 x Computer or tablet
2 x AAA Batteries & Battery Holder
Project walkthrough
- Go to the BBC micro:bit website, click 'create code' and begin a new Microsoft Block Editor project.
- First we need to program the basic metrics of the game, including your playable character, the score and what happens when the game ends.
- Using the command tiles from the Basic, Variables, Logic and Game menus, create the following block of code.
- This tell the device to create a playable 'sprite' at the X2, Y4 co-ordinate on the LED array. It also sets up the base score of zero and tells it to reset that score when the game over function is triggered.
- The next step is to set up the controls for your character. Using Input, Logic and Game commands, create the following block of code, separate from the first.
- This block of code tells your device that when the A button is pressed, to move the sprite by one position to the left, unless they have reached the edge of the screen, in which case nothing will happen.
- Now you need to tell the device to move your sprite one position to the right whenever the B button is pressed. To save time, simply duplicate your Button A coding block and make the following changes in the appropriate dialog boxes.
- Next, it's time to create some hazards! Create another block of code containing the following commands. You'll also need to create a new variable to reference the hazards where indicated. For the purposes of this demo, I've called it Hazard0 - the 0 references the column this hazard will fall from.
- This block of code essentially tells the device to send a sprite down column X0 on the LED array at random intervals every 0-5 seconds. If the sprite makes contact with your sprite, the game will end. If the sprite reaches the bottom of the column and you successfully avoid it, it will disappear and your score will be increased by one point.
- Now you need to create the same block of code to create sprites for columns X1 to X4. Fortunately, rather than building all of these from scratch, it's much easier to simply duplicate the block of code above, create a new variable for each (Hazard1, Hazard2, etc.) and alter the dialog boxes where required to ensure they each fall down a different column.
- You should now have eight distinct blocks of code in your coding editor - one for the basic metrics, two for the A and B button commands, and five controlling the falling hazards.
- Congratulations, you've just coded your first game! Hit 'Run' to test the game out on the emulator, before compiling your script and transferring it to your BBC micro:bit device.
Once your pupils have got to grips with this project, challenge them to play around with the new commands they've learned to alter the game. Can they figure out how to speed up the falling sprites, alter the scoring system or change the game so that players score points for catching the falling sprites rather than avoiding them? Let us know how they got on in the comments section below...
Top Comments