Browse code

Optimized and commented

haywalk authored on 03/12/2021 01:11:52
Showing 1 changed files

... ...
@@ -80,13 +80,17 @@ class Cell:
80 80
 
81 81
     def draw(self, win):
82 82
         '''
83
-        Draw the cell
83
+        Draw the cell to a window
84 84
         '''
85 85
         if self.is_living:
86 86
             pygame.draw.rect(win, (255, 255, 0), (self.col * 10, self.row * 10, 10, 10))
87 87
         else:
88 88
             pygame.draw.rect(win, (0, 0, 0), (self.col * 10, self.row * 10, 10, 10))
89 89
 
90
+'''
91
+Setup
92
+'''
93
+
90 94
 # Get number of rows and columns
91 95
 dimensions = int(input('Number of rows and columns: '))
92 96
 delay = int(input('Delay between cycles (ms): '))
... ...
@@ -111,7 +115,10 @@ for row in range(dimensions):
111 115
     # Add row to list of rows
112 116
     cell_array.append(this_row)
113 117
 
114
-# Main loop
118
+'''
119
+Main loop
120
+'''
121
+
115 122
 while True:
116 123
     # Check for quit
117 124
     for event in pygame.event.get():
... ...
@@ -121,19 +128,15 @@ while True:
121 128
     # Clear screen
122 129
     screen.fill((0, 0, 0))
123 130
 
124
-    # Draw each cell
131
+    # Draw each cell and count neighbours
125 132
     for row in cell_array:
126 133
         for cell in row:
127 134
             cell.draw(screen)
135
+            cell.get_neighbours(cell_array)
128 136
 
129 137
     # Update the screen
130 138
     pygame.display.update()
131 139
 
132
-    # Make each cell count its neighbours
133
-    for row in cell_array:
134
-        for cell in row:
135
-            cell.get_neighbours(cell_array)
136
-
137 140
     # Make each cell evolve
138 141
     for row in cell_array:
139 142
         for cell in row: