- from microbit import *
- import music
- # DIY两只老虎乐谱
- tiger= ["C4:4", "D4:4", "E4:4", "C4:4", "C4:4", "D4:4", "E4:4", "C4:4",
- "E4:4", "F4:4", "G4:8", "E4:4", "F4:4", "G4:8"]
- while not False:
- display.show(Image.MUSIC_QUAVER)
- if button_a.is_pressed():
- music.play(tiger, wait=False, loop=True)
- elif button_b.is_pressed():
- music.play(music.BIRTHDAY, wait=False, loop=True)
2.稍微复杂一点程序,带动态图像的
- from microbit import *
- import music
- pix = [(5,0,0,0,5),(5,5,0,5,5),(0,9,0,9,0),(7,0,8,0,7),(0,9,9,9,0)]
- # diy两只老虎乐谱
- tiger= ["C4:4", "D4:4", "E4:4", "C4:4", "C4:4", "D4:4", "E4:4", "C4:4",
- "E4:4", "F4:4", "G4:8", "E4:4", "F4:4", "G4:8"]
- while not False:
- display.show(Image.MUSIC_QUAVER)
- if button_a.is_pressed():
- music.play(tiger, wait=False, loop=True)
-
- for down in range(0,5):
- for y in range(0,5):
- for x in range(0,5):
- if y-down<0:
- display.set_pixel(x,y,0)
- else:
- display.set_pixel(x,y,pix[y-down][x])
- sleep(300)
-
- for right in range(0,5):
- for y in range(0,5):
- for x in range(0,5):
- if x-right<0:
- display.set_pixel(x,y,0)
- else:
- display.set_pixel(x,y,pix[y][x-right])
- sleep(300)
-
- for up in range(0,5):
- for y in range(0,5):
- for x in range(0,5):
- if y+up>4:
- display.set_pixel(x,y,0)
- else:
- display.set_pixel(x,y,pix[y+up][x])
- sleep(300)
-
- for left in range(0,5):
- for y in range(0,5):
- for x in range(0,5):
- if x+left>4:
- display.set_pixel(x,y,0)
- else:
- display.set_pixel(x,y,pix[y][x+left])
- sleep(300)
-
- sleep(1000)
- elif button_b.is_pressed():
- music.play(music.BIRTHDAY, wait=False, loop=True)