Skip to content

Commit c110a4d

Browse files
authored
Update examples to use SpriteCircle's new center_* keyword arguments (#2767)
* Use SpriteCircle's center_x and center_y in snow example * 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 * Update easing_example_1 with center_* * Use center_x and center_y arguments * Use shared kwargs to shorten code
1 parent cdf329b commit c110a4d

File tree

3 files changed

+86
-60
lines changed

3 files changed

+86
-60
lines changed

arcade/examples/conway_alpha.py

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
typing:
99
python -m arcade.examples.conway_alpha
1010
"""
11+
1112
import arcade
13+
from arcade import SpriteCircle, SpriteList
1214
import random
1315

1416
# Set how many rows and columns we will have
@@ -35,34 +37,47 @@
3537
ALPHA_OFF = 0
3638

3739

38-
def create_grids():
40+
def create_grids(
41+
cell_size: tuple[int, int] = (CELL_WIDTH, CELL_HEIGHT), cell_margin: int = CELL_MARGIN
42+
):
3943
"""
4044
Create a 2D and 1D grid of sprites. We use the 1D SpriteList for drawing,
4145
and the 2D list for accessing via grid. Both lists point to the same set of
4246
sprites.
4347
"""
4448
# One dimensional list of all sprites in the two-dimensional sprite list
45-
grid_sprites_one_dim = arcade.SpriteList()
49+
grid_sprites_one_dim: SpriteList[SpriteCircle] = SpriteList()
4650

4751
# This will be a two-dimensional grid of sprites to mirror the two
4852
# dimensional grid of numbers. This points to the SAME sprites that are
4953
# in grid_sprite_list, just in a 2d manner.
50-
grid_sprites_two_dim = []
54+
grid_sprites_two_dim: list[list[SpriteCircle]] = []
55+
56+
# Calculate values we'll re-use below
57+
cell_width, cell_height = cell_size
58+
half_width = cell_width // 2
59+
half_height = cell_height // 2
60+
61+
x_step = cell_width + cell_margin
62+
y_step = cell_height + cell_margin
63+
64+
center_offset_x = half_width + cell_margin
65+
center_offset_y = half_height + cell_margin
66+
67+
# Fit sprites into the cell size
68+
radius = min(half_width, half_height)
5169

5270
# Create a list of sprites to represent each grid location
5371
for row in range(ROW_COUNT):
5472
grid_sprites_two_dim.append([])
5573

5674
for column in range(COLUMN_COUNT):
75+
# Position the sprite
76+
x = column * x_step + center_offset_x
77+
y = row * y_step + center_offset_y
5778

5879
# 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
80+
sprite = SpriteCircle(radius, ALIVE_COLOR, True, center_x=x, center_y=y)
6681

6782
# Add the sprite to both lists
6883
grid_sprites_one_dim.append(sprite)
@@ -72,7 +87,7 @@ def create_grids():
7287

7388

7489
def randomize_grid(grid: arcade.SpriteList):
75-
""" Randomize the grid to alive/dead """
90+
"""Randomize the grid to alive/dead"""
7691
for cell in grid:
7792
pick = random.randrange(2)
7893
if pick:
@@ -106,24 +121,24 @@ def __init__(self):
106121
randomize_grid(self.layers_grid_sprites_one_dim[0])
107122

108123
def reset(self):
109-
""" Reset the grid """
124+
"""Reset the grid"""
110125
randomize_grid(self.layers_grid_sprites_one_dim[0])
111126

112127
def on_draw(self):
113-
""" Render the screen. """
128+
"""Render the screen."""
114129
# Clear all pixels in the window
115130
self.clear()
116131
self.layers_grid_sprites_one_dim[0].draw()
117132

118133
def on_key_press(self, symbol: int, modifiers: int):
119-
""" Handle key press events """
134+
"""Handle key press events"""
120135
if symbol == arcade.key.SPACE:
121136
self.reset()
122137
elif symbol == arcade.key.ESCAPE:
123138
self.window.close()
124139

125140
def on_update(self, delta_time: float):
126-
""" Update the grid """
141+
"""Update the grid"""
127142

128143
# Flip layers
129144
if self.cur_layer == 0:
@@ -140,31 +155,37 @@ def on_update(self, delta_time: float):
140155
for column in range(COLUMN_COUNT):
141156
live_neighbors = 0
142157
# -1 -1
143-
if row > 0 and column > 0 \
144-
and layer1[row - 1][column - 1].alpha == ALPHA_ON:
158+
if row > 0 and column > 0 and layer1[row - 1][column - 1].alpha == ALPHA_ON:
145159
live_neighbors += 1
146160
# -1 0
147161
if row > 0 and layer1[row - 1][column].alpha == ALPHA_ON:
148162
live_neighbors += 1
149163
# -1 +1
150-
if row > 0 and column < COLUMN_COUNT - 1\
151-
and layer1[row - 1][column + 1].alpha == ALPHA_ON:
164+
if (
165+
row > 0
166+
and column < COLUMN_COUNT - 1
167+
and layer1[row - 1][column + 1].alpha == ALPHA_ON
168+
):
152169
live_neighbors += 1
153170
# 0 +1
154-
if column < COLUMN_COUNT - 1 \
155-
and layer1[row][column + 1].alpha == ALPHA_ON:
171+
if column < COLUMN_COUNT - 1 and layer1[row][column + 1].alpha == ALPHA_ON:
156172
live_neighbors += 1
157173
# +1 +1
158-
if row < ROW_COUNT - 1 \
159-
and column < COLUMN_COUNT - 1 \
160-
and layer1[row + 1][column + 1].alpha == ALPHA_ON:
174+
if (
175+
row < ROW_COUNT - 1
176+
and column < COLUMN_COUNT - 1
177+
and layer1[row + 1][column + 1].alpha == ALPHA_ON
178+
):
161179
live_neighbors += 1
162180
# +1 0
163181
if row < ROW_COUNT - 1 and layer1[row + 1][column].alpha == ALPHA_ON:
164182
live_neighbors += 1
165183
# +1 -1
166-
if row < ROW_COUNT - 1 and column > 0 \
167-
and layer1[row + 1][column - 1].alpha == ALPHA_ON:
184+
if (
185+
row < ROW_COUNT - 1
186+
and column > 0
187+
and layer1[row + 1][column - 1].alpha == ALPHA_ON
188+
):
168189
live_neighbors += 1
169190
# 0 -1
170191
if column > 0 and layer1[row][column - 1].alpha == ALPHA_ON:
@@ -194,7 +215,7 @@ def on_update(self, delta_time: float):
194215

195216

196217
def main():
197-
""" Main function """
218+
"""Main function"""
198219
# Create a window class. This is what actually shows up on screen
199220
window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
200221
window.center_window()

arcade/examples/easing_example_1.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
If Python and Arcade are installed, this example can be run from the command line with:
1111
python -m arcade.examples.easing_example_1
1212
"""
13+
1314
import arcade
1415
from arcade import easing
1516
from arcade.types import Color
@@ -34,13 +35,13 @@
3435

