-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.py
More file actions
74 lines (58 loc) · 2.37 KB
/
Copy pathball.py
File metadata and controls
74 lines (58 loc) · 2.37 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
"""
Ball Class
Handles ball movement, bouncing physics, and drawing.
- Collides with top/bottom walls and bounces vertically.
- Collides with player and opponent paddles and bounces horizontally.
- Resets to the center if it goes past the left or right edge.
- Uses hardcoded 1280x720 bounds for simplicity.
(In larger projects, these would be dynamic
PolarisKit automates this.)
- Assumes all assets are correctly located in the /assets folder
"""
import pygame, random
class Ball:
def __init__(self, surface, x_position, y_position, x_speed, y_speed):
self.image = surface
self.rect = self.image.get_rect(center=(x_position, y_position))
self.x_speed = x_speed * random.choice((-1, 1))
self.y_speed = y_speed * random.choice((-1, 1))
self.active = False # Ball starts inactive until the game begins
self.paddle_hit = pygame.mixer.Sound("assets/paddle_hit.wav")
self.wall_hit = pygame.mixer.Sound("assets/wall_hit.wav")
def update(self, player, opponent):
if self.active:
self.move()
self.check_collisions(player, opponent)
def move(self):
self.rect.x += self.x_speed
self.rect.y += self.y_speed
def check_collisions(self, player, opponent):
# Paddle collisions (simple version only flips horizontal direction)
if self.rect.top <= 0:
self.rect.top = 0
self.y_speed *= -1
pygame.mixer.Sound.play(self.wall_hit)
if self.rect.bottom >= 720:
self.rect.bottom = 720
self.y_speed *= -1
pygame.mixer.Sound.play(self.wall_hit)
# Paddle collisions
if self.rect.colliderect(player.rect):
self.x_speed *= -1
pygame.mixer.Sound.play(self.paddle_hit)
if self.rect.colliderect(opponent.rect):
self.x_speed *= -1
pygame.mixer.Sound.play(self.paddle_hit)
def reset(self):
# re-center to middle of screen
self.rect.center = (
1280 // 2,
720 // 2
)
self.active = False
def start(self):
self.active = True
self.x_speed = abs(self.x_speed) * random.choice((-1, 1))
self.y_speed = abs(self.y_speed) * random.choice((-1, 1))
def draw(self, screen):
screen.blit(self.image, self.rect)