-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
executable file
·169 lines (138 loc) · 4.83 KB
/
components.py
File metadata and controls
executable file
·169 lines (138 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
Game components module for Pong.
This module contains the core game objects: Ball and Paddle.
Each class encapsulates the behavior and rendering of its respective game element.
"""
import pygame as py
from typing import Tuple
# Type alias for color
Color = Tuple[int, int, int]
WHITE: Color = (255, 255, 255)
BLACK: Color = (0, 0, 0)
GREEN: Color = (19, 58, 38)
class Ball:
"""
Represents the ball in the Pong game.
The ball moves across the screen and bounces off paddles and walls.
Its velocity can be modified based on where it hits the paddle.
Attributes:
COLOR: The color of the ball (RGB tuple)
MAX_VEL: Maximum velocity of the ball
x: Current x-coordinate
y: Current y-coordinate
radius: Radius of the ball
x_vel: Current velocity in x-direction
y_vel: Current velocity in y-direction
original_x: Starting x-coordinate for reset
original_y: Starting y-coordinate for reset
"""
COLOR: Color = WHITE
MAX_VEL: int = 5
def __init__(self, x: int, y: int, radius: int) -> None:
"""
Initialize a Ball instance.
Args:
x: Starting x-coordinate
y: Starting y-coordinate
radius: Radius of the ball
"""
self.x: int = x
self.y: int = y
self.original_x: int = x
self.original_y: int = y
self.radius: int = radius
self.x_vel: int = self.MAX_VEL
self.y_vel: int = 0
def draw(self, win: py.Surface) -> None:
"""
Draw the ball on the given surface.
Args:
win: Pygame surface to draw on
"""
py.draw.circle(win, self.COLOR, (int(self.x), int(self.y)), self.radius)
def move(self) -> None:
"""
Update the ball's position based on its current velocity.
This method should be called once per frame to animate the ball.
"""
self.x += self.x_vel
self.y += self.y_vel
def reset(self) -> None:
"""
Reset the ball to its original position and invert horizontal direction.
Called when a point is scored to restart play from the center.
"""
self.x = self.original_x
self.y = self.original_y
self.y_vel = 0
self.x_vel *= -1
def __repr__(self) -> str:
"""Return string representation of Ball for debugging."""
return f"Ball(x={self.x:.1f}, y={self.y:.1f}, vel=({self.x_vel}, {self.y_vel}))"
class Paddle:
"""
Represents a paddle in the Pong game.
Paddles are controlled by players to hit the ball back and forth.
They can move vertically within the screen boundaries.
Attributes:
COLOR: The color of the paddle (RGB tuple)
VEL: Movement speed of the paddle
x: Current x-coordinate
y: Current y-coordinate
width: Width of the paddle
height: Height of the paddle
original_x: Starting x-coordinate for reset
original_y: Starting y-coordinate for reset
"""
COLOR: Color = WHITE
VEL: int = 4
def __init__(self, x: int, y: int, width: int, height: int) -> None:
"""
Initialize a Paddle instance.
Args:
x: Starting x-coordinate
y: Starting y-coordinate
width: Width of the paddle
height: Height of the paddle
"""
self.x: int = x
self.y: int = y
self.original_x: int = x
self.original_y: int = y
self.width: int = width
self.height: int = height
def draw(self, win: py.Surface) -> None:
"""
Draw the paddle on the given surface.
Args:
win: Pygame surface to draw on
"""
py.draw.rect(win, self.COLOR, (self.x, self.y, self.width, self.height))
def move(self, up: bool = True) -> None:
"""
Move the paddle up or down.
Args:
up: If True, move up; if False, move down
"""
if up:
self.y -= self.VEL
else:
self.y += self.VEL
def reset(self) -> None:
"""
Reset the paddle to its original position.
Called when a new game round starts.
"""
self.x = self.original_x
self.y = self.original_y
def get_rect(self) -> py.Rect:
"""
Get a pygame Rect object representing the paddle's bounding box.
Useful for collision detection.
Returns:
pygame.Rect object with paddle dimensions and position
"""
return py.Rect(self.x, self.y, self.width, self.height)
def __repr__(self) -> str:
"""Return string representation of Paddle for debugging."""
return f"Paddle(x={self.x}, y={self.y}, size=({self.width}x{self.height}))"