Endliche Automaten

(Zustandsmaschinen)

Beispiel: Zustandsdiagramm

Beispiel: Pong

Beispiel: Pong

Beispiel: Pong

Ausgabe: "B" anzeigen

Beispiel: Pong

Event-Zustands Tabellen

Event-Zustands Tabellen: Pong

Event-Zustands Tabellen: Pong

from microbit import *

state = 'IDLE'

while True:
    btn_a = button_a.was_pressed()
    btn_b = button_b.was_pressed()

    if state == 'IDLE':
        pass
    elif state == 'MOVE_LEFT':
        pass
    elif state == 'MOVE_RIGHT':
        pass
    elif state == 'A_WINS':
        pass
    elif state == 'B_WINS':
        pass
    sleep(500)
        

Event-Zustands Tabellen: Pong

from microbit import *

state = 'IDLE'

while True:
    btn_a = button_a.was_pressed()
    btn_b = button_b.was_pressed()

    if state == 'IDLE':
        pass
    elif state == 'MOVE_LEFT':
        pass
    elif state == 'MOVE_RIGHT':
        pass
    elif state == 'A_WINS':
        pass
    elif state == 'B_WINS':
        pass
    sleep(500)
        
# Knopf wurde gedrückt:
print(button_a.was_pressed()) # => True
# ... ein weiteres mal abfragen
print(button_a.was_pressed()) # => False

Event-Zustands Tabellen: Pong

from microbit import *

state = 'IDLE'

while True:
    btn_a = button_a.was_pressed()
    btn_b = button_b.was_pressed()

    if state == 'IDLE':
        display.show(Image.HAPPY)
        if btn_a:
            set_ball(2)
            state = 'MOVE_LEFT'
        elif btn_b:
            set_ball(2)
            state = 'MOVE_RIGHT'
    elif state == 'MOVE_LEFT':
        pass
    elif state == 'MOVE_RIGHT':
        pass
    elif state == 'A_WINS':
        pass
    elif state == 'B_WINS':
        pass
    sleep(500)
        
def set_ball(pos):
    display.clear()
    display.set_pixel(pos, 2, 9)

Event-Zustands Tabellen: Pong

# ...
state = 'IDLE'

while True:
    btn_a = button_a.was_pressed()
    btn_b = button_b.was_pressed()
	pos = current_pos()
    
    if state == 'IDLE':
        display.show(Image.HAPPY)
        if btn_a:
            set_ball(2)
            state = 'MOVE_LEFT'
        elif btn_b:
            set_ball(2)
            state = 'MOVE_RIGHT'
    elif state == 'MOVE_LEFT':
        if pos == 0:
            if btn_a:
                state = 'MOVE_RIGHT'
            else:
                state = 'B_WINS'
        else:
            pos = pos - 1
            set_ball(pos)
    elif state == 'MOVE_RIGHT':
        pass
    elif state == 'A_WINS':
        pass
    elif state == 'B_WINS':
        pass
    sleep(500)
def current_pos():
    for i in range(5):
        if display.get_pixel(i, 2) == 9:
            return i
    return 2

Event-Zustands Tabellen: Pong

# ...
state = 'IDLE'

while True:
    btn_a = button_a.was_pressed()
    btn_b = button_b.was_pressed()
	pos = current_pos()
    
    if state == 'IDLE':
        display.show(Image.HAPPY)
        if btn_a:
            set_ball(2)
            state = 'MOVE_LEFT'
        elif btn_b:
            set_ball(2)
            state = 'MOVE_RIGHT'
    elif state == 'MOVE_LEFT':
        if pos == 0:
            if btn_a:
                state = 'MOVE_RIGHT'
            else:
                state = 'B_WINS'
        else:
            pos = pos - 1
            set_ball(pos)
    elif state == 'MOVE_RIGHT':
        pass
    elif state == 'A_WINS':
        pass
    elif state == 'B_WINS':
        pass
    sleep(500)
def current_pos():
    for i in range(5):
        if display.get_pixel(i, 2) == 9:
            return i
    return 2

Event-Zustands Tabellen: Pong

# ...

state = 'IDLE'

while True:
    # ...
    elif state == 'MOVE_RIGHT':
        if pos == 4:
            if btn_b:
                state = 'MOVE_LEFT'
            else:
                state = 'A_WINS'
        else:
            pos = pos + 1
            set_ball(pos)
    elif state == 'A_WINS':
        display.show('A')
        sleep(1500)
        state = 'IDLE'
    elif state == 'B_WINS':
        display.show('B')
        sleep(1500)
        state = 'IDLE'
    sleep(500)

Ausprobieren: ofi.gbsl.website