Ich habe sowohl beim Quelltext als auch bei der Fehlermeldung nur kopiert und hier eingefügt.
Zum Verständnis:
Ich habe bei der Auswahl der Teams drei Teams ausgewählt und dann mit dem Schnatz bestätigt.
Deine Zeile habe ich geändert, hier also der neue Quelltext:
Code: Alles auswählen
from RPi import GPIO
from queue import Queue, Empty
from termcolor import cprint
import spidev
import time
import random
import sys
import os
GREEN = 4
YELLOW = 3
RED = 27
BLUE = 17
SNATCH = 2
TEAM_LIGHTS = [BLUE, RED, YELLOW, GREEN]
ALL_LIGHTS = [SNATCH] + TEAM_LIGHTS
BUTTONS = {
14: GREEN,
24: YELLOW,
18: RED,
15: BLUE,
23: SNATCH,
}
TEAMS = {
GREEN: ("Slytherin", "green"),
YELLOW: ("Hufflepuff", "yellow"),
RED: ("Gryffindor", "red"),
BLUE: ("Ravenclaw", "blue"),
}
DURATIONS = {
BLUE: (2, "blau", "blue"),
RED: (5, "rot", "red"),
YELLOW: (10, "gelb", "yellow"),
GREEN: (15, "gruen", "green"),
}
LASER_GATES = [
(26, 19), (13,21), (20,16)
]
class Team:
def __init__(self, team_name, color):
self.team_name = team_name
self.color = color
selt.taster = taster
self.points = 0
def show_points(self):
cprint(f"{self.team_name}: {self.points}", color=self.color)
def light_off(pin):
GPIO.setup(pin, GPIO.HIGH)
def light_on(pin):
GPIO.setup(pin, GPIO.LOW)
def play_lights1():
for i in range(3):
light_on(LIGHTS)
time.sleep(0.2)
light_off(LIGHTS)
time.sleep(0.2)
def play_lights2():
for led_pin in TEAM_LIGHTS:
light_on(led_pin)
time.sleep(0.2)
light_off(led_pin)
time.sleep(0.2)
for i in range(3):
light_on(TEAM_LIGHTS)
time.sleep(0.2)
light_off(TEAM_LIGHTS)
time.sleep(0.2)
def play_lights3():
for pin in ALL_LIGHTS:
light_on(pin)
time.sleep(1)
time.sleep(4)
for pin in ALL_LIGHTS:
light_off(pin)
time.sleep(1)
def checkstart():
rings = []
for gate_a. gate_b in LASER_GATES:
rings.append(GPIO.input(gate_a) + GPIO.input(gate_b))
for nr, ring in enumerate(rings, 1):
p = [100, 50, 0][ring]
cprint(f"Status Ring {nr}: {p}%s")
def clear_queue(queue):
try:
while True:
queue.get_nowait()
except Empty:
pass
def select_teams(queue):
light_off(ALL_LIGHTS)
cprint("Bitte waehlen Sie mit den Farben die mitspielenden Teams aus. Bestaetigen Sie ihre Auswahl mit dem Schnatz")
selected = set()
clear_queue(queue)
while True:
color = BUTTONS[queue.get()]
if color == SNATCH:
if selected:
break
# blink lights if no team is selected
play_lights1()
else:
if color in selected:
selected.remove(color)
light_off(color)
else:
selected.add(color)
light_on(color)
light_off(ALL_LIGHTS)
return [
Team(*TEAMS[color])
for color in selected
]
def select_duration():
cprint("Bitte waehlen Sie die Dauer des Spiels aus:")
for duration, name, color in sorted(DURATIONS.values()):
cprint(f"{name} = {duration} Minuten", color=color)
cprint ("Das Spiel wird mit dem Schnatz gestartet")
selected_duration = 0
clear_queue(queue)
while True:
color = BUTTONS[queue.get()]
if color == SNATCH:
if selected_duration != 0:
break
play_lights2()
else:
selected_duration = DURATIONS[color] [0]
light_off(TEAM_LIGHTS)
light_on(color)
return selected_duration
def display_gates(gates):
cprint("Tore", end=" ")
for nr, gate in enumerate(gates, 1):
cprint(f"Tor {nr} ", end=" ")
if gate is None:
cprint("DEFEKT", color="grey", end=" ")
else:
cprint(gate.team_name, color=gate.color, end=" ")
def schnatz_active(teams):
selected = random.choice([None] + teams)
if selected is None:
licht_aus(LICHT_schnatz)
else:
licht_an(LICHT_schnatz)
cprint("Der Schnatz wurde gesichtet! Er kann von folgendem Team gefangen werde:")
cprint(selected.team_name, color=selected.color, end=" ")
return selected
def display_results(teams):
cprint("Das Spiel ist zu Ende! Eine herausragende Leistung von allen Teilnehmern:")
cprint("Ergebnis:")
teams = sort(teams, key=lambda t: t.points, reverse=True)
for team in teams[:3]:
team.show_points()
play_lights3()
def run_game(teams, duration):
schnatz_timer = None
gate_time = 0
start = time.time()
ende = start + duration
while Ture:
akt_zeit = time.time()
if akt_zeit > gate_time:
gates = [
random.choice([None] + teams)
for _ in range (3)
]
display_gates(gates)
gate_time = akt_time + 10
if akt_zeit > ende:
if schnatz_timer is None:
schnatz_sttatus = schnatz_active(teams)
schnatz_timer = akt_zeit + random.randint(1,10)
elif akt_zeit < schnatz_timer: #TODO: and schnatz == FALSE:
break
check_ring = check_gates()
if check_ring !=3:
team = gates[check_ring]
if team is not None:
team.points += 10
team.show_points()
cprint("Spielende")
if schnatz_status is not None:
schnatz_status.points += 150
def initialize(buttons):
queue = Queue()
GPIO.setmode(GPIO.BCM)
for button, led in buttons.items():
GPIO.setup(led, GPIO.OUT)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(button, GPIO.FALLING, queue.put)
for gates in LASER_GATES:
GPIO.setup(gates, GPIO.IN, pull_up_down=GPIO.PUD_UP)
return queue
def main():
try:
queue = initialize(BUTTONS)
while True:
teams = select_teams(queue)
duration = select_duration() * 60
checkstart()
cprint("Am Spiel nehmen die folgenden mannschaften teil:")
for team in teams:
cprint(team.team_name, color=team.color)
run_game(teams, duration)
display_results(teams)
finally:
GPIO.cleanup()
if __name__ == "__main__":
main()
Erneut ein Test mit drei Teams und nach Drücken des Schnatz hier der Fehler:
Traceback (most recent call last):
File "quidditch.py", line 233, in <module>
main()
File "quidditch.py", line 221, in main
teams = select_teams(queue)
File "quidditch.py", line 126, in select_teams
for color in selected
File "quidditch.py", line 126, in <listcomp>
for color in selected
File "quidditch.py", line 49, in __init__
selt.taster = taster
NameError: name 'taster' is not defined
Oh, und wo kann ich ein time.sleep(0.2) setzen, damit ich die Taste für die Teams besser drücken kann? Habe es an mehreren Stellen versucht, doch leider ohne Erfolg.