Skip to content

Commit ee5a683

Browse files
committed
Update easing_example_1 with center_*
* Use center_x and center_y arguments * Use shared kwargs to shorten code
1 parent 242bcc1 commit ee5a683

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

arcade/examples/conway_alpha.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111

1212
import arcade
13+
from arcade import SpriteCircle, SpriteList
1314
import random
1415

1516
# Set how many rows and columns we will have
@@ -45,12 +46,12 @@ def create_grids(
4546
sprites.
4647
"""
4748
# One dimensional list of all sprites in the two-dimensional sprite list
48-
grid_sprites_one_dim = arcade.SpriteList()
49+
grid_sprites_one_dim: SpriteList[SpriteCircle] = SpriteList()
4950

5051
# This will be a two-dimensional grid of sprites to mirror the two
5152
# dimensional grid of numbers. This points to the SAME sprites that are
5253
# in grid_sprite_list, just in a 2d manner.
53-
grid_sprites_two_dim = []
54+
grid_sprites_two_dim: list[list[SpriteCircle]] = []
5455

5556
# Calculate values we'll re-use below
5657
cell_width, cell_height = cell_size
@@ -63,12 +64,8 @@ def create_grids(
6364
center_offset_x = half_width + cell_margin
6465
center_offset_y = half_height + cell_margin
6566

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-
)
67+
# Fit sprites into the cell size
68+
radius = min(half_width, half_height)
7269

7370
# Create a list of sprites to represent each grid location
7471
for row in range(ROW_COUNT):
@@ -80,7 +77,7 @@ def create_grids(
8077
y = row * y_step + center_offset_y
8178

8279
# Make the sprite as a soft circle
83-
sprite = arcade.SpriteCircle(center_x=x, center_y=y, **shared_kwargs)
80+
sprite = SpriteCircle(radius, ALIVE_COLOR, True, center_x=x, center_y=y)
8481

8582
# Add the sprite to both lists
8683
grid_sprites_one_dim.append(sprite)

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

0 commit comments

Comments
 (0)