Procházet zdrojové kódy

Weitere Kommentare zum besseren Verständnis

ateubert %!s(int64=7) %!d(string=před) roky
rodič
revize
118c02e591
1 změnil soubory, kde provedl 21 přidání a 14 odebrání
  1. 21 14
      maze_fertig.py

+ 21 - 14
maze_fertig.py

@@ -15,8 +15,8 @@ import sys
15 15
 #IMPLEMENTIERUNG IN TKINTER
16 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 20
         self.maze = Maze(width, height)
21 21
         self.size = size
22 22
         self.steps = 0
@@ -33,7 +33,7 @@ class Application(tk.Frame):
33 33
         self.status = tk.Label(self)
34 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 37
         for i, row in enumerate(self.maze.maze):
38 38
             for j, col in enumerate(row):
39 39
                 x0 = j * self.size
@@ -41,22 +41,24 @@ class Application(tk.Frame):
41 41
                 x1 = x0 + self.size
42 42
                 y1 = y0 + self.size
43 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 45
                 if self.maze.start_cell == (j, i):
46 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 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 56
         self.canvas.bind_all('<KeyPress-Left>', self.move_cell)
55 57
         self.canvas.bind_all('<KeyPress-Right>', self.move_cell)
56 58
 
57 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 62
                 self.canvas.move(self.cell, 0, -self.size)
61 63
                 self.steps += 1
62 64
         if event.keysym == 'Down':
@@ -76,6 +78,8 @@ class Application(tk.Frame):
76 78
         self.status.config(text='Schritte: %d/%d' % args)
77 79
         self.check_status()
78 80
 
81
+
82
+#UEBERPRUEFUNG DER KOORDINATEN
79 83
     def check_move(self, x, y):
80 84
         x0, y0 = self.get_cell_coords()
81 85
         x1 = x0 + x
@@ -83,7 +87,7 @@ class Application(tk.Frame):
83 87
         return self.maze.maze[y1][x1] == 0
84 88
 
85 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 91
         x = int(position[0] / self.size)
88 92
         y = int(position[1] / self.size)
89 93
         return (x, y)
@@ -93,6 +97,8 @@ class Application(tk.Frame):
93 97
             args = (self.steps, self.maze.steps)
94 98
             self.status.config(text='Resultat: %d/%d Schritte!' % args)
95 99
 
100
+
101
+#FARBFESTLEGUNG
96 102
     def get_color(self, x, y):
97 103
         if self.maze.start_cell == (x, y):
98 104
             return 'red'
@@ -101,10 +107,11 @@ class Application(tk.Frame):
101 107
         if self.maze.maze[y][x] == 1:
102 108
             return 'black'
103 109
 
110
+
104 111
 #GENERIERUNG DES LABYRINTHS
105 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 115
         self.width = width
109 116
         self.height = height
110 117
         self.exit_cell = exit_cell
@@ -130,8 +137,8 @@ class Maze(object):
130 137
                 self._visit_cell(neighbor, depth+1)
131 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 142
         Beispiel:
136 143
           Die Nachbarzellen von a sind b
137 144
           # # # # # # #     # # # # # # #