Welcome to Building:bit Superkit repository
2.Bite the hand crocodile
Bite the hand crocodile
Bite the hand crocodile1. Learning Objectives2. Building Blocks3. Motor Wiring4. Code analysis5. Write and download the program6. Experimental phenomenon
1. Learning Objectives
In this course, we mainly learn how to use Python programming to make the Face Changing King building block shape realize the "hand-biting crocodile" gameplay. When we press the A button or the B button, the mask of the Face Changing King will fall off randomly.
2. Building Blocks
For detailed steps of building blocks, please refer to the installation drawings of [Assembly course]-[Changing face] or the building block installation album in the materials.
3. Motor Wiring
Insert the building block servo wiring into the Super:bit expansion board S1 interface, and the servo orange wiring into the yellow pin of S1.
As shown in the figure below:

! Notes:
When taking the course related to the building block servo for the first time, we need to remove the white building blocks installed on the servo and upload the program of this course to the micro:bit; then turn on the power switch of the Super:bit expansion board and wait for the building block servo to turn to the initial position; then, we can turn off the power, adjust the mask of the face-changing king to the closed state, as shown in the figure below, and then install the building blocks. (If you have used the program related to the face-changing king and the servo before, you can skip this step)

4. Code analysis
For the program of this course, please see the Bite the hand crocodile.py file.
from microbit import *import superbitimport microbitimport randomFirst, import the libraries needed for this lesson from microbit: the superbit library is dedicated to the superbit expansion board; the random library is dedicated to generating random numbers;
xxxxxxxxxxsuperbit.servo270(superbit.S1, 50)microbit.sleep(500)superbit.servo270(superbit.S1, 140)display.scroll("Go!")display.show(Image.ANGRY)superbit.servo270(superbit.S1, 50): Initialize the building block servo to rotate to 50°;
microbit.sleep(500): Delay 00 milliseconds;
superbit.servo270(superbit.S1, 140): Initialize the building block servo to rotate to 140°;
display.scroll("Go!"): scroll the string Go! on the dot matrix;
Display.show(Image.ANGRY): display an angry and ferocious expression.
xxxxxxxxxxglobal aglobal button_downbutton_down = 0Define variables a and button_down.
xwhile True:if button_a.is_pressed() is True and button_b.is_pressed() is False:if button_down == 0:a = random.randint(1, 3)if a < 3:superbit.servo270(superbit.S1, 140)a = 0else:superbit.servo270(superbit.S1, 50)microbit.sleep(500)superbit.servo270(superbit.S1, 140)a = 0button_down = 1...Infinite loop to determine the status of buttons A and B.
If buttons A and B are pressed, the mask of the Face Changing King will fall randomly.
5. Write and download the program
- Open the Mu software and enter the code in the editing window. Note! All English and symbols should be entered in English mode, use the Tab key for indentation, and the last line ends with a blank program.
- Click the thumb 'Check' button to check whether there are any errors in our code. If a cursor or underline appears in a line, it means a syntax error. Please check and modify it. If there is no error, the lower left corner will prompt that there is no problem with the detection.

- Click the 'REPL' button to check whether the Superbit library has been downloaded. If not, please refer to [Preparation before class] --> [2.4 Python Programming Guide].

- After the program is written, connect the computer and microbit mainboard with a microUSB data cable, please click the 'Flash' button to download the program to the micro:bit mainboard. (You need to click the 'REPL' button again to turn off the import library file function before you can download the program normally).

- If the download fails, please confirm whether the microbit is connected to the computer normally via the microUSB data cable and the Superbit Python library has been imported.
6. Experimental phenomenon
After the program is downloaded successfully, turn on the power switch, the servo will be initialized to 50° (the face-changing king mask is off), and after 500 milliseconds, the servo will rotate to 140° (the face-changing king mask is on), and the dot matrix will display "GO!". Then a ferocious expression pattern will be displayed on the micro:bit dot matrix, as shown below.

When we press the A button or the B button, the face-changing king's mask will fall off randomly, achieving the game effect of a crocodile biting your hand.
If you need to restart, please press the reset button on the back of the micro:bit motherboard.