Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-thompson committed Jan 2, 2025
1 parent 6018433 commit 15c6569
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
52 changes: 27 additions & 25 deletions src/pong_arcade/ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

from pygame import Rect


class Ball:
def __init__(self):
self.default_x = (SCREEN_W >> 1) + 10
self.default_y = SCREEN_H >> 1
self.default_ball_pos = self.default_x, self.default_y
default_x = (SCREEN_W >> 1) + 10
default_y = SCREEN_H >> 1
self.default_pos = default_x, default_y

ball_size = 10, 10
self.rect = Rect(self.default_ball_pos, ball_size)
self.rect = Rect(self.default_pos, ball_size)

self.default_speed = self.x_diff = 6
self.y_diff = 0
Expand All @@ -23,8 +24,7 @@ def __init__(self):
self.bflag_paddle = False

def reset_position(self):
self.rect.x = self.default_x
self.rect.y = self.default_y
self.rect.x, self.rect.y = self.default_pos

def reset_movement(self):
self.x_diff = self.default_speed
Expand All @@ -43,14 +43,10 @@ def increase_speed(self):
increment = 2

if abs(self.x_diff) < self.rect.w << 1:
if self.x_diff > 0: self.x_diff += increment
else: self.x_diff -= increment

def flip_direction_horizontal(self):
self.x_diff = -self.x_diff

def flip_direction_vertical(self):
self.y_diff = -self.y_diff
if self.x_diff > 0:
self.x_diff += increment
else:
self.x_diff -= increment

def get_collision_point(self, paddle):
collision_rect = self.rect.clip(paddle)
Expand All @@ -75,16 +71,21 @@ def trajectory(self, paddle):

if collision_point < paddle.y:
trajectory = max_t
if self.y_diff == 0: trajectory = -trajectory
if self.y_diff == 0:
trajectory = -trajectory
elif collision_point in outer_top:
trajectory = mid_t
if self.y_diff == 0: trajectory = -trajectory
if self.y_diff == 0:
trajectory = -trajectory
elif collision_point in inner_top:
trajectory = low_t
if self.y_diff == 0: trajectory = -trajectory
if self.y_diff == 0:
trajectory = -trajectory
elif collision_point in center:
if self.y_diff == 0: trajectory = randint(-low_t, low_t)
else: trajectory = 0
if self.y_diff == 0:
trajectory = randint(-low_t, low_t)
else:
trajectory = 0
elif collision_point in inner_bot:
trajectory = low_t
elif collision_point in outer_bot:
Expand All @@ -94,18 +95,19 @@ def trajectory(self, paddle):
else:
trajectory = randint(min_t, max_t)

if self.y_diff < 0: trajectory = -trajectory
if self.y_diff < 0:
trajectory = -trajectory

return trajectory

def bounce_off_edges(self):
if self.rect.clipline(self.top_edge_line) != ():
self.flip_direction_vertical()
self.y_diff = -self.y_diff

self.bflag_edge = True

if self.rect.clipline(self.bot_edge_line) != ():
self.flip_direction_vertical()
self.y_diff = -self.y_diff

self.bflag_edge = True

Expand All @@ -114,7 +116,7 @@ def bounce_off_paddles(self, paddle1, paddle2):
self.bflag_paddle = True

self.increase_speed()
self.flip_direction_horizontal()
self.x_diff = -self.x_diff
self.rect.x = paddle1.right + 1

self.y_diff = self.trajectory(paddle1)
Expand All @@ -123,7 +125,7 @@ def bounce_off_paddles(self, paddle1, paddle2):
self.bflag_paddle = True

self.increase_speed()
self.flip_direction_horizontal()
self.x_diff = -self.x_diff
self.rect.x = paddle2.left - self.rect.w - 1

self.y_diff = self.trajectory(paddle2)
Expand All @@ -141,4 +143,4 @@ def move(self, paddle1, paddle2):
self.shift_pos_horizontal()

self.bounce_off_edges()
self.shift_pos_vertical()
self.shift_pos_vertical()
2 changes: 1 addition & 1 deletion src/pong_arcade/meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SCREEN_W = 800
SCREEN_H = 600
TITLE = "Pong"
FPS = 60
FPS = 60
17 changes: 8 additions & 9 deletions src/pong_arcade/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pygame import Rect


class Player:
def __init__(self, number, color):
self.id = number
Expand All @@ -16,21 +17,19 @@ def reset_score(self):
self.score = 0
self.winner = False

def set_color(self):
if self.id == 1:
self.color = 0, 0, 255
else:
self.color = 255, 0, 0

def set_paddle_pos(self):
distance_from_edge = self.paddle.h >> 1
default_y = SCREEN_H >> 1

if self.id == 1:
paddle_pos = distance_from_edge, default_y
x = distance_from_edge
y = default_y
paddle_pos = x, y
self.paddle = Rect(paddle_pos, self.paddle.size)
else:
paddle_pos = SCREEN_W - distance_from_edge - self.paddle.w, default_y
x = SCREEN_W - distance_from_edge - self.paddle.w
y = default_y
paddle_pos = x, y
self.paddle = Rect(paddle_pos, self.paddle.size)

def ensure_in_bound_top(self):
Expand All @@ -43,4 +42,4 @@ def ensure_in_bound_bot(self):
defect = 20

if self.paddle.y > SCREEN_H - self.paddle.h - defect:
self.paddle.y = SCREEN_H - self.paddle.h - defect
self.paddle.y = SCREEN_H - self.paddle.h - defect
13 changes: 8 additions & 5 deletions src/pong_arcade/pong.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ def set_window_properties(self):
pygame.display.set_icon(icon)

def print_info(self):
print("Thank you for playing Pong! https://github.com/jacob-thompson/pong-arcade")
link = "https://github.com/jacob-thompson/pong-arcade"
print(f"Thank you for playing Pong! {link}")

def reset_game(self):
self.p1.set_paddle_pos()
self.p1.set_color()
self.p1.color = self.blue

self.p2.set_paddle_pos()
self.p2.set_color()
self.p2.color = self.red

self.p1.reset_score()
self.p2.reset_score()
Expand Down Expand Up @@ -207,15 +208,17 @@ def check_for_score(self):
self.p1.score += 1
self.ball.reset()

if self.opt3_selected: self.sound_score.play()
if self.opt3_selected:
self.sound_score.play()

self.check_for_winner(self.p1)

if p2_score:
self.p2.score += 1
self.ball.reset()

if self.opt3_selected: self.sound_score.play()
if self.opt3_selected:
self.sound_score.play()

self.check_for_winner(self.p2)

Expand Down

0 comments on commit 15c6569

Please sign in to comment.