Beispiel #1
0
def update_shapes(world):
    # Check to see if any objects are sleeping or coming to a stop. 
    # Then give them a little push.
    for body in world.bodyList:
        v = body.GetLinearVelocity()
        if body.IsSleeping() or v.LengthSquared() < 0.2:
            i = body.GetWorldVector((box2d.b2Random(-200,200), box2d.b2Random(-200,200)))
            p = body.GetWorldPoint((0.0, 0.0))
            body.ApplyImpulse(i, p)
 def LaunchRandomBomb(self):
     """
     Create a new bomb and launch it at the testbed.
     """
     p = box2d.b2Vec2( box2d.b2Random(-15.0, 15.0), 30.0 )
     v = -5.0 * p
     self.LaunchBomb(p, v)
Beispiel #3
0
 def LaunchRandomBomb(self):
     """
     Create a new bomb and launch it at the testbed.
     """
     p = box2d.b2Vec2(box2d.b2Random(-15.0, 15.0), 30.0)
     v = -5.0 * p
     self.LaunchBomb(p, v)
Beispiel #4
0
def main():
    # Initialize pygame
    screen = pygame.display.set_mode((width,height))

    caption= "Python Box2D Testbed Demos"
    pygame.display.set_caption(caption)

    # Initialize the GUI
    theme = gui.Theme("default")
    app = gui.Desktop(theme=theme)

    app.connect(gui.QUIT,app.quit,None)

    main = gui.Container(width=width, height=height)

    # Label at the top left
    main.add(gui.Label("Box2D Testbed Demos", cls="h1"), 20, 20)

    # The dimensions of the list of the demos
    list_size= (width / 2, height / 2)
    list_pos = (width/2 - list_size[0]/2, height/2 - list_size[1]/2)

    # Create the actual widget
    demolist = gui.List(width=list_size[0], height=list_size[1])
    main.add(demolist, list_pos[0], list_pos[1])

    # Add all the demos found in the directory to the list
    add_demos(demolist)

    
    buttonw = (list_size[0]/2-20)        # width of the buttons
    bottom = list_pos[1]+list_size[1]+20 # the y-location of the bottom of the list

    # Create a Run button, calling run_demo on click
    b = gui.Button("Run", width=buttonw)
    main.add(b, list_pos[0], bottom)
    b.connect(gui.CLICK, run_demo, demolist)

    # Create a Quit button
    b = gui.Button("Quit", width=buttonw)
    main.add(b, list_pos[0]+buttonw+30, bottom)
    b.connect(gui.CLICK, lambda x:pygame.event.post(pygame.event.Event(pygame.QUIT)), None)

    # box2d initialization
    z=10 #scale (10 pixels/physics unit)

    # Since we're taking the debug draw implementation from the testbed, it requires a bit
    # of ugliness to set it up properly.
    renderer = fwDebugDraw()
    renderer.surface = screen
    renderer.viewZoom=z
    renderer.viewCenter=box2d.b2Vec2(0,0)
    renderer.width, renderer.height = width, height
    renderer.viewOffset = renderer.viewCenter - box2d.b2Vec2(width, height)/2
    renderer.SetFlags(box2d.b2DebugDraw.e_shapeBit) # we only want shapes to be drawn
    renderer.DrawSolidPolygon = lambda *args: 0 # make it not draw the polygons!
    renderer.DrawPolygon = lambda *args: 0      #

    # Create the world
    worldAABB=box2d.b2AABB()
    worldAABB.lowerBound = (-100.0, -100.0)
    worldAABB.upperBound = ( 100.0, 100.0)
    gravity = (0.0, -10.0)

    world = box2d.b2World(worldAABB, gravity, True)
    world.SetDebugDraw(renderer)

    # Create the ground
    bd = box2d.b2BodyDef()
    bd.position = (0.0, 0.0)
    ground = world.CreateBody(bd)
    
    # The borders of the screen
    sd = box2d.b2PolygonDef()
    sd.SetAsBox(1, height/z, (-width/(2*z)-1, 0), 0)
    ground.CreateShape(sd)
    sd.SetAsBox(1, height/z, (width/(2*z)+1, 0), 0)
    ground.CreateShape(sd)
    sd.SetAsBox(width/z, 1, (0,-height/(2*z)-1), 0)
    ground.CreateShape(sd)
    sd.SetAsBox(width/z, 1, (0,height/(2*z)+1), 0)
    ground.CreateShape(sd)

    # The shape for the file list
    sd.SetAsBox(list_size[0]/(2*z), list_size[1]/(2*z))
    ground.CreateShape(sd)

    # Create a few balls to bounce around the screen
    for i in range(10):
        bd = box2d.b2BodyDef()
        bd.allowSleep = True
        bd.position = (box2d.b2Random(-width/(2*z), width/(2*z)), box2d.b2Random(-height/(2*z), height/(2*z)))
        bd.isBullet = True
        bomb = world.CreateBody(bd)
        bomb.SetLinearVelocity(-5.0 * bd.position)

        sd = box2d.b2CircleDef()
        sd.radius = 1
        sd.density = 1.0
        sd.restitution = 0.7
        bomb.CreateShape(sd)
        
        bomb.SetMassFromShapes()

    app.init(main)
    main_loop(world, screen, demolist, app)