In part 1, you learned the absolute basics of the Microsoft block editor. Now you are going to learn more by creating a number guessing game.

1. Open the block editor like we showed you in part 1.

2. First you’ll need a forever block and a show string block from the basic tab. Drag the show string into the forever block and change it from”Hello!” to “Select a number”.

4. ‘While’ loops repeat a piece of code until a certain condition is true. Drag a while block from the loops tab into the forever block. Next to the ‘while’ text drag an and block from the logic tab, then into each space of the and block drag a 0 = 0 block from the logic tab. For the first 0 = 0 replace the first zero with a button A is pressed block from the input tab, and the second zero with a false from the logic tab. For the second 0 = 0 replace the first zero with a button B is pressed block and the second zero with another false. It should look like this:

Capture

3. ‘If’ statements determine if a certain condition is true, and change what the program does accordingly. Drag 2 if blocks from the logic tab into the while loop.

4.Now, next to the ‘if’ of each if block, drag the following: next to the first ‘if place button A is pressed and next to the second ‘if’ drag button B is pressed. Now your code should look like this:

Capture

 

5. A variable is a changeable piece of data (for example a number) that is stored by the program. From the variables tab drag a set item to block into each if block. To the first set item to attach a zero from the maths tab. To the second do the same thing, but change the number from 0 to 1.

6. Into each if block you should drag a show number block, and change the number to the same number as the set item to block.  Now your code should look like this:

Capture.PNG

 

7. Now you need to add a pause (ms) block from the basic tab underneath the while loop and change the number to 1000. This will pause the program for 1 second.

8. Then add another if statement underneath and click on the small blue gear. Then drag an else block into the if block, and click on the blue gear again.

9. Next to the ‘if’ place a 0 = 0 block from the logic tab. Replace the first 0 with an pick random 0 to 4 block from the variables tab and the second with a item block from the maths tab, then change the 4 to a 1.

10. Next to ‘do’ in the if block place a show string block and change the message to “Correct!”, and next to ‘else’ place another show string with the message set to “Wrong”.

11. Place another pause (ms) block and set the number to 5000. Now your code should look like this:

Capture

 

The game asks the player to select a number, and the player then selects 0 or 1 using the A or B buttons. A variable is set to the player’s selection, and if the variable and a randomly selected number are the same, the player wins.

12. Download your code to your micro:bit.

Advertisements