Skip to content

Commit 242bcc1

Browse files
committed
Update conway_alpha example with center_* and min
* Use center_x and center_y arguments for sprites * Account for grid cells that are short and wide via min
1 parent 91ace75 commit 242bcc1

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

arcade/examples/conway_alpha.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
typing:
99
python -m arcade.examples.conway_alpha
1010
"""
11+
1112
import arcade
1213
import random
1314

@@ -35,7 +36,9 @@
3536
ALPHA_OFF = 0
3637

3738

38-
def create_grids():
39+
def create_grids(
40+
cell_size: tuple[int, int] = (CELL_WIDTH, CELL_HEIGHT), cell_margin: int = CELL_MARGIN
41+
):
3942
"""
4043
Create a 2D and 1D grid of sprites. We use the 1D SpriteList for drawing,
4144
and the 2D list for accessing via grid. Both lists point to the same set of
@@ -49,20 +52,35 @@ def create_grids():
4952
# in grid_sprite_list, just in a 2d manner.
5053
grid_sprites_two_dim = []
5154

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+
5273
# Create a list of sprites to represent each grid location
5374
for row in range(ROW_COUNT):
5475
grid_sprites_two_dim.append([])
5576

5677
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
5781

5882
# 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)
6684

6785
# Add the sprite to both lists
6886
grid_sprites_one_dim.append(sprite)
@@ -72,7 +90,7 @@ def create_grids():
7290

7391

7492
def randomize_grid(grid: arcade.SpriteList):
75-
""" Randomize the grid to alive/dead """
93+
"""Randomize the grid to alive/dead"""
7694
for cell in grid:
7795
pick = random.randrange(2)
7896
if pick:
@@ -106,24 +124,24 @@ def __init__(self):
106124
randomize_grid(self.layers_grid_sprites_one_dim[0])
107125

108126
def reset(self):
109-
""" Reset the grid """
127+
"""Reset the grid"""
110128
randomize_grid(self.layers_grid_sprites_one_dim[0])
111129

112130
def on_draw(self):
113-
""" Render the screen. """
131+
"""Render the screen."""
114132
# Clear all pixels in the window
115133
self.clear()
116134
self.layers_grid_sprites_one_dim[0].draw()
117135

118136
def on_key_press(self, symbol: int, modifiers: int):
119-
""" Handle key press events """
137+
"""Handle key press events"""
120138
if symbol == arcade.key.SPACE:
121139
self.reset()
122140
elif symbol == arcade.key.ESCAPE:
123141
self.window.close()
124142

125143
def on_update(self, delta_time: float):
126-
""" Update the grid """
144+
"""Update the grid"""
127145

128146
# Flip layers
129147
if self.cur_layer == 0:
@@ -140,31 +158,37 @@ def on_update(self, delta_time: float):
140158
for column in range(COLUMN_COUNT):
141159
live_neighbors = 0
142160
# -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:
145162
live_neighbors += 1
146163
# -1 0
147164
if row > 0 and layer1[row - 1][column].alpha == ALPHA_ON:
148165
live_neighbors += 1
149166
# -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+
):
152172
live_neighbors += 1
153173
# 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:
156175
live_neighbors += 1
157176
# +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+
):
161182
live_neighbors += 1
162183
# +1 0
163184
if row < ROW_COUNT - 1 and layer1[row + 1][column].alpha == ALPHA_ON:
164185
live_neighbors += 1
165186
# +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+
):
168192
live_neighbors += 1
169193
# 0 -1
170194
if column > 0 and layer1[row][column - 1].alpha == ALPHA_ON:
@@ -194,7 +218,7 @@ def on_update(self, delta_time: float):
194218

195219

196220
def main():
197-
""" Main function """
221+
"""Main function"""
198222
# Create a window class. This is what actually shows up on screen
199223
window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
200224
window.center_window()

0 commit comments

Comments
 (0)