Loeffel-ei-loesung: Unterschied zwischen den Versionen

Aus microbit - Das Schulbuch
Wechseln zu: Navigation, Suche
(Detailschritte)
(Detailschritte)
Zeile 6: Zeile 6:
  
 
<pre id="01">
 
<pre id="01">
input.onGesture(Gesture.Shake, function () {
+
let accY = 0
     if (Math.randomBoolean()) {
+
let accX = 0
   
+
let x = 2
 +
let y = 2
 +
basic.forever(function () {
 +
    led.plot(x, y)
 +
    accX = input.acceleration(Dimension.X)
 +
     accY = input.acceleration(Dimension.Y)
 +
    if (accX < -150 && x > 0) {
 +
        x += -1
 +
    } else if (accX > 150 && x < 4) {
 +
        x += 1
 
     }
 
     }
 +
    if (accY < -150 && y > 0) {
 +
        y += -1
 +
    } else if (accY > 150 && y < 4) {
 +
        y += 1
 +
    }
 +
    basic.pause(500)
 +
    basic.clearScreen()
 
})
 
})
 +
 
</pre>
 
</pre>
 
<htmlet>makecode_embed</htmlet>
 
<htmlet>makecode_embed</htmlet>

Version vom 18. Januar 2022, 00:21 Uhr

Detailschritte

Die Lösung zu diesem Projekt wird hier in Teilen gezeigt:

  • Ereignis "wenn geschüttelt"
  • wenn-dann-Verzweigung
let accY = 0
let accX = 0
let x = 2
let y = 2
basic.forever(function () {
    led.plot(x, y)
    accX = input.acceleration(Dimension.X)
    accY = input.acceleration(Dimension.Y)
    if (accX < -150 && x > 0) {
        x += -1
    } else if (accX > 150 && x < 4) {
        x += 1
    }
    if (accY < -150 && y > 0) {
        y += -1
    } else if (accY > 150 && y < 4) {
        y += 1
    }
    basic.pause(500)
    basic.clearScreen()
})