-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHATGPTPONGM1.py
More file actions
332 lines (293 loc) · 11.4 KB
/
CHATGPTPONGM1.py
File metadata and controls
332 lines (293 loc) · 11.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import pygame as pg
import sys
import math
import random
from enum import Enum, auto
from typing import Tuple
# Define game states
class GameState(Enum):
MENU = auto()
PLAYING = auto()
GAME_OVER = auto()
# Define color palette
colors = {
'WHITE': (255, 255, 255),
'NEON_PINK': (255, 20, 147),
'NEON_BLUE': (0, 191, 255),
'BG_TOP': (15, 0, 30),
'BG_BOTTOM': (30, 0, 60),
'GRID': (50, 50, 50),
'GAME_OVER': (255, 0, 0) # Red color for Game Over text
}
# Define configuration settings
class Config:
WIDTH = 800
HEIGHT = 600
FPS = 60
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 100
BALL_SIZE = 20
PADDLE_SPEED = 7
BALL_SPEED = 8
WIN_SCORE = 5
FONT_SIZE = 64
# Background class with animation
class Background:
def __init__(self, config: Config):
self.config = config
self.surface = self._create_background()
self.scroll_offset = 0
self.scroll_speed = 1
def _create_background(self) -> pg.Surface:
"""
Create a gradient background that transitions from BG_TOP to BG_BOTTOM.
"""
surface = pg.Surface((self.config.WIDTH, self.config.HEIGHT))
for y in range(self.config.HEIGHT):
ratio = y / self.config.HEIGHT
color = tuple(
int(colors['BG_TOP'][i] * (1 - ratio) + colors['BG_BOTTOM'][i] * ratio)
for i in range(3)
)
pg.draw.line(surface, color, (0, y), (self.config.WIDTH, y))
return surface
def update(self):
"""
Update the scrolling effect for the background grid lines.
"""
self.scroll_offset = (self.scroll_offset + self.scroll_speed) % self.config.HEIGHT
def draw(self, screen: pg.Surface):
"""
Draw the gradient background and overlay scrolling grid lines.
"""
screen.blit(self.surface, (0, 0))
for y in range(0, self.config.HEIGHT, 40):
grid_y = (y + self.scroll_offset) % self.config.HEIGHT
pg.draw.line(screen, colors['GRID'], (0, grid_y), (self.config.WIDTH, grid_y))
# Ball class
class Ball:
def __init__(self, config: Config):
self.config = config
self.rect = pg.Rect(
config.WIDTH // 2 - config.BALL_SIZE // 2,
config.HEIGHT // 2 - config.BALL_SIZE // 2,
config.BALL_SIZE,
config.BALL_SIZE
)
self.reset()
def reset(self):
"""
Place the ball at the center and launch it in a random direction.
"""
self.rect.center = (self.config.WIDTH // 2, self.config.HEIGHT // 2)
angle = random.uniform(-45, 45) # Randomize angle between -45° and 45°
direction = random.choice([-1, 1]) # Randomize horizontal direction
self.speed_x = direction * self.config.BALL_SPEED * math.cos(math.radians(angle))
self.speed_y = self.config.BALL_SPEED * math.sin(math.radians(angle))
def update(self, paddles):
"""
Move the ball and handle collisions with walls and paddles.
"""
self.rect.x += self.speed_x
self.rect.y += self.speed_y
# Bounce off top or bottom
if self.rect.top <= 0 or self.rect.bottom >= self.config.HEIGHT:
self.speed_y *= -1
# Paddle collisions
for paddle in paddles:
if self.rect.colliderect(paddle.rect):
hit_pos = (self.rect.centery - paddle.rect.centery) / (self.config.PADDLE_HEIGHT / 2)
# Increase speed slightly and reverse direction
self.speed_x *= -1.1
self.speed_y += hit_pos * 2
# Prevent ball from "sticking" inside the paddle
if self.speed_x > 0:
self.rect.left = paddle.rect.right
else:
self.rect.right = paddle.rect.left
def draw(self, screen: pg.Surface):
pg.draw.ellipse(screen, colors['WHITE'], self.rect)
# Paddle class with optional glow effect
class Paddle:
def __init__(self, x: int, config: Config, color: Tuple[int, ...]):
self.config = config
self.color = color
self.speed = config.PADDLE_SPEED
self.rect = pg.Rect(
x,
config.HEIGHT // 2 - config.PADDLE_HEIGHT // 2,
config.PADDLE_WIDTH,
config.PADDLE_HEIGHT
)
self.glow_timer = 0
def move(self, up: bool):
"""
Move the paddle up or down, ensuring it stays in the screen.
"""
self.rect.y += -self.speed if up else self.speed
self.rect.y = max(0, min(self.config.HEIGHT - self.config.PADDLE_HEIGHT, self.rect.y))
def trigger_glow(self):
"""
Trigger a temporary glow effect on the paddle.
"""
self.glow_timer = 10
def update(self):
if self.glow_timer > 0:
self.glow_timer -= 1
def draw(self, screen: pg.Surface):
# Optional: Draw paddle glow
if self.glow_timer > 0:
glow_surface = pg.Surface((self.rect.width + 20, self.rect.height + 20), pg.SRCALPHA)
pg.draw.ellipse(glow_surface, (*self.color, 100), glow_surface.get_rect())
screen.blit(glow_surface, (self.rect.x - 10, self.rect.y - 10))
# Draw the paddle rectangle
pg.draw.rect(screen, self.color, self.rect)
# Main game class
class PongGame:
def __init__(self):
pg.init()
self.config = Config()
self.screen = pg.display.set_mode((self.config.WIDTH, self.config.HEIGHT))
pg.display.set_caption("Synthwave Pong")
self.clock = pg.time.Clock()
self.font = pg.font.Font(None, self.config.FONT_SIZE)
self.background = Background(self.config)
self.reset_game()
def reset_game(self):
"""
Reset all variables to initial conditions.
"""
self.state = GameState.MENU
self.single_player = False
self.score = [0, 0]
self.ball = Ball(self.config)
self.paddles = [
Paddle(25, self.config, colors['NEON_PINK']),
Paddle(self.config.WIDTH - 35, self.config, colors['NEON_BLUE'])
]
def handle_events(self):
"""
Handle all pygame events.
Returns False if the window should close, True otherwise.
"""
for event in pg.event.get():
if event.type == pg.QUIT:
return False
elif event.type == pg.KEYDOWN:
# MENU state inputs
if self.state == GameState.MENU:
if event.key == pg.K_1:
self.single_player = True
self.state = GameState.PLAYING
elif event.key == pg.K_2:
self.single_player = False
self.state = GameState.PLAYING
# GAME_OVER state inputs
elif self.state == GameState.GAME_OVER:
if event.key == pg.K_ESCAPE:
self.reset_game()
# Other states
else:
if event.key == pg.K_ESCAPE:
self.reset_game()
return True
def update(self):
"""
Update game objects based on the current game state.
"""
# MENU state: Display static or animate background
if self.state == GameState.MENU:
self.background.update()
# PLAYING state
elif self.state == GameState.PLAYING:
self.background.update()
self.ball.update(self.paddles)
# Update paddles (including glow timers)
for paddle in self.paddles:
paddle.update()
# Single-player AI
if self.single_player:
# Move AI paddle with a bit of buffer
if self.ball.rect.centery > self.paddles[1].rect.centery + 20:
self.paddles[1].move(False)
elif self.ball.rect.centery < self.paddles[1].rect.centery - 20:
self.paddles[1].move(True)
# Multiplayer controls for right paddle if not single-player
keys = pg.key.get_pressed()
if keys[pg.K_w]:
self.paddles[0].move(True)
if keys[pg.K_s]:
self.paddles[0].move(False)
if not self.single_player:
if keys[pg.K_UP]:
self.paddles[1].move(True)
if keys[pg.K_DOWN]:
self.paddles[1].move(False)
# Scoring logic
if self.ball.rect.left <= 0:
self.score[1] += 1
self.ball.reset()
elif self.ball.rect.right >= self.config.WIDTH:
self.score[0] += 1
self.ball.reset()
# Check for Game Over
if max(self.score) >= self.config.WIN_SCORE:
self.state = GameState.GAME_OVER
# GAME_OVER state: Wait for ESC to reset
def draw_menu(self):
"""
Draw the main menu screen.
"""
self.background.draw(self.screen)
title_text = self.font.render("Synthwave Pong", True, colors['WHITE'])
single_text = self.font.render("Press 1 for Single-Player", True, colors['NEON_PINK'])
multi_text = self.font.render("Press 2 for Multiplayer", True, colors['NEON_BLUE'])
self.screen.blit(title_text, (self.config.WIDTH // 2 - title_text.get_width() // 2, 150))
self.screen.blit(single_text, (self.config.WIDTH // 2 - single_text.get_width() // 2, 300))
self.screen.blit(multi_text, (self.config.WIDTH // 2 - multi_text.get_width() // 2, 400))
def draw_playing(self):
"""
Draw the gameplay screen including background, ball, paddles, and score.
"""
self.background.draw(self.screen)
self.ball.draw(self.screen)
for paddle in self.paddles:
paddle.draw(self.screen)
# Score display
score_text = self.font.render(f"{self.score[0]} - {self.score[1]}", True, colors['WHITE'])
self.screen.blit(score_text, (self.config.WIDTH // 2 - score_text.get_width() // 2, 20))
def draw_game_over(self):
"""
Display a Game Over screen with the winner and reset instructions.
"""
self.background.draw(self.screen)
winner = "Player 1" if self.score[0] > self.score[1] else ("Player 2" if not self.single_player else "AI")
game_over_text = self.font.render(f"{winner} Wins!", True, colors['WHITE'])
restart_text = self.font.render("Press ESC to Restart", True, colors['WHITE'])
self.screen.blit(game_over_text, (self.config.WIDTH // 2 - game_over_text.get_width() // 2, 250))
self.screen.blit(restart_text, (self.config.WIDTH // 2 - restart_text.get_width() // 2, 350))
def draw(self):
"""
Draw the correct screen depending on the game state.
"""
if self.state == GameState.MENU:
self.draw_menu()
elif self.state == GameState.PLAYING:
self.draw_playing()
elif self.state == GameState.GAME_OVER:
self.draw_game_over()
pg.display.flip()
def run(self):
"""
Main game loop.
"""
running = True
while running:
running = self.handle_events()
self.update()
self.draw()
self.clock.tick(self.config.FPS)
pg.quit()
sys.exit()
if __name__ == "__main__":
PongGame().run()