ich möchte das Spiel Memory als Programmierübung programmieren, dabei soll das Spiel sich nicht vom "reallife" Spiel unterscheiden. Wenn ich einen Button drücke soll das Bild auf ihm geändert werden und wenn ich
ihn nochmal drücke soll das Rückenbild wieder angezeigt werden. Ich habe die Zeilen um die es sich handelt mit ? markiert, hab nur zum Verständnis den ganzen Code Kopiert. Ich weiß nicht ob ich Logikfehler habe
oder was sonst das Problem sein könnte, habe es auch schon ohne Klasse in einer Funktion probiert, weil ich das erste mal mit Klassen arbeite.
Ich würde mich über Rückmeldung freuen,
Grüße Troxon
Code: Alles auswählen
import tkinter as tk
root = tk.Tk()
root.geometry("850x850")
root.config(background="black")
apfel_image = tk.PhotoImage(file="ApfelBild.png")
astronaut_image = tk.PhotoImage(file="astronaut.png")
bananen_image = tk.PhotoImage(file="BananenBild.png")
deutsche_post_image = tk.PhotoImage(file="DeutschePostLogo.png")
kiwi_image = tk.PhotoImage(file="KiwiBild.png")
science_for_kids_image = tk.PhotoImage(file="ScienceForKidsBild.png")
weißer_hase_image = tk.PhotoImage(file="WeißerHase.png")
world_of_warcraft_image = tk.PhotoImage(file="WorldOfWarcraftLogo.png")
ruecken_image = tk.PhotoImage(file="KartenRuekseite.png") # Rückseitenbild
# Bild_Liste mit 16 Bildern jedes zweimal
bild_liste = [apfel_image, apfel_image, astronaut_image, astronaut_image, bananen_image, bananen_image,
deutsche_post_image, deutsche_post_image,
kiwi_image, kiwi_image, science_for_kids_image, science_for_kids_image, weißer_hase_image,
weißer_hase_image, world_of_warcraft_image,
world_of_warcraft_image]
class button_erzeugung(tk.Button):
def __init__(self, bild):
self.bild = bild
tk.Button.__init__(self, image=ruecken_image, borderwidth=0, command=lambda: self.bild_wechsel(bild))
def bild_wechsel(function_button, bild): ?
if function_button["image"] == ruecken_image: ?
function_button.config(image=bild) ?
else: ?
function_button.config(image=ruecken_image) ?
# Erstellung von 16 Objekten mit Bildern aus der bild_liste
button_0 = button_erzeugung(bild_liste[0])
button_0.place(x=10, y=10)
button_1 = button_erzeugung(bild_liste[1])
button_1.place(x=220, y=10)
button_2 = button_erzeugung(bild_liste[2])
button_2.place(x=430, y=10)
button_3 = button_erzeugung(bild_liste[3])
button_3.place(x=640, y=10)
button_4 = button_erzeugung(bild_liste[4])
button_4.place(x=10, y=220)
button_5 = button_erzeugung(bild_liste[5])
button_5.place(x=220, y=220)
button_6 = button_erzeugung(bild_liste[6])
button_6.place(x=430, y=220)
button_7 = button_erzeugung(bild_liste[7])
button_7.place(x=640, y=220)
button_8 = button_erzeugung(bild_liste[8])
button_8.place(x=10, y=430)
button_9 = button_erzeugung(bild_liste[9])
button_9.place(x=220, y=430)
button_10 = button_erzeugung(bild_liste[10])
button_10.place(x=430, y=430)
button_11 = button_erzeugung(bild_liste[11])
button_11.place(x=640, y=430)
button_12 = button_erzeugung(bild_liste[12])
button_12.place(x=10, y=640)
button_13 = button_erzeugung(bild_liste[13])
button_13.place(x=220, y=640)
button_14 = button_erzeugung(bild_liste[14])
button_14.place(x=430, y=640)
button_15 = button_erzeugung(bild_liste[15])
button_15.place(x=640, y=640)
root.tk.mainloop()