Skip to content

活動

Which way now?

初學者 | MakeCode, Python | LED 螢幕, 加速計 | 性能工具, 變數, 選擇, 隨機化

步驟1:製作

它是什麼?

Shake your micro:bit and be given a random direction to walk. You will learn about variables, and using random numbers, selection and comparison logic blocks.

這兩支影片為您示範了製作內容和編碼方法:

介紹

編程指引

運作方式

  • The program uses a variable called “direction” to store a random number.
  • Variables store numbers or values that can change in a computer program.
  • Shake the micro:bit to set “direction” to a random number between one and three.
  • The program then uses selection and comparison logic blocks to test the value of the variable.
  • If “direction” is equal to 1, then an arrow pointing north shows on the micro:bit’s LED display. If “direction” is equal to 2, then the arrow points east. Otherwise, the arrow points west.
  • To make the battery last longer, the program pauses for half a second after the arrow appears then clears the screen.

你需要的東西

  • micro:bit (或,MakeCode simulator)
  • MakeCode編輯器
  • battery pack (optional but recommended)

步驟2:編碼

1# Imports go at the top
2
3
4
5from microbit import *
6import random
7
8while True:
9    if accelerometer.was_gesture('shake'):
10        random_number = random.randint(1, 3)
11        if random_number == 1:
12            display.show(Image.ARROW_N)
13        elif random_number == 2:
14            display.show(Image.ARROW_E)
15        else:
16            display.show(Image.ARROW_W)
17    sleep(2000)
18    display.clear()

步驟3:進階

  • Add more directions.
  • Use buttons to show icons for skipping, running, and jumping.
  • Combine with the touch stopwatch program to make a motivational tool to use in sports lessons.

Thanks to David Hay, an educator in Alberta, Canada for sharing this brilliant project idea with us.