Skulls or Snakes? – A game of chance

Skulls or Snakes? – A game of chance

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

Step 2 – Create a variable

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

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

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

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!

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!

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

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

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!