-
Notifications
You must be signed in to change notification settings - Fork 0
/
aipong.py
233 lines (160 loc) · 4.74 KB
/
aipong.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
import pygame as py
import random
import math
dwidth,dheight=1000,800
display=py.display.set_mode((dwidth,dheight))
clock=py.time.Clock()
py.init()
def distance(a,b,c,d):
return math.sqrt((c-a)**2+(d-b)**2)
def updateupos():
global urect
global uxv
urect.x+=uxv
if urect.x+uwidth>dwidth or urect.x<0:
uxv=0
def drawline(y):
if ballvy<0:
m=ballvy/(ballvx+0.01)
c=bally-m*ballx
return int((y-c)/m)
else:
return ballx
def updateenemypos():
global enemyurect
global enemyvx
if enemyvx=="AI":
if enemyurect.x>0 and enemyurect.x+uwidth<dwidth:
if ballvy<0:
target=drawline(20+uheight)
if target<0:
target*=-1
elif target>dwidth:
target=2*dwidth-target
difference=target-enemyurect.x-uwidth/2
else:
difference=dwidth/2-enemyurect.x-uwidth/2
if difference<-5:
enemyurect.x-=velocity
elif difference>5:
enemyurect.x+=velocity
else:
if enemyurect.x<0:
enemyurect.x=1
else:
enemyurect.x=dwidth-uwidth-1
else:
enemyurect.x+=enemyvx
if enemyurect.x+uwidth>dwidth or enemyurect.x<0:
enemyvx=0
def updateballpos():
global ballx
global bally
global ballvx
global ballvy
global bounce
global fps
global uscore
global enemyscore
global label1
global label2
if ballx<radius or ballx+radius>dwidth:
ballvx*=-1
bounce+=1
if urect.collidepoint((ballx,bally)) or enemyurect.collidepoint((ballx,bally)):
ballvy*=-1
bally+=ballvy
ballvx+=random.randint(-2,2)
bounce+=1
if (bounce+1)%10==0:
if random.randint(0,100)<30:
if ballvy<=0:
ballvy-=1
else:
ballvy+=1
else:
fps+=5
bounce+=1
ballx+=ballvx
bally+=ballvy
if bally<0:
uscore+=1
label1=font.render(str(uscore),False,py.Color("white"))
reset()
elif bally>dheight:
enemyscore+=1
label2=font.render(str(enemyscore),False,py.Color("white"))
reset()
def reset():
global ux
global uy
global urect
global uxv
global enemyux
global enemyuy
global enemyurect
global ballx
global bally
global ballvx
global ballvy
global bounce
global fps
global enemyvx
ux,uy=int(dwidth/2-uwidth/2),dheight-20-uheight
urect=py.Rect(ux,uy,uwidth,uheight)
uxv=0
enemyux,enemyuy=ux,20
enemyurect=py.Rect(enemyux,enemyuy,uwidth,uheight)
enemyvx="AI"
ballx,bally=int(dwidth/2),radius+20+uheight
ballvx,ballvy=-0,0
bounce=0
fps=60
uwidth,uheight=180,25
uscore=0
enemyscore=0
velocity=6
radius=10
gameexit=False
fontsize=70
font=py.font.Font(None,fontsize)
reset()
label1=font.render(str(uscore),False,py.Color("white"))
label2=font.render(str(enemyscore),False,py.Color("white"))
while not gameexit:
display.fill(py.Color("black"))
for event in py.event.get():
if event.type==py.QUIT:
gameexit=True
if event.type==py.KEYDOWN:
if event.key==py.K_RIGHT and urect.x+uwidth<dwidth:
uxv=velocity
elif event.key==py.K_LEFT and urect.x>0:
uxv=-velocity
if event.key==py.K_d and enemyurect.x+uwidth<dwidth:
enemyvx=velocity
elif event.key==py.K_a and enemyurect.x>0:
enemyvx=-velocity
elif event.type==py.KEYUP:
if event.key==py.K_RIGHT and uxv==velocity:
uxv=0
elif event.key==py.K_LEFT and uxv==-velocity:
uxv=0
elif event.key==py.K_SPACE and ballvy==0:
ballvx,ballvy=-5,5
if event.key==py.K_d and enemyvx==velocity:
enemyvx=0
elif event.key==py.K_a and enemyvx==-velocity:
enemyvx=0
updateupos()
updateenemypos()
updateballpos()
py.draw.circle(display,py.Color("yellow"),(ballx,bally),radius)
py.draw.rect(display,py.Color("gray"),urect)
py.draw.rect(display,py.Color("gray"),enemyurect)
display.blit(label1,(int(dwidth/2),int(dheight/2+fontsize)))
display.blit(label2,(int(dwidth/2),int(dheight/2-fontsize)))
#py.draw.line(display,py.Color("red"),(ballx,bally),(drawline(0),0),2)
py.display.update()
clock.tick(fps)
#print(clock.get_fps())