This chapter provides some basic techniques that you can use to develop simple games with the micro:bit LED display and two built-in buttons.
15-1. Creating a Sprite
Problem
You want to create a sprite at (x2,y2) on the micro:bit LED screen.
Solution
In the Toolbox, click on the Variables category. Next, click on the Make a Variable… button. In the New variable name box, type sprite for the variable name. Then click the Ok button.
In the Toolbox, click on the Variables category. Then click and drag the set sprite to block over, and place it inside the on start block.
In the Toolbox, click on the Game category. Then click and drag the create sprite block over, and place it inside the placeholder of the set sprite to block to replace the 0.
In the create sprite at block, type the value 2 for x and type the value 2 for y.
- Once completed, your code should look like the following (Figure 15-1 ).
How It Works
When you build games with micro:bit, the LEDs on the front side of the board will act as the graphical user interface just like the LCD or CRT screen of a video game console. Sprites are the building blocks of a game. You can create sprites, tell them to move and turn, detect whether a sprite has bumped into another sprite, and many more things. Cool!
x: between 0 and 4
y: between 0 and 4
Any number less than 0 or greater than 4 is considered as 0 and 4, respectively. As an example, −1 is considered as 0 and 5 is considered as 4.
15-2. Moving a Sprite Straightly
Problem
You want to move the sprite created in Recipe 15-1 to the left by 1 LED each time when you press the button A and to the right by 1 LED each time when you press the button B.
Solution
In the Toolbox, click on the Input category and then click on the on button A pressed event block.
In the Toolbox, click on the Games category. Next, click and drag the move by block over, and place it inside the on button A pressed block. Then type the value −1 in the value box.
Duplicate the on button A pressed block. Next, choose B from the drop-down menu. Then in the move by block, type the value 1 in the value box.
- Once completed, your code should look like this (Figure 15-4 ).
How It Works
With the move by block, you can tell a sprite to move straight on a row from left to right or right to left. In the above example, when you press the button A, the sprite moves to left by 1 LED. When you press the button B, the sprite moves to the right by 1 LED. A negative value tells how many LEDs the sprite should move to the left, and a positive value tells how many LEDs the sprite should move to the right. You can move a sprite straightly to the left, until it reaches to the first column. Similarly, you can move a sprite straightly to the right, until it reaches to the last column.
15-3. Moving a Sprite by Turning
Problem
You want to create a sprite in the middle of the screen. Then move the sprite by turning 45 degrees to the right each time by 1 LED.
Solution
In the Toolbox, click on the Variables category. Next, click on the Make a Variable… button. In the New variable name box, type sprite for the variable name. Then click the Ok button.
In the Toolbox, click on the Variables category. Then click and drag the set sprite to block over, and place it inside the on start block.
In the Toolbox, click on the Game category. Then click and drag the create sprite block over, and place it inside the placeholder of the set sprite to block to replace the 0.
In the create sprite at block, type the value 2 for x and type the value 2 for y.
In the Toolbox, click on the Input category and then click on the on button A pressed event block.
In the Toolbox, click on the Game category. Then click and drag the turn right by 45 block over, and place it inside the on button A pressed block.
In the Toolbox, click on the Game category. Then click and drag the move by block over, and place it underneath the turn right by 45 block.
- Once completed, your code should look like this (Figure 15-6 ).
How It Works
15-4. Deleting a Sprite
Problem
You want to delete a sprite.
Solution
In the Toolbox, click on the Input category and then click on the on button A pressed event block.
- In the Toolbox, click on the Game category. Then click on the delete block over, and place it inside the on button A pressed block (Figure 15-9 ).
How It Works
The delete block allows you to delete a sprite from the game. If you have more than one sprite in your game, choose the correct variable for the sprite from the drop-down list.
In the above example, when you press the button A, the sprite at x2,y2 will be deleted from the screen.
15-5. Holding and Displaying Score
Problem
You want to increment the score by pressing the button A and displaying the current score by pressing the button B.
Solution
In the Toolbox, click on the Game category. Then click and drag the set score block over, and place it inside the on start block.
In the Toolbox, click on the Input category and then click on the on button A pressed event block.
Repeat the above step to add an on button B pressed event block.
In the Toolbox, click on the Game category. Then click and drag the change score by block over, and place it inside the on button A pressed block.
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on button B pressed block.
In the Toolbox , click on the Game category. Then click and drag the score block over, and place it inside the placeholder of the show number block.
- Once you have completed these steps, your code should look like this (Figure 15-10 ).
How It Works
set score : sets the score of the game by assigning an initial value.
change score: update the score by a given value.
score: holds the current score.
15-6. Life
Problem
You want to add and remove life from your game.
Solution
In the Toolbox, click on the Game category. Next, click and drag the set life block over, and place it inside the on start block. Then type the value 100 in the value box.
In the Toolbox, click on the Input category and then click on the on button A pressed event block.
Repeat the above step to add an event block, on button B pressed.
In the Toolbox, click on the Game category. Next, click and drag the add life block over, and place it inside the on button A pressed block. Then type the value 50 in the value box.
In the Toolbox, click on the Game category. Next, click and drag the remove life block over, and place it inside the on button B pressed block. Then type the value 50 in the value box.
- Once completed, your code should look like this (Figure 15-11 ).
How It Works
The set life block allows you to add life to your game. In the above solution under Recipe 15-6, initially the life is set to 100. The add life block is used to add a number of play-turns that a player character has, to the life variable. The remove life block is used to remove a number of play-turns from the life variable. When the life reaches 0, the game will finish and display ‘GAME OVER’ on the LED screen.
15-7. Hitting with Another Sprite
Problem
Your game has two sprites. One sprite is defined as the enemy, and the other sprite is defined as the hero. You can move the hero by pressing the button A. If the hero hits with the enemy, the game should be over.
Solution
In the Toolbox, click on the Variables category. Next, click on the Make a Variable… button. In the New variable name box, type hero for the variable name. Then click the Ok button.
In the Toolbox, click on the Variables category. Then click and drag the set hero to block over, and place it inside the on start block.
In the Toolbox, click on the Game category. Then click and drag the create sprite block over, and place it inside the placeholder of the set hero to block to replace the 0.
In the create sprite at block, type the value 0 for x and type the value 2 for y.
Repeat the above steps to create another variable named enemy and create a sprite at x2,y2.
In the Toolbox, click on the Input category and then click on the on button A pressed event block.
In the Toolbox, click on the Game category. Next, click and drag the move by block over, and place it inside the on button A pressed block. Then choose the variable hero from the drop-down menu.
In the Toolbox, click on the Logic category. Next, click and drag the if-then block over, and place it underneath the move by block.
In the Toolbox, click on the Game category. Next, click and drag the touching block over, and place it inside the placeholder of the if-then block. Then choose the first operand as the hero and the second operand as the enemy.
In the Toolbox, click on the Game category. Then click and drag the game over block over, and place it inside the then branch of the if-then block.
- Once completed, your code should look like this (Figure 15-12 ).
How It Works
The touching block can be used to detect touching (hitting) of two sprites. The game over block will finish the game.