We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.
The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ...
For more information on how Google's third-party cookies operate and handle your data, see: Google Privacy Policy
Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.
Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
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.