def main(self): # Window Size w, h = size = (900, 900) # OpenGL Init win = window.Window(w, h) initGL() # Create the Physical Space Class self.world = elements.Elements(size, renderer='opengl_pyglet') # Set pyglet properties self.world.set_inputAxisOrigin(left=True, top=False) # Add ground self.world.add.ground() # Mouse click callback win.on_mouse_press = self.mouse_click # Main Loop while not win.has_exit: win.dispatch_events() win.clear() self.world.update() self.world.draw() win.flip()
def __init__(self,screen): self.screen = screen # get everything set up self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 18) # font object #self.canvas = olpcgames.canvas self.joystickobject = None self.debug = True # create the name --> instance map for components self.tb_rects = {} self.toolList = {} for c in tools.allTools: self.toolList[c.name] = c(self) self.currentTool = self.toolList[tools.allTools[0].name] # set up the world (instance of Elements) self.world = elements.Elements(self.screen.get_size()) self.world.renderer.set_surface(self.screen) # set up static environment #self.world.add.ground() self.world.run_physics = False self.bridge = Bridge(self) self.bridge.create_world()
def __init__(self, screen): self.screen = screen # Get everything set up self.clock = pygame.time.Clock() self.canvas = olpcgames.ACTIVITY.canvas self.in_focus = True # Create the name --> instance map for components self.toolList = {} for c in tools.allTools: self.toolList[c.name] = c(self) self.currentTool = self.toolList[tools.allTools[0].name] # Set up the world (instance of Elements) self.box2d = box2d self.world = elements.Elements(self.screen.get_size()) self.world.renderer.set_surface(self.screen) # Set up static environment self.world.add.ground() # Fake a Sugar cursor for the pyGame canvas area self.show_fake_cursor = False pygame.mouse.set_cursor((8, 8), (0, 0), (0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0)) self.cursor_picture = pygame.image.load('standardcursor.png') self.cursor_picture.convert_alpha() self.canvas.connect("enter_notify_event", self.switch_on_fake_pygame_cursor_cb) self.canvas.connect("leave_notify_event", self.switch_off_fake_pygame_cursor_cb) self.canvas.add_events(gtk.gdk.ENTER_NOTIFY_MASK | gtk.gdk.LEAVE_NOTIFY_MASK)
def __init__(self): # PyGame Init pygame.init() self.load_sounds() self.clock = pygame.time.Clock() size = (1200, 900) self.screen = pygame.display.set_mode(size) self.world = elements.Elements(size) self.world.renderer.set_surface(self.screen)
def __init__(self, size=(640, 480)): print "Initializing the window..." # Create a new window self.window = win = gtk.Window(gtk.WINDOW_TOPLEVEL) win.set_title("Demo 11 - Using GTK+/Cairo") # Connect the destroy function with the destruction of the window win.set_default_size(size[0], size[1]) # Add the drawing area to the window da = gtk.DrawingArea() win.add(da) win.connect("destroy", self.destroy) win.connect("key_press_event", self.keydown) win.connect("key_release_event", self.keyup) da.set_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK) da.connect("button_press_event", self.mousedown) da.connect("button_release_event", self.mouseup) win.connect("motion_notify_event", self.mousemove) # show the window win.show_all() # Create the Physical Space Class self.world = world = elements.Elements(screen_size=size, renderer="cairo") #self.world.set_inputAxisOrigin(left=True, top=True) self.world.renderer.set_drawing_area(da) world.callbacks.add(CALLBACK_DRAWING_END, self.custom_draw) b1 = world.add.ball((211, 101), 50) b2 = world.add.ball((200, 100), 50) # Add contact callbacks world.callbacks.add(CALLBACK_CONTACT_ADD, self.contact_add) world.callbacks.add(CALLBACK_CONTACT_ADD, self.contact_add_ball, [b1]) # Add the ground world.add.ground() gobject.timeout_add(1000 / 60, self.physics_update) gobject.timeout_add(1000 / 60, self.draw_update) self.world.renderer.set_circle_image("smiley.png")
def main(): # PyGame Init pygame.init() size = (1200, 900) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() # Create the Physical Space Class world = elements.Elements(size) world.renderer.set_surface(screen) # Default Settings running = True # Main Loop while running: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): # Bye Bye running = False elif event.type == KEYDOWN and event.key == K_SPACE: # Pause with SPACE world.run_physics = not world.run_physics elif event.type == MOUSEBUTTONDOWN and event.button == 1: # Add Ball world.add.ball(event.pos, radius=30, dynamic=False) elif event.type == MOUSEBUTTONDOWN and event.button == 3: # Add Square #world.add_triangle(event.pos, 60) world.add.rect(event.pos, width=100, height=20) # Clear Display screen.fill((255,255,255)) # Update & Draw World world.update() world.draw() # Flip Display pygame.display.flip() # Try to stay at 50 FPS clock.tick(50) # output framerate in caption pygame.display.set_caption("elements: %i | fps: %i" % (world.element_count, int(clock.get_fps())))
def mass(self, **specialisations): """ specialisations maps symbol to a dictionary d providing a mass by d["mass"], eg: specialisations = { 'C' : 12.0 } inst.mass(C=12.0) or if you use the mass module: inst.mass(C=mass.C12) or you use mass in connection with the elements module: inst.mass(C=elements.C12) """ el = elements.Elements() items = self._dictForm.items() def get_mass(sym, massnum): # if mass num is None, and there is a specialisation # provided, we take this specialisation. Else we use # data from el, where a massnumber None is mapped to the # monoisotopic element: if massnum is None: specialisation = specialisations.get(sym) if specialisation is not None: if isinstance(specialisation, collections.Mapping): return specialisation["mass"] try: return float(specialisation) except: raise Exception("specialisation %r for %s invalid"\ % (specialisation, sym)) return el.getMass(sym, massnum) masses = list(get_mass(sym, massnum) for (sym, massnum), _ in items) if None in masses: return None return sum(m * c for m, (_, c) in zip(masses, items))
def main(): print "(i) Draw a line between two bodies to create a distance joint" # PyGame Init pygame.init() size = (1200, 900) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() # Create the Physical Space Class world = elements.Elements(size) world.renderer.set_surface(screen) # Add A Ground world.add.ground() # Joint 1: # Fix a Rectangle to the Background with a Revolute Joint in the center body = world.add.rect((440, 200), width=160, height=20) world.add.joint(body) # Default Settings running = True draw_poly = False points = [] p2 = [] p3 = [] # Joint Bodies jb1 = None jb2 = None # Main Loop while running: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): # Bye Bye running = False elif event.type == KEYDOWN and event.key == K_SPACE: # Pause with SPACE world.run_physics = not world.run_physics elif event.type == MOUSEBUTTONDOWN and event.button == 3: # Add Square world.add.triangle(event.pos, sidelength=50) elif event.type == MOUSEBUTTONDOWN and event.button == 1: jb1 = jb2 = None # Joint Bodies jb1 = world.get_bodies_at_pos(event.pos) if not draw_poly: draw_poly = True points = [] points.append(event.pos) elif event.type == MOUSEBUTTONUP and event.button == 1: if jb1: jb2 = world.get_bodies_at_pos(event.pos) if jb2: if str(jb1) != str(jb2): print jb1, jb2 print "add joint" world.add.joint(jb1[0], jb2[0], points[0], points[-1]) draw_poly = False continue if draw_poly: # Create Polygon draw_poly = False points.append(event.pos) if len(points) > 2: #print points #print len(points) body, p2 = world.add.complexPoly(points) else: world.add.rect(event.pos, width=70, height=20) elif event.type == MOUSEMOTION and draw_poly: world.run_physics = False points.append(event.pos) # Clear Display screen.fill((255,255,255)) # Update & Draw World world.update() world.draw() # Show line if drawing a wall if draw_poly and len(points) > 1: #print points world.renderer.draw_lines(THECOLORS["black"], False, points, 3) # Draw polygon reduced spots and line if p2 != None and len(p2) > 2: world.renderer.draw_lines(THECOLORS["red"], False, p2, 2) for p in p2: world.renderer.draw_circle(THECOLORS["red"], p, 5, 0) # Flip Display pygame.display.flip() # Try to stay at 50 FPS clock.tick(50) # output framerate in caption pygame.display.set_caption("elements: %i | fps: %i" % (world.element_count, int(clock.get_fps())))
def main(): # PyGame Init pygame.init() size = (1200, 900) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() # Create the Physical Space Class world = elements.Elements(size) world.renderer.set_surface(screen) menu = MenuClass() # Add Main Menu Items item1 = menu.addItem('File', callback=click_menu) item2 = menu.addItem('Item2') # Add SubMenu Items (parent=...) # 1. For [File] (parent=item1) menu.addItem('Pause', callback=click_menu, parent=item1, userData=world) menu.addItem('Screenshot', callback=click_menu, parent=item1, userData=world) menu.addItem('Quit', callback=click_menu, parent=item1) # 2. For [Item2] (parent=item2) menu.addItem('SubItem', callback=click_menu, parent=item2) menu.addItem('SubItem2', callback=click_menu, parent=item2) # Add A Ground world.add.ground() # Joint 1: # Fix a Rectangle to the Background with a Revolute Joint in the center body = world.add.rect((140, 700), width=160, height=20) world.add.joint(body) body = world.add.rect((640, 100), width=320, height=20) world.add.joint(body) # Start at x,y=(100,100), means 100 pixels to the right and 100 pixels down #world.camera.set_offset((100, 100)) # Default Settings running = True draw_poly = False points = [] p2 = [] p3 = [] # Joint Bodies jb1 = None jb2 = None # Main Loop while running: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): # Bye Bye running = False elif event.type == KEYDOWN: #print event.key if event.key == K_SPACE: # Pause with SPACE world.run_physics = not world.run_physics elif event.unicode == "+": world.camera.inc_scale_factor(+0.1) elif event.unicode == "-": world.camera.inc_scale_factor(-0.1) elif event.key in [271, 13]: pygame.event.post( pygame.event.Event(MOUSEBUTTONUP, { 'button': 2, 'pos': pygame.mouse.get_pos() })) elif event.key == 273: # up world.camera.inc_offset((0, -30)) elif event.key == 274: # down world.camera.inc_offset((0, 30)) elif event.key == 276: # left world.camera.inc_offset((-30, 0)) elif event.key == 275: # right world.camera.inc_offset((30, 0)) elif event.type == MOUSEBUTTONDOWN: if menu.click(event.pos): continue if event.button == 1: jb1 = jb2 = None # Joint Bodies jb1 = world.get_bodies_at_pos(event.pos) if not draw_poly: draw_poly = True points = [] points.append(event.pos) elif event.button == 4: #scroll up world.camera.inc_scale_factor(+0.02) elif event.button == 5: # scroll down world.camera.inc_scale_factor(-0.02) elif event.type == MOUSEBUTTONUP: if event.button == 1: if jb1: jb2 = world.get_bodies_at_pos(event.pos) if jb2: if str(jb1[0]) != str(jb2[0]): print "- Add Joint between:", jb1[0], jb2[0] world.add.joint(jb1[0], jb2[0], points[0], points[-1]) draw_poly = False continue if draw_poly: # Create Polygon draw_poly = False points.append(event.pos) if len(points) > 2: #print points #print len(points) body, p2 = world.add.complexPoly(points) else: #world.add.rect(event.pos, width=70, height=30) world.add.ball(event.pos, radius=40) elif event.button == 2: # we want the position to become center of the screen world.camera.center(event.pos) elif event.button == 3: # Add Square # body = world.add.triangle(event.pos, sidelength=50) # world.camera.track(body) bodies = world.get_bodies_at_pos(event.pos) if bodies: world.camera.track(bodies[0]) elif event.type == MOUSEMOTION and draw_poly: world.run_physics = False points.append(event.pos) # Clear Display screen.fill((255, 255, 255)) # Update & Draw World world.update() world.draw() # Draw the Menu menu.draw(screen) # Show line if drawing a wall if draw_poly and len(points) > 1: #print points world.renderer.draw_lines(THECOLORS["black"], False, points, 3) # Draw polygon reduced spots and line if p2 != None and len(p2) > 2: world.renderer.draw_lines(THECOLORS["red"], False, p2, 2) for p in p2: world.renderer.draw_circle(THECOLORS["red"], p, 5, 0) # Flip Display pygame.display.flip() # Try to stay at 50 FPS clock.tick(90) # output framerate in caption pygame.display.set_caption("elements: %i | fps: %i" % (world.element_count, int(clock.get_fps())))
def main(args=None): description = colored( "Madcore CLI {0} - (c) 2016-2018 Madcore Ltd <https://madcore.ai>". format(get_version()), 'white', attrs=['bold']) parser = MyParser(prog="./madcore.py", description=description) group = parser.add_mutually_exclusive_group() group.add_argument('-p', '--provision', dest="provision", metavar=('CLUSTERFILE'), help='provision based on <cllusterfile>', action='store') group.add_argument('-c', '--clusterfile', dest="clusterfile", metavar=('CLUSTERFILE'), help='set default clusterfile to input <clusterfile>', action='store') group.add_argument( '-i', '--init', dest="init", metavar=('SOURCE_CLUSTERFILE', 'NEW_CLUSTERFILE'), nargs=2, help= 'initialize new yaml clusterfile using existing from template folder', action='store') group.add_argument('--destroy', help='destroy infrastructure', action='store_true') group.add_argument('--kops-update', help='kops update', action='store_true') group.add_argument('--kops-validate', help='kopds validate', action='store_true') group.add_argument('--kubectl-use-context', help='kubectl use context', action='store_true') group.add_argument('--mini-hostname', help='set minikube hostname (will sudo)', action='store_true') group.add_argument('--get-attr', dest="attr", help='get atribute', action='store') group.add_argument('--install-core', help='install core of Madcore', action='store_true') group.add_argument('--install-elk', help='install elk', action='store_true') group.add_argument('--install-neo4j', help='install neo4j', action='store_true') group.add_argument('--install-kafka', help='install apache kafka', action='store_true') group.add_argument('--install-flink', help='install apache flink', action='store_true') group.add_argument('--install-scrapy', help='install scrapy cluster', action='store_true') group.add_argument('--install-scrapyrc', help='install scrapy rc cluster', action='store_true') group.add_argument('--install-tron', help='install tron network', action='store_true') group.add_argument('--install-storm', help='install storm', action='store_true') group.add_argument('--install-keycloak', help='install keycloak', action='store_true') group.add_argument('--install-cert-manager', help='install cert manager', action='store_true') group.add_argument('--install-postgresql', help='install postgresql', action='store_true') args = parser.parse_args() if not args.attr: print print description print args = parser.parse_args() sett = settings.Settings(args) if args.provision: sett.set_clusterfile() sett.save_settings_file() sett.load_clusterfile() prov = provision.Provision(sett) prov.start() return elif args.clusterfile: # switch happens in settings sett.set_clusterfile() sett.save_settings_file() sett.load_clusterfile() kc = cmdkubectl.CmdKubectl(sett) kc.use_context() return elif args.init: sett.initialize_new_clusterfile() kc = cmdkubectl.CmdKubectl(sett) kc.get_context() return # settings for the sett.set_clusterfile() sett.save_settings_file() sett.load_clusterfile() sett.set_zone() if args.destroy: prov = provision.Provision(sett) prov.destroy() elif args.kops_update: kops = cmdkops.CmdKops(sett) kops.update_settings() elif args.kops_validate: kops = cmdkops.CmdKops(sett) kops.validate_cluster() elif args.install_core: el = elements.Elements(sett) el.install_elements("core") elif args.install_elk: el = elements.Elements(sett) el.install_elements("elk") elif args.install_neo4j: el = elements.Elements(sett) el.install_elements("neo4j") elif args.install_kafka: el = elements.Elements(sett) el.install_elements("kafka") elif args.install_flink: el = elements.Elements(sett) el.install_elements("flink") elif args.install_scrapy: el = elements.Elements(sett) el.install_elements("scrapy") elif args.install_scrapyrc: el = elements.Elements(sett) el.install_elements("scrapyrc") elif args.install_storm: el = elements.Elements(sett) el.install_elements("storm") elif args.install_tron: el = elements.Elements(sett) el.install_elements("tron") elif args.install_keycloak: el = elements.Elements(sett) el.install_elements("keycloak") elif args.install_cert_manager: el = elements.Elements(sett) el.install_elements("certmanager") elif args.install_postgresql: el = elements.Elements(sett) el.install_elements("postgresql") elif args.kubectl_use_context: kc = cmdkubectl.CmdKubectl(sett) kc.use_context() elif args.mini_hostname: prov = provision.Provision(sett) prov.mini_hostname() elif args.attr: if args.attr == "domain": sys.stdout.write(settings.provision.domain) else: Static.figletcyber("STATUS") kc = cmdkubectl.CmdKubectl(sett) kc.get_nodes() kc.get_pods() kc.get_svc() kc.get_ing()
def run(self): self.rightscore = self.leftscore = 0 self.forcespeed = 75 self.jumpforce = 20 self.leftDPress = False self.rightDPress = False self.leftLPress = False self.leftRPress = False self.leftJump = False self.rightLPress = False self.rightRPress = False self.rightJump = False self.updateList = [] pygame.init() self.screen = pygame.display.get_surface() # get everything set up self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 24) # font object self.joystickobject = None self.debug = True # kids laptop # create the name --> instance map for components self.toolList = {} for c in tools.allTools: self.toolList[c.name] = c(self) #self.currentTool = self.toolList[tools.allTools[0].name] # no tools, eh? # set up the world (instance of Elements) self.world = elements.Elements(self.screen.get_size()) self.world.renderer.set_surface(self.screen) # set up static environment self.world.set_color((0, 255, 0)) self.world.add.ground() self.ball = self.world.add.ball((600, 0), 50) # ADD LEFT BORDER self.world.set_color((255, 0, 0)) self.world.add.rect((0,-20), 25, 900, dynamic=False, density=1.0, restitution=0.16, friction=0.5) self.leftplayer = self.world.add.poly(( 264.0, 81.0 ), ((-109.9405166666667, -64.244016666666653), (110.60718333333335, -63.089316666666605), (-0.66666666666668561, 127.33333333333337)) , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False) # ADD RIGHT BORDER self.world.set_color((0, 0, 255)) self.world.add.rect((1180,-20), 25, 900, dynamic=False, density=1.0, restitution=0.16, friction=0.5) self.rightplayer = self.world.add.poly(( 885.0, 78.0 ), [(108.94051666666667, -65.976066666666611), (2.6666666666666288, 127.33333333333337), (-111.60718333333341, -61.357266666666646)] , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False) # we're getting 2 grounds - grey and green. wtf. # self.leftplayer = # self.world.add.poly((900,800),((-300,300), (300, 300), (300, -300)), dynamic=True, density=1.0, restitution=0.16, friction=0.5) # self.leftplayer = self.world.add.rect((500,0), 25, 90, dynamic=True, density=1.0, restitution=0.16, friction=0.5) self.leftplayer.linearDamping = 0.07 self.test = self.leftplayer.worldCenter # self.rightplayer = self.world.add.rect((900,775), 25, 90, dynamic=True, density=1.0, restitution=0.16, friction=0.5) # hack fix: set_color early! self.running = True while self.running: while Gtk.events_pending(): Gtk.main_iteration() for event in pygame.event.get(): if (event.type == KEYDOWN and (event.key == K_a or event.key == K_KP4)): self.leftLPress = True if (event.type == KEYUP and (event.key == K_a or event.key == K_KP4)): self.leftLPress = False if (event.type == KEYDOWN and (event.key == K_s or event.key == K_KP2)): self.leftDPress = True if (event.type == KEYUP and (event.key == K_s or event.key == K_KP2)): self.leftDPress = False if (event.type == KEYDOWN and (event.key == K_d or event.key == K_KP6)): self.leftRPress = True if (event.type == KEYUP and (event.key == K_d or event.key == K_KP6)): self.leftRPress = False if (event.type == KEYDOWN and (event.key == K_w or event.key == K_KP8)): self.leftJump = True if (event.type == KEYUP and (event.key == K_w or event.key == K_KP8)): self.leftJump = False if (event.type == KEYDOWN and (event.key == K_LEFT or event.key == K_KP7)): self.rightLPress = True if (event.type == KEYUP and (event.key == K_LEFT or event.key == K_KP7)): self.rightLPress = False if (event.type == KEYDOWN and (event.key == K_RIGHT or event.key == K_KP1)): self.rightRPress = True if (event.type == KEYUP and (event.key == K_RIGHT or event.key == K_KP1)): self.rightRPress = False if (event.type == KEYDOWN and (event.key == K_UP or event.key == K_KP9)): self.rightJump = True if (event.type == KEYUP and (event.key == K_UP or event.key == K_KP9)): self.rightJump = False if (event.type == KEYDOWN and (event.key == K_DOWN or event.key == K_KP3)): self.rightDPress = True if (event.type == KEYUP and (event.key == K_DOWN or event.key == K_KP3)): self.rightDPress = False if (event.type == pygame.QUIT): self.running = False pygame.quit() sys.exit() # for event in pygame.event.get(): #self.currentTool.handleEvents(event) if self.leftLPress: self.leftplayer.ApplyForce(box2d.b2Vec2(-self.forcespeed,0), self.leftplayer.worldCenter, True) if self.leftRPress: self.leftplayer.ApplyForce(box2d.b2Vec2(self.forcespeed,0), self.leftplayer.worldCenter, True) if self.leftJump: if self.leftplayer.worldCenter.y < 0.80: self.leftplayer.ApplyLinearImpulse(box2d.b2Vec2(0,self.jumpforce), self.leftplayer.worldCenter, True) if self.rightLPress: self.rightplayer.ApplyForce(box2d.b2Vec2(-self.forcespeed,0), self.rightplayer.worldCenter, True) if self.rightRPress: self.rightplayer.ApplyForce(box2d.b2Vec2(self.forcespeed,0), self.rightplayer.worldCenter, True) if self.rightDPress: self.rightplayer.ApplyLinearImpulse(box2d.b2Vec2(0,-self.jumpforce), self.rightplayer.worldCenter, True) if self.rightJump: if self.rightplayer.worldCenter.y < 0.80: self.rightplayer.ApplyLinearImpulse(box2d.b2Vec2(0,self.jumpforce), self.rightplayer.worldCenter, True) if self.leftDPress: self.leftplayer.ApplyLinearImpulse(box2d.b2Vec2(0,-self.jumpforce), self.leftplayer.worldCenter, True) #if self.leftleft == True # self.leftplayer.ApplyForce((50,0), self.leftplayer.worldCenter) # Clear Display if self.ball.worldCenter.x < 1: print "Goal Blue!", self.rightscore self.leftscore += 1 self.world.set_color((0, 0, 255)) self.ball = self.world.add.ball((600, 0), 50) elif self.ball.worldCenter.x > 11: print "Goal Red!", self.rightscore self.rightscore += 1 self.world.set_color((255, 0, 0)) self.ball = self.world.add.ball((600, 0), 50) # For some reason this isn't being reached... thats # a problem. THe screen is gray. self.screen.fill((255,255,255)) # Update & Draw World self.world.update() self.world.draw() # draw output from tools #self.currentTool.draw() #Print all the text on the screen #text = self.font.render("Current Tool: "+self.currentTool.name, True, (255,255,255)) #textpos = text.get_rect(left=700,top=7) # self.screen.blit(text,textpos) # for displaying text ^ # Flip Display pygame.display.flip() # Try to stay at 30 FPS self.clock.tick(30) # originally 50
def main(): # PyGame Init pygame.init() size = (800, 800) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() # Create the Physical Space Class world = elements.Elements(size) world.renderer.set_surface(screen) # Add A Ground world.add.ground() world.add.wall((100, 100), (300, 300), 5) # Default Settings running = True a = 0 # Main Loop while running: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): # Bye Bye running = False elif event.type == KEYDOWN and event.key == K_SPACE: # Pause with SPACE world.run_physics = not world.run_physics elif event.type == MOUSEBUTTONDOWN and event.button == 1: # Add Mouse Joint if at an Object bodylist = world.get_bodies_at_pos(event.pos, include_static=False) print bodylist if bodylist and len(bodylist) > 0: world.add.mouseJoint(bodylist[0], event.pos) elif event.type == MOUSEBUTTONUP and event.button == 1: # Delete Mouse Joint world.add.remove_mouseJoint() elif event.type == MOUSEMOTION and event.buttons[0]: world.mouse_move(event.pos) elif event.type == MOUSEBUTTONDOWN and event.button == 3: # Add Square world.add.rect(event.pos, width=40, height=20, angle=a) a += 10 elif event.type == KEYDOWN: if event.unicode == "1": # Add many Balls x, y = pygame.mouse.get_pos() for i in range(5): for j in range(5): world.add.ball((x - i, y - j), radius=20) elif event.unicode == "2": # Add many Balls x, y = pygame.mouse.get_pos() for i in range(5): for j in range(5): world.add.rect((x - i, y - j), width=40, height=20, angle=a) # Clear Display screen.fill((255, 255, 255)) # Update & Draw World world.update() world.draw() # Flip Display pygame.display.flip() # Try to stay at 50 FPS clock.tick(50) # output framerate in caption pygame.display.set_caption("elements: %i | fps: %i" % (world.element_count, int(clock.get_fps())))
def main(): # PyGame Init pygame.init() size = (800, 800) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() # Create the Physical Space Class world = elements.Elements(size) # Add A Ground world.add.ground() world.renderer.set_surface(screen) # Default Settings running = True show = True # Main Loop while running: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): # Bye Bye running = False elif event.type == KEYDOWN and event.key == K_SPACE: # Pause with SPACE world.run_physics = not world.run_physics elif event.type == MOUSEBUTTONDOWN and event.button == 1: # Add Ball world.add.rect(event.pos, width=40, height=20, dynamic=False) elif event.type == MOUSEBUTTONDOWN and event.button == 3: # Add Square world.add.rect(event.pos, width=50, height=20) elif event.type == KEYDOWN: if event.unicode == "s": show = not show elif event.unicode == "1": # Add many Balls x, y = pygame.mouse.get_pos() for i in range(5): for j in range(5): world.add.ball((x - i, y - j), 20) elif event.unicode == "2": # Add many Balls x, y = pygame.mouse.get_pos() for i in range(5): for j in range(5): world.add.rect((x - i, y - j), 20, 50) # Update & Draw World world.update() screen.fill((255, 255, 255)) if show: world.draw() pygame.display.flip() # Try to stay at 50 FPS clock.tick(50) # output framerate in caption pygame.display.set_caption("elements: %i | fps: %i" % (world.element_count, int(clock.get_fps())))
def main(): # PyGame Init pygame.init() size = (1200, 900) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() # Create the Physical Space Class world = elements.Elements(size) world.renderer.set_surface(screen) b1 = world.add.ball((100, 100), 100) # Add Contact Callbacks world.callbacks.add(CALLBACK_CONTACT_ADD, contact_add) world.callbacks.add(CALLBACK_CONTACT_ADD, contact_add_ball, [b1]) # Add A Ground world.add.ground() # Default Settings running = True draw_poly = False points = [] # Main Loop while running: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): # Bye Bye running = False elif event.type == KEYDOWN and event.key == K_SPACE: # Pause with SPACE world.run_physics = not world.run_physics elif event.type == MOUSEBUTTONDOWN and event.button == 3: # Add Square world.add.triangle(event.pos, sidelength=50) elif event.type == MOUSEBUTTONDOWN and event.button == 1: # Start/Stop Wall-Drawing #world.add_ball(event.pos) if not draw_poly: draw_poly = True points = [] points.append(event.pos) elif event.type == MOUSEBUTTONUP and event.button == 1: if draw_poly: # Create Polygon draw_poly = False points.append(event.pos) if len(points) > 2: print points print len(points) body = world.add.complexPoly(points) world.callbacks.add(CALLBACK_CONTACT_ADD, contact_add_poly, body) else: world.add.rect(event.pos, width=80, height=30) elif event.type == MOUSEMOTION and draw_poly: points.append(event.pos) # Clear Display screen.fill((255, 255, 255)) # Update & Draw World world.update() world.draw() # Show line if drawing a wall if draw_poly and len(points) > 1: pygame.draw.lines(screen, THECOLORS["black"], False, points) # Flip Display pygame.display.flip() # Try to stay at 50 FPS clock.tick(50) # output framerate in caption pygame.display.set_caption("elements: %i | fps: %i" % (world.element_count, int(clock.get_fps())))