A1
from microbit import *
display.show(Image.DUCK)
A2
from microbit import *
myImages = [Image.SAD, Image.MEH, Image.YES, Image.NO, Image.COW]
display.show(myImages, delay = 1000, loop = True)
A3
from microbit import *
all_1 = Image("11111:"
"11111:"
"11111:"
"11111:"
"11111")
all_3 = Image("33333:"
"33333:"
"33333:"
"33333:"
"33333")
all_5 = Image("55555:"
"55555:"
"55555:"
"55555:"
"55555")
all_7 = Image("77777:"
"77777:"
"77777:"
"77777:"
"77777")
all_9 = Image("99999:"
"99999:"
"99999:"
"99999:"
"99999")
myImages = [all_1, all_3, all_5, all_7, all_9]
display.show(myImages, delay = 100, loop = True)
A4
from microbit import *
while True:
for brightness in range(1,9,2):
sleep(200)
for column in range(5):
for row in range (5):
display.set_pixel(column, row, brightness)
B1
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.GHOST)
if button_b.is_pressed():
display.show(Image.RABBIT)
if pin_logo.is_touched():
display.clear()
B2
from microbit import *
while True:
if button_a.is_pressed():
display.scroll("Taste_A")
if button_b.is_pressed():
display.show(Image.RABBIT)
if pin_logo.is_touched():
display.clear()
B3
from microbit import *
while True:
if button_a.is_pressed():
display.scroll("Taste_A", wait=False)
if button_b.is_pressed():
display.show(Image.RABBIT)
if pin_logo.is_touched():
display.clear()
B4
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HAPPY)
elif button_b.is_pressed():
display.show(Image.SAD)
elif pin_logo.is_touched():
display.show(Image.HEART)
else:
display.clear()
B5
from microbit import *
while True:
if pin_logo.is_touched():
display.scroll(str(button_a.get_presses()-button_b.get_presses()))
C1
from microbit import *
while True:
if accelerometer.was_gesture("shake"):
display.show(Image.DUCK)
elif accelerometer.was_gesture("face up"):
display.show(Image.HOUSE)
C2
C3
from microbit import *
import random
while True:
if accelerometer.was_gesture("shake"):
display.show(random.randint(1,6))
sleep(1000)
display.clear()
C4
from microbit import *
import random
while True:
if accelerometer.was_gesture("face up"):
display.show(random.randint(1,6))
sleep(1000)
if pin_logo.is_touched():
display.clear()
C5 – Variante A
from microbit import *
myImages = []
for i in range(0, 9, 1):
myImg = Image()
myImg.fill(i)
myImages.append(myImg)
myPos = 0
while True:
sleep(200)
if myPos < len(myImages) - 1:
myPos += 1
else:
myPos = 0
display.show(myImages[myPos])
C5 – Variante B
from microbit import *
myImages = []
for i in range(0, 9, 1):
myImg = Image()
myImg.fill(i)
myImages.append(myImg)
display.show(myImages, delay = 200, loop = True)
C6
from microbit import *
myImages = []
myPos = 0
for i in range(0, 9, 1):
myImg = Image()
myImg.fill(i)
myImages.append(myImg)
while True:
if(button_a.is_pressed()):
sleep(100)
if myPos < len(myImages) - 1:
myPos += 1
else:
myPos = 0
display.show(myImages[myPos])
C7
from microbit import *
myImages = []
myPos = 0
for i in range(0, 10, 1):
myImg = Image()
myImg.fill(i)
myImages.append(myImg)
while True:
xAcceleration = accelerometer.get_x()
# xAcceleration von +/-2040 nach 0...9 umrechnen:
myPos = abs(xAcceleration) // 205
print(myPos)
display.show(myImages[myPos])
D1
from microbit import*
import music
# Frère Jacques:
melodyJacques = ['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',
'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
'c4:4', 'g3:4', 'c4:8', 'c4:4', 'g3:4', 'c4:8']
note = 0
while True:
if(button_a.is_pressed()):
music.play(melodyJacques[note])
sleep(30)
if(note < len(melodyJacques)-1):
note += 1
else:
note = 0
D2
from microbit import*
import music
# Frère Jacques:
melodyJacques = ['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',
'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
'c4:4', 'g3:4', 'c4:8', 'c4:4', 'g3:4', 'c4:8']
note = 0
while True:
if(button_a.is_pressed()):
display.show(Image.HAPPY)
music.play(melodyJacques[note])
sleep(30)
if(note < len(melodyJacques)-1):
note += 1
else:
note = 0
else:
display.show(Image.ARROW_W)
D3
from microbit import*
import music
# Frère Jacques:
melodyJacques = ['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',
'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
'c4:4', 'g3:4', 'c4:8', 'c4:4', 'g3:4', 'c4:8']
# Popcorn:
melodyPop = ['b4:2', 'a4:2', 'b4:2', 'f4:2','d4:2', 'f4:2', 'b3:4',
'b4:2', 'a4:2', 'b4:2', 'f4:2','d4:2', 'f4:2', 'b3:4',
'b4:2', 'c#5:2', 'd5:2', 'c#5:2', 'd5:2', 'b4:2', 'c#5:2', 'b4:2', 'c#5:2', 'a4:2',
'b4:2', 'a4:2', 'b4:2', 'g#4:2', 'b4:4','r:4']
noteA = 0
noteB = 0
while True:
if(button_a.is_pressed()):
music.set_tempo(bpm = 120) # Diese Melodie normal spielen
display.show(Image.ASLEEP)
music.play(melodyJacques[noteA])
sleep(30)
if(noteA < len(melodyJacques)-1):
noteA += 1
else:
noteA = 0
elif(button_b.is_pressed()):
music.set_tempo(bpm = 200) # Diese Melodie schneller spielen
display.show(Image.MUSIC_CROTCHET)
music.play(melodyPop[noteB])
sleep(30)
if(noteB < len(melodyPop)-1):
noteB += 1
else:
noteB = 0
else:
display.clear()
D4
from microbit import *
import music
while True:
for freq in range(330, 770, 10):
music.pitch(freq, 100)
for freq in range(770, 330, -10):
music.pitch(freq, 100)
D5
from microbit import *
import music
freq = 440 # A4
while True:
if button_a.is_pressed():
if freq < 4000:
freq += 100
music.pitch(freq, 50)
if button_b.is_pressed():
if freq > 100:
freq -= 100
music.pitch(freq, 50)
D6
from microbit import *
import music
while True:
if accelerometer.was_gesture("face up"):
display.show(Image.HAPPY)
music.play(music.JUMP_UP)
elif accelerometer.was_gesture("face down"):
display.show(Image.SAD)
music.play(music.JUMP_DOWN)
else:
display.clear()
D7
from microbit import *
import music
#Cancan von Jacques Offenbach:
melodyCancan = ['c4:16',
'd4:4', 'f4:4', 'e4:4', 'd4:4', 'g4:8', 'g4:8',
'g4:4', 'a4:4', 'e4:4', 'f4:4', 'd4:8', 'd4:8',
'd4:4', 'f4:4', 'e4:4', 'd4:4',
'c4:4', 'c5:4', 'b4:4', 'a4:4',
'g4:4', 'f4:4', 'e4:4', 'd4:4',
'c4:16',
'd4:4', 'f4:4', 'e4:4', 'd4:4', 'g4:8', 'g4:8',
'g4:4', 'a4:4', 'e4:4', 'f4:4', 'd4:8', 'd4:8',
'd4:4', 'f4:4', 'e4:4', 'd4:4',
'c4:4', 'g4:4', 'd4:4', 'e4:4',
'c4:8', 'c5:4', 'r:4']
beats = 320
doPlay = 0
note = 0
while True:
if(pin_logo.is_touched()):
display.show(Image.HAPPY)
doPlay ^= 1 # Variable toggeln: 0 setzen, wenn sie 1 ist – und umgekehrt.
sleep(200)
display.clear()
if(doPlay):
music.set_tempo(bpm = beats) # Tempo setzen
music.play(melodyCancan[note]) # Aktuelle Note spielen
sleep(25) # ganz kurz warten (Pause zwischen Noten)
if(note < len(melodyCancan)-1):
note += 1 # Note erhöhen...
else:
note = 0 # ...oder auf 0 setzen
if(button_a.is_pressed()):
beats += 40
if(button_b.is_pressed()):
beats -= 40
D8
from microbit import *
import music
#Cancan von Jacques Offenbach:
melodyCancan = ['c4:16',
'd4:4', 'f4:4', 'e4:4', 'd4:4', 'g4:8', 'g4:8',
'g4:4', 'a4:4', 'e4:4', 'f4:4', 'd4:8', 'd4:8',
'd4:4', 'f4:4', 'e4:4', 'd4:4',
'c4:4', 'c5:4', 'b4:4', 'a4:4',
'g4:4', 'f4:4', 'e4:4', 'd4:4',
'c4:16',
'd4:4', 'f4:4', 'e4:4', 'd4:4', 'g4:8', 'g4:8',
'g4:4', 'a4:4', 'e4:4', 'f4:4', 'd4:8', 'd4:8',
'd4:4', 'f4:4', 'e4:4', 'd4:4',
'c4:4', 'g4:4', 'd4:4', 'e4:4',
'c4:8', 'c5:4', 'r:4']
beats = 320
doPlay = 0
note = 0
def changeOctave(step = 1):
for i, element in enumerate(melodyCancan): # für jede Note in der Melodie-Liste...
# ...i = Indexwert (0...x) des jeweiligen Elements (von Funktion enumerate()),
# element = Element (Zeichenkette) in der Melodie-Liste):
pos = element.find(':')-1 # ermittle Position links des Doppelpunkts
if(element[pos] != 'r'): # sofern hier nicht 'r:' steht, sollte hier die Oktavenzahl stehen
octave = int(element[pos]) # ermittle Oktavenzahl: wandle Zeichen in Integer-Zahl (Funktion int())
octave += step # ändere Oktavenzahl
element = element[:pos] + str(octave) + element[pos+1:] # ersetze alte Oktavenzahl im Element
melodyCancan[i] = element # ersetze entsprechenden Eintrag in der Melodien-Liste
while True:
if(pin_logo.is_touched()):
display.show(Image.HAPPY)
doPlay ^= 1 # Variable toggeln: 0 setzen, wenn sie 1 ist – und umgekehrt.
sleep(200)
display.clear()
if(doPlay):
music.set_tempo(bpm = beats) # Tempo setzen
music.play(melodyCancan[note]) # aktuelle Note spielen
sleep(25) # ganz kurz warten (Pause zwischen Noten)
if(note < len(melodyCancan)-1):
note += 1 # Note erhöhen...
else:
note = 0 # ...oder auf 0 setzen
if(button_a.is_pressed()):
beats += 40
changeOctave(1)
if(button_b.is_pressed()):
beats -= 40
changeOctave(-1)