Habe gerade mit Python angefangen und mich am Anfängerklassiker "Schere-Stein-Papier" versucht.
Es funktioniert zwar prima, ist aber bestimmt Murks. Ich bin für alle Anregungen dankbar. Habe mich gerade ein bisschen in OOP eingelesen und versucht, dasselbe Programm im OOP-Stil zu schreiben. Mit bemerkenswertem Misserfolg. Macht das bei so einem Projekt überhaupt Sinn?
Wie dem auch sei, anbei der Code. Sieger ist, wer drei Runden "Schere-Stein-Papier" gewinnt.
Ps: Falls ihr beim nochmal spielen, ein "PermissionError: [Errno 13]" bekommt, ich musste das Programm auf "chmod 755" setzen.
beste Grüße
Wundertüte
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import tkinter as tk
import os
import sys
liste = ["schere", "stein", "papier"]
Spielerpunkte = 0
Computerpunkte = 0
def spieler_gewinnt():
if Spielerpunkte == 3:
child = tk.Tk()
child.title("Nochmal?")
tk.Label(child, text="Spielende, du hast gewonnen! Noch eine Runde?").pack()
def neustart():
sys.stdout.flush()
os.execv(sys.argv[0], sys.argv)
def schließen():
child.destroy()
root.destroy()
tk.Button(child, text="Ja", command=neustart).pack()
tk.Button(child, text="Nein, beenden", command=schließen).pack()
def computer_gewinnt():
if Computerpunkte == 3:
child = tk.Tk()
child.title("Nochmal?")
tk.Label(child, text="Spielende, ich hab gewonnen! Noch eine Runde?").pack()
def neustart():
sys.stdout.flush()
os.execv(sys.argv[0], sys.argv)
def schließen():
child.destroy()
root.destroy()
tk.Button(child, text="Ja", command=neustart).pack()
tk.Button(child, text="Nein, beenden", command=schließen).pack()
def okbutton():
computerwahl = random.choice(liste)
spielerwahl = e1.get()
if spielerwahl.lower() == computerwahl:
tk.Label(root, text="Meine Wahl: " + computerwahl).pack()
tk.Label(root, text="Unentschieden!").pack()
if spielerwahl.lower() == "schere" and computerwahl == "papier":
tk.Label(root, text="Meine Wahl: " + computerwahl).pack()
tk.Label(root, text="Du gewinnst").pack()
global Spielerpunkte
Spielerpunkte = Spielerpunkte + 1
spieler_gewinnt()
if spielerwahl.lower() == "schere" and computerwahl == "stein":
tk.Label(root, text="Meine Wahl: " + computerwahl).pack()
tk.Label(root, text="Ich gewinne").pack()
global Computerpunkte
Computerpunkte = Computerpunkte + 1
computer_gewinnt()
if spielerwahl.lower() == "papier" and computerwahl == "schere":
tk.Label(root, text="Meine Wahl: " + computerwahl).pack()
tk.Label(root, text="Ich gewinne").pack()
Computerpunkte = Computerpunkte + 1
computer_gewinnt()
if spielerwahl.lower() == "papier" and computerwahl == "stein":
tk.Label(root, text="Meine Wahl: " + computerwahl).pack()
tk.Label(root, text="Du gewinnst").pack()
Spielerpunkte = Spielerpunkte + 1
spieler_gewinnt()
if spielerwahl.lower() == "stein" and computerwahl == "schere":
tk.Label(root, text="Meine Wahl: " + computerwahl).pack()
tk.Label(root, text="Du gewinnst").pack()
Spielerpunkte = Spielerpunkte + 1
spieler_gewinnt()
if spielerwahl.lower() == "stein" and computerwahl == "papier":
tk.Label(root, text="Meine Wahl: " + computerwahl).pack()
tk.Label(root, text="Ich gewinne").pack()
Computerpunkte = Computerpunkte + 1
computer_gewinnt()
root = tk.Tk()
XPOS = 500
YPOS = 200
root.geometry("+%d+%d" % (XPOS, YPOS))
root.title("Schere-Stein-Papier")
tk.Label(root, text="Wähle deine Waffe! Sieger ist, wer 3 Mal gewinnt!").pack()
e1 = tk.Entry()
e1.pack()
tk.Button(root, text="OK", command=lambda: [okbutton()]).pack()
root.bind("<Return>", lambda event: [okbutton()])
root.mainloop()