Bladeren bron

Weitere Kommentare zum besseren Verständnis

ateubert 7 jaren geleden
bovenliggende
commit
118c02e591
1 gewijzigde bestanden met toevoegingen van 21 en 14 verwijderingen
  1. 21 14
      maze_fertig.py

+ 21 - 14
maze_fertig.py

15
 #IMPLEMENTIERUNG IN TKINTER
15
 #IMPLEMENTIERUNG IN TKINTER
16
 class Application(tk.Frame):
16
 class Application(tk.Frame):
17
 
17
 
18
-    def __init__(self, width=21, height=21, size=10):								
19
-        tk.Frame.__init__(self)
18
+    def __init__(self, width=21, height=21, size=10):					#Erstellung des Rahmens vom Maze			
19
+        tk.Frame.__init__(self)								#hier werden alle weiteren Funktionen initiiert	
20
         self.maze = Maze(width, height)
20
         self.maze = Maze(width, height)
21
         self.size = size
21
         self.size = size
22
         self.steps = 0
22
         self.steps = 0
33
         self.status = tk.Label(self)
33
         self.status = tk.Label(self)
34
         self.status.grid()
34
         self.status.grid()
35
 
35
 
36
-    def draw_maze(self):
36
+    def draw_maze(self):								#das unten generierte Maze wird hier nun grafisch umgesetzt
37
         for i, row in enumerate(self.maze.maze):
37
         for i, row in enumerate(self.maze.maze):
38
             for j, col in enumerate(row):
38
             for j, col in enumerate(row):
39
                 x0 = j * self.size
39
                 x0 = j * self.size
41
                 x1 = x0 + self.size
41
                 x1 = x0 + self.size
42
                 y1 = y0 + self.size
42
                 y1 = y0 + self.size
43
                 color = self.get_color(x=j, y=i)
43
                 color = self.get_color(x=j, y=i)
44
-                id = self.canvas.create_rectangle(x0, y0, x1, y1, width=0, fill=color)
44
+                id = self.canvas.create_rectangle(x0, y0, x1, y1, width=0, fill=color)	#rectangle=Rechteck. Das Maze besteht aus Rechtecken(Quadraten)
45
                 if self.maze.start_cell == (j, i):
45
                 if self.maze.start_cell == (j, i):
46
                     self.cell = id
46
                     self.cell = id
47
 
47
 
48
-        self.canvas.tag_raise(self.cell) 
48
+        self.canvas.tag_raise(self.cell) 						#self.cell wird an die Spitze des canvas stacks gehoben
49
         self.status.config(text='minimale Anzahl Schritte: %d' % self.maze.steps)
49
         self.status.config(text='minimale Anzahl Schritte: %d' % self.maze.steps)
50
 
50
 
51
-    def create_events(self):
52
-        self.canvas.bind_all('<KeyPress-Up>', self.move_cell)
53
-        self.canvas.bind_all('<KeyPress-Down>', self.move_cell)
51
+
52
+#STEUERUNG
53
+    def create_events(self):								#bind <- Beteatigung einer Taste ruft einen callback hervor. 
54
+        self.canvas.bind_all('<KeyPress-Up>', self.move_cell)				#der callback ist die Bewegung des Quadrats
55
+        self.canvas.bind_all('<KeyPress-Down>', self.move_cell)				# bind bindet ein event an einen callback
54
         self.canvas.bind_all('<KeyPress-Left>', self.move_cell)
56
         self.canvas.bind_all('<KeyPress-Left>', self.move_cell)
55
         self.canvas.bind_all('<KeyPress-Right>', self.move_cell)
57
         self.canvas.bind_all('<KeyPress-Right>', self.move_cell)
56
 
58
 
57
     def move_cell(self, event):
59
     def move_cell(self, event):
58
-        if event.keysym == 'Up':
59
-            if self.check_move(0, -1):
60
+        if event.keysym == 'Up':							#keysym ermoeglicht nur events ueber das Keyboard
61
+            if self.check_move(0, -1):							#es sind nur Eingaben uebers Keyboard moeglich
60
                 self.canvas.move(self.cell, 0, -self.size)
62
                 self.canvas.move(self.cell, 0, -self.size)
61
                 self.steps += 1
63
                 self.steps += 1
62
         if event.keysym == 'Down':
64
         if event.keysym == 'Down':
76
         self.status.config(text='Schritte: %d/%d' % args)
78
         self.status.config(text='Schritte: %d/%d' % args)
77
         self.check_status()
79
         self.check_status()
78
 
80
 
81
+
82
+#UEBERPRUEFUNG DER KOORDINATEN
79
     def check_move(self, x, y):
83
     def check_move(self, x, y):
80
         x0, y0 = self.get_cell_coords()
84
         x0, y0 = self.get_cell_coords()
81
         x1 = x0 + x
85
         x1 = x0 + x
83
         return self.maze.maze[y1][x1] == 0
87
         return self.maze.maze[y1][x1] == 0
84
 
88
 
85
     def get_cell_coords(self):
89
     def get_cell_coords(self):
86
-        position = self.canvas.coords(self.cell)
90
+        position = self.canvas.coords(self.cell)					#coords gibt die Koordiaten der sich bewegenden Zelle(dir) wieder
87
         x = int(position[0] / self.size)
91
         x = int(position[0] / self.size)
88
         y = int(position[1] / self.size)
92
         y = int(position[1] / self.size)
89
         return (x, y)
93
         return (x, y)
93
             args = (self.steps, self.maze.steps)
97
             args = (self.steps, self.maze.steps)
94
             self.status.config(text='Resultat: %d/%d Schritte!' % args)
98
             self.status.config(text='Resultat: %d/%d Schritte!' % args)
95
 
99
 
100
+
101
+#FARBFESTLEGUNG
96
     def get_color(self, x, y):
102
     def get_color(self, x, y):
97
         if self.maze.start_cell == (x, y):
103
         if self.maze.start_cell == (x, y):
98
             return 'red'
104
             return 'red'
101
         if self.maze.maze[y][x] == 1:
107
         if self.maze.maze[y][x] == 1:
102
             return 'black'
108
             return 'black'
103
 
109
 
110
+
104
 #GENERIERUNG DES LABYRINTHS
111
 #GENERIERUNG DES LABYRINTHS
105
 class Maze(object):
112
 class Maze(object):
106
 
113
 
107
-    def __init__(self, width=21, height=21, exit_cell=(19,1), start_cell=(1,19)):
114
+    def __init__(self, width=21, height=21, exit_cell=(19,1), start_cell=(1,19)):	#Initierung des Rahmens des Maze, sowie Start und Ziel Festlegung
108
         self.width = width
115
         self.width = width
109
         self.height = height
116
         self.height = height
110
         self.exit_cell = exit_cell
117
         self.exit_cell = exit_cell
130
                 self._visit_cell(neighbor, depth+1)
137
                 self._visit_cell(neighbor, depth+1)
131
         self._update_start_cell(cell, depth)
138
         self._update_start_cell(cell, depth)
132
 
139
 
133
-    def _get_neighbors(self, cell):
134
-        """
140
+    def _get_neighbors(self, cell):							#Zur Generierung des Maze wird der Depth-first-search Algorithmus verwendet 
141
+        """										
135
         Beispiel:
142
         Beispiel:
136
           Die Nachbarzellen von a sind b
143
           Die Nachbarzellen von a sind b
137
           # # # # # # #     # # # # # # #
144
           # # # # # # #     # # # # # # #