-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarter.py
More file actions
327 lines (279 loc) · 16.2 KB
/
starter.py
File metadata and controls
327 lines (279 loc) · 16.2 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
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
GlowScript 3.0 VPython
scene.bind('keydown', keydown_fun) # Function for key presses
scene.bind('click', click_fun) # Function for mouse clicks
scene.background = vec(.7, .85, .98) # Light gray (0.8 out of 1.0)
scene.width = 640 # Make the 3D canvas larger
scene.height = 480
ground = box(size = vec(20, 1, 20), pos = vec(0, -1, 0), color = .9999*vec(1,1,1))
# +++ start of OBJECT_CREATION section
# These functions create "container" objects, called "compounds"
def make_penguin(starting_position, starting_vel = vec(0, 0, 0)):
"""The lines below make a new "frame", which is a container with a
local coordinate system.
The arguments to make_alien allow for any initial starting position
and initial starting velocity, with a default starting velocity
of vec(0, 0, 0).
Compounds can have any number of components. Here are the
penguin's components:
"""
penguin_body = ellipsoid(length = 2, width = 2, height = 3, pos = vec(0, 1, 0), color = color.blue)
penguin_eye1 = sphere(size = 2*0.5*vec(1, 1, 1), pos = vec(.2, 1.5, .5), color = color.white)
penguin_eye2 = sphere(size = 2*0.5*vec(1, 1, 1), pos = vec(-.2, 1.5, .5), color = color.white)
penguin_pupil1 = sphere(size = 2*0.3*vec(1, 1, 1), pos = vec(.25, 1.5, 1), color = color.black)
penguin_pupil2 = sphere(size = 2*0.3*vec(1, 1, 1), pos = vec(-.25, 1.5, 1), color = color.black)
penguin_mouth = ellipsoid(length = 0.8, width = 0.4, height = 0.4, pos = vec(0, 1, 1), color = color.orange)
# make a list to "fuse" with a compound
penguin_objects = [penguin_body, penguin_eye1, penguin_eye2, penguin_pupil1, penguin_pupil2, penguin_mouth]
# now, we create a compound -- we'll name it com_alien:
com_penguin = compound(penguin_objects, pos = starting_position)
com_penguin.vel = starting_vel # set the initial velocity
return com_penguin
def make_puffle(starting_position, starting_vel = vec(0, 0, 0)):
"""The lines below make a new "frame", which is a container with a
local coordinate system.
The arguments to puffle allow for any initial starting position
and initial starting velocity, with a default starting velocity
of vec(0, 0, 0).
Compounds can have any number of components. Here are the
puffle's components:
"""
colList = [color.green, color.gray, color.yellow, color.red, color.orange, color.purple, color.blue]
puffle_color = vec(choice(colList))
puffle_body = ellipsoid(size = 1.0*vec(1, 1, 1), pos = vec(0, 0, 0), length = 1, width = 1.2, height = 1, color = puffle_color)
puffle_eye1 = sphere(size = 0.5*vec(1, 1, 1), pos = vec(.25, .1, .2), color = color.white)
puffle_eye2 = sphere(size = 0.5*vec(1, 1, 1), pos = vec(.25, .1, -.2), color = color.white)
puffle_pupil1 = sphere(size = 0.3*vec(1, 1, 1), pos = vec(.4, .1, -.21), color = color.black)
puffle_pupil2 = sphere(size = 0.3*vec(1, 1, 1), pos = vec(.4, .1, .21), color = color.black)
puffle_hair1 = cone(pos = 0.42*vec(0, .9, .1), axis = vec(0, .2, 0.05), size = 0.3*vec(1, 1, 1), radius = vec(0.1, 0.1, 0.1), color = puffle_color)
puffle_hair6 = cone(pos = 0.42*vec(0, .9, -.1), axis = vec(0, .2, -0.05), size = 0.3*vec(1, 1, 1), radius = vec(0.1, 0.1, 0.1), color = puffle_color)
puffle_hair2 = cone(pos = 0.42*vec(0 , .9, .4), axis = vec(0, .2, 0.1), size = 0.3*vec(1, 1, 1), radius = vec(0.1, 0.1, 0.1), color = puffle_color)
puffle_hair3 = cone(pos = 0.42*vec(0 , .9, -.4), axis = vec(0, .2, -0.1), size = 0.3*vec(1, 1, 1), radius = vec(0.1, 0.1, 0.1), color = puffle_color)
puffle_hair4 = cone(pos = 0.42*vec(0 , .9, -.6), axis = vec(0, .2, -0.2), size = 0.3*vec(1, 1, 1), radius = vec(0.01, 0.01, 0.01), color = puffle_color)
puffle_hair5 = cone(pos = 0.42*vec(0 , .9, .6), axis = vec(0, .2, 0.2), size = 0.3*vec(1, 1, 1), radius = vec(0.01, 0.01, 0.01), color = puffle_color)
puffle_hair7 = cone(pos = 0.42*vec(0 , .9, .8), axis = vec(0, .2, 0.4), size = 0.3*vec(1, 1, 1), radius = vec(0.01, 0.01, 0.01), color = puffle_color)
puffle_hair8 = cone(pos = 0.42*vec(0 , .9, -.8), axis = vec(0, .2, -0.4), size = 0.3*vec(1, 1, 1), radius = vec(0.01, 0.01, 0.01), color = puffle_color)
# make a list to "fuse" with a compound
puffle_objects = [puffle_body, puffle_eye1, puffle_eye2, puffle_pupil1, puffle_pupil2, puffle_hair1, puffle_hair6, puffle_hair2, puffle_hair3, puffle_hair4, puffle_hair5, puffle_hair7, puffle_hair8]
# now, we create a compound -- we'll name it com_alien:
com_puffle = compound(puffle_objects, pos = starting_position)
com_puffle.vel = starting_vel # set the initial velocity
return com_puffle
def make_tree(starting_position):
""" The lines below make a tree at a position starting_position
"""
height = 2
wide = 2
thickness = 2
tree_leaves1 = cone(size = vec(wide,height,thickness), axis = vec(0,1,0), pos = vec(0,2,0), color = vec(0,.5,0))
tree_leaves2 = cone(size = .8*vec(wide,height,thickness), axis = vec(0,1,0), pos = vec(0,3,0), color = vec(0,.5,0))
tree_leaves3 = cone(size = .6*vec(wide,height,thickness), axis = vec(0,1,0), pos = vec(0,4.0,0), color = vec(0,.5,0))
snow_leaves1 = cone(size = vec(wide,height,thickness), axis = vec(0,1,0), pos = vec(0,2.2,0), color = .9999*vec(1,1,1))
snow_leaves2 = cone(size = .8*vec(wide,height,thickness), axis = vec(0,1,0), pos = vec(0,3.2,0), color = .9999*vec(1,1,1))
snow_leaves3 = cone(size = .6*vec(wide,height,thickness), axis = vec(0,1,0), pos = vec(0,4.2,0), color = .9999*vec(1,1,1))
tree_trunk = cylinder(size = 1.0*vec(.5,.5,.5), axis = vec(0,1,0), pos = vec(0,1,0), color = .4*vec(1.0, 0.7, 0.3))
tree_trunk1 = cylinder(size = 1.0*vec(.5,.5,.5), axis = vec(0,1,0), pos = vec(0,1.5,0), color = .4*vec(1.0, 0.7, 0.3))
tree_objects = [tree_leaves1, tree_leaves2, tree_leaves3, snow_leaves1, snow_leaves2, snow_leaves3, tree_trunk, tree_trunk1]
com_tree = compound(tree_objects, pos = starting_position)
com_tree.pos.y = 1.5
return com_tree
def make_fence(starting_position):
""" The lines below make a fence at a position starting_position
"""
pOst = cylinder(size = 1.0*vec(1,.5,.5), axis = vec(0,1,0), pos = vec(0,1,0), color = .35*vec(1.0, 0.7, 0.3))
pOst1 = cylinder(size = 1.0*vec(1,.5,.5), axis = vec(0,1,0), pos = vec(3,1,0), color = .35*vec(1.0, 0.7, 0.3))
pOst2 = cylinder(size = 1.0*vec(1,.5,.5), axis = vec(0,1,0), pos = vec(-3,1,0), color = .35*vec(1.0, 0.7, 0.3))
pOst3 = cylinder(size = 1.0*vec(1,.5,.5), axis = vec(0,1,0), pos = vec(3,1,-3), color = .35*vec(1.0, 0.7, 0.3))
pOst4 = cylinder(size = 1.0*vec(1,.5,.5), axis = vec(0,1,0), pos = vec(-3,1,-3), color = .35*vec(1.0, 0.7, 0.3))
pOst5 = cylinder(size = 1.0*vec(1,.5,.5), axis = vec(0,1,0), pos = vec(3,1,-6), color = .35*vec(1.0, 0.7, 0.3))
pOst6 = cylinder(size = 1.0*vec(1,.5,.5), axis = vec(0,1,0), pos = vec(-3,1,-6), color = .35*vec(1.0, 0.7, 0.3))
slat1 = box(size = vec(.1, .5, 6), axis = vec(0,0,1), pos = vec(0,1.7,0), color = .35*vec(1.0, 0.7, 0.3))
slat2 = box(size = vec(.1, .5, 6), axis = vec(1,0,0), pos = vec(3,1.7,-3), color = .35*vec(1.0, 0.7, 0.3))
slat3 = box(size = vec(.1, .5, 6), axis = vec(1,0,0), pos = vec(-3,1.7,-3), color = .35*vec(1.0, 0.7, 0.3))
fence_objects = [pOst, pOst1, pOst2, pOst3, pOst4, pOst5, pOst6, slat1, slat2, slat3]
com_fence = compound(fence_objects, pos = starting_position)
com_fence.pos.y = 0
return com_fence
def make_snow(starting_position):
""" The lines below make a snowflake that falls from position starting_position
"""
rod1 = cylinder(size = 1.0*vec(1,.15,.15), axis = vec(0,1,0), pos = vec(0,0,0), color = vec(.8, .95, .999))
rod2 = cylinder(size = 1.0*vec(1,.15,.15), axis = vec(.6,.4,0), pos = vec(-.4,.2,0), color = vec(.8, .95, .999))
rod3 = cylinder(size = 1.0*vec(1,.15,.15), axis = vec(-.6,.4,0), pos = vec(.4,.2,0), color = vec(.8, .95, .999))
rod4 = cylinder(size = 1.0*vec(1,.15,.15), axis = vec(0,.4,.6), pos = vec(0,.2,-.4), color = vec(.8, .95, .999))
rod5 = cylinder(size = 1.0*vec(1,.15,.15), axis = vec(0,.4,-.6), pos = vec(0,.2,0.4), color = vec(.8, .95, .999))
snow_objects = [rod1, rod2, rod3, rod4, rod5]
com_snow = compound(snow_objects, pos = starting_position)
com_snow.pos.y = 5
return com_snow
# create boundaries for environment
wallA = box(pos = vec(0, 0, -10), axis = vec(1, 0, 0), size = vec(20, 1, .2), color = vec(.7, .85, .98))
wallB = box(pos = vec(-10, 0, 0), axis = vec(0, 0, 1), size = vec(20, 1, .2), color = vec(.7, .85, .98))
wallC = box(pos = vec(0, 0, 10), axis = vec(1, 0, 0), size = vec(20, 1, .2), color = vec(.7, .85, .98))
wallD = box(pos = vec(10, 0, 0), axis = vec(0, 0, 1), size = vec(20, 1, .2), color = vec(.7, .85, .98))
ground = box(size = vec(20, 1, 20), pos = vec(0, -1, 0), color = .4*vec(1, 1, 1))
# A ball that we will be able to control
ball = make_penguin(starting_position = vec(-5, 0, -5), starting_vel = vec(0, 0, 0)) # ball is an object of class sphere
ball.vel = vec(0, 0, 0) # this is its initial velocity
# puffle creation
puffle = make_puffle(starting_position = vec(6, 0, -6), vel = vec(1, 0, -1))
puffle.rotate(axis = vec(0, 1, 0), angle=(3*pi/2))
# tree creation
tree1 = make_tree(starting_position = vec(-7,0,6.5))
tree2 = make_tree(starting_position = vec(7,0,6.5))
tree3 = make_tree(starting_position = vec(-5.5,0,7.5))
tree3 = make_tree(starting_position = vec(5.5,0,7.5))
tree4 = make_tree(starting_position = vec(-8,0,7.5))
tree5 = make_tree(starting_position = vec(8,0,7.5))
tree6 = make_tree(starting_position = vec(-4.5,0,8.5))
tree7 = make_tree(starting_position = vec(4.5,0,8.5))
tree8 = make_tree(starting_position = vec(-8.5,0,8.5))
tree9 = make_tree(starting_position = vec(8.5,0,8.5))
tree10 = make_tree(starting_position = vec(-8.5,0,5.5))
tree11 = make_tree(starting_position = vec(8.5,0,5.5))
#fence
fence = make_fence(starting_position = vec(0,0,3))
#snowflakes
snow1 = make_snow(starting_position = 8*vec.random())
snow2 = make_snow(starting_position = 8*vec.random())
snow3 = make_snow(starting_position = 8*vec.random())
snow4 = make_snow(starting_position = 8*vec.random())
snow5 = make_snow(starting_position = 8*vec.random())
snow6 = make_snow(starting_position = 8*vec.random())
snow7 = make_snow(starting_position = 8*vec.random())
snow8 = make_snow(starting_position = 8*vec.random())
snow9 = make_snow(starting_position = 8*vec.random())
snow10 = make_snow(starting_position = 8*vec.random())
snow11 = make_snow(starting_position = 8*vec.random())
snow12 = make_snow(starting_position = 8*vec.random())
snow13 = make_snow(starting_position = 8*vec.random())
snow14 = make_snow(starting_position = 8*vec.random())
snow15 = make_snow(starting_position = 8*vec.random())
snow16 = make_snow(starting_position = 8*vec.random())
snow17 = make_snow(starting_position = 8*vec.random())
snow18 = make_snow(starting_position = 8*vec.random())
snow = [snow1, snow2, snow3, snow4, snow5, snow6, snow7,
snow8, snow9, snow10, snow11, snow12, snow13, snow14,
snow15, snow16, snow17, snow18]
for i in range(len(snow)):
snow[i].vel = vec(0,-1,0)
# +++ end of OBJECT_CREATION section
# +++ start of ANIMATION section
# 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
#
time = 0 #keeps track of snowflakes
count = 0 # keeps track of time for puffle to change location
while True:
rate(RATE) # maximum number of times per second the while loop runs
count+= 1
#for snowflakes falling
time = time + dt
for i in range(len(snow)):
snow[i].pos = snow[i].pos + (snow[i].vel) * dt
snowfall(snow[i])
# +++ Start of PHYSICS UPDATES -- update all positions here, every time step
if count > choice([180, 270]):
count = 0
puffle.vel = randvel(1,1.5)
# Update the alien's position
puffle.pos = puffle.pos + puffle.vel/(1.0*RATE) # Update the ball's position
# +++ Start of COLLISIONS -- check for collisions & do the "right" thing
# If the ball hits walls
corral_collide(ball)
corral_collide(puffle)
# If the ball collides with the puffle, nudge the puffle in that direction
if mag(ball.pos - puffle.pos) < 1.0:
puffle.pos.x += (ball.vel.x)*.1*RATE
print(puffle.pos.x)
puffle.pos.z += (ball.vel.z)*.1*RATE
print(puffle.pos.y)
# +++ End of COLLISIONS
# +++ start of EVENT_HANDLING section -- separate functions for
# keypresses and mouse clicks...
def snowfall(snow):
""" Falling snow
Snow must have a .pos field
"""
if snow.pos.y < 0:
snow.pos.y = 6 + choice([1,2,3,4,5,6])
snow.pos.x = 7*random()*choice([1,-1])
snow.pos.z = 7*random()*choice([1,-1])
def keydown_fun(event):
"""This function is called each time a key is pressed."""
# ball.color = randcolor() # this turns out to be very distracting!
key = event.key
ri = randint(0, 10)
print("key:", key, ri) # Prints the key pressed -- caps only...
amt = 0.42
# move the ball
if key == 'up':
ball.pos = ball.pos + vec(0, 0, -amt)
ball.vel.z = 1
elif key == 'left':
ball.pos = ball.pos + vec(-amt, 0, 0)
ball.vel.x = -1
elif key == 'down':
ball.pos = ball.pos + vec(0, 0, amt)
ball.vel.z = -1
elif key == 'right':
ball.pos = ball.pos + vec(amt, 0, 0)
ball.vel.x = 1
elif key in ' rR':
ball.vel = vec(0, 0, 0) # Reset! via the spacebar, " "
ball.pos = vec(0, 0, 0)
def click_fun(event):
"""This function is called each time the mouse is clicked."""
print("event is", event.event, event.which)
# +++ End of EVENT_HANDLING section
# +++ Other functions can go here...
def choice(L):
"""Implements Python's choice using the random() function."""
LEN = len(L) # Get the length
randomindex = int(LEN*random()) # Get a random index
return L[randomindex] # Return that element
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
def randvel(low, hi):
"""Returns velocity vector of random from low to hi"""
if hi < low:
low, hi = hi, low
LEN = hi - low + 1
randValue1 = LEN*random() + low
randValue2 = LEN*random() + low
return vec(randValue1, 0, randValue2)
def randcolor():
"""Returns a vector of (r, g, b) random from 0.0 to 1.0."""
r = random(0.0, 1.0)
g = random(0.0, 1.0)
b = random(0.0, 1.0)
return vec(r, g, b) # A color is a three-element vector
def corral_collide(ball):
"""Corral collisions!
Ball must have a .vel field and a .pos field.
"""
if ball.pos.z < wallA.pos.z: # Hit -- check for z
ball.pos.z = wallA.pos.z # 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: # Hit -- check for x
ball.pos.x = wallB.pos.x # Bring back into bounds
ball.vel.x *= -1.0 # Reverse the x velocity
if ball.pos.z > wallC.pos.z:
ball.pos.z = wallC.pos.z
ball.vel.z *= -1.0
if ball.pos.x > wallD.pos.x:
ball.pos.x = wallD.pos.x
ball.vel.x *= -1.0