In diesem Projekt ging es darum, eine kleine Glücksspielapp zu schreiben.
Das Programm erstellt ein Fenster, in dem drei Label für die Räder, ein Label für die Anzeige, ob man gewonnen hat, ein Butten zum drehen und ein Label, dass das Guthaben anzeigt. Wenn man den “drehen” Button drückt, ändern die Label schnell ihre Farben. Dann kommen sie von links nach rechts nacheinander zum stehen. Zum Schluss wird überprüft, ob alle oder zwei gleiche Label nebeneinander gleich sind und entsprechend Geld zum Guthaben hinzugefügt.
from tkinter import Tk, Label, Button
import random as r
from time import sleep
from threading import Thread
WIDTH = 300
HEIGHT = 300
NUM_WHEELS = 3
SPIN_DELAY = 0.025
SPIN_COUNTER = 30 #wie oft darf sich jedes Rad mehr als das davor drehen
spin_running = False
num_same_wheels = 0
#Geld
cash = 1000
cost_one_game = 25
#Die Farben die die Raeder am ende haben
color_result = [None, None, None]
color_label = []
colors = ["blue", "royal blue", "green", "yellow", "red", "purple", "grey"]
#Dreht die angegebenne Menge Raeder x mal
def spin_wheels(how_much_wheels, how_much_times):
for i_1 in range(how_much_times):
for i_2 in range(1, how_much_wheels):
i_2 -= i_2 * 2
ran_color = r.choice(colors)
color_label[i_2]["bg"] = ran_color
color_result[i_2] = ran_color
time.sleep(SPIN_DELAY)
#Funktion zum Drehen: zufallsfarben fuer die Label
def spin():
global spin_running
for i in range(NUM_WHEELS):
spin_wheels(NUM_WHEELS-i+1, SPIN_COUNTER)
time.sleep(0.2)
spin_running = False
check_if_win()
#Ueberpruefen ob was gewonnen wurde
def check_if_win():
global cash, cost_one_game, num_same_wheels, win_info, color_result
num_same_wheels = 0
#Keins gleich
last_color = None
for i in color_result:
if last_color == None:
last_color == i
elif color_result[i] != last_color:
last_color = i
else:
break
#Zwei nebeneinanderliegende gleich
last_color = None
for i in color_result:
if last_color == None:
last_color = i
elif i == last_color:
num_same_wheels = 2
break
else:
last_color = i
#Alle drei gleich
last_color = None
counter = 0
for i in color_result:
counter += 1
if counter == 1:
last_color = i
elif counter == 2:
if last_color != i:
break
else:
last_color = i
elif counter == 3:
if i == last_color:
num_same_wheels = NUM_WHEELS
else:
break
#Label beschreiben und Gewinn vergeben
if num_same_wheels == 2:
win_cash = cost_one_game*2
string_to_show = str(win_cash) + " gewonnen!"
win_info["text"] = string_to_show
cash += win_cash
elif num_same_wheels == NUM_WHEELS:
win_cash = cost_one_game*8
string_to_show = str(win_cash) + " gewonnen!"
win_info["text"] = string_to_show
cash += win_cash
elif num_same_wheels == 0:
win_cash = 0
win_info["text"] = "Naechstes Mal klappts bestimmt"
#Cash anzeige Label updaten
cash_info_text = "Guthaben: " + str(cash)
cash_info["text"] = cash_info_text
def pressed_spin_button ():
global cash, spin_running, win_info
if spin_running == False:
if cash >= cost_one_game:
cash -= cost_one_game
#Cash anzeige Label updaten
cash_info_text = "Guthaben: " + str(cash)
cash_info["text"] = cash_info_text
if spin_running == False:
win_info["text"] = "Spinning..."
spin_running = True
t_spin = Thread(target=spin)
t_spin.start()
#update_widgets()
else:
win_info["text"] = "Kein Guthaben mehr..."
if __name__ == "__main__":
main_window = Tk()
main_window.title("Einarmiger Bandit")
win_size_str = str(WIDTH) + "x" + str(HEIGHT)
main_window.geometry(win_size_str)
main_window.resizable(0, 0)
#Was gewonnen? Label erstellen
win_info_text="Du hast noch nichts gewonen"
win_info = Label(text=win_info_text, anchor="center")
win_info.place(x=WIDTH/100*50-len(win_info_text)*2.5, y=HEIGHT/100*50)
#"Drehen"-Knopf erstellen
spin_button = Button(text="Drehen!", command=pressed_spin_button)
spin_button.place(x=WIDTH/100*50-20, y=HEIGHT/100*70)
# Farben-Label erstellen
for i in range(NUM_WHEELS):
show_string = " "
color_lab = Label(main_window, text=show_string)
color_lab.place(x=WIDTH/NUM_WHEELS*i+WIDTH/NUM_WHEELS/4, y=HEIGHT/100*10)
color_label.append(color_lab)
#Guthaben
cash_info_text = "Guthaben: " + str(cash)
cash_info = Label(text=cash_info_text)
cash_info.place(x=WIDTH/100*50-35, y=HEIGHT/100*80)
main_window.mainloop()