-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpongCoPilot.py
More file actions
221 lines (183 loc) · 7.21 KB
/
pongCoPilot.py
File metadata and controls
221 lines (183 loc) · 7.21 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""
Simple Pong game using Pygame.
Controls:
- Left paddle: W (up), S (down)
- Right paddle: Up Arrow, Down Arrow
- Toggle single-player AI: TAB
- Pause / Resume: P
- Reset scores & ball: R
- Quit: ESC or close window
"""
import pygame
import sys
import random
# ---------- Configuration ----------
WIDTH, HEIGHT = 800, 600
FPS = 60
PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100
PADDLE_SPEED = 6
BALL_SIZE = 14
BALL_SPEED = 5
BALL_SPEED_INCREMENT = 0.5 # speed increase after each paddle hit, small
SCORE_FONT_SIZE = 48
INFO_FONT_SIZE = 20
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# ---------- Game Objects ----------
class Paddle:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT)
self.speed = PADDLE_SPEED
def move(self, dy):
self.rect.y += dy
# Clamp to screen
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
def draw(self, surf):
pygame.draw.rect(surf, WHITE, self.rect)
class Ball:
def __init__(self):
self.rect = pygame.Rect((WIDTH // 2 - BALL_SIZE // 2,
HEIGHT // 2 - BALL_SIZE // 2,
BALL_SIZE, BALL_SIZE))
self.vx = BALL_SPEED * random.choice((-1, 1))
self.vy = random.choice((-1, 1)) * BALL_SPEED * 0.75
def reset(self, direction=None):
self.rect.center = (WIDTH // 2, HEIGHT // 2)
speed = BALL_SPEED
self.vx = speed * (direction if direction is not None else random.choice((-1, 1)))
self.vy = random.choice((-1, 1)) * speed * 0.75
def update(self):
self.rect.x += int(self.vx)
self.rect.y += int(self.vy)
# bounce top/bottom
if self.rect.top <= 0:
self.rect.top = 0
self.vy = -self.vy
if self.rect.bottom >= HEIGHT:
self.rect.bottom = HEIGHT
self.vy = -self.vy
def draw(self, surf):
pygame.draw.rect(surf, WHITE, self.rect)
# ---------- Utilities ----------
def draw_centered_text(surf, text, font, color, y):
rendered = font.render(text, True, color)
rect = rendered.get_rect(center=(WIDTH // 2, y))
surf.blit(rendered, rect)
# ---------- Main Game ----------
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong - Python / Pygame")
clock = pygame.time.Clock()
score_left = 0
score_right = 0
left = Paddle(20, HEIGHT // 2 - PADDLE_HEIGHT // 2)
right = Paddle(WIDTH - 20 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2)
ball = Ball()
score_font = pygame.font.SysFont(None, SCORE_FONT_SIZE)
info_font = pygame.font.SysFont(None, INFO_FONT_SIZE)
paused = False
single_player = False # toggle AI for right paddle
# Simple AI parameters
ai_max_speed = PADDLE_SPEED - 1 # slightly slower than player
ai_error_chance = 0.15 # chance to miss / not follow perfectly
running = True
while running:
dt = clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_p:
paused = not paused
elif event.key == pygame.K_r:
score_left = 0
score_right = 0
ball.reset()
paused = False
elif event.key == pygame.K_TAB:
single_player = not single_player
keys = pygame.key.get_pressed()
if not paused:
# Left paddle controls (W/S)
if keys[pygame.K_w]:
left.move(-left.speed)
if keys[pygame.K_s]:
left.move(left.speed)
# Right paddle controls (Up/Down) or AI
if single_player:
# Very simple predictive AI: follow ball's y with limited speed
# Add slight randomness to make it beatable
if random.random() < ai_error_chance:
# occasionally do nothing (makes mistakes)
pass
else:
if right.rect.centery < ball.rect.centery:
right.move(ai_max_speed)
elif right.rect.centery > ball.rect.centery:
right.move(-ai_max_speed)
else:
if keys[pygame.K_UP]:
right.move(-right.speed)
if keys[pygame.K_DOWN]:
right.move(right.speed)
# Move ball
ball.update()
# Collision with paddles
if ball.rect.colliderect(left.rect):
# Ensure ball moves to the right
ball.rect.left = left.rect.right
ball.vx = -ball.vx + BALL_SPEED_INCREMENT # small speed change
# add angle based on where it hit the paddle
offset = (ball.rect.centery - left.rect.centery) / (PADDLE_HEIGHT / 2)
ball.vy = BALL_SPEED * offset
elif ball.rect.colliderect(right.rect):
ball.rect.right = right.rect.left
ball.vx = -ball.vx - BALL_SPEED_INCREMENT
offset = (ball.rect.centery - right.rect.centery) / (PADDLE_HEIGHT / 2)
ball.vy = BALL_SPEED * offset
# Score check
if ball.rect.left <= 0:
score_right += 1
ball.reset(direction=-1)
paused = True # pause briefly until unpaused
elif ball.rect.right >= WIDTH:
score_left += 1
ball.reset(direction=1)
paused = True
# ---------- Drawing ----------
screen.fill(BLACK)
# middle line
for y in range(10, HEIGHT, 30):
pygame.draw.rect(screen, WHITE, (WIDTH // 2 - 1, y, 2, 20))
left.draw(screen)
right.draw(screen)
ball.draw(screen)
# Scores
left_surf = score_font.render(str(score_left), True, WHITE)
right_surf = score_font.render(str(score_right), True, WHITE)
screen.blit(left_surf, (WIDTH // 4 - left_surf.get_width() // 2, 20))
screen.blit(right_surf, (WIDTH * 3 // 4 - right_surf.get_width() // 2, 20))
# Info
info_lines = [
"Left: W/S Right: Up/Down",
"TAB: Toggle single-player AI P: Pause",
"R: Reset scores ESC: Quit",
f"Mode: {'Single-player (AI)' if single_player else 'Two-player'}",
f"Paused: {'Yes' if paused else 'No'}"
]
for i, line in enumerate(info_lines):
surf = info_font.render(line, True, WHITE)
screen.blit(surf, (10, HEIGHT - (len(info_lines) - i) * (INFO_FONT_SIZE + 4)))
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()