Skip to content

Commit 4b2cad9

Browse files
committed
refactor constants.py with separate classes and Enums for readability
1 parent a865697 commit 4b2cad9

10 files changed

+552
-554
lines changed

ball.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
from random import randint
33

44
from constants import (
5-
BALL_START_POSITION, VERTICAL_SURFACE, HORIZONTAL_SURFACE, BALL_COLOR, BALL_SHAPE, BALL_RADIUS,
6-
NORTH, SOUTH, EAST, WEST, COMPLETE_ANGLE, MIN_PADDLE_ANGLE, MAX_PADDLE_ANGLE, DEFAULT_BALL_SPEED, RED
5+
BallAttributes, VERTICAL_SURFACE, HORIZONTAL_SURFACE,
6+
NORTH, SOUTH, EAST, WEST, COMPLETE_ANGLE, PaddleAttributes, Color
77
)
88

99

1010
class Ball(RawTurtle):
1111
def __init__(self, canvas, **kwargs):
1212
super().__init__(canvas, **kwargs)
1313

14-
self.move_speed = DEFAULT_BALL_SPEED
14+
self.move_speed = BallAttributes.DEFAULT_SPEED
1515
self.fireball = False
1616
self.latest_barrier_hit = None
1717

1818
self.set_default_ball()
1919

2020
def set_default_ball(self):
2121
self.penup()
22-
self.color(BALL_COLOR)
23-
self.shape(BALL_SHAPE)
24-
self.setposition(BALL_START_POSITION)
22+
self.color(BallAttributes.COLOR)
23+
self.shape(BallAttributes.SHAPE)
24+
self.setposition(BallAttributes.START_POSITION)
2525
self.set_random_starting_direction()
2626
self.hideturtle()
2727

@@ -36,7 +36,8 @@ def move(self):
3636

3737
def get_bbox(self):
3838
ball_x, ball_y = self.xcor(), self.ycor()
39-
return ball_x - BALL_RADIUS, ball_y + BALL_RADIUS, ball_x + BALL_RADIUS, ball_y - BALL_RADIUS
39+
ball_radius = BallAttributes.RADIUS
40+
return ball_x - ball_radius, ball_y + ball_radius, ball_x + ball_radius, ball_y - ball_radius
4041

4142
def get_location(self):
4243
ball_x, ball_y = self.xcor(), self.ycor()
@@ -76,7 +77,7 @@ def clear_latest_barrier_hit(self):
7677

7778
@staticmethod
7879
def clamp_angle_to_reflection_range(angle):
79-
return max(min(angle, MAX_PADDLE_ANGLE), MIN_PADDLE_ANGLE)
80+
return max(min(angle, PaddleAttributes.MAX_ANGLE), PaddleAttributes.MIN_ANGLE)
8081

8182
@staticmethod
8283
def is_moving_vertically(direction):
@@ -167,11 +168,11 @@ def set_speed(self, speed):
167168
self.move_speed = speed
168169

169170
def reset_speed(self):
170-
self.move_speed = DEFAULT_BALL_SPEED
171+
self.move_speed = BallAttributes.DEFAULT_SPEED
171172

172173
def activate_fireball(self):
173174
self.fireball = True
174-
self.color(RED)
175+
self.color(Color.RED.value)
175176

176177
def is_fireball(self):
177178
return self.fireball

brick.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from turtle import RawTurtle
22

3-
from constants import (
4-
TYPE, BRICK_COLOR, BRICK_SHAPE,
5-
BRICK_WIDTH, BRICK_LENGTH, BrickType, NORMAL_BRICK_SCORE, BROKEN_BRICK_SCORE, STRONG_BRICK_SCORE
6-
)
3+
from constants import TYPE, BRICK_COLOR, BrickAttributes, BrickType
74

85

96
class Brick(RawTurtle):
@@ -20,19 +17,19 @@ def __init__(self, canvas, brick_attributes: dict, **kwargs):
2017

2118
def set_properties(self):
2219
self.penup()
23-
self.shape(BRICK_SHAPE)
24-
self.shapesize(stretch_len=BRICK_LENGTH)
25-
self.brick_length = BRICK_WIDTH * BRICK_LENGTH
20+
self.shape(BrickAttributes.SHAPE)
21+
self.shapesize(stretch_len=BrickAttributes.LENGTH)
22+
self.brick_length = BrickAttributes.WIDTH * BrickAttributes.LENGTH
2623
self.color(self.brick_color)
2724
self.hideturtle()
2825

