Es soll ein Minesweeper Spiel werden und ich wollte es komplett ohne Tutorial selbst probieren deshalb ist es sehr chaotisch, da es mein erstes Projekt ist
Code: Alles auswählen
import tkinter
import random
import time
from tkinter import messagebox
from functools import partial
# Fenster erschaffen
root = tkinter.Tk()
root.title("Minesweeper")
root.minsize(width=700, height=260)
#Richtige Zählen
Right = tkinter.IntVar()
Right.set(0)
richtige = int()
#random
random.seed()
Bomb = random.randint(2,2)
#Spiel schließen Def VERLIEREN
def destr():
if messagebox.showinfo("Exit", "Sie haben verloren, drücken Sie Ok um das Spiel zu beenden"):
root.destroy()
#Spiel schließen Def GEWINNEN
while richtige == 8:
print(richtige)
messagebox.showinfo("Exit", "Herzlichen Glückwunsch Sie haben gewonnen")
root.destroy()
#Bomben abfragen
def Bomben1():
global richtige
if Bomb == 1:
print("Game Over")
Button1.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww1 = tkinter.Label(root, image=Bild2)
ww1.image = Bild2
ww1.place(x=0, y=0)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button1.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w1 = tkinter.Label(root, image=Bild1)
w1.image = Bild1
w1.place(x=0, y=0)
richtige = richtige + 1
print(richtige)
def Bomben2():
global richtige
if Bomb == 2:
print("Game Over")
Button2.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww2 = tkinter.Label(root, image=Bild2)
ww2.image = Bild2
ww2.place(x=80, y=0)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button2.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w2 = tkinter.Label(root, image=Bild1)
w2.image = Bild1
w2.place(x=80, y=0)
richtige = richtige + 1
print(richtige)
def Bomben3():
global richtige
if Bomb == 3:
print("Game Over")
Button3.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww3 = tkinter.Label(root, image=Bild2)
ww3.image = Bild2
ww3.place(x=160, y=0)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button3.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w3 = tkinter.Label(root, image=Bild1)
w3.image = Bild1
w3.place(x=160, y=0)
richtige = richtige + 1
print(richtige)
def Bomben4():
global richtige
if Bomb == 4:
print("Game Over")
Button4.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww4 = tkinter.Label(root, image=Bild2)
ww4.image = Bild2
ww4.place(x=0, y=87)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button4.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w4 = tkinter.Label(root, image=Bild1)
w4.image = Bild1
w4.place(x=0, y=87)
richtige = richtige + 1
print(richtige)
def Bomben5():
global richtige
if Bomb == 5:
print("Game Over")
Button5.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww5 = tkinter.Label(root, image=Bild2)
ww5.image = Bild2
ww5.place(x=80, y=87)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button5.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w5 = tkinter.Label(root, image=Bild1)
w5.image = Bild1
w5.place(x=80, y=87)
richtige = richtige + 1
print(richtige)
def Bomben6():
global richtige
if Bomb == 6:
print("Game Over")
Button6.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww6 = tkinter.Label(root, image=Bild2)
ww6.image = Bild2
ww6.place(x=160, y=87)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button6.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w6 = tkinter.Label(root, image=Bild1)
w6.image = Bild1
w6.place(x=160, y=87)
richtige = richtige + 1
print(richtige)
def Bomben7():
global richtige
if Bomb == 7:
print("Game Over")
Button7.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww7 = tkinter.Label(root, image=Bild2)
ww7.image = Bild2
ww7.place(x=0, y=174)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button7.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w7 = tkinter.Label(root, image=Bild1)
w7.image = Bild1
w7.place(x=0, y=174)
richtige = richtige + 1
print(richtige)
def Bomben8():
global richtige
if Bomb == 8:
print("Game Over")
Button8.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww8 = tkinter.Label(root, image=Bild2)
ww8.image = Bild2
ww8.place(x=80, y=174)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button8.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w8 = tkinter.Label(root, image=Bild1)
w8.image = Bild1
w8.place(x=80, y=174)
richtige = richtige + 1
print(richtige)
def Bomben9():
global richtige
if Bomb == 9:
print("Game Over")
Button9.config(state= tkinter.DISABLED)
Bild2 = tkinter.PhotoImage(file="Bombe2.png")
ww9 = tkinter.Label(root, image=Bild2)
ww9.image = Bild2
ww9.place(x=160, y=174)
destr()
else:
print("Glück gehabt")
Right.set(Right.get()+1)
Button9.config(state= tkinter.DISABLED)
root.update()
Bild1 = tkinter.PhotoImage(file="wiese.png")
w5 = tkinter.Label(root, image=Bild1)
w5.image = Bild1
w5.place(x=160, y=174)
richtige = richtige + 1
print(richtige)
# Buttons erschaffen
Button1 = tkinter.Button(root, text="", command=Bomben1, height = 5, width = 10)
Button1.grid(row=1, column=0)
Button2 = tkinter.Button(root, text="", command=Bomben2, height = 5, width = 10)
Button2.grid(row=1, column=1)
Button3 = tkinter.Button(root, text="", command=Bomben3, height = 5, width = 10)
Button3.grid(row=1, column=2)
Button4 = tkinter.Button(root, text="", command=Bomben4, height = 5, width = 10)
Button4.grid(row=2, column=0)
Button5 = tkinter.Button(root, text="", command=Bomben5, height = 5, width = 10)
Button5.grid(row=2, column=1)
Button6 = tkinter.Button(root, text="", command=Bomben6, height = 5, width = 10)
Button6.grid(row=2, column=2)
Button7 = tkinter.Button(root, text="", command=Bomben7, height = 5, width = 10)
Button7.grid(row=3, column=0)
Button8 = tkinter.Button(root, text="", command=Bomben8, height = 5, width = 10)
Button8.grid(row=3, column=1)
Button9 = tkinter.Button(root, text="", command=Bomben9, height = 5, width = 10)
Button9.grid(row=3, column=2)
#Labels
Headline = tkinter.Label(root, text="Minesweeper", font="Courier 16 bold")
Headline.place(x=420,y=15)
Infos = tkinter.Label(root, text="In 9 Feldern befindet sich 1 Bombe das Ziel ist,")
Infos.place(x=380, y=35)
Infos2 = tkinter.Label(root, text="soviel Felder wie möglich zu erraten ohne die Bombe zu treffen")
Infos2.place(x=330 , y=55)
Entschärft = tkinter.Label(root, textvariable = Right, font="Courier 16 bold")
Entschärft.place(x=480, y=85)
ent = tkinter.Label(root, text="Felder entschärft")
ent.place(x=440 , y=120)
root.mainloop()