from __future__ import division, print_function from visual import * from time import sleep from random import random from thread import start_new_thread display(background=(1, 1, 1)) def add_positions(a, b): return a[0] + b[0], a[1] + b[1], a[2] + b[2] class table: def __init__(self, pos, height, color): self.top = box(pos=pos, length=height*1.4, width=height*1.4, height=height/5, color=color) # lo stands for "leg offset" tx, ty, tz = pos lo = height*0.6 xlo = tx + lo nxlo = tx - lo zlo = tz + lo nzlo = tz - lo self.l1 = cylinder(pos=(xlo, ty, zlo), axis=(0, -height, 0), radius=height/10, color=color) self.l2 = cylinder(pos=(nxlo, ty, zlo), axis=(0, -height, 0), radius=height/10, color=color) self.l3 = cylinder(pos=(xlo, ty, nzlo), axis=(0, -height, 0), radius=height/10, color=color) self.l4 = cylinder(pos=(nxlo, ty, nzlo), axis=(0, -height*.95, 0), radius=height/10, color=color) def delete(self): self.top.visible = False del self.top self.l1.visible = False del self.l1 self.l2.visible = False del self.l2 self.l3.visible = False del self.l3 self.l4.visible = False del self.l4 # This just starts the explosion in a new thread def explode(self): start_new_thread(self.do_explode, tuple()) def do_explode(self): our_color = self.top.color our_position = self.top.pos self.delete() all_particles = [] for x in range(100): particle = sphere(pos=our_position, color=our_color) particle.trajectory = (0.5 - random(), 0.5 - random(), 0.5 - random()) all_particles.append(particle) for x in range(100): sleep(0.01) for particle in all_particles: particle.pos = add_positions(particle.pos, particle.trajectory) for p in all_particles: p.visible = False del p def move(self, direction): self.top.pos = add_positions(direction, self.top.pos) self.l1.pos= add_positions(direction, self.l1.pos) self.l2.pos= add_positions(direction, self.l2.pos) self.l3.pos= add_positions(direction, self.l3.pos) self.l4.pos= add_positions(direction, self.l4.pos) our_tables = [] our_tables.append(table((0, 5, 0), 3, (1, 1, 1))) our_tables.append(table((7,3, 0), 2, (1, 0, 0))) our_tables.append(table((0, 7, 0), 2, (.3, .3, 1))) our_tables.append(table((0, 8, 0), 1, (.3, 1, .3))) our_tables.append(table((0, 8.6, 0), .6, (1, 1, .3))) our_tables.append(table((0, 4, 0), 2, (1, .3, 1))) for t in our_tables: sleep(.5) t.explode() def launch_table(): new_table = table((0, 0, 0), 10, (random(), random(), random())) trajectory = (6*(0.5 - random()), 5 + random(), 6*(0.5 - random())) for x in range(40): sleep(0.01) new_table.move(trajectory) new_table.explode() while True: sleep(random()) start_new_thread(launch_table, tuple())