-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPong.py
380 lines (311 loc) · 11.3 KB
/
Pong.py
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# Pong Game made using Pygame
# Made By TANAY KAR
import os
import pygame
from pygame.locals import *
from sys import exit
import random
#__game_start__ = input("Start ?") # Only to be used to halt program startup during presentation
pygame.init()
pygame.font.init()
icon = pygame.image.load("Pictures/icon.png")
height = 480
width = 640
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption("Ping Pong")
pygame.display.set_icon(icon)
# Sounds
pygame.mixer.music.load("Audio/Background.mp3")
pygame.mixer.music.set_volume(0.7)
pygame.mixer.music.play(-1)
hit_sound = pygame.mixer.Sound("Audio/Hit.wav")
back = pygame.Surface((640, 480))
startback = pygame.image.load("Pictures/splashback.jpg")
startback = pygame.transform.scale(startback, (640, 480))
background = back.convert()
background.fill((0, 0, 0))
bar = pygame.Surface((10, 50))
bar1 = bar.convert()
bar1.fill((255, 204, 0))
bar2 = bar.convert()
bar2.fill((0, 155, 250))
circ_sur = pygame.Surface((15, 15))
circ = pygame.draw.circle(circ_sur, (255,255,255), (15 / 2, 15 / 2), 15 / 2)
circle = circ_sur.convert()
circle.set_colorkey((0, 0, 0))
Pause_Back = pygame.image.load("Pictures/PAUSE.png")
Pause_Back.set_alpha(100)
Pause_Back = pygame.transform.scale(Pause_Back, (640, 480))
# some definitions
bar1_x, bar2_x = 10., 620.
bar1_y, bar2_y = 215., 215.
circle_x, circle_y = 307.5, 232.5
bar1_move, bar2_move = 0., 0.
speed_x, speed_y, speed_circ = 200., 200., 240.
bar1_score, bar2_score = 0, 0
# clock and font objects
clock = pygame.time.Clock()
font = pygame.font.Font("Other/Font.TTF", 40)
msg_font = pygame.font.Font("Other/Font.TTF", 30)
abt2_font = pygame.font.Font("Other/Font.TTF", 50)
abt_font = pygame.font.Font("Other/Font.TTF", 20)
# Font
PauseMSG1 = font.render("Game Paused", True, (255, 255, 255))
PauseMSG2 = msg_font.render("Press SPACE to resume", True, (255, 255, 255))
PauseMSG1_rect = PauseMSG1.get_rect(
center=((width // 2), ((height // 2) - 35)))
PauseMSG2_rect = PauseMSG2.get_rect(
center=((width // 2), ((height // 2) + 35)))
# -------
About_MSG1 = abt2_font.render("ABOUT", True, (255, 255, 255))
About_MSG1_rect = About_MSG1.get_rect(
center=((width // 2), ((height // 2) - 86)))
About_MSG2 = abt_font.render("Pong Game", True, (255, 255, 255))
About_MSG2_rect = About_MSG2.get_rect(
center=((width // 2), ((height // 2) - 38)))
About_MSG3 = abt_font.render("Version - 1.9", True, (255, 255, 255))
About_MSG3_rect = About_MSG3.get_rect(center=((width // 2), ((height // 2))))
About_MSG4 = abt_font.render("Developed by Tanay Kar", True, (255, 255, 255))
About_MSG4_rect = About_MSG4.get_rect(
center=((width // 2), ((height // 2) + 38)))
About_MSG5 = abt_font.render("Press 'A' to resume game", True, (255, 255, 255))
About_MSG5_rect = About_MSG5.get_rect(
center=((width // 2), ((height // 2) + 86)))
# -------
Help_MSG1 = abt2_font.render("Help",True,(255,255,255))
Help_MSG1_rect = Help_MSG1.get_rect(center = ((width // 2), ((height // 2) - 86)))
Help_MSG2 = abt_font.render("Press 'SPACE' to pause or resume game", True, (255, 255, 255))
Help_MSG2_rect = Help_MSG2.get_rect(
center=((width // 2), ((height // 2) - 38)))
Help_MSG3 = abt_font.render("Press 'A' to show about page", True, (255, 255, 255))
Help_MSG3_rect = Help_MSG3.get_rect(center=((width // 2), ((height // 2))))
Help_MSG4 = abt_font.render("Press 'S' to take a screenshot of the game", True, (255, 255, 255))
Help_MSG4_rect = Help_MSG4.get_rect(
center=((width // 2), ((height // 2) + 38)))
Help_MSG5 = abt_font.render("Press 'R' to restart game", True, (255, 255, 255))
Help_MSG5_rect = Help_MSG5.get_rect(
center=((width // 2), ((height // 2) + 86)))
Help_MSG6 = abt_font.render("Press 'H' to return", True, (255, 255, 255))
Help_MSG6_rect = Help_MSG6.get_rect(
center=((width // 2), ((height // 2) + 124)))
# --------
muted = True
pause = False
run = True
start = True
def count_files(path: str) -> int:
file_entries = os.listdir(path)
return len(file_entries)
def mute():
global muted
if muted is False:
hit_sound.set_volume(0.0)
pygame.mixer.music.pause()
else:
hit_sound.set_volume(1.0)
pygame.mixer.music.unpause()
def paused():
global pause
screen.blit(Pause_Back, (0, 0))
screen.blit(PauseMSG1, PauseMSG1_rect)
screen.blit(PauseMSG2, PauseMSG2_rect)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.init()
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_SPACE or event.key == K_ESCAPE:
pause = False
pygame.display.update()
clock.tick(30)
def save():
pygame.time.delay(300)
print(count_files("Screenshots/"))
file_name = "Screenshots/Pong_screenshot" + \
str(count_files("Screenshots/") + 1) + ".png"
pygame.image.save(screen, file_name)
clock.tick(30)
def _help():
global pause
screen.blit(Pause_Back, (0, 0))
screen.blit(Pause_Back, (0, 0))
screen.blit(Help_MSG1, Help_MSG1_rect)
screen.blit(Help_MSG2, Help_MSG2_rect)
screen.blit(Help_MSG3, Help_MSG3_rect)
screen.blit(Help_MSG4, Help_MSG4_rect)
screen.blit(Help_MSG5, Help_MSG5_rect)
screen.blit(Help_MSG6, Help_MSG6_rect)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.init()
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_h:
pause = False
pygame.display.update()
clock.tick(30)
def about():
global pause
screen.blit(Pause_Back, (0, 0))
screen.blit(About_MSG1, About_MSG1_rect)
screen.blit(About_MSG2, About_MSG2_rect)
screen.blit(About_MSG3, About_MSG3_rect)
screen.blit(About_MSG4, About_MSG4_rect)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.init()
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_a:
pause = False
pygame.display.update()
clock.tick(30)
def START():
global start, muted, pause
while start:
pygame.display.init()
screen.blit(startback, (0, 0))
mute()
help_info_text = abt_font.render(
"Press H for help", True, (255, 255, 255))
screen.blit(help_info_text, (10, 450))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
start = False
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
start = False
elif event.key == K_a:
pause = True
about()
elif event.key == K_m:
muted = not muted
mute()
elif event.key == K_h:
pause = True
_help()
clock.tick(30)
pygame.display.flip()
while run:
for event in pygame.event.get():
if event.type == QUIT:
pygame.init()
pygame.quit()
run = False
exit()
if event.type == KEYDOWN:
if event.key == K_UP:
bar1_move = -ai_speed
elif event.key == K_DOWN:
bar1_move = ai_speed
elif event.key == K_SPACE:
pause = True
paused()
elif event.key == K_a:
pause = True
about()
elif event.key == K_s:
save()
elif event.key == K_m:
muted = not muted
mute()
elif event.key == K_h:
pause = True
_help()
elif event.key == K_r:
# restart()
bar1_score = 0
bar2_score = 0
score1 = font.render(str(bar1_score), True, (255, 255, 255))
score2 = font.render(str(bar2_score), True, (255, 255, 255))
screen.blit(score1, (250., 210.))
screen.blit(score2, (380., 210.))
START()
continue
elif event.type == KEYUP:
if event.key == K_UP:
bar1_move = 0.
elif event.key == K_DOWN:
bar1_move = 0.
START()
info_text = abt_font.render("Press SPACE to pause", True, (255, 255, 255))
score1 = font.render(str(bar1_score), True, (255, 255, 255))
score2 = font.render(str(bar2_score), True, (255, 255, 255))
screen.blit(background, (0, 0))
frame = pygame.draw.rect(screen, (255, 255, 255),
Rect((5, 5), (630, 470)), 2)
middle_line = pygame.draw.aaline(
screen, (255, 255, 255), (330, 5), (330, 475))
screen.blit(bar1, (bar1_x, bar1_y))
screen.blit(bar2, (bar2_x, bar2_y))
screen.blit(circle, (circle_x, circle_y))
screen.blit(info_text, (10, 450))
screen.blit(score1, (250., 210.))
screen.blit(score2, (380., 210.))
bar1_y += bar1_move
# movement of circle
time_passed = clock.tick(30)
time_sec = time_passed / 1000.0
circle_x += speed_x * time_sec
circle_y += speed_y * time_sec
ai_speed = speed_circ * time_sec
ai_power = 1.5
# Algorithm of red paddle
if circle_x >= 305.:
if not bar2_y == circle_y + 7.5:
if bar2_y < circle_y + 7.5:
bar2_y += ai_speed - ai_power
if bar2_y > circle_y - 42.5:
bar2_y -= ai_speed + ai_power
else:
bar2_y == circle_y + 7.5
if bar1_y >= 420.:
bar1_y = 420.
elif bar1_y <= 10.:
bar1_y = 10.
if bar2_y >= 420.:
bar2_y = 420.
elif bar2_y <= 10.:
bar2_y = 10.
# Collision
# Paddle collision
if circle_x <= bar1_x + 15.:
# Paddle 1 (Player)
if circle_y >= bar1_y - 7.5 and circle_y <= bar1_y + 42.5:
circle_x = 20.
speed_x = -speed_x
bar1_score += 1
pygame.mixer.Sound.play(hit_sound)
if circle_x >= bar2_x - 15.:
# Paddle 2 (AI)
if circle_y >= bar2_y - 7.5 and circle_y <= bar2_y + 42.5:
circle_x = 605.
speed_x = -speed_x
bar2_score += 1
pygame.mixer.Sound.play(hit_sound)
# Wall collision
# No-Bounce
if circle_x < 5.:
bar2_score += 1
value = random.randint(5, 475)
circle_x, circle_y = 320., value
elif circle_x > 620.:
bar1_score += 1
value = random.randint(5, 475)
circle_x, circle_y = 307.5, value
# Bounce
if circle_y <= 10.:
speed_y = -speed_y
circle_y = 10.
elif circle_y >= 457.5:
speed_y = -speed_y
circle_y = 457.5
pygame.display.update()