3536

3637
class EasingCircle(arcade.SpriteCircle):
37-
""" Player class """
38+
"""Player class"""
3839

39-
def __init__(self, radius, color):
40-
""" Set up the player """
40+
def __init__(self, radius, color, center_x: float = 0, center_y: float = 0):
41+
"""Set up the player"""
4142

4243
# Call the parent init
43-
super().__init__(radius, color)
44+
super().__init__(radius, color, center_x=center_x, center_y=center_y)
4445

4546
self.easing_x_data = None
4647
self.easing_y_data = None
@@ -52,10 +53,12 @@ def update(self, delta_time: float = 1 / 60):
5253
x = X_START
5354
if self.center_x < WINDOW_WIDTH / 2:
5455
x = X_END
55-
ex, ey = easing.ease_position(self.position,
56-
(x, self.center_y),
57-
rate=180,
58-
ease_function=self.easing_x_data.ease_function)
56+
ex, ey = easing.ease_position(
57+
self.position,
58+
(x, self.center_y),
59+
rate=180,
60+
ease_function=self.easing_x_data.ease_function,
61+
)
5962
self.easing_x_data = ex
6063

6164
if self.easing_y_data is not None:
@@ -65,10 +68,10 @@ def update(self, delta_time: float = 1 / 60):
6568

6669

6770
class GameView(arcade.View):
68-
""" Main application class. """
71+
"""Main application class."""
6972

7073
def __init__(self):
71-
""" Initializer """
74+
"""Initializer"""
7275

7376
# Call the parent class initializer
7477
super().__init__()
@@ -81,15 +84,16 @@ def __init__(self):
8184
self.lines = None
8285

8386
def setup(self):
84-
""" Set up the game and initialize the variables. """
87+
"""Set up the game and initialize the variables."""
8588

8689
# Sprite lists
8790
self.ball_list = arcade.SpriteList()
8891
self.lines = arcade.shape_list.ShapeElementList()
92+
color = Color.from_hex_string(BALL_COLOR)
93+
shared_ball_kwargs = dict(radius=BALL_RADIUS, color=color)
8994

