Wednesday, 21 February 2018

Week 1 exercise 3 rock paper scissors for micro:bit

This tutorial will introduce you to making games for the micro:bit , if else statements, adding variables, random number generation and accessing the onboard accelerometer.

Outcome:
Play rock, paper, scissors game with the micro:bit. 


Links:
Code editor https://makecode.microbit.org/
How to copy files to the micro:bithttps://makecode.microbit.org/device/usb

Step 1
Create a new project. We want the micro:bit to choose rock, paper, or scissors when you shake it. Place a on shake  code block so when you shake the micro:bit, it will run part of a program.



Step 2
Adding a variable
Select Variables from the menu then set item to.  
Drag the variable item into the on shake block, your code should now look like the following

now click on the set item code block and select Rename variable
 this will open a popup, go ahead and rename the variable to tool



Step 3
Adding random number generation
Select Math from the menu, then select  pick random 0 to 4.

 Add the pick random code block to the end of the set tool variable. Once in place change the number  to 2.


Now when you shake the micro:bit, it will pick a random number from 0 to 2 and store it in the variable tool.



Step 3 
Adding If else statement
What we want to do now is to show a different image on the LEDs that is matches each possible number (0 , 1 or 2)
Select Logic from the menu and select the if then else block.

Now place it under the set variable code block.


Select a condition check from the logic menu.


and add it to the first if statement

As rock paper scissors has three variables, we need to add another condition check and else if statement to the code to do this click the wheel icon next to the word if, which will open a popup
Select and drag else if from the left/grey panel, to the white panel
You can now close this popup by clicking the wheel icon again. Now add another condition check to the new else if statement so that it looks like the following.


Step 4
Now that the statements and condition checks are in we need to get the code to check for which random number has been generated and then for it to do something.
Select the variable tool from the Variables menu,



And place it into the start of both condition statements as seen below.


Now change the second condition statement check from 0 to 1.
You don’t need to check if tool is 2 because 2 is the only number left out of 0, 1, and 2. That’s why you can use an else instead of an else if.



Step 5
Adding images
Now from the Basic menu select show icon and add icons to each of the statements, select a different image for each statement.

Step 6
Now save the file. Select  the text field next to the save button and type in Rockpaperscissors. This will be the file name for the project. Go ahead and click the save button to the right of this text field. This will download the code to you computer saving it as a .hex file. Copy this file to the micro:bit, as the data is transfered to the micro:bit a light will flash showing that a transfer is in progress. Visit https://makecode.microbit.org/device/usb for more information on uploading files to the micro bit.

No comments:

Post a Comment