forked from v-s-v-i-s-h-w-a-s/ping-pong
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
82 lines (64 loc) · 2.3 KB
/
main.py
File metadata and controls
82 lines (64 loc) · 2.3 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
import pygame
from game.game_engine import GameEngine
# Initialize pygame/Start application
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ping Pong - Pygame Version")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Clock
clock = pygame.time.Clock()
FPS = 60
# Game loop
engine = GameEngine(WIDTH, HEIGHT)
class DummySound:
def play(self):
pass # Silent implementation for when sound files are missing
def main():
# Initialize pygame mixer for sound
sounds = {
'paddle_hit': DummySound(),
'wall_hit': DummySound(),
'score': DummySound()
}
try:
pygame.mixer.init()
sounds['paddle_hit'] = pygame.mixer.Sound("sounds/paddle_hit.wav")
sounds['wall_hit'] = pygame.mixer.Sound("sounds/wall_hit.wav")
sounds['score'] = pygame.mixer.Sound("sounds/score.wav")
except (pygame.error, FileNotFoundError):
print("Warning: Sound files not found. Game will run without sound.")
running = True
while running:
SCREEN.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if engine.game_over:
engine.handle_game_over_input(event)
elif engine.show_replay_menu:
if engine.handle_replay_menu_input(event):
running = False
if not engine.game_over and not engine.show_replay_menu:
engine.handle_input()
# Store ball position before update
old_velocity_x = engine.ball.velocity_x
old_velocity_y = engine.ball.velocity_y
engine.update()
# Check for collisions and play sounds
if old_velocity_x != engine.ball.velocity_x:
sounds['paddle_hit'].play()
elif old_velocity_y != engine.ball.velocity_y:
sounds['wall_hit'].play()
# Check for scoring
if engine.ball.x <= 0 or engine.ball.x >= WIDTH:
sounds['score'].play()
engine.render(SCREEN)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == "__main__":
main()