5% OFF Over $19, Code: Elephant5; 10% OFF Over $59, Code: Elephant10



Micro Python Basic Course 5: Button game

Posted by SuFiona on

Learning goals:

In this lesson, we will make a very simple micro:bit game. When we press the A button, the micro:bit dot matrix will display an arrow pointing to the A button; when we press the B button, the micro:bit will display an arrow pointing to the B button; if no button is pressed, the micro:bit It shows a heart.

Code

from microbit import *

while True:

    if button_a.is_pressed():

        display.show(Image.ARROW_W)

    elif button_b.is_pressed():

        display.show(Image.ARROW_E)

    else:

        display.show(Image. HEART)

        display.clear()

 

Programming and downloading

1.You should open the Mu software, and enter the code in the edit window, , as shown in Figure 5-1.

Note! All English and symbols should be entered in English, and the last line must be a space.

image.png 

Figure 5-1

2.As shown in Figure 5-2, you need to click the Check button to check if our code has an error. If a line appears with a cursor or an underscore, the program indicating this line is wrong.

image.png 

Figure 5-2

3.You need to connect the micro data cable to micro:bit and the computer, then click the Flash button to download the program to micro:bit as shown in Figure 5-3.

image.png 

Figure 5-3

4.After the download is successful, we can see that the micro:bit shows a heart,as shown in Figure 5-4.When we press the A button, the micro:bit dot matrix will display an arrow pointing to the A button, as shown in Figure 5-5; when we press the B button, the micro:bit will display an arrow pointing to the B button,as shown in Figure 5-6.

image.png image.png 

Figure 5-4                                          Figure 5-5                                                          

image.png 

Figure 5-6  

The code of the experiment:5.Button game.zip

Micro Python Basic Course

0 comments

Leave a comment

Please note, comments must be approved before they are published