-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
287 lines (268 loc) · 9.7 KB
/
pong.py
File metadata and controls
287 lines (268 loc) · 9.7 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
import pygame
import numpy as np
pygame.init()
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 30)
#loading resources
#------end lol
ballSpeedX = 5
ballSpeedY = 5
ballPos = [400,300]
ballNextPos = [0,0]
player2HitBox = []
playerHitBox = []
playerScore = 0
player2Score = 0
playerx = 20
playery = 0
player2x = 800-40
player2y = 600-100
player = pygame.Rect(playerx, playery,20,80)
player2 = pygame.Rect(player2x, player2y,20,80)
goingdown = False
goingup = False
goingdown2 = False
goingup2 = False
#273 is up 274 is down
gameDisplay = pygame.display.set_mode((800,600))
title = 'i like pennies'
pygame.display.set_caption(title)
clock = pygame.time.Clock()
crashed = False
victory = False
x,y = [0,0]
while not crashed:
if(ballSpeedY ==0):
ballSpeedY = -2
# --- game running ---
# --- AI ---
# PLAYER 2
'''
if(ballPos[0]>400 and ballPos[0]<410):
ballNextPos[1] = ballPos[1]+ballSpeedY*85
print(f'First: {ballNextPos[1]}')
if(ballNextPos[1]<0):
ballNextPos[1] *= -1
print(f'Second: {ballNextPos[1]}')
elif(ballNextPos[1]>600):
ballNextPos[1] -= 600
ballNextPos[1] = 600 - ballNextPos[1]
print(f'Second: {ballNextPos[1]}')
if(ballPos[0]>500):
if (ballNextPos[1]>=player2y+30 and ballNextPos[1]<player2y+40):
goingdown2 = False
goingup2 = False
elif(ballNextPos[1]< player2y+40):
goingdown2 = False
goingup2 = True
#print(f'predicted ball position: {ballNextPos[1]}, My position:{player2y}')
elif(ballNextPos[1]> player2y+40):
goingdown2 = True
goingup2 = False
#print(f'predicted ball position: {ballNextPos[1]}, My position:{player2y}')
else:
if(player2y < 220 ):
goingup2 = False
goingdown2 = True
elif(player2y+80 > 380):
goingup2 = True
goingdown2 = False
'''
# PLAYER 1
if(ballPos[0]<400 and ballPos[0]>(400+(ballSpeedX*3))):
ishframes = 355/ballSpeedX
ishframes = ishframes * -1
ballNextPos[1] = ballPos[1]+ballSpeedY*int(ishframes)
ballNextPos[0] = ballPos[0]+ballSpeedX*int(ishframes)
print(f'First: {ballNextPos[0]};{ballNextPos[1]}, {ishframes}')
if(ballNextPos[1]<0):
ballNextPos[1] *= -1
print(f'Second: {ballNextPos[1]}')
elif(ballNextPos[1]>600):
ballNextPos[1] -= 600
ballNextPos[1] = 600 - ballNextPos[1]
print(f'Second: {ballNextPos[1]}')
if(ballPos[0]<400):
if (ballNextPos[1]>=playery+30 and ballNextPos[1]<playery+40):
goingdown = False
goingup = False
elif(ballNextPos[1]< playery+40):
goingdown = False
goingup = True
#print(f'predicted ball position: {ballNextPos[1]}, My position:{player2y}')
elif(ballNextPos[1]> playery+40):
goingdown = True
goingup = False
#print(f'predicted ball position: {ballNextPos[1]}, My position:{player2y}')
else:
if(playery < 220 ):
goingup = False
goingdown = True
elif(playery+80 > 380):
goingup = True
goingdown = False
# --- AI END ---
for event in pygame.event.get():
textsurface = myfont.render(f'{playerScore} - {player2Score}', False, (255,255,255))
gameDisplay.fill((0,0,0))
if event.type == pygame.QUIT:
crashed = True
elif event.type == pygame.KEYDOWN:
if event.dict['unicode'] == 's':
goingdown = True
goingup = False
elif event.dict['unicode'] == 'w':
goingup = True
goingdown = False
if event.dict['key'] == 274:
goingdown2 = True
goingup2 = False
elif event.dict['key'] == 273:
goingup2 = True
goingdown2 = False
elif event.dict['unicode'] == 'f':
ballSpeedX += 1
elif event.type == pygame.KEYUP:
if event.dict['key'] == 115:
goingdown = False
elif event.dict['key'] == 119:
goingup = False
if event.dict['key'] == 274:
goingdown2 = False
elif event.dict['key'] == 273:
goingup2 = False
if event.dict['key'] == 102:
print(f'{playerHitBox}')
if event.dict['key'] == 103:
print(f'{player2HitBox}')
# --- change title ---
elif event.type == pygame.MOUSEMOTION:
x,y = event.dict['pos']
title = f'X={x}, Y={y}'
pygame.display.set_caption(title)
# --- draw image ---
if goingdown:
if playery <= 600-80:
playery += 5
elif goingup:
if playery >= 0:
playery -= 5
if goingdown2:
if player2y <= 600-80:
player2y += 5
elif goingup2:
if player2y >=0:
player2y -= 5
if ballPos[1] >= 600-7 or ballPos[1] <=0:
ballSpeedY *= -1
if ballPos[0]+7 >=player2x and ballSpeedX>0:
for idx, bploop in enumerate(player2HitBox):
if ballPos[1] == bploop:
ballSpeedX *= -1
if idx <=5:
ballSpeedY = -8
elif idx >5 and idx <10:
ballSpeedY = -7
elif idx <=10 and idx <15:
ballSpeedY = -6
elif idx <=15 and idx <20:
ballSpeedY = -5
elif idx <=20 and idx <25:
ballSpeedY = -4
elif idx <=25 and idx <30:
ballSpeedY = -3
elif idx <=30 and idx <35:
ballSpeedY = -2
elif idx <=35 and idx <40:
ballSpeedY = -1
elif idx <=40 and idx <45:
ballSpeedY = 0
elif idx >45 and idx <50:
ballSpeedY = 1
elif idx <=50 and idx <55:
ballSpeedY = 2
elif idx <=55 and idx <60:
ballSpeedY = 3
elif idx <=60 and idx <65:
ballSpeedY = 4
elif idx <=65 and idx <70:
ballSpeedY = 5
elif idx <=70 and idx <75:
ballSpeedY = 6
elif idx <=75 and idx <80:
ballSpeedY = 7
elif idx <=80:
ballSpeedY = 8
#print(f'{bploop},{ballPos[1]}')
break
elif ballPos[0]-20 <= playerx and ballSpeedX<0:
for idx,bploop in enumerate(playerHitBox):
if ballPos[1] == bploop:
ballSpeedX *= -1
if idx <=5:
ballSpeedY = -8
elif idx >5 and idx <10:
ballSpeedY = -7
elif idx <=10 and idx <15:
ballSpeedY = -6
elif idx <=15 and idx <20:
ballSpeedY = -5
elif idx <=20 and idx <25:
ballSpeedY = -4
elif idx <=25 and idx <30:
ballSpeedY = -3
elif idx <=30 and idx <35:
ballSpeedY = -2
elif idx <=35 and idx <40:
ballSpeedY = -1
elif idx <=40 and idx <45:
ballSpeedY = 0
elif idx >45 and idx <50:
ballSpeedY = 1
elif idx <=50 and idx <55:
ballSpeedY = 2
elif idx <=55 and idx <60:
ballSpeedY = 3
elif idx <=60 and idx <65:
ballSpeedY = 4
elif idx <=65 and idx <70:
ballSpeedY = 5
elif idx <=70 and idx <75:
ballSpeedY = 6
elif idx <=75 and idx <80:
ballSpeedY = 7
elif idx <=80:
ballSpeedY = 8
#print(f'{bploop},{ballPos[1]}')
break
if ballPos[0] <= 20 and ballSpeedX<0:
player2Score += 1
ballPos = [400,300]
textsurface = myfont.render(f'{playerScore} - {player2Score}', False, (255,255,255))
elif ballPos[0] >= 780 and ballSpeedX>0:
playerScore += 1
ballPos = [400,300]
textsurface = myfont.render(f'{playerScore} - {player2Score}', False, (255,255,255))
ballPos[0] += ballSpeedX
ballPos[1] += ballSpeedY
#check if off the screen
if victory == False:
#s playery = y-40
gameDisplay.fill((0,0,0))
pygame.draw.rect(gameDisplay, (255,255,255), (pygame.Rect(playerx, playery,20,80)))
pygame.draw.rect(gameDisplay, (255,255,255), (pygame.Rect(player2x, player2y,20,80)))
player2HitBox = []
playerHitBox = []
for hitboxx in range(80):
player2HitBox.append(hitboxx+player2y)
for hitboxy in range(80):
playerHitBox.append(hitboxy+playery)
pygame.draw.line(gameDisplay, (255,255,255), (400,0),(400,600))
pygame.draw.circle(gameDisplay, (255,255,255), ballPos, 7)
gameDisplay.blit(textsurface,(368,0))
# --- end draw ---
#print(event)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()