Microbit programmieren: Lösungen zu den Aufgaben

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)
  • gf_informatik/microbit_programmieren_loesungen.1631527648.txt.gz
  • Zuletzt geändert: 2021-09-13 10:07
  • von gra