-
Notifications
You must be signed in to change notification settings - Fork 0
/
2p.py
335 lines (282 loc) · 10.2 KB
/
2p.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
#import pygame library
import pygame,sys,random,math,os
from tkinter import messagebox
import threading
import time
pygame.init()
def wait_and_change_var():
global player1_frozen
global player2_frozen
time.sleep(3000)
player1_frozen = False
player2_frozen = False
return
#player animation
def playeranimation():
if player1.top<=10:
player1.top=10
if player1.bottom>=screenheight-10:
player1.bottom=screenheight-10
if player2.top<=10:
player2.top=10
if player2.bottom>=screenheight-10:
player2.bottom=screenheight-10
#ball animation
def ballanimation():
global ballspeedx,ballspeedy,player1score,player2score,scoretime,win1time,win2time
#speed
ball.x+=ballspeedx
ball.y+=ballspeedy
#bounce back and regeneration
if ball.top<=10 or ball.bottom>=screenheight-10:
pygame.mixer.Sound.play(pongsound)
ballspeedy*=-1
if ball.left<=10:
pygame.mixer.Sound.play(scoresound)
player2score+=1
if player2score==winscore:
win2time=pygame.time.get_ticks()#return present time in milliseconds
scoretime=pygame.time.get_ticks()
if ball.right>=screenwidth-10:
pygame.mixer.Sound.play(scoresound)
player1score+=1
if player1score==winscore:
win1time=pygame.time.get_ticks()
scoretime=pygame.time.get_ticks()
#collide with player
if ball.colliderect(player2) and ballspeedx>0:
pygame.mixer.Sound.play(pongsound)
if abs(ball.right-player2.left)<10:
ballspeedx*=-1
elif abs(ball.bottom-player2.top)<10 and ballspeedy>0:
ballspeedy*=-1
elif abs(ball.top-player2.bottom)<10 and ballspeedy<0:
ballspeedy*=-1
if ball.colliderect(player1) and ballspeedx<0:
pygame.mixer.Sound.play(pongsound)
if abs(ball.left-player1.right)<10:
ballspeedx*=-1
elif abs(ball.bottom-player1.top)<10 and ballspeedy>0:
ballspeedy*=-1
elif abs(ball.top-player1.bottom)<10 and ballspeedy<0:
ballspeedy*=-1
#ball regeneration
def ballstart():
global ballspeedx,ballspeedy,scoretime
currenttime=pygame.time.get_ticks()
ball.center = (screenwidth/2, screenheight/2)
if currenttime-scoretime<3000:
ballspeedx,ballspeedy=0,0
timetext=timefont.render(f"{math.floor(4-((currenttime-scoretime)/1000))}",False,timecolor)
screen.blit(timetext,(screenwidth-30,20))
else:
ballspeedy=ballspeed*random.choice((1,-1)) # this is done to move the ball after colliding with side walls
ballspeedx=ballspeed*random.choice((1,-1))
scoretime=None
#display win message
def display1win():
global win1time,ballspeedx,ballspeedy
ball.center = (screenwidth/2, screenheight/2)
close1time=pygame.time.get_ticks()
if close1time-win1time<2000:
ballspeedx,ballspeedy=0,0
win1text=winfont.render("!! Player1 wins !!",False,wincolor)
screen.blit(win1text,(screenwidth/2-360,screenheight/2+100))
else:
#pygame.quit()
#sys.quit()
#os.system("python jkpingpong.py")
# messagebox.showinfo("Message","THANK YOU FOR PLAYING")
pygame.quit()
def display2win():
global win2time,ballspeedx,ballspeedy
ball.center = (screenwidth/2, screenheight/2)
close2time=pygame.time.get_ticks()
if close2time-win2time<2000:
ballspeedx,ballspeedy=0,0
win2text=winfont.render("!! Player2 wins !!",False,wincolor)
screen.blit(win2text,(screenwidth/2+20,screenheight/2+100))
else:
#pygame.quit()
#sys.quit()
#os.system("python jkpingpong.py")
# messagebox.showinfo("Message","THANK YOU FOR PLAYING")
pygame.quit()
def iceanimation():
global ice,icetime
icefinal=pygame.time.get_ticks()
ice.y+=icespeed
screen.blit(iceimage,(ice.x,ice.y))
if ((icefinal-iceinitial)>icetime):
icetime+=icetime
ice.x=random.randint(10,screenwidth-10)
ice.y=0
ice.y+=icespeed
pygame.mixer.pre_init(44100,-16,2,512)
clock=pygame.time.Clock()
#creating new window
screenwidth=1200
screenheight=700
screen=pygame.display.set_mode((screenwidth,screenheight))
screen.set_alpha(None)
#name for window
name=pygame.display.set_caption("Ping Pong Game")
#icon for game
icon=pygame.image.load("./media/images/image.jpg")
pygame.display.set_icon(icon)
# ice block
icespeed=1
iceimage=pygame.image.load("./media/images/ice.png").convert_alpha()
iceimage=pygame.transform.scale(iceimage,(75,75))
ice=iceimage.get_rect(topleft=(random.randint(10,screenwidth-10),0))
iceinitial=pygame.time.get_ticks()
icetime=5000
#colours
bgcolor=(144,238,144)
ballcolor=(255,165,0)
player1color=(255,0,0)
player2color=(0,0,0)
linecolor=(0,0,0)
scorecolor=(0,96,255)
timecolor=(255,0,255)
welcomecolor=(255,0,0)
wincolor=(255,0,127)
aimcolor=(81,4,0)
#creating shapes
ball=pygame.Rect(screenwidth/2-10,screenheight/2-10,20,20)
player1=pygame.Rect(10, screenheight / 2 - 70,10,120)
player2=pygame.Rect(screenwidth - 20, screenheight / 2 - 70,10,120)
#speed
ballspeed=6
ballspeedx=ballspeed * random.choice((1,-1))#this is done so that ball will move in random direction after colliding with side walls
ballspeedy=ballspeed * random.choice((1,-1))
player1speed=0
player2speed=0
speed=4
#score details
player1score=0
player2score=0
scorefont=pygame.font.SysFont("castellar",20)
#score time
scoretime=True
timefont=pygame.font.SysFont("algerian",32)
#welcome
welcomefont=pygame.font.SysFont("algerian",32)
#win
win1time=None
win2time=None
winfont=pygame.font.SysFont("script",50)
winscore=5
#aim
aimfont=pygame.font.SysFont("algerian",25)
#sound
pongsound=pygame.mixer.Sound("./media/sounds/pong.wav")
scoresound=pygame.mixer.Sound("./media/sounds/score.wav")
player1_frozen = False
player2_frozen = False
#run the window
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
#pygame.quit()
#sys.quit()
#os.system("python jkpingpong.py")
# messagebox.showinfo("Message","THANK YOU FOR PLAYING")
pygame.quit()
# if event.type==pygame.KEYDOWN:
# if event.key==pygame.K_s:
# player1speed+=speed
# if event.key==pygame.K_w:
# player1speed-=speed
# if event.type==pygame.KEYUP:
# if event.key==pygame.K_s:
# player1speed-=speed
# if event.key==pygame.K_w:
# player1speed+=speed
# if event.type==pygame.KEYDOWN:
# if event.key==pygame.K_DOWN:
# player2speed+=speed
# if event.key==pygame.K_UP:
# player2speed-=speed
# if event.type==pygame.KEYUP:
# if event.key==pygame.K_DOWN:
# player2speed-=speed
# if event.key==pygame.K_UP:
# player2speed+=speed
if player1_frozen:
pass
else:
#configuring buttons
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_s:
player1speed+=speed
if event.key==pygame.K_w:
player1speed-=speed
if event.type==pygame.KEYUP:
if event.key==pygame.K_s:
player1speed-=speed
if event.key==pygame.K_w:
player1speed+=speed
if player2_frozen:
pass
else:
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_DOWN:
player2speed+=speed
if event.key==pygame.K_UP:
player2speed-=speed
if event.type==pygame.KEYUP:
if event.key==pygame.K_DOWN:
player2speed-=speed
if event.key==pygame.K_UP:
player2speed+=speed
player1.y+=player1speed
player2.y+=player2speed
ballanimation()
playeranimation()
if ball.colliderect(ice):
if ballspeedx > 0:
# freeze player 1
player1_frozen = True
else:
# freeze player 2
player2_frozen = True
# create a new thread
t = threading.Thread(target=wait_and_change_var)
# start the thread
t.start()
#drawing objects and Lines
screen.fill(bgcolor)
pygame.draw.rect(screen,player1color,player1)
pygame.draw.rect(screen,player2color,player2)
pygame.draw.line(screen,linecolor,[10,10],[screenwidth-10,10])
pygame.draw.line(screen,linecolor,[10,10],[10,screenheight-10])
pygame.draw.line(screen,linecolor,[10,screenheight-10],[screenwidth-10,screenheight-10])
pygame.draw.line(screen,linecolor,[screenwidth-10,10],[screenwidth-10,screenheight-10])
pygame.draw.line(screen,linecolor,[screenwidth/2,10],[screenwidth/2,screenheight-10])
#displays welcome message
#welcometext=welcomefont.render("Welcome to my ping pong game",False,welcomecolor)
#screen.blit(welcometext,(screenwidth/2-250,20))
#aimtext=aimfont.render(f"First to score {winscore} point wins",False,aimcolor)
#screen.blit(aimtext,(screenwidth/2-175,screenheight-50))
#displaying scores
player1text=scorefont.render(f"{player1score}",False,scorecolor)
screen.blit(player1text,(screenwidth/2-32,screenheight/2))
player2text=scorefont.render(f"{player2score}",False,scorecolor)
screen.blit(player2text,(screenwidth/2+20,screenheight/2))
playername1text=scorefont.render("Player 1",False,scorecolor)
screen.blit(playername1text,(screenwidth/2-115,screenheight/2-20))
playername2text=scorefont.render("Player 2",False,scorecolor)
screen.blit(playername2text,(screenwidth/2+20,screenheight/2-20))
pygame.draw.ellipse(screen,ballcolor,ball)
iceanimation()
ice.y+=icespeed
if win1time:
display1win()
if win2time:
display2win()
if scoretime:
ballstart()
#updating window
pygame.display.flip()
clock.tick(100)