-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.py
353 lines (278 loc) · 12 KB
/
pool.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
# GlowScript 3.2 VPython
scene.bind('keydown', keydown_fun) # Function for key presses
#Set up background
scene.background = 0.8 * vec(1, 1, 1) # Light gray (0.8 out of 1.0)
scene.width = 640 # Make the 3D canvas larger
scene.height = 480
friction = 0.985
ballSize = 0.58
hit = vec(1, 0, 0)
#Set up the pool table
ground = box(size = vec(10, 1, 20),
pos = vec(0, -0.5, 0),
color = vec(0, 0.42, 0.35)) #Green
wallA = box(pos = vec(0, -0.5, -10),
axis = vec(1, 0, 0),
size = vec(10, 2, .4),
color = vec(.21, .15, .14)) #Top
wallB = box(pos = vec(-5, -0.5, 0),
axis = vec(0, 0, 1),
size = vec(20.4, 2, .4),
color = vec(.21, .15, .14)) #Left
wallC = box(pos = vec(5, -0.5, 0),
axis = vec(0, 0, 1),
size = vec(20.4, 2, .4),
color = vec(.21, .15, .14)) #Right
wallD = box(pos = vec(0, -0.5, 10),
axis = vec(1, 0, 0),
size = vec(10, 2, 0.4),
color = vec(.21, .15, .14)) #Bottom
hole1 = cylinder(size = 1*vec(1, 1, 1),
axis = vec(0, 1, 0),
color = color.black,
pos = vec(-4.7, -0.99, -9.7))
hole2 = cylinder(size = 1*vec(1, 1, 1),
axis = vec(0, 1, 0),
color = color.black,
pos = vec(-4.7, -0.99, 9.7))
hole3 = cylinder(size = 1*vec(1, 1, 1),
axis = vec(0, 1, 0),
color = color.black,
pos = vec(4.7, -0.99, -9.7))
hole4 = cylinder(size = 1*vec(1, 1, 1),
axis = vec(0, 1, 0),
color = color.black,
pos = vec(4.7, -0.99, 9.7))
hole5 = cylinder(size = 1*vec(1, 1, 1),
axis = vec(0, 1, 0),
color = color.black,
pos = vec(4.7, -0.99, 0))
hole6 = cylinder(size = 1*vec(1, 1, 1),
axis = vec(0, 1, 0),
color = color.black,
pos = vec(-4.7, -0.99, 0))
cueBall = sphere(size = ballSize*vec(1, 1, 1),
color = vec(1, 1, 0.98),
pos = vec(0, ballSize/2, 6),
vel = vec(0, 0, 0))
# triangle pos at z = -5
ball1 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(1, 0.84, 0.0),
pos = vec(0, ballSize/2, -5),
vel = vec(0, 0, 0))
ball2 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.6, 0.24, 1),
pos = vec(ballSize/2, ballSize/2, -5-ballSize),
vel = vec(0, 0, 0))
ball3 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(.79, 0.15, 0.15),
pos = vec(-ballSize/2, ballSize/2, -5-ballSize),
vel = vec(0, 0, 0))
ball4 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.37, 0, 0.68),
pos = vec(ballSize, ballSize/2, -5-2 * ballSize),
vel = vec(0, 0, 0))
ball5 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.98, 0.54, 0.2),
pos = vec(-ballSize, ballSize/2, -5-2 * ballSize),
vel = vec(0, 0, 0))
ball6 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0, 0.53, 0.32),
pos = vec(3 * ballSize/2, ballSize/2, -5-3 * ballSize),
vel = vec(0, 0, 0))
ball7 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.42, 0, 0),
pos = vec(ballSize/2, ballSize/2, -5-3 * ballSize),
vel = vec(0, 0, 0))
ball8 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0, 0, 0),
pos = vec(0, ballSize/2, -5-2 * ballSize),
vel = vec(0, 0, 0))
ball9 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(1, 0.91, 0.49),
pos = vec(-ballSize/2, ballSize/2, -5-3*ballSize),
vel = vec(0, 0, 0))
ball10 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.4, 0.25, 1),
pos = vec(-3 * ballSize/2, ballSize/2, -5-3*ballSize),
vel = vec(0, 0, 0))
ball11 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(1, 0.43, 0.43),
pos = vec(ballSize*2, ballSize/2, -5-4*ballSize),
vel = vec(0, 0, 0))
ball12 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.55, 0.22, 0.83),
pos = vec(ballSize, ballSize/2, -5-4*ballSize),
vel = vec(0, 0, 0))
ball13 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(1, 0.66, 0.40),
pos = vec(0, ballSize/2, -5-4*ballSize),
vel = vec(0, 0, 0))
ball14 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.29, 0.86, 0.63),
pos = vec(-ballSize, ballSize/2, -5-4*ballSize),
vel = vec(0, 0, 0))
ball15 = sphere(size = ballSize*vec(1, 1, 1),
color = vec(0.69, 0.23, 0.23),
pos = vec(-ballSize*2, ballSize/2, -5-4*ballSize),
vel = vec(0, 0, 0))
cue = makePoolCue(vec(cueBall.pos.x, ballSize+1.65, cueBall.pos.z+8)) #Make the pool cue
cue.rotate(angle = radians(-90),
axis = vec(0, 1, 0),
origin = vector(cue.pos.x, cue.pos.y, cue.pos.z))
balls = [cueBall, ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8, ball9, ball10, ball11, ball12, ball13, ball14, ball15]
holes = [hole1, hole2, hole3, hole4, hole5, hole6]
# Other constants
RATE = 30 # The number of times the while loop runs each second
dt = 1.0/(1.0*RATE) # The time step each time through the while loop
scene.autoscale = False # Avoids changing the view automatically
scene.forward = vec(0, -3, -2) # Ask for a bird's-eye view of the scene...
# This is the "event loop" or "animation loop"
# Each pass through the loop will animate one step in time, dt
while True:
rate(RATE) # Maximum number of times per second
# ..that the while loop runs
for ball in balls:
ball.pos = ball.pos + ball.vel*dt
ball.vel = ball.vel * friction
wallCollide(ball)
vList = []
for i in balls:
vList = vList + [i.vel.mag]
if max(vList) < 0.15 and not cue.visible:
cue.pos = vec(cueBall.pos.x, ballSize+1.65, cueBall.pos.z+8)
cue.axis = vec(0, 0, 1)
#cue.rotate(angle = radians(-90),
# axis = vec(0, 1, 0),
# origin = vector(cue.pos.x, cue.pos.y, cue.pos.z))
cue.visible = True
for i in balls:
i.vel = vec(0, 0, 0)
for i in range(len(balls) - 1):
for j in range(i + 1, len(balls)):
ballCollide(balls[i], balls[j])
for i in balls:
for j in holes:
ballFall(i, j)
if not cueBall.visible:
cueBall.vel = vec(0, 0, 0)
cueBall.pos = vec(0, ballSize/2, 6)
cueBall.visible = True
visList = []
count = 0
if not ball8.visible:
for ball in balls:
if not ball.visible:
count = count + 1
print(count)
if count == 15:
print("You win! :D")
else:
print("You lose")
def makePoolCue(startingPosition):
"""Creates a pool cue
Argument: A vector indicating the starting position
Returns: Nothing
"""
cueBrown = cylinder(pos = vec(5.3, 4.325, 3),
axis = vec(2, 0.5, 0),
size = vec(10, 0.2, 0.2),
color = vec(.21, .15, .14)) #Dark brown
cueWood = cylinder(pos = vec(0.3, 3.075, 3),
axis = vec(2, 0.5, 0),
size = vec(5.15, 0.2, 0.2),
color = vec(1.0, 0.9, 0.7)) #Yellow
cueTip = cylinder(pos = vec(0, 3, 3),
axis = vec(2., 0.5, 0),
size = vec(0.31, 0.2, 0.2),
color = color.white)
cueObjects = [cueBrown, cueWood, cueTip]
comCue = compound(cueObjects,
pos = startingPosition) #Combines the pieces together
comCue.vel = vec(0, 0, 0) #Set the initial velocity
return comCue
def keydown_fun(event):
"""This function is called each time a key is pressed."""
key = event.key
ri = randint(0, 10)
amount = 0.42 # "Strength" of the keypress's velocity changes
if key == 'up' or key in 'wWiI':
if hit.x < 10:
hit.x = hit.x + 1
print("Power: " + hit.x)
else:
print("Can't go higher")
elif key == 'down' or key in 'sSkK':
if hit.x > 1:
hit.x = hit.x - 1
print("Power: " + hit.x)
else:
print("Can't go lower")
elif key == 'left' or key in 'aAjJ':
cue.rotate(angle = radians(-2.5),
axis = vec(0, 1, 0),
origin = cueBall.pos)
elif key == 'right' or key in "dDlL":
cue.rotate(angle = radians(2.5),
axis = vec(0, 1, 0),
origin = cueBall.pos)
elif key == 'space' or key in ' rR':
where = cue.pos - cueBall.pos
where = vec(where.x, 0, where.z) * -1
where = where.norm()
cueBall.vel = where * hit.x * 1.5
cue.visible = False
elif key == "q":
for i in range(len(balls)):
if i != 0:
balls[i].pos = hole1.pos
def wallCollide(ball):
"""Changes the velocity of the ball if the ball collides with a wall.
Argument: A sphere object
Returns: Nothing
"""
# If the ball hits wallA
if ball.pos.z < wallA.pos.z + 0.49: # Hit--check for z
ball.pos.z = wallA.pos.z + 0.49 # Bring back into bounds
ball.vel.z *= -1.0 # Reverse the z velocity
# If the ball hits wallB
if ball.pos.x < wallB.pos.x + 0.49: # Hit--check for x
ball.pos.x = wallB.pos.x + 0.49 # Bring back into bounds
ball.vel.x *= -1.0 # Reverse the x velocity
if ball.pos.x > wallC.pos.x - 0.49: # Hit--check for x
ball.pos.x = wallC.pos.x - 0.49 # Bring back into bounds
ball.vel.x *= -1.0 # Reverse the x velocity
if ball.pos.z > wallD.pos.z - 0.49: # Hit--check for z
ball.pos.z = wallD.pos.z - 0.49 # Bring back into bounds
ball.vel.z *= -1.0 # Reverse the z velocity
def ballCollide(ballA, ballB):
"""Changes the velocity of two balls if they collide
Argument: Two sphere objects
Returns: Nothing
"""
DISTANCE = ballSize
diff = ballB.pos - ballA.pos
if (diff).mag < DISTANCE:
gent = rotate(diff, radians(90), vec(0, 1, 0))
v1 = ballA.vel; v2 = ballB.vel
ballA.pos -= (DISTANCE - diff.mag) *v1.norm()*1.001
ballB.pos -= (DISTANCE - diff.mag) *v2.norm()*1.001
v1_rad = proj(v1, diff); v1_tan = proj(v1, gent)
v2_rad = proj(v2, -diff); v2_tan = proj(v2, gent)
ballA.vel = v2_rad + v1_tan
ballB.vel = v1_rad + v2_tan
def ballFall(ball, hole):
diff = ball.pos - vec(hole.pos.x, hole.pos.y + 1, hole.pos.z)
if diff.mag < 0.5:
ball.vel = vec(0, 0, 0)
ball.visible = False
ball.pos.y = randint(10, 1000)
def randint(low, hi):
"""Implements Python's randint using the random() function.
returns an int from low to hi _inclusive_ (so, it's not 100% Pythonic)
"""
if hi < low:
low, hi = hi, low # Swap if out of order!
LEN = int(hi) - int(low) + 1. # Get the span and add 1
randvalue = LEN*random() + int(low) # Get a random value
return int(randvalue) # Return the integer part of it