About the Author
Les is a maker and trainer who has worked with the Raspberry Pi Foundation and the BBC to deliver computing training.
Head or tails? A game where you have a 1 in 2 chance of winning, has been used to decide many things such as who kicks off at a football/soccer match and who gets the last sweet in the bag. In this project, we shall create our own digital game of chance using a micro:bit and a few blocks of code to decide… Skulls or Snakes?!
For this project, we shall be using the Javascript Blocks Editor https://microbit.org/code/ and we should already be comfortable copying code to our micro:bit.
From the Input menu drag the “on shake” block into the coding area.
Any code inside the block will be run when the micro:bit is shaken.
In the Variables menu click on “Make a Variable” and call it “choice”.
We shall use this variable to store a random number later.
From the Variables menu drag the “set item to 0” block to the coding area. Make sure the variable is called choice and not item. If it is, click on the dropdown to change it.
From the Math menu, drag “pick random 0 to 4” change it to “0 to 1” then connect it to the “set choice to” block. Lastly, place these blocks inside the “On shake” loop.
Go to the Logic menu and drag “if true..then..else” and place it inside the loop, but under our code.
This block will run code if a condition is met.
Our first block is from Logic and it is “0 = 0” drag this and connect it to the “if”.
Next, from Variables drag “choice” and place it inside the first “0”.
All of the following blocks are from Basic, first, we use the “show icon” block to display a skull. Then we pause the image for 1 second before clearing the screen.
Here we repeat step 7, but have a snake appear on the screen. Remember the clear screen block can be found under Basic … More.
Our skull block of code is placed inside the “then” part of the logic, and the snake is placed inside “else”.
All of the code for this project should now look like the image above.
That’s it, now download the code to your micro:bit and when it has finished, give the micro:bit a shake to see either a skull or a snake appear on the LED grid!
Well done you made your own decision-making micro:bit!
You can view a demo of the game on YouTube.