Embedded programming (Micro:bit) Rock,Paper & Scissors (Week 6)

How to code Rock,Paper & Scissors onto your Micro:bit

Step 1: Open Micro:bit on your browser and start coding on “Make code editor” which should look like the image below.

545454

 

Step 2: Firstly add an “On shake” block which will run code when the micro:bit is shook. This block is located in the “input” section.

efs

Step 3: Add a new variable and call it “hand”. Next select the block “set hand to” and place it inside the pervious block.

dfs

5yy

Step 4: Next you want to go into “math” and select the block called “pick random x-y” which will be placed into the “set hand to” block. Next you want to change the 0-10 numbers to 1 and 3. This tells the micro:bit that there are three choices to choose from.

dfg

vfd

Step 5: Next you want to select an “if” block from “logic” and place it underneath the “set hand to” block. Next you are going to want to select a “0 = 0” block and place it in the “if” block over the “true” section. Next select the variable “hand” and place it in the first “0”. The second “0” change to 1. Then select a basic block called “show leds” and place it into the “if” block as shown below. Finally draw a pattern of LEDs which create a square shape which will resemble “paper”.

t34t

f34r3

67j5

fwrf

rg

Tip:You can test the code by clicking on the button labelled “shake”.

sv

Step 6: Click on the “plus” sign and add two “else” options.

Step 7: Just like step 5 you want to add another “0=0” block and fill it with the variable “hand” on the left and in the second box type the number “2” this time.

ef

Step 8: Add another “show led” block below and form the shape of a rock as shown below.

rew

Step 9: Finally add the last “show led” block and create the shape of scissors.

edw

The image above shows the completed set of code which generates the game, Rock, Paper and Scissors. Finally as shown in my tutorial of using a micro:bit you need to download this code as a hex.file and convert it to the micro:bit. All you have to do now is shake your micro:bit and it should randomly generate rock, paper or scissors.

 

 

 

 

 

 

 

 

 

 

 

 

Print Friendly, PDF & Email
Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *