493
Bearbeitungen
Ngb (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Ngb (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
| Zeile 49: | Zeile 49: | ||
# Implementiere eine Methode <code>check_display()</code> die prüft, ob alle LEDs an sind und in diesem Fall <code>True</code> liefert, ansonten <code>False</code>. | # Implementiere eine Methode <code>check_display()</code> die prüft, ob alle LEDs an sind und in diesem Fall <code>True</code> liefert, ansonten <code>False</code>. | ||
# Nutze <code>check_display()</code>, um vor jedem <code>sleep()</code> das Display zu prüfen. | # Nutze <code>check_display()</code>, um vor jedem <code>sleep()</code> das Display zu prüfen. | ||
# Falls das Display komplett an ist, schalte alle LEDs auf 6 und breche die Endlosschleife ab. | # Falls das Display komplett an ist, schalte alle LEDs auf <code>6</code> und breche die Endlosschleife ab. | ||
{{Aufgabe:End}} | {{Aufgabe:End}} | ||
{{Lösung:Start}} | |||
<syntaxhighlight lang="python" line="1"> | |||
from microbit import * | |||
## Hilfsfunktion | |||
def check_coords(x, y): | |||
if x > 4: | |||
x = 4 | |||
elif x < 0: | |||
x = 0 | |||
if y > 4: | |||
y = 4 | |||
elif y < 0: | |||
y = 0 | |||
return (x, y) | |||
## Hilfsfunktion, um das Display zu prüfen | |||
def check_display(): | |||
for y in range(5): | |||
for x in range(5): | |||
if display.get_pixel(x, y) == 0: | |||
return False | |||
return True | |||
## Hilfsfunktion, um das Display einzuschalten | |||
def on(value=6): | |||
for y in range(5): | |||
for x in range(5): | |||
display.set_pixel(x, y, value) | |||
## Initialisierung | |||
sens = 350 # Mess-Sensitivität des Akzelerometer | |||
x = 0 | |||
y = 0 | |||
display.clear() | |||
display.set_pixel(x, y, 6) | |||
## Endlosschleife | |||
while True: | |||
x1 = x + int(accelerometer.get_x()/sens) | |||
y1 = y + int(accelerometer.get_y()/sens) | |||
x1, y1 = check_coords(x1, y1) | |||
if x1 != x or y1 != y: | |||
display.set_pixel(x, y, 3) | |||
display.set_pixel(x1, y1, 6) | |||
x, y = x1, y1 | |||
if check_display(): | |||
on() | |||
break | |||
sleep(200) | |||
</syntaxhighlight> | |||
{{Lösung:End}} | |||