8
8
typing:
9
9
python -m arcade.examples.conway_alpha
10
10
"""
11
+
11
12
import arcade
12
13
import random
13
14
35
36
ALPHA_OFF = 0
36
37
37
38
38
- def create_grids ():
39
+ def create_grids (
40
+ cell_size : tuple [int , int ] = (CELL_WIDTH , CELL_HEIGHT ), cell_margin : int = CELL_MARGIN
41
+ ):
39
42
"""
40
43
Create a 2D and 1D grid of sprites. We use the 1D SpriteList for drawing,
41
44
and the 2D list for accessing via grid. Both lists point to the same set of
@@ -49,20 +52,35 @@ def create_grids():
49
52
# in grid_sprite_list, just in a 2d manner.
50
53
grid_sprites_two_dim = []
51
54
55
+ # Calculate values we'll re-use below
56
+ cell_width , cell_height = cell_size
57
+ half_width = cell_width // 2
58
+ half_height = cell_height // 2
59
+
60
+ x_step = cell_width + cell_margin
61
+ y_step = cell_height + cell_margin
62
+
63
+ center_offset_x = half_width + cell_margin
64
+ center_offset_y = half_height + cell_margin
65
+
66
+ shared_kwargs = dict (
67
+ # Fit the cell into the box
68
+ radius = min (half_width , half_height ),
69
+ color = ALIVE_COLOR ,
70
+ soft = True ,
71
+ )
72
+
52
73
# Create a list of sprites to represent each grid location
53
74
for row in range (ROW_COUNT ):
54
75
grid_sprites_two_dim .append ([])
55
76
56
77
for column in range (COLUMN_COUNT ):
78
+ # Position the sprite
79
+ x = column * x_step + center_offset_x
80
+ y = row * y_step + center_offset_y
57
81
58
82
# Make the sprite as a soft circle
59
- sprite = arcade .SpriteCircle (CELL_WIDTH // 2 , ALIVE_COLOR , soft = True )
60
-
61
- # Position the sprite
62
- x = column * (CELL_WIDTH + CELL_MARGIN ) + (CELL_WIDTH / 2 + CELL_MARGIN )
63
- y = row * (CELL_HEIGHT + CELL_MARGIN ) + (CELL_HEIGHT / 2 + CELL_MARGIN )
64
- sprite .center_x = x
65
- sprite .center_y = y
83
+ sprite = arcade .SpriteCircle (center_x = x , center_y = y , ** shared_kwargs )
66
84
67
85
# Add the sprite to both lists
68
86
grid_sprites_one_dim .append (sprite )
@@ -72,7 +90,7 @@ def create_grids():
72
90
73
91
74
92
def randomize_grid (grid : arcade .SpriteList ):
75
- """ Randomize the grid to alive/dead """
93
+ """Randomize the grid to alive/dead"""
76
94
for cell in grid :
77
95
pick = random .randrange (2 )
78
96
if pick :
@@ -106,24 +124,24 @@ def __init__(self):
106
124
randomize_grid (self .layers_grid_sprites_one_dim [0 ])
107
125
108
126
def reset (self ):
109
- """ Reset the grid """
127
+ """Reset the grid"""
110
128
randomize_grid (self .layers_grid_sprites_one_dim [0 ])
111
129
112
130
def on_draw (self ):
113
- """ Render the screen. """
131
+ """Render the screen."""
114
132
# Clear all pixels in the window
115
133
self .clear ()
116
134
self .layers_grid_sprites_one_dim [0 ].draw ()
117
135
118
136
def on_key_press (self , symbol : int , modifiers : int ):
119
- """ Handle key press events """
137
+ """Handle key press events"""
120
138
if symbol == arcade .key .SPACE :
121
139
self .reset ()
122
140
elif symbol == arcade .key .ESCAPE :
123
141
self .window .close ()
124
142
125
143
def on_update (self , delta_time : float ):
126
- """ Update the grid """
144
+ """Update the grid"""
127
145
128
146
# Flip layers
129
147
if self .cur_layer == 0 :
@@ -140,31 +158,37 @@ def on_update(self, delta_time: float):
140
158
for column in range (COLUMN_COUNT ):
141
159
live_neighbors = 0
142
160
# -1 -1
143
- if row > 0 and column > 0 \
144
- and layer1 [row - 1 ][column - 1 ].alpha == ALPHA_ON :
161
+ if row > 0 and column > 0 and layer1 [row - 1 ][column - 1 ].alpha == ALPHA_ON :
145
162
live_neighbors += 1
146
163
# -1 0
147
164
if row > 0 and layer1 [row - 1 ][column ].alpha == ALPHA_ON :
148
165
live_neighbors += 1
149
166
# -1 +1
150
- if row > 0 and column < COLUMN_COUNT - 1 \
151
- and layer1 [row - 1 ][column + 1 ].alpha == ALPHA_ON :
167
+ if (
168
+ row > 0
169
+ and column < COLUMN_COUNT - 1
170
+ and layer1 [row - 1 ][column + 1 ].alpha == ALPHA_ON
171
+ ):
152
172
live_neighbors += 1
153
173
# 0 +1
154
- if column < COLUMN_COUNT - 1 \
155
- and layer1 [row ][column + 1 ].alpha == ALPHA_ON :
174
+ if column < COLUMN_COUNT - 1 and layer1 [row ][column + 1 ].alpha == ALPHA_ON :
156
175
live_neighbors += 1
157
176
# +1 +1
158
- if row < ROW_COUNT - 1 \
159
- and column < COLUMN_COUNT - 1 \
160
- and layer1 [row + 1 ][column + 1 ].alpha == ALPHA_ON :
177
+ if (
178
+ row < ROW_COUNT - 1
179
+ and column < COLUMN_COUNT - 1
180
+ and layer1 [row + 1 ][column + 1 ].alpha == ALPHA_ON
181
+ ):
161
182
live_neighbors += 1
162
183
# +1 0
163
184
if row < ROW_COUNT - 1 and layer1 [row + 1 ][column ].alpha == ALPHA_ON :
164
185
live_neighbors += 1
165
186
# +1 -1
166
- if row < ROW_COUNT - 1 and column > 0 \
167
- and layer1 [row + 1 ][column - 1 ].alpha == ALPHA_ON :
187
+ if (
188
+ row < ROW_COUNT - 1
189
+ and column > 0
190
+ and layer1 [row + 1 ][column - 1 ].alpha == ALPHA_ON
191
+ ):
168
192
live_neighbors += 1
169
193
# 0 -1
170
194
if column > 0 and layer1 [row ][column - 1 ].alpha == ALPHA_ON :
@@ -194,7 +218,7 @@ def on_update(self, delta_time: float):
194
218
195
219
196
220
def main ():
197
- """ Main function """
221
+ """Main function"""
198
222
# Create a window class. This is what actually shows up on screen
199
223
window = arcade .Window (WINDOW_WIDTH , WINDOW_HEIGHT , WINDOW_TITLE )
200
224
window .center_window ()
0 commit comments