9095
def create_ball(ball_y, ease_function):
91-
ball = EasingCircle(BALL_RADIUS, Color.from_hex_string(BALL_COLOR))
92-
ball.position = X_START, ball_y
96+
ball = EasingCircle(**shared_ball_kwargs, center_x=X_START, center_y=ball_y)
9397
p1 = ball.position
9498
p2 = (X_END, ball_y)
9599
ex, ey = easing.ease_position(p1, p2, time=TIME, ease_function=ease_function)
@@ -100,9 +104,12 @@ def create_ball(ball_y, ease_function):
100104

101105
def create_line(line_y):
102106
line = arcade.shape_list.create_line(
103-
X_START, line_y - BALL_RADIUS - LINE_WIDTH,
104-
X_END, line_y - BALL_RADIUS,
105-
line_color, line_width=LINE_WIDTH,
107+
X_START,
108+
line_y - BALL_RADIUS - LINE_WIDTH,
109+
X_END,
110+
line_y - BALL_RADIUS,
111+
line_color,
112+
line_width=LINE_WIDTH,
106113
)
107114
return line
108115

@@ -161,7 +168,7 @@ def add_item(item_y, ease_function, text):
161168
add_item(y, easing.ease_in_out_sin, "Ease in out sin")
162169

163170
def on_draw(self):
164-
""" Render the screen. """
171+
"""Render the screen."""
165172

166173
# This command has to happen before we start drawing
167174
self.clear()
@@ -175,15 +182,15 @@ def on_draw(self):
175182
text.draw()
176183

177184
def on_update(self, delta_time):
178-
""" Movement and game logic """
185+
"""Movement and game logic"""
179186

180187
# Call update on all sprites (The sprites don't do much in this
181188
# example though.)
182189
self.ball_list.update(delta_time)
183190

184191

185192
def main():
186-
""" Main function """
193+
"""Main function"""
187194
# Create a window class. This is what actually shows up on screen
188195
window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
189196

arcade/examples/snow.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Snowflake(arcade.SpriteCircle):
2525
Based on drawing filled-circles.
2626
"""
2727

28-
def __init__(self, size, speed, drift):
29-
super().__init__(size, arcade.color.WHITE)
28+
def __init__(self, size, speed, drift, center_x: float = 0, center_y: float = 0):
29+
super().__init__(size, arcade.color.WHITE, center_x=center_x, center_y=center_y)
3030
self.speed = speed
3131
self.drift = drift
3232

@@ -37,7 +37,7 @@ def reset_pos(self):
3737
random.randrange(WINDOW_HEIGHT, WINDOW_HEIGHT + 100),
3838
)
3939

40-
def update(self, delta_time: float = 1/60) -> None:
40+
def update(self, delta_time: float = 1 / 60) -> None:
4141
self.center_y -= self.speed * delta_time
4242

4343
# Check if snowflake has fallen below screen
@@ -50,10 +50,10 @@ def update(self, delta_time: float = 1/60) -> None:
5050

5151

5252
class GameView(arcade.View):
53-
""" Main application class. """
53+
"""Main application class."""
5454

5555
def __init__(self):
56-
""" Initializer """
56+
"""Initializer"""
5757
# Calls "__init__" of parent class (arcade.Window) to setup screen
5858
super().__init__()
5959

@@ -66,38 +66,36 @@ def __init__(self):
6666
self.background_color = arcade.color.BLACK
6767

6868
def start_snowfall(self):
69-
""" Set up snowfall and initialize variables. """
69+
"""Set up snowfall and initialize variables."""
7070
for i in range(SNOWFLAKE_COUNT):
7171
# Create snowflake instance
7272
snowflake = Snowflake(
7373
size=random.randrange(1, 4),
7474
speed=random.randrange(20, 40),
7575
drift=random.uniform(math.pi, math.pi * 2),
76-
)
77-
# Randomly position snowflake
78-
snowflake.position = (
79-
random.randrange(WINDOW_WIDTH),
80-
random.randrange(WINDOW_HEIGHT + 200),
76+
# Randomly position snowflake
77+
center_x=random.randrange(WINDOW_WIDTH),
78+
center_y=random.randrange(WINDOW_HEIGHT + 200),
8179
)
8280
# Add snowflake to snowflake list
8381
self.snowflake_list.append(snowflake)
8482

8583
def on_draw(self):
86-
""" Render the screen. """
84+
"""Render the screen."""
8785
# Clear the screen to the background color
8886
self.clear()
8987

9088
# Draw the current position of each snowflake
9189
self.snowflake_list.draw()
9290

9391
def on_update(self, delta_time):
94-
""" All the logic to move, and the game logic goes here. """
92+
"""All the logic to move, and the game logic goes here."""
9593
# Call update on all the snowflakes
9694
self.snowflake_list.update(delta_time)
9795

9896

9997
def main():
100-
""" Main function """
98+
"""Main function"""
10199
# Create a window class. This is what actually shows up on screen
102100
window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
103101

0 commit comments

Comments
 (0)