Code: Alles auswählen
from __future__ import print_function
from termcolor import cprint as print
import RPi.GPIO as GPIO
import spidev
import time
import random
import sys
import os
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LASER_TOR1_A=26
LASER_TOR1_B=19
LASER_TOR2_A=13
LASER_TOR2_B=21
LASER_TOR3_A=20
LASER_TOR3_B=16
LICHT_schnatz = 2
LICHT_gelb = 3
LICHT_gruen = 4
LICHT_blau = 17
LICHT_rot = 27
TASTER_gruen = 14
TASTER_blau = 15
TASTER_rot = 18
TASTER_schnatz = 23
TASTER_gelb = 24
GPIO.setup(LICHT_schnatz, GPIO.OUT)
GPIO.setup(LICHT_gelb, GPIO.OUT)
GPIO.setup(LICHT_gruen, GPIO.OUT)
GPIO.setup(LICHT_blau, GPIO.OUT)
GPIO.setup(LICHT_rot, GPIO.OUT)
GPIO.setup(TASTER_gruen, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(TASTER_blau, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(TASTER_rot, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(TASTER_schnatz, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(TASTER_gelb, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LASER_TOR1_A, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LASER_TOR1_B, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LASER_TOR2_A, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LASER_TOR2_B, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LASER_TOR3_A, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LASER_TOR3_B, GPIO.IN, pull_up_down=GPIO.PUD_UP)
team_huf = 0
team_gry = 0
team_rav = 0
team_sly = 0
team_all = 0
def licht_aus(x):
GPIO.setup(x, GPIO.HIGH)
def licht_an(x):
GPIO.setup(x, GPIO.LOW)
def alles_licht_aus():
licht_aus(LICHT_schnatz)
licht_aus(LICHT_blau)
licht_aus(LICHT_rot)
licht_aus(LICHT_gelb)
licht_aus(LICHT_gruen)
def select_sly(sly):
if (sly == 0):
sly = 1
elif (sly == 1):
sly = 0
return sly
def select_huf(huf):
if (huf == 0):
huf = 1
elif (huf == 1):
huf = 0
return huf
def select_gry(gry):
if (gry == 0):
gry = 1
elif (gry == 1):
gry = 0
return gry
def select_rav(rav):
if (rav == 0):
rav = 1
elif (rav == 1):
rav = 0
return rav
def team_all(rav, gry, huf, sly):
team_all = rav + gry + huf + sly
return team_all
def change_gate1(teamliste):
teamcount = len(teamliste)
r = random.randint(0,teamcount)
if (r==0):
gate1 = "dead"
else:
gate1 = teamliste[r-1]
return gate1
def change_gate2(teamliste):
teamcount = len(teamliste)
r = random.randint(0,teamcount)
if (r==0):
gate2 = "dead"
else:
gate2 = teamliste[r-1]
return gate2
def change_gate3(teamliste):
teamcount = len(teamliste)
r = random.randint(0,teamcount)
if (r==0):
gate3 = "dead"
else:
gate3 = teamliste[r-1]
return gate3
def tor(gate):
if (gate == "rav"):
print("Ravenclaw", color="blue", end=" ")
tor="r"
elif (gate == "gry"):
print("Gryffindor", color="red", end=" ")
tor="g"
elif (gate == "huf"):
print("Hufflepuff", color="yellow", end=" ")
tor="h"
elif (gate == "sly"):
print("Slytherin", color="green", end=" ")
tor="s"
elif (gate =="dead"):
print("DEFEKT", color="grey", end=" ")
tor="d"
return tor
def tore(g1, g2, g3):
print("Tore", end=" ")
print("Tor 1 ", end=" ")
t1=tor(g1)
print("Tor 2 ", end=" ")
t2=tor(g2)
print("Tor 3 ", end=" ")
t3=tor(g3)
torliste = []
torliste.append(t1)
torliste.append(t2)
torliste.append(t3)
return torliste
def lichtauswahl(team_rav, team_gry, team_huf, team_sly):
if (team_sly == 1):
licht_an(LICHT_gruen)
if (team_sly == 0):
licht_aus(LICHT_gruen)
if (team_gry == 1):
licht_an(LICHT_rot)
if (team_gry == 0):
licht_aus(LICHT_rot)
if (team_huf == 1):
licht_an(LICHT_gelb)
if (team_huf == 0):
licht_aus(LICHT_gelb)
if (team_rav == 1):
licht_an(LICHT_blau)
if (team_rav == 0):
licht_aus(LICHT_blau)
def checkstart():
ring1a = GPIO.input(LASER_TOR1_A)
ring1b = GPIO.input(LASER_TOR1_B)
ring2a = GPIO.input(LASER_TOR2_A)
ring2b = GPIO.input(LASER_TOR2_B)
ring3a = GPIO.input(LASER_TOR3_A)
ring3b = GPIO.input(LASER_TOR3_B)
ring1 = 0
ring2 = 0
ring3 = 0
if (ring1a == False):
ring1=ring1+1
if (ring1b == False):
ring1=ring1+1
if (ring2a == False):
ring2=ring2+1
if (ring2b == False):
ring2=ring2+1
if (ring3a == False):
ring3=ring3+1
if (ring3b == False):
ring3=ring3+1
print ("Status Ring 1:")
if (ring1 == 2):
print("100%")
elif (ring1 == 1):
print("50%")
else:
print("0%")
print ("Status Ring 2:")
if (ring2 == 2):
print("100%")
elif (ring2 == 1):
print("50%")
else:
print("0%")
print ("Status Ring 3:")
if (ring3 == 2):
print ("100%")
elif (ring3 == 1):
print ("50%")
else:
print("0%")
def schnatz_active(team_s):
team_c = len(team_s)
team_c = team_c+1
s = random.randint(0,team_c)
if (s==0):
licht_aus(LICHT_schnatz)
status_schnatz="inactive"
else:
licht_an(LICHT_schnatz)
status_schnatz=team_s[s-1]
print("Der Schnatz wurde gesichtet! Er kann von folgendem Team gefangen werden:")
if (status_schnatz == "rav"):
print("Ravenclaw", color="blue", end=" ")
elif (status_schnatz == "gry"):
print("Gryffindor", color="red", end=" ")
elif (status_schnatz == "huf"):
print("Hufflepuff", color="yellow", end=" ")
elif (status_schnatz == "sly"):
print("Slytherin", color="green", end=" ")
return status_schnatz
def checkgates():
ring1a = GPIO.input(LASER_TOR1_A)
ring1b = GPIO.input(LASER_TOR1_B)
ring2a = GPIO.input(LASER_TOR2_A)
ring2b = GPIO.input(LASER_TOR2_B)
ring3a = GPIO.input(LASER_TOR3_A)
ring3b = GPIO.input(LASER_TOR3_B)
ring = 3
if (ring1a == True):
ring = 0
if (ring1b == True):
ring = 0
if (ring2a == False):
ring = 1
if (ring2b == True):
ring = 1
if (ring3b == True):
ring = 2
if (ring != 3):
print("PUNKTE")
print(str(ring))
time.sleep(1)
return ring
def showpoint(show_team):
if (show_team == "rav"):
print("Ravenclaw", color="blue")
print(points_rav)
if (show_team == "gry"):
print("Gryffindor", color="red")
print(points_gry)
if (show_team == "huf"):
print("Hufflepuff", color="yellow")
print(points_huf)
if (show_team == "sly"):
print("Slytherin", color="green")
print(points_sly)
return 0
phase = 1
while True:
alles_licht_aus()
print("Bitte waehlen Sie mit den Farben die mitspielenden Teams aus. Bestaetigen Sie ihre Auswahl mit dem Schnatz")
while phase == 1:
gruen = GPIO.input(TASTER_gruen)
blau = GPIO.input(TASTER_blau)
rot = GPIO.input(TASTER_rot)
schnatz = GPIO.input(TASTER_schnatz)
gelb = GPIO.input(TASTER_gelb)
if (gruen == False):
team_sly = select_sly(team_sly)
lichtauswahl(team_rav, team_gry, team_huf, team_sly)
time.sleep(0.5)
if (blau == False):
team_rav = select_rav(team_rav)
lichtauswahl(team_rav, team_gry, team_huf, team_sly)
time.sleep(0.5)
if (rot == False):
team_gry = select_gry(team_gry)
lichtauswahl(team_rav, team_gry, team_huf, team_sly)
time.sleep(0.5)
if (gelb == False):
team_huf = select_huf(team_huf)
lichtauswahl(team_rav, team_gry, team_huf, team_sly)
time.sleep(0.5)
if (schnatz == False):
team_all = team_all(team_rav, team_gry, team_huf, team_sly)
if (team_all == 0):
lichtauswahl(1, 1, 1, 1)
time.sleep(0.2)
lichtauswahl(0, 0, 0, 0)
time.sleep(0.2)
lichtauswahl(1, 1, 1, 1)
time.sleep(0.2)
lichtauswahl(0, 0, 0, 0)
time.sleep(0.2)
lichtauswahl(1, 1, 1, 1)
time.sleep(0.2)
lichtauswahl(0, 0, 0, 0)
time.sleep(0.2)
else:
phase = 2
time.sleep(0.5)
alles_licht_aus()
print ("Bitte waehlen Sie die Dauer des Spiels aus:")
print ("blau = 2 Minuten", color="blue")
print ("rot = 5 Minuten", color="red")
print ("gelb = 10 Minuten", color="yellow")
print ("gruen = 15 Minuten", color="green")
print ("Das Spiel wird mit dem Schnatz gestartet")
dauer = 0
while phase == 2:
blau = GPIO.input(TASTER_blau)
rot = GPIO.input(TASTER_rot)
gelb = GPIO.input(TASTER_gelb)
gruen = GPIO.input(TASTER_gruen)
schnatz = GPIO.input(TASTER_schnatz)
if (blau == False):
dauer = 2
licht_an(LICHT_blau)
licht_aus(LICHT_rot)
licht_aus(LICHT_gelb)
licht_aus(LICHT_gruen)
time.sleep(0.5)
if (rot == False):
dauer = 5
licht_an(LICHT_rot)
licht_aus(LICHT_blau)
licht_aus(LICHT_gelb)
licht_aus(LICHT_gruen)
time.sleep(0.5)
if (gelb == False):
dauer = 10
licht_an(LICHT_gelb)
licht_aus(LICHT_blau)
licht_aus(LICHT_rot)
licht_aus(LICHT_gruen)
time.sleep(0.5)
if (gruen == False):
dauer = 15
licht_an(LICHT_gruen)
licht_aus(LICHT_blau)
licht_aus(LICHT_rot)
licht_aus(LICHT_gelb)
time.sleep(0.5)
if (schnatz == False):
if (dauer == 0):
licht_an(LICHT_blau)
time.sleep(0.2)
licht_aus(LICHT_blau)
licht_an(LICHT_rot)
time.sleep(0.2)
licht_aus(LICHT_rot)
licht_an(LICHT_gelb)
time.sleep(0.2)
licht_aus(LICHT_gelb)
licht_an(LICHT_gruen)
time.sleep(0.2)
licht_aus(LICHT_gruen)
time.sleep(0.2)
lichtauswahl(1, 1, 1, 1)
time.sleep(0.2)
lichtauswahl(0, 0, 0, 0)
time.sleep(0.2)
lichtauswahl(1, 1, 1, 1)
time.sleep(0.2)
lichtauswahl(0, 0, 0, 0)
time.sleep(0.2)
lichtauswahl(1, 1, 1, 1)
time.sleep(0.2)
lichtauswahl(0, 0, 0, 0)
else:
checkstart()
phase = 3
time.sleep(0.5)
print ("Am Spiel nehmen die folgenden Mannschaften teil:")
teamliste = []
if (team_rav == 1):
print ("Ravenclaw", color="blue")
teamliste.append('rav')
if (team_gry == 1):
print ("Gryffindor", color="red")
teamliste.append('gry')
if (team_huf == 1):
print ("Hufflepuff", color="yellow")
teamliste.append('huf')
if (team_sly == 1):
print ("Slytherin", color="green")
teamliste.append('sly')
start = time.time()
gate_time = time.time()+10
dauer = dauer *60
ende = dauer+start
schnatz_time = start + dauer - 10
schnatz_status = "inactive"
schnatz_timer = time.time()
gate1 = change_gate1(teamliste)
gate2 = change_gate2(teamliste)
gate3 = change_gate3(teamliste)
points_rav = 0
points_gry = 0
points_huf = 0
points_sly = 0
change = 0
endrunde = 0
gateliste=tore(gate1, gate2, gate3)
while phase == 3:
akt_zeit=time.time()
if ((akt_zeit > ende) and (akt_zeit > schnatz_timer)):
endrunde = 1
if (endrunde==1):
schnatz_status = schatz_active(teamliste)
schnatz_timer = random.randint(1,10)
schnatz_timer = time.time()+schnatz_timer
endrunde==0
if (akt_zeit > ende and akt_zeit < schnatz_timer and schnatz == FALSE):
print ("Spielende")
if (schnatz_status=="gry"):
points_gry=points_gry+150
elif (schnatz_status=="sly"):
points_sly=points_sly+150
elif (schnatz_status=="rav"):
points_rav=points_rav+150
elif (schnatz_status=="huf"):
points_huf=points_huf+150
phase=4
check_ring=checkgates()
if (check_ring!=3):
goal=gateliste[check_ring]
if (goal=="rav"):
points_rav = points_rav+10
showpoint(goal)
elif (goal=="sly"):
points_sly = points_sly+10
showpoint(goal)
elif (goal=="huf"):
points_huf = points_huf+10
showpoint(goal)
elif (goal=="gry"):
points_gry=points_gry+10
showpoint(goal)
check_ring=3
if (akt_zeit>gate_time):
gate1 = change_gate1(teamliste)
gate2 = change_gate2(teamliste)
gate3 = change_gate3(teamliste)
gateliste=tore(gate1, gate2, gate3)
gate_time=time.time()+10
if (phase == 4):
print("Das Spiel ist zu Ende! Eine herausragende Leistung von allen Teilnehmern!")
print ("ERGEBNIS:")
final_count=len(teamliste)
final_points = []
if ("rav" in teamliste):
final_points.append(points_rav)
if ("sly" in teamliste):
final_points.append(points_sly)
if ("gry" in teamliste):
final_points.append(points_gry)
if ("huf" in teamliste):
finale_points.append(points_huf)
final_points=sorted(final_points)
if (final_points[0]==points_rav and points_rav!=0):
showpoint(rav)
elif (final_points[0]==points_gry and points_gry!=0):
showpoint(gry)
elif (final_points[0]==points_sly and points_sly!=0):
showpoint(sly)
elif (final_points[0]==points_huf and points_huf!=0):
showpoint(huf)
if (final_points[1]==points_rav and points_rav!=0 and final_count>0):
showpoint(rav)
elif (final_points[1]==points_gry and points_gry!=0 and final_count>0):
showpoint(gry)
elif (final_points[1]==points_sly and points_sly!=0 and final_count>0):
showpoint(sly)
elif (final_points[1]==points_huf and points_huf!=0 and final_count>0):
showpoint(huf)
if (final_points[2]==points_rav and points_rav!=0 and final_count>1):
showpoint(rav)
elif (final_points[2]==points_gry and points_gry!=0 and final_count>1):
showpoint(gry)
elif (final_points[2]==points_sly and points_sly!=0 and final_count>1):
showpoint(sly)
elif (final_points[2]==points_huf and points_huf!=0 and final_count>1):
showpoint(huf)
if (final_points[3]==points_rav and points_rav!=0 and final_count>2):
showpoint(rav)
elif (final_points[3]==points_gry and points_gry!=0 and final_count>2):
showpoint(gry)
elif (final_points[3]==points_sly and points_sly!=0 and final_count>2):
showpoint(sly)
elif (final_points[3]==points_huf and points_huf!=0 and final_count>2):
showpoint(huf)
licht_an(LICHT_schnatz)
time.sleep(1)
licht_an(LICHT_gelb)
time.sleep(1)
licht_an(LICHT_gruen)
time.sleep(1)
licht_an(LICHT_blau)
time.sleep(1)
licht_an(LICHT_rot)
print('AN')
time.sleep(5)
licht_aus(LICHT_schnatz)
time.sleep(1)
licht_aus(LICHT_gelb)
time.sleep(1)
licht_aus(LICHT_gruen)
time.sleep(1)
licht_aus(LICHT_blau)
time.sleep(1)
licht_aus(LICHT_rot)
print('AUS')
time.sleep(1)