#! /usr/bin/python # -*- coding: iso-8859-15 -*- from Tkinter import * import random # hier könnten noch Parameter für Kantenlänge und Farbkombination rein class prog(Tk): def __init__(self): Tk.__init__(self) self.allLabels = {} # hier kommen die Tk-Labels rein, wie zweidimensionales Array organisiert self.liste = [] # Liste der Labelbeschriftungen self.mix() # erstmal Beschriftungen erstellen und dann mischen for j in range(1, 5): f = Frame(self) f.pack(side=TOP) for i in range(1, 5): l = Label(f, bd=3, relief = RAISED, text=self.liste[(j-1)*4+i-1], width = 13, pady=40) l.pack(side=LEFT) self.allLabels[(j, i)] = l self.allLabels[(4, 4)].config(relief = SUNKEN, text = ' ') for i in self.allLabels.keys(): self.allLabels[i].bind('', self.zug) def mix(self): for i in range(1, 4 * 4): # Füllen der Liste self.liste.append(str(i)) self.liste.append('0') # die '0' steht für das Loch for i in range(1, 201): # nun mischen, aber so, dass das Spiel auch wieder glatt aufgeht a = random.randint(0, 4 * 4 - 4) # Loch bleibt hinten self.liste[a], self.liste[a + 2] = self.liste[a + 2], self.liste[a] def zug(self, event): # auf welche Position wurde geklickt for i in self.allLabels.keys(): if event.widget == self.allLabels[i]: aktiv = i if self.allLabels[i].cget('text') == ' ': loch = i if self.ifnachbar(aktiv, loch): self.tausch(aktiv, loch) else: self.ifreihe(aktiv, loch) def tausch(self, aktiv, loch): self.allLabels[loch].config(text = self.allLabels[aktiv].cget('text')) self.allLabels[loch].config(relief = RAISED) self.allLabels[aktiv].config(relief = SUNKEN) self.allLabels[aktiv].config(text = ' ') def ifnachbar(self, pos1, pos2): # testet, ob die pos neben dem Loch liegt''' result = False if (abs(pos1[0] - pos2[0]) < 2) and (abs(pos1[1] - pos2[1]) < 2): if pos1[0] == pos2[0] or pos1[1] == pos2[1]: if pos1 <> pos2: result = True return result def ifreihe(self, aktiv, loch): if aktiv <> loch: diff = 1 if (aktiv[0] - loch[0] == 0): if (aktiv[1] < loch[1]): diff = -1 for i in range (loch[1] + diff, aktiv[1] + diff, diff): p = aktiv[0], i self.tausch(p, loch) loch = p if (aktiv[1] - loch[1] == 0): if (aktiv[0] < loch[0]): diff = -1 for i in range (loch[0] + diff, aktiv[0] + diff, diff): p = i, aktiv[1] self.tausch(p, loch) loch = p my = prog() my.title('Schiebefax') my.geometry('%dx%d+%d+%d' % (400, 400, 300,150)) my.resizable(0, 0) # verhindert nur das Verkleinern my.maxsize(400, 400) my.mainloop()