Coding
Importing Python Libraries
-
Select Basic. Select and drag a
from microbit import *
block to the code area and drop it. -
Select Basic. Select and drag an
import random
block to the code area and attach it under thefrom microbit import *
block.
Here is what your code looks like so far:
Creating the While Loop
- Select Basic. Select and drag a
while True:
block to the code area and attach it under theimport random
block.
Your code should look like this:
Creating the Rock Paper Scissor Variables
Think of a variable as a box that stores information that can be used throughout our program. We give variables a descriptive name so we and others can understand what is going on within our program.
Creating Paper
-
Select Display. Select and drag an
image =
block to the code area and attach it within thewhile True:
block. -
Drag another two
image =
blocks to the code area attaching them under the firstimage =
block. -
Within the first
image =
block create the following image to represent Paper by typing 9 (this represents the brightness of the LEDs) in the corresponding squares.
- Select the drop-down arrow next to image and select New variable and name it Paper.
Creating Rock
- Create the following image to represent Rock by typing 9 within the corresponding squares. in the next
image =
block.
- Select the arrow next to image and Select New variable and name it Rock.
Creating Scissors
- Create the following image to represent Scissors by typing 9 within the corresponding boxes in the remaining
image =
block.
- Select the arrow next to image and select New variable and name it Scissors.
Your code should look like this:
Creating the on shake Action
-
Select Basic. Select and drag an
if True:
block to the code area and attach it under Scissors. -
Select Accelerometer. Select and drag an
accelerometer.was_gesture(‘shake’)
block to the code area and attach it within the True of theif
block. -
Select Variables. Select create variable and name it choice. Select OK or press Enter on the keyboard.
-
Select Variables. Select and drag a
choice =
block to the code area and attach it within theif accelerometer.was_gesture(‘shake’)
block. Within the blank space type random.randint(0,2).
Creating the Choice = 0 Condition
-
Select Basic. Select and drag an
if True:
block to the code area and attach it below thechoice = random.randint(0,2)
block. -
Select Basic. Select and drag a
0 == 0
block to the code area and attach it within the True of theif
block. -
Select Variables. Select and drag a
choice
block to the code area and attach it within the first 0 of theif
block. -
Select Display. Select and drag a
display.show(Image.HAPPY)
block to the code area and attach it within theif choice == 0:
block. -
Select Variables. Select and drag the
Paper
block to the code area and attach it within Image.HAPPY.
Your code should look like this:
Creating the Choice = 1 Condition
-
Select Basic. Select and drag an
elif
block to the code area and attach it below theif choice == 0:
block. -
Right-click on
choice == 0
and select duplicate. Attach the duplicated block within the True of theelif
block. select 0 and type 1. -
Right-click on the
display.show(Paper)
block and select duplicate. Attach the duplicated block within theelif choice == 1:
block. Select Paper and choose Rock.
Your code should look like this:
Creating the Choice = 2 Condition
-
Select Basic. Select and drag an
else:
block to the code area and attach it under theelif choice == 1:
block. -
Right-click on
display.show(Rock)
and select duplicate. Attach the duplicated block within theelse:
block. Select Rock and choose Scissors.
Completed Code:
We have now finished coding rock paper scissors the next step is to download our code to the micro:bit.