© Pradeeka Seneviratne  2019
Pradeeka SeneviratneBBC micro:bit Recipeshttps://doi.org/10.1007/978-1-4842-4913-0_15

15. Building Simple Games

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 

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 ).
    ../images/474604_1_En_15_Chapter/474604_1_En_15_Fig1_HTML.jpg
    Figure 15-1

    Full code listing

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!

The LED screen consists of 5 columns and 5 rows, for a total of 25 LEDs. The columns belong to the x-axis and the rows belong to the y-axis, like a Cartesian chart. The address of the LED in the top-left corner can be written as (x0,y0). The address of the LED in the top-right corner can be written as (x4,y0). Figure 15-2 shows the column and row numbers associated with the LED grid. You can read the column numbers (0 to 5) along the x-axis and row numbers (0 to 5) along the y-axis.
../images/474604_1_En_15_Chapter/474604_1_En_15_Fig2_HTML.jpg
Figure 15-2

Built-in LED display consists of columns and rows

The create sprite block accepts the x and y positions of the sprite that you want to create:
  • x: between 0 and 4

  • y: between 0 and 4

When you run the above code, the LED at (x2,y2) will turn on (Figure 15-3 ).
../images/474604_1_En_15_Chapter/474604_1_En_15_Fig3_HTML.jpg
Figure 15-3

Creating a sprite at x2,y2

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 ).
    ../images/474604_1_En_15_Chapter/474604_1_En_15_Fig4_HTML.jpg
    Figure 15-4

    Full code listing

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.

When you run the above code, you can move the sprite to the left and right by pressing the buttons A and B. Figure 15-5 shows the left and right boundaries.
../images/474604_1_En_15_Chapter/474604_1_En_15_Fig5_HTML.jpg
Figure 15-5

Left and right boundaries for the sprite. The sprite can only move on row 2.

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 ).
    ../images/474604_1_En_15_Chapter/474604_1_En_15_Fig6_HTML.jpg
    Figure 15-6

    Full code listing

How It Works

The turn block allows your sprite to turn left or right by a number of degrees. Figure 15-7 and Figure 15-8 show the path of the sprite, each time when you press the button A.
../images/474604_1_En_15_Chapter/474604_1_En_15_Fig7_HTML.jpg
Figure 15-7

Moving to the right by turning 45 degrees

../images/474604_1_En_15_Chapter/474604_1_En_15_Fig8_HTML.jpg
Figure 15-8

Path from start to end and continues the same path

15-4. Deleting a Sprite

Problem

You want to delete a sprite.

Solution

This solution assumes that you already have a variable named sprite and it holds a sprite (initially 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. Then click on the delete block over, and place it inside the on button A pressed block (Figure 15-9 ).
    ../images/474604_1_En_15_Chapter/474604_1_En_15_Fig9_HTML.jpg
    Figure 15-9

    Full code listing

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 ).
    ../images/474604_1_En_15_Chapter/474604_1_En_15_Fig10_HTML.jpg
    Figure 15-10

    Full code listing

How It Works

The score of your game can be initialized, updated, and accessed from the following blocks.
  • 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 ).
    ../images/474604_1_En_15_Chapter/474604_1_En_15_Fig11_HTML.jpg
    Figure 15-11

    Full code listing

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 ).
    ../images/474604_1_En_15_Chapter/474604_1_En_15_Fig12_HTML.jpg
    Figure 15-12

    Full code listing

How It Works

The touching block can be used to detect touching (hitting) of two sprites. The game over block will finish the game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.232.11