Spring Sale: 5% OFF Over $89, Code: Elephant5; 10% OFF Over $149, Code:Elephant10



Microbit Python Tutorials 8: Dice game

Posted by SuFiona on

Course8-Dice game

Learning goals:

In this lesson, we will achieve shake a roll of micro:bit. There are number 1-6 randomly appearing on the dot matrix, which is exactly the same as playing the dice.

Code

from microbit import *

import random

while True:

    gesture = accelerometer.current_gesture()

    if gesture == "shake":

        display.show(str(random.randint(1, 6)))

Programming and downloading

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

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

blob.png 

Figure 8-1

2.As shown in Figure 8-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.

blob.png 

Figure 8-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 8-3.

blob.png 

Figure 8-3

4.After downloading the program, shake a roll of micro:bit. There are number 1-6 randomly appearing on the dot matrix, which is exactly the same as playing the dice, as shown in Figure 8-4, Figure 8-5.

blob.pngblob.png 

Figure 8-4                                                              Figure 8-5

The code of the experiment: 8.Dice game.zip

 

0 comments

Leave a comment

Please note, comments must be approved before they are published