2926
def set_score(self):
3027
if self.is_normal():
31-
return NORMAL_BRICK_SCORE
28+
return BrickAttributes.NORMAL_SCORE
3229
elif self.is_broken():
33-
return BROKEN_BRICK_SCORE
30+
return BrickAttributes.BROKEN_SCORE
3431
elif self.is_strong():
35-
return STRONG_BRICK_SCORE
32+
return BrickAttributes.STRONG_SCORE
3633
else:
3734
return 0
3835

@@ -41,7 +38,7 @@ def get_score(self):
4138

4239
def set_location(self, brick_left_x, brick_top_y):
4340
brick_x = brick_left_x + self.brick_length / 2
44-
brick_y = brick_top_y - BRICK_WIDTH / 2
41+
brick_y = brick_top_y - BrickAttributes.WIDTH / 2
4542
self.brick_location = (brick_x, brick_y)
4643
self.setposition(self.brick_location)
4744

@@ -58,8 +55,8 @@ def get_bbox(self):
5855
brick_x, brick_y = self.brick_location
5956
brick_left_x = brick_x - self.brick_length / 2
6057
brick_right_x = brick_x + self.brick_length / 2
61-
brick_top_y = brick_y + BRICK_WIDTH / 2
62-
brick_bottom_y = brick_y - BRICK_WIDTH / 2
58+
brick_top_y = brick_y + BrickAttributes.WIDTH / 2
59+
brick_bottom_y = brick_y - BrickAttributes.WIDTH / 2
6360
return brick_left_x, brick_top_y, brick_right_x, brick_bottom_y
6461

6562
def get_type(self):

constants.py

+70-66
Original file line numberDiff line numberDiff line change
@@ -25,91 +25,46 @@
2525
LIVES_IMAGE_Y_COORD = (SCREEN_TOP_EDGE + 26) * -1
2626
SCORE_POSITION = (0, SCREEN_HEIGHT / 2 - 50)
2727
HIGHSCORE_POSITION = (SCREEN_LEFT_EDGE + 50, SCREEN_HEIGHT / 2 - 50)
28+
GAME_FONT = ('Courier', 32, 'bold')
29+
2830
STARTING_LIVES = 3
2931
MAX_LIVES = 5
30-
GAME_FONT = ('Courier', 32, 'bold')
31-
NORMAL_BRICK_SCORE = 10
32-
BROKEN_BRICK_SCORE = 30
33-
STRONG_BRICK_SCORE = 50
34-
35-
PADDLE_WIDTH = 20
36-
DEFAULT_PADDLE_LENGTH = 5
37-
MIN_PADDLE_ANGLE = 15
38-
MAX_PADDLE_ANGLE = 165
39-
PADDLE_COLOR = 'yellow'
40-
PADDLE_LASER_COLOR = 'red'
41-
PADDLE_SHAPE = 'square'
42-
PADDLE_START_POSITION = (0, SCREEN_BOTTOM_EDGE + PADDLE_WIDTH)
43-
44-
BALL_ANIMATION_SPEED = 50
45-
DEFAULT_BALL_SPEED = 1
46-
BALL_SIZE = 20
47-
BALL_RADIUS = BALL_SIZE / 2
48-
BALL_COLOR = 'white'
49-
BALL_SHAPE = 'circle'
50-
BALL_START_POSITION = (0, SCREEN_BOTTOM_EDGE + PADDLE_WIDTH + BALL_SIZE)
5132

5233
SPACING = 'spacing'
5334
SPACE_SIZE = 'space_size'
54-
5535
TYPE = 'type'
56-
NORMAL = 'normal'
57-
STRONG = 'strong'
58-
BROKEN = 'broken'
59-
BARRIER = 'barrier'
60-
36+
BRICK_COLOR = 'brick_color'
37+
VERTICAL_SURFACE = 'vertical'
38+
HORIZONTAL_SURFACE = 'horizontal'
6139

62-
class BrickType(Enum):
63-
NORMAL = 'normal'
64-
STRONG = 'strong'
65-
BROKEN = 'broken'
66-
BARRIER = 'barrier'
40+
EAST = 0
41+
NORTH = 90
42+
WEST = 180
43+
SOUTH = 270
44+
COMPLETE_ANGLE = 360
6745

