from visual import * from math import * from thread import start_new_thread from time import sleep from random import * G = 9.8 # m/s^2 timestep = .001 class pumpkin: def __init__(self, Vx, Vy, Vz, Px=0, Py=3, Pz=0, frag=True, rad=1): self.sph = sphere(pos=(Px, Py, Pz), radius=rad, color=(1, 0.34, 0.2)) self.time = 0 self.Vy = Vy self.Vx = Vx self.Vz = Vz self.Py = Py self.Px = Px self.Pz = Pz self.frag = frag def thread_launch(self): start_new_thread(self.launch, tuple()) def launch(self): time = 0 while self.Py > 0: # Move the pumpkin along (change position) self.Px += self.Vx * timestep self.Py += self.Vy * timestep self.Pz += self.Vz * timestep self.sph.pos = (self.Px, self.Py, self.Pz) # Update Velocity self.Vy -= G * timestep # Vx would change if we added drag # Go on to the next timestep time += timestep sleep(timestep) if self.frag: self.land() self.delete() def delete(self): self.sph.visible = False del self.sph def land(self): fragments = [pumpkin(self.Vx * .5 + vaddr(10), -(0.5 * self.Vy), vaddr(10), self.Px, .1, self.Pz, False, rad=0.5) for i in range(10)] for f in fragments: f.thread_launch() def vaddr(m): return randrange(-m * 100, m*100)/ 100.0 D = display(background=(1, 1, 1)) treb = box(pos=(-50, 0, 0), height=3, width=3, color=(1, 0, 0)) D.autoscale = False while True: pumpkins = [pumpkin(7 + vaddr(5), 30 + vaddr(10), vaddr(3), Px=-50) for i in range(10)] for p in pumpkins: p.thread_launch() sleep(.1) sleep(5)