2024年1月27日 星期六

micro:bit Python 初體驗以樹藝工坊為例

在南投縣草屯鎮國立臺灣工藝研究暨發展中心有一間樹藝工坊,進駐藝術家是李永謨老師,李老師說:樹藝的定義是代表一種;生命的重生、樹生命「特性力量」的發揮。大家可以到臺灣樹藝再生之美李永謨粉絲頁查看李老師的相關作品,我們今天micro:bit Python就以樹藝工坊為例,來說明micro:bit Python的基本程式設計。

功能如下:

  1. 啟動顯示HAPPY圖示
  2. 按下LOGO顥示:Welcome to Tree-Art Workshop!
  3. 拍掌顯示:'I am Lee, Yung-Moo.
  4. 按下A鍵或Pin0,隨機顯示1-6其中一個數字。
  5. 按下B鍵或Pin1,播放音樂。
  6. 按下Pin2顯示ANGRY圖示。

程式編輯器:micro:bit Python Editor (microbit.org)



程式如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Imports go at the top
from microbit import *
import random
import music
display.show(Image.HAPPY)

# Code in a 'while True:' loop repeats forever
while True:
    if pin_logo.is_touched():
        display.scroll('Welcome to Tree-Art Workshop!')
        display.show(Image.HEART)
    if microphone.current_event() == SoundEvent.LOUD:
        display.scroll('I am Lee, Yung-Moo.')
        display.show(Image.HAPPY)
    if button_a.was_pressed() or pin0.is_touched():
        display.show(random.randint(1, 6))
    if button_b.was_pressed() or pin1.is_touched():
        music.play(['c', 'd', 'e', 'c'])
        display.show(random.randint(0, 99))
    if pin2.is_touched():
        display.show(Image.ANGRY)
    sleep(100)

沒有留言:

張貼留言