6846

69-
class BrickColor(Enum):
47+
class Color(Enum):
7048
BLUE = 'blue'
7149
RED = 'red'
7250
YELLOW = 'yellow'
7351
ORANGE = 'orange'
7452
GREEN = 'green'
7553
PURPLE = 'purple'
54+
WHITE = 'white'
7655

7756

78-
BRICK_COLOR = 'brick_color'
79-
BLUE = 'blue'
80-
RED = 'red'
81-
YELLOW = 'yellow'
82-
ORANGE = 'orange'
83-
GREEN = 'green'
84-
PURPLE = 'purple'
85-
86-
BRICK_SHAPE = 'square'
87-
BRICK_WIDTH = 20
88-
BRICK_LENGTH = 3
89-
90-
BRICK_SPACING = 5
91-
92-
VERTICAL_SURFACE = 'vertical'
93-
HORIZONTAL_SURFACE = 'horizontal'
94-
EAST = 0
95-
NORTH = 90
96-
WEST = 180
97-
SOUTH = 270
98-
COMPLETE_ANGLE = 360
57+
class Shape(Enum):
58+
SQUARE = 'square'
59+
CIRCLE = 'circle'
60+
ARROW = 'arrow'
9961

100-
POWERUP_SHAPE = 'arrow'
101-
POWERUP_COLOR = 'white'
102-
POWERUP_SPEED = 1.5
103-
POWERUP_WIDTH = 20
104-
POWERUP_IMAGE_TIME_LIMIT = 750
105-
POWERUP_IMAGE_SPEED = -1
10662

107-
LASER_SHAPE = 'arrow'
108-
LASER_COLOR = 'white'
109-
LASER_SPEED = 1.5
110-
LASER_WIDTH = 20
111-
LASER_TIME_LIMIT = 20000
112-
LASER_FREQUENCY = 2000
63+
class BrickType(Enum):
64+
NORMAL = 'normal'
65+
STRONG = 'strong'
66+
BROKEN = 'broken'
67+
BARRIER = 'barrier'
11368

11469

11570
class PowerupType(Enum):
@@ -121,3 +76,52 @@ class PowerupType(Enum):
12176
SMALL_PADDLE = 'small_paddle'
12277
BIG_PADDLE = 'big_paddle'
12378
EXTRA_LIFE = 'extra_life'
79+
80+
81+
class BrickAttributes:
82+
SHAPE = Shape.SQUARE.value
83+
WIDTH = 20
84+
LENGTH = 3
85+
SPACING = 5
86+
NORMAL_SCORE = 10
87+
BROKEN_SCORE = 30
88+
STRONG_SCORE = 50
89+
90+
91+
class PaddleAttributes:
92+
WIDTH = 20
93+
DEFAULT_LENGTH = 5
94+
MIN_ANGLE = 15
95+
MAX_ANGLE = 165
96+
COLOR = Color.YELLOW.value
97+
LASER_PADDLE_COLOR = Color.RED.value
98+
SHAPE = Shape.SQUARE.value
99+
START_POSITION = (0, SCREEN_BOTTOM_EDGE + WIDTH)
100+
101+
102+
class BallAttributes:
103+
ANIMATION_SPEED = 50
104+
DEFAULT_SPEED = 1
105+
SIZE = 20
106+
RADIUS = SIZE / 2
107+
COLOR = Color.WHITE.value
108+
SHAPE = Shape.CIRCLE.value
109+
START_POSITION = (0, SCREEN_BOTTOM_EDGE + PaddleAttributes.WIDTH + SIZE)
110+
111+
112+
class PowerupAttributes:
113+
SHAPE = Shape.ARROW.value
114+
COLOR = Color.YELLOW.value
115+
SPEED = 1.5
116+
WIDTH = 20
117+
IMAGE_TIME_LIMIT = 750
118+
IMAGE_SPEED = -1
119+
120+
121+
class LaserAttributes:
122+
SHAPE = Shape.ARROW.value
123+
COLOR = Color.ORANGE.value
124+
SPEED = 1.5
125+
WIDTH = 20
126+
TIME_LIMIT = 20000
127+
FREQUENCY = 2000

0 commit comments

Comments
 (0)