Customize Consent Preferences

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

Always Active

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.

No cookies to display.

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.

No cookies to display.

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.

No cookies to display.

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

No cookies to display.

Les PounderSkulls or Snakes - A Game of Chance

About the Author

Les is a maker and trainer who has worked with the Raspberry Pi Foundation and the BBC to deliver computing training.

@biglesp bigl.es

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.

Step 1 – Working with input

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.

Working with input

Step 2 – Create a variable

Creating a variable called choice

In the Variables menu click on “Make a Variable” and call it “choice”.

We shall use this variable to store a random number later.

Step 3 – Using the variable

Using the choice Variable that we created

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.

Step 4 – Creating a random number generator

Creating a Random Number Generator

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.

Step 5 – Logical Comparisons

Logical Comparisons - Using an If else block

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.

Step 6 – Decisions…

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”.

Step 7 – Skulls!

Displaying a Skull

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.

Step 8 – Snakes!

Displaying a Snake

Here we repeat step 7, but have a snake appear on the screen. Remember the clear screen block can be found under Basic … More.

Step 9 – Putting it together

Putting all the code together - attaching it all within the if else block

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!

Skulls Or Snakes Demo:

You can view a demo of the game on YouTube.