-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.py
505 lines (394 loc) · 16.8 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import pygame
from pygame.locals import QUIT
import random
import time
from tkinter import *
from tkinter import ttk
# Put the pygame window inside of a function in order to initialize it later
def screen_init(mode):
global screen
pygame.init()
# mode 1 is single player and mode 2 is two player
if mode == 1:
screen = pygame.display.set_mode((400,300))
elif mode == 2:
screen = pygame.display.set_mode((600,450))
# A pause window for when the window is out of focus
def paused(mode):
# This for loop keeps pygame responding and allows for smooth window closing
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
# Make a blank screen
screen.fill("black")
# Make the font bigger
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 35)
# Define the shape of the text for the pause window
pause = font.render("PAUSED",False,"white")
pause2 = pause.get_rect()
# Make the font smaller
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 18)
# Define the shape of the text for the pause window
info = font.render("The window is out of focus.",False,"white")
info2 = info.get_rect()
# Make the font smaller
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 15)
# Define the shape of the text for the pause windows
resume = font.render("Click on this window to resume play.",False,"white")
resume2 = resume.get_rect()
# Mode 1 is for the single player window, else is for mode 2 (two player)
if mode == 1:
pause2.center = (200, 75)
info2.center = (200, 150)
resume2.center = (200, 185)
else:
pause2.center = (300, 150)
info2.center = (300, 225)
resume2.center = (300, 260)
# Render the text
screen.blit(pause, pause2)
screen.blit(info, info2)
screen.blit(resume, resume2)
# Display the text on the screen
pygame.display.update()
# The winner/loser screen for the two player mode
def winner(score1,score2):
# The dimensions and placement for the dummy paddles and ball
pad_width = 10
pad_height = 55
center_x = screen.get_width()/2
center_y = screen.get_height()/2
pad1_x = 20
pad1_y = center_y-(pad_height/2)
pad2_x = 570
pad2_y = center_y-(pad_height/2)
ball_x = (center_x)-4
ball_y = (center_y)-4
pygame.display.set_caption("Pong")
greater = ""
# go is another way to stop the while loop
go = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
# If Enter hit, set go to False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
go = False
break
# If go set False by hitting Enter, stop the while loop
if go == False:
break
# Draw the summy paddles and ball + the final scores
screen.fill("black")
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 75)
ball = pygame.draw.rect(screen, "white", (ball_x, ball_y, 8, 8))
paddle1 = pygame.draw.rect(screen, "white", (pad1_x, pad1_y, pad_width, pad_height))
paddle2 = pygame.draw.rect(screen, "white", (pad2_x, pad2_y, pad_width, pad_height))
scr1 = font.render(str(score1), True, "white")
scr2 = font.render(str(score2), True, "white")
textRect = scr1.get_rect()
textRect.center = (150, 75)
screen.blit(scr1, textRect)
textRect = scr2.get_rect()
textRect.center = (450, 75)
screen.blit(scr2, textRect)
better = font.render(greater,False,"white")
textRect = better.get_rect()
textRect.center = (300, 75)
screen.blit(better, textRect)
# Orientes the loser/winner text
if score1 > score2:
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 50)
greater = ">"
worl1 = font.render("Winner!",False,"white")
worl2 = font.render("Loser",False,"white")
elif score1 < score2:
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 50)
greater = "<"
worl1 = font.render("Loser",False,"white")
worl2 = font.render("Winner!",False,"white")
else:
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 50)
greater = "="
worl1 = font.render("Loser",False,"white")
worl2 = font.render("Loser",False,"white")
textRect = worl1.get_rect()
textRect.center = (150, 325)
screen.blit(worl1, textRect)
textRect = worl2.get_rect()
textRect.center = (450, 325)
screen.blit(worl2, textRect)
# More text
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 30)
hitenter = font.render("Hit Enter to continue...",False,"white")
textRect = hitenter.get_rect()
textRect.center = (300, 400)
screen.blit(hitenter, textRect)
# Display everything
pygame.display.update()
# Get it? "fun" - because it's fun when you have friends
def fun(smooth):
# Define dimensions and coordinates for all objects
pygame.display.set_caption("Pong")
pad_width = 10
pad_height = 55
center_x = screen.get_width()/2
center_y = screen.get_height()/2
pad1_x = 20
pad1_y = center_y-(pad_height/2)
pad2_x = 570
pad2_y = center_y-(pad_height/2)
ball_x = (center_x)-4
ball_y = (center_y)-4
rate_x = 2
rate_y = 2
game_speed = 1
score1 = 0
score2 = 0
font = pygame.font.Font("PixeloidSansBold-PKnYd.ttf", 30)
if random.randint(1,2) == 1:
rate_x = -1*rate_x
rate_y = -1*rate_y
# Resets everything back to their original values and displays the winner/loser screen
def reset(score1,score2,pad1_y,pad2_y):
if score1 == 11 or score2 == 11:
winner(score1,score2)
score1 = 0
score2 = 0
pad1_y = center_y-(pad_height/2)
pad2_y = pad1_y
ball_x = (center_x)-4
ball_y = (center_y)-4
game_speed = 1
return ball_x,ball_y,game_speed,score1,score2,pad1_y,pad2_y
# Returns the value of the variable true if the ball is touching the given paddle or not
def is_touching(ball_x,ball_y,paddle):
true = False
for i in range(8):
if paddle.collidepoint(ball_x,(ball_y+i)) == True:
true = True
break
return true
# This function makes the entire game work
def move_ball(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y,clock_speed):
def reset_sequence(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y):
ball_x,ball_y,game_speed,score1,score2,pad1_y,pad2_y = reset(score1,score2,pad1_y,pad2_y)
# Choose the direction for the ball to go after everything has been reset
if random.randint(1,2) == 1:
rate_x = -2
rate_y = -2
else:
rate_x = 2
rate_y = 2
return game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y
# If the ball hits the left or right walls
if ball_x <= 1:
score2 += 1
game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y = reset_sequence(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y)
move_ball(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y,clock_speed)
elif (ball_x+8) >= 599:
score1 += 1
game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y = reset_sequence(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y)
move_ball(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y,clock_speed)
# If the ball hits walls or the paddles, change movement accordingly
else:
if (ball_y) <= 1:
rate_y = -1*rate_y
elif (ball_y+8) >= 449:
rate_y = -1*rate_y
if is_touching(ball_x,ball_y,paddle1) == True:
rate_x = -1*rate_x
elif is_touching((ball_x+8),ball_y,paddle2) == True:
rate_x = -1*rate_x
ball_x += ((game_speed*rate_x)/(clock_speed/60))
ball_y += ((game_speed*rate_y)/(clock_speed/60))
# Return everything that was changed
return rate_x,rate_y,ball_x,ball_y,game_speed,score1,score2,pad1_y,pad2_y
# count is for game speed; as count increases, so do the game speed
count = 0
if smooth:
clock_speed = 600
else:
clock_speed = 60
while True:
tick = pygame.time.Clock().tick(clock_speed)
# If the window isn't focused, run the paused function - In a while loop so that it keeps checking
if pygame.key.get_focused() == False:
while pygame.key.get_focused() == False:
paused(2)
count += 1
# Keypress detection
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
if pad2_y > 0:
pad2_y -= 5/(clock_speed/60)
if keys[pygame.K_DOWN]:
if pad2_y < (screen.get_height()-pad_height):
pad2_y += 5/(clock_speed/60)
if keys[pygame.K_w]:
if pad1_y > 0:
pad1_y -= 5/(clock_speed/60)
if keys[pygame.K_s]:
if pad1_y < (screen.get_height()-pad_height):
pad1_y += 5/(clock_speed/60)
screen.fill("black")
# Increases game_speed with count
if count == (25*(clock_speed/60)):
count = 0
game_speed += .015
# Draw the Pong board, paddles, ball, and score
top_wall = pygame.draw.rect(screen, "white", (0, 0, 600, 1))
right_wall = pygame.draw.rect(screen, "white", (599, 0, 1, 450))
bottom_wall = pygame.draw.rect(screen, "white", (0, 449, 600, 1))
left_wall = pygame.draw.rect(screen, "white", (0, 0, 1, 450))
paddle1 = pygame.draw.rect(screen, "white", (pad1_x, pad1_y, pad_width, pad_height))
paddle2 = pygame.draw.rect(screen, "white", (pad2_x, pad2_y, pad_width, pad_height))
ball = pygame.draw.rect(screen, "white", (ball_x, ball_y, 8, 8))
rate_x,rate_y,ball_x,ball_y,game_speed,score1,score2,pad1_y,pad2_y=move_ball(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,paddle2,ball,score1,score2,pad1_y,pad2_y,clock_speed)
scr1 = font.render(str(score1), True, "white")
scr2 = font.render(str(score2), True, "white")
textRect = scr1.get_rect()
textRect.center = (150, 30)
screen.blit(scr1, textRect)
textRect = scr2.get_rect()
textRect.center = (450, 30)
screen.blit(scr2, textRect)
# Display everything
pygame.display.update()
# This is the single player version of everything from above (meaning it's simpler). If you have a question about this one, just find the bit of code from above closest to it and read the comments for that code (I'm not writing comments twice).
def lonely(smooth):
print(smooth)
pygame.display.set_caption('1 Player Pong')
pad_width = 10
pad_height = 55
center_x = screen.get_width()/2
center_y = screen.get_height()/2
pad_x = 20
pad_y = center_y-(pad_height/2)
ball_x = (center_x)-4
ball_y = (center_y)-4
rate_x = 2
rate_y = 2
game_speed = 1
score = 0
font = pygame.font.Font('PixeloidSansBold-PKnYd.ttf', 20)
if random.randint(1,2) == 1:
rate_x = -1*rate_x
rate_y = -1*rate_y
def reset():
ball_x = (center_x)-4
ball_y = (center_y)-4
game_speed = 1
score = 0
return ball_x,ball_y,game_speed,score
def is_touching(ball_x,ball_y,paddle1):
true = False
for i in range(8):
if paddle1.collidepoint(ball_x,(ball_y+i)) == True:
true = True
break
return true
def move_ball(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,ball,score,clock_speed):
if (ball_x) <= 1:
ball_x,ball_y,game_speed,score = reset()
move_ball(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,ball,score,clock_speed)
if random.randint(1,2) == 1:
rate_x = -2
rate_y = -2
else:
rate_x = 2
rate_y = 2
else:
if (ball_y) <= 1:
rate_y = -1*rate_y
elif (ball_y+8) >= 299:
rate_y = -1*rate_y
if (ball_x+8) >= 399:
rate_x = -1*rate_x
if is_touching(ball_x,ball_y,paddle1) == True:
rate_x = -1*rate_x
score += 1
ball_x += ((game_speed*rate_x)/(clock_speed/60))
ball_y += ((game_speed*rate_y)/(clock_speed/60))
return rate_x,rate_y,ball_x,ball_y,game_speed,score
count = 0
if smooth:
clock_speed = 600
else:
clock_speed = 60
while True:
tick = pygame.time.Clock().tick(clock_speed)
if pygame.key.get_focused() == False:
while pygame.key.get_focused() == False:
paused(1)
count += 1
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
if pad_y > 0:
pad_y -= 5/(clock_speed/60)
if keys[pygame.K_DOWN]:
if pad_y < (screen.get_height()-pad_height):
pad_y += 5/(clock_speed/60)
screen.fill("black")
if count == (25/(clock_speed/60)):
count = 0
game_speed += .015
top_wall = pygame.draw.rect(screen, "white", (0, 0, 400, 1))
right_wall = pygame.draw.rect(screen, "white", (399, 0, 1, 300))
bottom_wall = pygame.draw.rect(screen, "white", (0, 299, 400, 1))
left_wall = pygame.draw.rect(screen, "white", (0, 0, 1, 300))
paddle1 = pygame.draw.rect(screen, "white", (pad_x, pad_y, pad_width, pad_height))
ball = pygame.draw.rect(screen, "white", (ball_x, ball_y, 8, 8))
rate_x,rate_y,ball_x,ball_y,game_speed,score=move_ball(game_speed,rate_x,rate_y,ball_x,ball_y,paddle1,ball,score,clock_speed)
text = font.render(str(score), True, "white")
textRect = text.get_rect()
textRect.center = (400 // 2, 20)
screen.blit(text, textRect)
pygame.display.update()
print(count2)
display_window = True
# Putting the Tkinter window in a while loop allows the mode-selection window to popup again if pong is closed.
while display_window == True:
root = Tk()
root.title("Pong Selection")
smooth = IntVar()
# The one_player(), two_players(), and stop_all() functions are so multiple commands can be run by one button-press.
def one_player():
global root,smooth
root.destroy()
screen_init(1)
lonely(smooth.get())
def two_players():
global root,smooth
root.destroy()
screen_init(2)
fun(smooth.get())
def stop_all():
global root,display_window
root.destroy()
display_window = False
# Defining the window's geometry and placement
root.geometry(f"300x200+{int((root.winfo_screenwidth()/2)-150)}+{int((root.winfo_screenheight()/2)-125)}")
# The text and buttons in the Tkinter windows
ttk.Label(root, text="Which mode of Pong would you like to play?").place(x=30,y=35)
ttk.Button(root, text="1 Player", command=one_player).place(x=75,y=90)
ttk.Button(root, text="2 Players", command=two_players).place(x=155,y=90)
ttk.Button(root, text="Quit", command=stop_all).place(x=115,y=140)
ttk.Label(root, text="Zakkai Thomas | 2023").place(x=10,y=180)
ttk.Checkbutton(root, text="Smooth Animation", variable=smooth).place(x=170,y=180)
root.mainloop()
# This took way longer to make than I care to admit.
#
# I hope that you learned something cool from my code.
#
# Thanks for being cool,
#
# - Zakkai Thomas