def main(): # global variables global screen, pd, physenv, w, h, bubbles ## Graphics Initialization w, h = 400, 400 screen = pygame.display.set_mode((w, h)) pygame.display.set_caption("Bubble") pd = pygame.draw ## Setup physenv grav_dir_index = 0 physenv = cake.c_master(friction=1.0, gravity_glob=[0., [0, 50, -50.][grav_dir_index]], slip=0.0) ## Bubbles bubble_starts = [[w/2, h/2 + [0, -100, 100][grav_dir_index]]] bubbles = [bubble.bubble(physenv, c, 500) for c in bubble_starts] bubbles[0].area *= 6 ## Walls c42 = [w/2,h/2-30] make_wye(org = c42, channelWidth = 30, theta = n.pi/5) oy58 = -100 orx58 = 50 # physenv.make_wall([[w/2 - orx58, h/2 + oy58], [w/2 + orx58, h/2 + oy58]]) run_continuous()
def main(): # global variables global screen, pd, master, w, h, dcBright ## Graphics Initialization w = 1200 h = 250 screen = pygame.display.set_mode((w, h)) pygame.display.set_caption("Amoebas") pd = pygame.draw drawmode = 'stick' dcBright = randColorBright() master = cake.c_master(friction=.92, gravity_glob=[0., 200.], slip=.8) amoebas = [] amoeba0 = amoeba.amoeba(master, [100, h-80]) amoeba0.circle_res = 14 amoeba0.circle_radius = 50. amoeba0.node_mass = 1.0 amoeba0.treading = 3 amoeba0.treadk = 100. amoeba0.tread_damp = .5 amoeba0.musclek = 100 amoeba0.muscle_period = .2*2*pi amoeba0.muscle_amp = 50. amoeba0.muscle_damp = .5 amoeba1 = amoeba.amoeba(master, [100, h-80]) amoeba1.circle_res = 20 amoeba1.circle_radius = 50. amoeba1.node_mass = 1.0 amoeba1.treading = 2 amoeba1.treadk = 200. amoeba1.tread_damp = .5 amoeba1.musclek = 400 amoeba1.muscle_period = 1.2 amoeba1.muscle_amp = 60. amoeba1.muscle_damp = .9 amoeba2 = amoeba.amoeba(master, [150, h-70]) amoeba2.circle_res = 14 amoeba2.circle_radius = 20. amoeba2.node_mass = 1. amoeba2.treading = 2 amoeba2.treadk = 100. amoeba2.tread_damp = .9 amoeba2.musclek = 400. amoeba2.muscle_period = .7 amoeba2.muscle_amp = 30. amoeba2.muscle_damp = amoeba2.tread_damp amoeba3 = amoeba.amoeba(master, [100, h-80]) amoeba3.circle_res = 8 amoeba3.circle_radius = 50. amoeba3.node_mass = 1.0 amoeba3.treading = 2 amoeba3.treadk = 200. amoeba3.tread_damp = .5 amoeba3.musclek = 400 amoeba3.muscle_period = .2*2*pi amoeba3.muscle_amp = 60. amoeba3.muscle_damp = .9 amoeba4 = amoeba.amoeba(master, [100, h-80]) amoeba4.circle_res = 10 amoeba4.circle_radius = 50. amoeba4.node_mass = 1.0 amoeba4.treading = 2 amoeba4.treadk = 200. amoeba4.tread_damp = .5 amoeba4.musclek = 400 amoeba4.muscle_period = .18*2*pi amoeba4.muscle_amp = 90. amoeba4.muscle_damp = .9 amoeba5 = amoeba.amoeba(master, [100, h-180]) amoeba5.circle_res = 16 amoeba5.circle_radius = 30. amoeba5.node_mass = 1.0 amoeba5.treading = 2 amoeba5.treadk = 600. amoeba5.tread_damp = .5 amoeba5.musclek = 40 amoeba5.muscle_period = .18*2*pi amoeba5.muscle_amp = 20. amoeba5.muscle_damp = .2 amoebas.append(amoeba5) amoebas.append(amoeba0) amoebas.append(amoeba1) amoebas.append(amoeba2) amoebas.append(amoeba3) amoebas.append(amoeba4) activeID = 0 amoebas[activeID].assemble() # terrain ground_wall = master.make_wall([(-200, h-20), (w+200, h-10)]) slope_wall = master.make_wall([(200, h-5), (400, h-50)]) master.make_wall([(400, h-50), (500, h)]) timescale = 1 timestep = time.time() time_last = time.time() time_start = time.time() last_render = 0. lagged = False frame = -1 while True: frame += 1 ## Event handler for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit(); elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.unicode == 'q': pygame.quit(); sys.exit(); elif event.key == pygame.K_v: dcBright = randColorBright() if drawmode == 'stick': drawmode = 'solid' elif drawmode == 'solid': drawmode = 'stick' elif event.unicode in ['1', '2', '3', '4', '5', '6', '7', '8', '9']: if int(event.unicode) in range(1,len(amoebas)+1): dcBright = randColorBright() amoebas[activeID].destroy() activeID = int(event.unicode)-1 amoebas[activeID].assemble() print "amoeba ID:\t", activeID elif event.key == pygame.K_RIGHT: amoebas[activeID].control.right = True elif event.key == pygame.K_LEFT: amoebas[activeID].control.left = True elif event.key == pygame.K_DOWN: amoebas[activeID].control.down = True elif event.key == pygame.K_UP: amoebas[activeID].control.up = True elif event.key == pygame.K_SPACE: amoebas[activeID].control.poof = True elif event.key == pygame.K_a: amoebas[activeID].control.stick = True # elif event.key == pygame.K_s: # master.slip = .99 elif event.key == pygame.K_d: amoebas[activeID].control.fat = True elif event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT: amoebas[activeID].control.right = False elif event.key == pygame.K_LEFT: amoebas[activeID].control.left = False elif event.key == pygame.K_DOWN: amoebas[activeID].control.down = False elif event.key == pygame.K_UP: amoebas[activeID].control.up = False elif event.key == pygame.K_SPACE: amoebas[activeID].control.poof = False elif event.key == pygame.K_a: amoebas[activeID].control.stick = False # elif event.key == pygame.K_s: # master.slip = .8 elif event.key == pygame.K_d: amoebas[activeID].control.fat = False elif event.type == pygame.MOUSEBUTTONDOWN: amoebas[activeID].destroy() activeID = (activeID + 1) % len(amoebas) amoebas[activeID].assemble() dcBright = randColorBright() print "amoeba ID:\t", activeID ## Time Steppage timestep = time.time() - time_last time_last = time.time() if master.update((time.time()-time_start)*timescale, max=.033) == False: lagged = True # print "simtime\t" + str(master.simtime) # print "time offset\t" + str((time.time() - time_start) - master.simtime) # print "frame\t" + str(frame) # print "step\t" + str(timestep) ## Update Amoeba amoebas[activeID].update(master.timestep) # Win Test if amoebas[activeID].circle[0].pos[0] >= w-15: amoebas[activeID].destroy() activeID = (activeID + 1) % len(amoebas) amoebas[activeID].assemble() dcBright = randColorBright() print "amoeba ID:\t", activeID ## Render # fps frames per second since_render = time.time() - last_render fps = 60. dectime = time.time() - int(time.time()) if since_render >= 1/fps: screen.fill((255, 255, 255)) render(drawmode, lagged) pygame.display.flip() lagged = False last_render = time.time()
def main(): # global variables global screen, pd, master, w, h ## Graphics Initialization w = 200 h = 200 screen = pygame.display.set_mode((w, h)) pygame.display.set_caption("Cake Physics Engine") pd = pygame.draw ## Setup Master master = cake.c_master(friction=0.92, gravity_glob=[0.0, 150.0], slip=0.3) ## Object Creation # physics sandbox na = master.make_node(1.0, [w - 100.0, 10.0], i_vel=[0.01, 0.0]) nb = master.make_node(1.0, [w - 200.0, 200.0]) nc = master.make_node(1.0, [w - 120.0, 130.0]) tmpk = 30.0 tmpd = 1.0 controlme = master.make_spring(na, nb, 90.0, tmpk, damp=tmpd) controlme2 = master.make_spring(na, nc, 90.0, tmpk, damp=tmpd) master.make_spring(nb, nc, 90.0, tmpk) master.make_node(1.0, [430, 190]) master.make_node(1.0, [300, 190]) master.make_node(1.0, [450, 190]) # test slip spring master.make_wall([((w / 2) - 100.0, 50.0), ((w / 2) + 40.0, 30.0)], slip=1.0) tna = master.make_node(1.0, [w / 2.0, 20.0]) tnb = master.make_node(1.0, [(w / 2) + 2.0, 60.0]) master.make_spring(tna, tnb, 41.0, 50.0, damp=1.0) ttna = master.make_node(1.0, [w / 2 + 20.0, 15.0]) ttnb = master.make_node(1.0, [(w / 2 + 20) + 2.0, 55.0]) master.make_spring(ttna, ttnb, 41.0, 50.0, damp=0.0) independent_node = master.make_node(1.0, [w / 2 + 10.0, 20.0]) # test slip spring 2 # master.make_wall([(500.,200.),(550.,170.)], slip=1.) # master.make_wall([(550.,170.),(600.,200.)], slip=1.) # master.make_node(1.0, n.array([525., 170.])) # master.make_node(1.0, n.array([575., 170.])) # master.make_node(1.0, n.array([580., 170.])) # test damp spring master.make_spring(master.make_node(10, [w / 2, h - 20]), master.make_node(10, [w / 2, h - 80]), 120, 100.0, damp=1) # terrain ground_wall = master.make_wall([(-200, h - 20), (w + 200, h - 10)]) slope_wall = master.make_wall([(200, h - 5), (400, h - 50)]) master.make_wall([(400, h - 50), (500, h)]) master.make_wall([(w - 10, h), (w - 100, 5)], 1.0) timescale = 1 timestep = time.time() time_last = time.time() time_start = time.time() last_render = 0.0 lagged = False frame = -1 while True: frame += 1 ## Event handler for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.unicode == "q": pygame.quit() sys.exit() if event.key == pygame.K_SPACE: pass if event.unicode in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]: pass if event.type == pygame.MOUSEBUTTONDOWN: pass timestep = time.time() - time_last time_last = time.time() if master.update((time.time() - time_start) / timescale, max=0.033) == False: lagged = True # print "simtime\t" + str(master.simtime) # print "time offset\t" + str((time.time() - time_start) - master.simtime) # print "frame\t" + str(frame) # print "step\t" + str(timestep) ## Render # fps frames per second since_render = time.time() - last_render fps = 60.0 dectime = time.time() - int(time.time()) if since_render >= 1 / fps: render(lagged) lagged = False last_render = time.time()