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.
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.
Step 3: Add a new variable and call it “hand”. Next select the block “set hand to” and place it inside the pervious block.
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.
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”.
Tip:You can test the code by clicking on the button labelled “shake”.
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.
Step 8: Add another “show led” block below and form the shape of a rock as shown below.
Step 9: Finally add the last “show led” block and create the shape of scissors.
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.
Be First to Comment