## 210903 Scracth & Python Final Term期末考
# 題目1
#### 兩層loop和 setpixel 顯示一自訂符號
:::info
1.想法:領頭的起始點設為(5,2),設定移動範圍0到6行,x遞減1,畫面顯示在1到5行往左移動。(條件一)若x0 >= 0則亮燈領頭的點。將x設為x0+1。(條件二)若x小於等於0且x小於5,亮燈後面3個點(排直排)及1個點,形成卜。和領頭點合在一起構成圖案+
設定範圍第0行(當領頭點跑到畫面外左邊1行),亮燈第2個+圖案,結合之前的第1個+圖案,組合成++
2.程式
3.結果
{%vimeo 575706182 %}
:::
```python=12
from microbit import * # 往左跑十十
while True:
x0 = 5
y0 = 2
for _ in range(7):
x0 -= 1
if x0 >= 0:
display.set_pixel(x0, y0, 9)
x = x0 + 1
if 0 <= x and x < 5 :
display.set_pixel(x, y0-1, 9)
display.set_pixel(x, y0, 9)
display.set_pixel(x+1, y0, 9)
display.set_pixel(x, y0+1, 9)
sleep(200)
display.clear()
for i in range(1):
display.set_pixel(x, y0-1, 9)
display.set_pixel(x, y0, 9)
display.set_pixel(x+1, y0, 9)
display.set_pixel(x, y0+1, 9)
```
# h1題目 2-- LED 程式
:::info
請使用範例程式,修改成使用函數完成下圖
:::
## 範例程式
from microbit import *
while True:
for x in range(5):
if x % 2 == 0: # even column
yrange = range(5)
else: # odd column
yrange = range(4,-1,-1)
for y in yrange:
display.set_pixel(x, y, 9)
sleep(100)
sleep(500)
display.clear()
:::info
程式
結果
:::
題目 2 Ans
# h1題目 3-- LED 跑馬燈
開始顯示 <–>
按 A 鍵 顯示 <- -
按 B 鍵 顯示 -->
同按 AB 鍵
依上列 LED <— 跑馬燈之方向循環顯示 Image.HAPPY 一次
依上列 LED —> 跑馬燈之方向循環顯示 Image.SAD 一次
請參考 Image图片的移动
搖晃板子離開程式
:::info
程式
:::
```python=
from microbit import *
# 開始顯示 <–>
# 按 A 鍵 顯示 <- -
# 按 B 鍵 顯示 -->
# 同按 AB 鍵
# 依上列 LED <— 跑馬燈之方向循環顯示 Image.HAPPY 一次
# 依上列 LED —> 跑馬燈之方向循環顯示 Image.SAD 一次
# 晃板子離開程式
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(Image.HAPPY)
sleep(200)
display.show(Image.SAD)
sleep(200)
elif button_a.is_pressed():
for i in range(0,6):
a="00900:09000:99999:09000:00900"
display.show(Image(a).shift_left(i))
sleep(200);
sleep(1000)
elif button_b.is_pressed():
display.show(Image.ARROW)
sleep(300)
display.clear()
```
結果
{%vimeo %}
## 心得
發現自己打字速度太慢,應該練習英打。其他人已經跟上進度嘗試練習修改變化(後續第二題第三題)。我還在輸入第一題。