Original number game – https://www.microbit.co.uk/nrjdzu
Improved game – https://www.microbit.co.uk/joryiq
One of the first games I created with the micro:bit was a number guessing game. I wanted to use the built-in compass so that pupils didn’t need to purchase or utilise any additional equipment other than what would be given to them.
Randomly generate a number using button A. Guess the number by pointing in a direction and pressing button B. The number will be between 0 and 359.
I originally started using the block editor to make the game but I wanted to see the code. I realise that my pupils will not all want to see the code…they may be scared of the code. I started by declaring the variables (item & guesses) so that the game can be played. Pressing A will display “SAFE!” then randomly generate a number and store it in item…
All variables need declaring before they are used, which is why I gave item a value of 0 to begin with. Guesses is used below and I set it to 1 just in case the number was guessed straight away.
Every time B is pressed, if the current compass heading is within 3 (either side) of item, the number has been guessed correctly, or else tell the player how far away they are (in degrees) and increase guesses by 1.
Problems – Draw a circle
At this moment in time, I don’t know why but each time I played the game, the first time I set the number, it wanted me to draw a circle. This appears to be a built in demo game – I don’t know if it starts when the code is bad or if there is a bug with me device! I hope (and expect) it is the former because the code is a bit rough and ready and I just wanted to get it to work.