Contenu connexe
Similaire à micro:bit加速度感測應用 (20)
Plus de 吳錫修 (ShyiShiou Wu) (20)
micro:bit加速度感測應用
- 10. Python程式
def on_gesture_shake():
global Roll
Roll = randint(1, 6)
if Roll == 1:
basic.show_leds("""
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
""")
elif Roll == 2:
basic.show_leds("""
. . . . .
. . . # .
. . . . .
. # . . .
. . . . .
""")
實作練習 - 電子骰子 3/5
10
- 11. elif Roll == 3:
basic.show_leds("""
. . . . #
. . . . .
. . # . .
. . . . .
# . . # .
""")
elif Roll == 4:
basic.show_leds("""
. . . . .
. # . # .
. . . . .
. # . # .
. . . . .
""")
elif Roll == 5:
basic.show_leds("""
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .
""")
實作練習 - 電子骰子 4/5
11
- 12. else:
basic.show_leds("""
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .
""")
basic.pause(3000)
basic.show_string("?")
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
Roll = 0
basic.show_string("?")
def on_forever():
pass
basic.forever(on_forever)
實作練習 - 電子骰子 5/5
12
- 13. 實作練習 - 運動方向指示器 1/5
顯示micro:bit控制板運動方向
使用方向箭頭指示micro:bit控制板左右及前後移動,上下移動以大小菱
形表示
13
- 16. Python程式
forward_backward = 0
up_down = 0
left_right = 0
def check_Y():
if forward_backward >= 1200:
basic.show_arrow(ArrowNames.NORTH)
if forward_backward <= -1200:
basic.show_arrow(ArrowNames.SOUTH)
def check_Z():
if up_down >= 100:
basic.show_icon(IconNames.SMALL_DIAMOND)
if up_down <= -2000:
basic.show_icon(IconNames.DIAMOND)
def check_X():
if left_right >= 1200:
basic.show_arrow(ArrowNames.WEST)
if left_right <= -1200:
basic.show_arrow(ArrowNames.EAST)
實作練習 - 運動方向指示器 4/5
16
- 17. def on_forever():
global left_right, forward_backward, up_down
left_right = input.acceleration(Dimension.X)
forward_backward = input.acceleration(Dimension.Y)
up_down = input.acceleration(Dimension.Z)
check_X()
check_Y()
check_Z()
basic.forever(on_forever)
實作練習 - 運動方向指示器 5/5
17
- 23. Python程式
def moving():
if input.acceleration(Dimension.X) > 150:
ball.change(LedSpriteProperty.X, 1)
score += 1
if input.acceleration(Dimension.X) < -150:
ball.change(LedSpriteProperty.X, -1)
score += 1
if input.acceleration(Dimension.Y) > 150:
ball.change(LedSpriteProperty.Y, 1)
score += 1
if input.acceleration(Dimension.Y) < -150:
ball.change(LedSpriteProperty.Y, -1)
score += 1
def isTouchingEdge():
if ball.is_touching_edge():
game.set_score(score)
game.game_over()
實作練習 - 平衡控制遊戲 4/5
23
- 24. ball = game.create_sprite(2, 2)
score = 0
def on_forever():
moving()
isTouchingdge()
basic.pause(200)
basic.forever(on_forever)
實作練習 - 平衡控制遊戲 5/5
24