Micro:bit Guessing Game - School Assessment

by 0102316 in Circuits > Gadgets

639 Views, 1 Favorites, 0 Comments

Micro:bit Guessing Game - School Assessment

overview.png

Micro:bits are small computors that come with a 5 by 5 grid of lights, buttons, a radio and even a accelerometer. Because of this Micro:bits can be used to create a verity of projects from simple games to quite complex designs using all of the sensors.

For this instructable I will be showing you how to create a simple game using just two Micro:bits and their inbuilt radios. the game consists of just two micro:bits with both micro:bits trying to guess a hidden number on the other micro:bit. The game can be played with each player taking turns or an all who ever gets the victory message fist.

If you do not have access to Micro:bits then you the following activities can be completed on makecode.microbit.org, which although a somewhat limiting website will be able to create this simple guessing game.

This project consists of just four parts, the startup of the Micro:bits, guessing a number, receiving a guess and sending feedback and receiving the feedback on your guess.

this instructable was done using block code in Makecode.

Supplies

A computer with an internet connection
Two Micro:bits (optional)

Creating the Start Up of the Mirco:Bits

startup.png

This part of the of the project is the eaist as we only need to do three things, create a varible of the players guess, create a random varible the other player is trying to guess and allow the two Micro:bits to talk to each other.

the first thing we do is create two variables "Guess", the players current guess and "Value" the number the other player is trying to guess to win.

once that is done we can set a random number for "Value" using the set Set and Pick random blocks. the range of 0 to 9 us to avoid scroll text as if the range where 1 to 10 whenever we display a 10 it would have to display both a 1 and 0 which can cause a delay between what the player is doing and what the screen is presenting.

all we need to do for "Guess" is just set it to 0.

Then for the Micro:bits to communicate we need to use the "radio set group" block this accepts a value between 0 and 255, I have used 1 in this example but you can use whatever value you like as long as it does not match any other micro:bits that might be communicating nearby.

Choosing a Guess

making guess.png

The first thing we need to do is add three on button pressed blocks and set one to button A one to button B and one to both A and B.

Pressing the A button will lower the guess by one, so under that code block add and "If" block to check is "Guess" is below 0 as we dont want to go outside our range of 0-9. Then in the "if" statement add the "change by" block to descress guess by one. Finally add the "show number" block so the player can see their current guess.

Add the same blocks of code under the "on button B pressed" but check if the number is above one and instead of taking one away add one instead.

With this you can increase the guess by one if the B button is pressed. Finally if A and B are pressed send the "Guess" variable over radio to the other Micro:bit using the "radio send number" block.

Receiving a Guess

step 3.png

Now that we can change and send the guess we need code that can appropriately react to the guesses. the guess could ether be above, below or the same as the value the player is trying to guess, because of this we can use an "if" block to account for all of these.

The first thing we need to add is an "on radio received" block and an if statement with three checks inside of that.

the first if will check if the value received was a correct guess using the "equals logic block" which compare the "receivedNumber" and "value". If this is true it will send the message "win" back to the Micro:bit that made the guess to say that it won.

It will go through this process again checking if the reviced number is the lower or higher and send a message back to the Micro:bit conveying this information. if the guess is too low the message "up" will be sent if it is too high "down" will be sent back to the guessing Micro:bit.

Receiving Feedback on the Guess

step 4.png

finally the Micro:bit will revie the information about its guess and display the correct information based on the message received.

If the guess was too high or too low and arrow will be displayed pointing if the guess needs to go up or down. this will be determined based on the message received from the other Micro:bit.

However If the guess was correct and the Micro:bit receives the "win” message, then it will display the message "you win!" followed by a smiling face.

This is all done using the same if statement setup in the last step but instead it is inside a "on radio received string" block.

After someone has won just restart the micro bits to play again.