コード例 #1
0
ファイル: RollCats.py プロジェクト: bcjordan/rollcats
 def release(self):
     self.isSwitchHit = True
     X2OGame.game.drawbridgeSound.play()
     if self.fallsLeft:
         self.bridgeJoint.getAsType().SetLimits(-3.1415 / 2, 0.1)
         self.bridgeBody.ApplyImpulse(box2d.b2Vec2(-10.0,0),self.bridgeBody.GetWorldCenter())
     else:
         self.bridgeJoint.getAsType().SetLimits(-0.1, 3.1415 / 2)
         self.bridgeBody.ApplyImpulse(box2d.b2Vec2(10.0,0),self.bridgeBody.GetWorldCenter())            
コード例 #2
0
ファイル: RollCats.py プロジェクト: bcjordan/rollcats
 def __init__(self, bridgeLoc, switchLoc, fallsLeft):
     self.isSwitchHit = False
     # create bridge
     bodyDef = box2d.b2BodyDef()
     bodyDef.position.Set( bridgeLoc[0], bridgeLoc[1] + BridgeAndSwitch.bridgeLength * .5 )
     self.bridgeBody = X2OGame.game.world.CreateBody(bodyDef)
     self.bridgeBody.SetUserData(BRIDGE)
     self.fallsLeft = fallsLeft
     
     shapeDef = box2d.b2PolygonDef()
     shapeDef.SetAsBox(BridgeAndSwitch.bridgeThickness*.5, BridgeAndSwitch.bridgeLength*.5)
     shapeDef.density = 1.0
     shape = self.bridgeBody.CreateShape(shapeDef)
     self.bridgeBody.SetMassFromShapes()
     shape.SetUserData(BRIDGE)
     
     rjd = box2d.b2RevoluteJointDef()
     rjd.Initialize(self.bridgeBody, X2OGame.game.world.GetGroundBody(), box2d.b2Vec2(bridgeLoc[0],bridgeLoc[1]))
     rjd.enableLimit = True
     rjd.lowerAngle = -.05
     rjd.upperAngle = .05
     rjd.collideConnected = True
     
     self.bridgeJoint = X2OGame.game.world.CreateJoint(rjd)
     
     bodyDef2 = box2d.b2BodyDef()
     bodyDef2.position.Set( switchLoc[0], switchLoc[1] + BridgeAndSwitch.switchLength*.5 )
     self.switchBody = X2OGame.game.world.CreateBody(bodyDef2)
     self.switchBody.SetUserData(BRIDGE_SWITCH)
     
     shapeDef2 = box2d.b2PolygonDef()
     shapeDef2.SetAsBox(BridgeAndSwitch.switchThickness*.5, BridgeAndSwitch.switchLength*.5)
     shapeDef2.density = 5.0
     shape2 = self.switchBody.CreateShape(shapeDef2)
     self.switchBody.SetMassFromShapes()
     shape2.SetUserData(BRIDGE_SWITCH)
     
     rjd2 = box2d.b2RevoluteJointDef()
     rjd2.Initialize(self.switchBody, X2OGame.game.world.GetGroundBody(), box2d.b2Vec2(switchLoc[0],switchLoc[1]))
     rjd2.enableLimit = True
     rjd2.lowerAngle = -.02
     rjd2.upperAngle = .02
     rjd2.collideConnected = True
     
     self.switchJoint = X2OGame.game.world.CreateJoint(rjd2)
     BodySprite(self.bridgeBody,BridgeAndSwitch.bridgePicLoc,BridgeAndSwitch.bridgeThickness,BridgeAndSwitch.bridgeLength,0,0,X2OGame.game.worldToScreen,X2OGame.game.screenToWorld)
     BodySprite(self.switchBody,BridgeAndSwitch.switchPicLoc,BridgeAndSwitch.switchThickness,BridgeAndSwitch.switchLength,0,0,X2OGame.game.worldToScreen,X2OGame.game.screenToWorld)
     BodySprite(X2OGame.game.world.GetGroundBody(), BridgeAndSwitch.basePicLoc, BridgeAndSwitch.baseWidth, BridgeAndSwitch.baseHeight, -switchLoc[0], switchLoc[1],X2OGame.game.worldToScreen,X2OGame.game.screenToWorld)
コード例 #3
0
ファイル: RollCats.py プロジェクト: bcjordan/rollcats
    def __init__(self, hingeLoc, opensLeft):
        self.resetTimer = 0
        bodyDef = box2d.b2BodyDef()
        endX = hingeLoc[0] + Launcher.launcherLength
        if opensLeft:
            endX = hingeLoc[0] - Launcher.launcherLength
        bodyDef.position.Set( (hingeLoc[0]+endX) * .5, hingeLoc[1] )
        self.launcherBody = X2OGame.game.world.CreateBody(bodyDef)
        self.launcherBody.SetUserData(LAUNCHER)
        self.opensLeft = opensLeft
        
        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(Launcher.launcherLength*.5, Launcher.launcherThickness*.5)
        shapeDef.density = 1.0
        shape = self.launcherBody.CreateShape(shapeDef)
        self.launcherBody.SetMassFromShapes()
        shape.SetUserData(LAUNCHER)
        
        rjd = box2d.b2RevoluteJointDef()
        rjd.Initialize(self.launcherBody, X2OGame.game.world.GetGroundBody(), box2d.b2Vec2(hingeLoc[0],hingeLoc[1]))
        rjd.enableLimit = True
        rjd.lowerAngle = -3.1415 * .333
        rjd.upperAngle = 0
        
        if opensLeft:        
            rjd.lowerAngle = 0
            rjd.upperAngle = 3.1415 * .333

        
        self.hingeJoint = X2OGame.game.world.CreateJoint(rjd)
        
        BodySprite(self.launcherBody,Launcher.launcherPicLoc,Launcher.launcherLength,Launcher.launcherThickness,0,0,X2OGame.game.worldToScreen,X2OGame.game.screenToWorld)
コード例 #4
0
    def get_bodies_at_pos(self, search_point, include_static=False, area=0.01):
        """ Check if given point (screen coordinates) is inside any body.
            If yes, return all found bodies, if not found return False
        """
        sx, sy = self.to_world(search_point)
        sx /= self.ppm  # le sigh, screen2world returns pixels, so convert them to meters here.
        sy /= self.ppm

        f = area / self.camera.scale_factor

        AABB = box2d.b2AABB()
        AABB.lowerBound.Set(sx - f, sy - f)
        AABB.upperBound.Set(sx + f, sy + f)

        amount, shapes = self.world.Query(AABB, 2)

        if amount == 0:
            return False
        else:
            bodylist = []
            for s in shapes:
                body = s.GetBody()
                if not include_static:
                    if body.IsStatic() or body.GetMass() == 0.0:
                        continue

                if s.TestPoint(body.GetXForm(), box2d.b2Vec2(sx, sy)):
                    bodylist.append(body)

            return bodylist
コード例 #5
0
ファイル: elements.py プロジェクト: FOSSRIT/fortunehunter
    def get_bodies_at_pos(self, search_point, include_static=False, area=0.01):
        """ Check if given point (screen coordinates) is inside any body.
            If yes, return all found bodies, if not found return False
        """
        sx, sy = self.to_world(search_point)
        sx /= self.ppm # le sigh, screen2world returns pixels, so convert them to meters here.
        sy /= self.ppm

        f = area/self.camera.scale_factor

        AABB=box2d.b2AABB()
        AABB.lowerBound.Set(sx-f, sy-f);
        AABB.upperBound.Set(sx+f, sy+f);

        amount, shapes = self.world.Query(AABB, 2)

        if amount == 0:
            return False
        else:
            bodylist = []
            for s in shapes:
                body = s.GetBody()
                if not include_static:
                    if body.IsStatic() or body.GetMass() == 0.0:
                        continue
                        
                if s.TestPoint(body.GetXForm(), box2d.b2Vec2(sx, sy)):
                    bodylist.append(body)

            return bodylist
コード例 #6
0
 def bodiesAtPoint(self,pt,include_static=False,include_sensor=False):
     # modified from the elements source
     # thanks guys!
     
     sx,sy = toWorld(pt)
     f = 0.01
 
     AABB=box2d.b2AABB()
     AABB.lowerBound.Set(sx-f, sy-f);
     AABB.upperBound.Set(sx+f, sy+f);
 
     amount, shapes = self.world.Query(AABB, 2)
 
     if amount == 0:
         return False
     else:
         bodylist = []
         for s in shapes:                
             if s.IsSensor() and not include_sensor: continue
             body = s.GetBody()
             if not include_static:
                 if body.IsStatic() or body.GetMass() == 0.0:
                     continue
                     
             if s.TestPoint(body.GetXForm(), box2d.b2Vec2(sx, sy)):
                 bodylist.append(body)
 
         return bodylist        
コード例 #7
0
ファイル: elements.py プロジェクト: FOSSRIT/fortunehunter
 def mouse_move(self, pos):
     pos = self.to_world(pos)
     x, y = pos
     x /= self.ppm
     y /= self.ppm
                     
     if self.mouseJoint:
         self.mouseJoint.SetTarget(box2d.b2Vec2(x,y))
コード例 #8
0
    def mouse_move(self, pos):
        pos = self.to_world(pos)
        x, y = pos
        x /= self.ppm
        y /= self.ppm

        if self.mouseJoint:
            self.mouseJoint.SetTarget(box2d.b2Vec2(x, y))
コード例 #9
0
ファイル: RollCats.py プロジェクト: bcjordan/rollcats
    def update(self):
        if self.resetTimer > 0:
            self.resetTimer -= 1
            #print self.resetTimer
            return
        torque = -self.hingeJoint.GetReactionTorque()
#        print torque
        if self.opensLeft:
            torque *= -1
        if (torque > Launcher.launchThreshold):
            self.launcherBody.ApplyImpulse( box2d.b2Vec2(0.0, Launcher.launchImpulse), self.launcherBody.GetWorldCenter())
            self.resetTimer = 240
            X2OGame.game.hissSound.play()
コード例 #10
0
ファイル: RollCats.py プロジェクト: bcjordan/rollcats
    def update(self, keyL, keyR, keyJ):
        self.onGround()
        cj = self.canJump()
        if (self.jumpState == RollCat.NO_GROUND_TOUCH and self.cachedOnGround):
            self.jumpState = RollCat.NO_KEY_RELEASE
        elif (keyJ == False):
            self.jumpState = RollCat.READY_TO_JUMP
        jumpImpulse = 70
        moveSpeed = .1
        moveForce = 100
        angle = self.torso.GetAngle() - self.targetAngle
        angVel = self.torso.GetAngularVelocity()
        if (keyL):
            self.targetAngle = .95*self.targetAngle + .05 * .4
#            X2OGame.game.accelSound.play()
            if cj:
                self.wheel.ApplyForce(box2d.b2Vec2(-moveForce,0),self.wheel.GetWorldCenter())
            else:
                self.torso.ApplyForce(box2d.b2Vec2(-moveForce*.4,0),self.torso.GetWorldCenter())
            
        elif (keyR):
            self.targetAngle = .95*self.targetAngle - .05 * .4
#            X2OGame.game.accelSound.play()
            if cj:
                self.wheel.ApplyForce(box2d.b2Vec2(moveForce,0),self.wheel.GetWorldCenter())
            else:
                self.torso.ApplyForce(box2d.b2Vec2(moveForce*.4,0),self.torso.GetWorldCenter())
        else:
            vx = self.wheel.GetLinearVelocity().x
            self.targetAngle = .9*self.targetAngle
            if cj:
                self.wheel.ApplyForce(box2d.b2Vec2(-.1*moveForce*vx,0),self.wheel.GetWorldCenter())
        while (angle > 3.1415):
            angle -= 2*3.1415
        while (angle < -3.1415):
            angle += 2*3.1415
        correctionFactor = 2
        correctionDragFactor = .25
        #torque = -correctionFactor*angle - correctionDragFactor*angVel
        #self.torso.ApplyTorque(torque)
        changeFactor = 1.0
        if (self.cachedOnGround == False):
            changeFactor = 0.3
        self.motorSpeed += changeFactor*(correctionFactor*angle + correctionDragFactor*angVel)
        self.axle.SetMotorSpeed(self.motorSpeed) 
        

        
        if (keyJ and cj and self.jumpState == RollCat.READY_TO_JUMP and self.jumpTimer < 0):
            self.torso.ApplyImpulse(box2d.b2Vec2(0,jumpImpulse),self.torso.GetWorldCenter())
            X2OGame.game.meowSound.play()
            self.jumpTimer = RollCat.initialJumpTimer
            self.jumpState = RollCat.NO_GROUND_TOUCH
        
        self.jumpTimer -= 1
コード例 #11
0
ファイル: RollCats.py プロジェクト: bcjordan/rollcats
    def initializeWorld(self):
        global contactList
        contactList = []
        worldAABB = box2d.b2AABB()
        worldAABB.lowerBound.Set(- 100, - 100)
        worldAABB.upperBound.Set(100, 100)
        gravity = box2d.b2Vec2(0, - 10)
        doSleep = True
        self.world = box2d.b2World(worldAABB, gravity, doSleep)
        self.levelSwitchCountingDown = False
        self.levelSwitchCounter = 0

        # make sun
        BodySprite(self.world.GetGroundBody(), 'Rollcats_Sun.png', 20, 20, -10, -10, self.worldToScreen, self.screenToWorld)

        self.contactListener = myContactListener()
        self.world.SetContactListener(self.contactListener)
コード例 #12
0
 def __init__(self):
     global SCREENHEIGHT
     SCREENHEIGHT = Globals.game.screen.get_size()[1]
     
     # set up box2D    
     worldAABB=box2d.b2AABB()
     worldAABB.lowerBound.Set(-100, -100)
     worldAABB.upperBound.Set(100, 100)
     gravity = box2d.b2Vec2(0, -10)
     self.world = box2d.b2World(worldAABB, gravity, True)
     
     self.timeStep = 1.0/60
     self.run = True
     
     self.contactListener = X2OContactListener()
     self.world.SetContactListener(self.contactListener)
     
     self.makeBoundingBox()
コード例 #13
0
    def __init__(self,
                 screen_size,
                 gravity=(0.0, -9.0),
                 ppm=100.0,
                 renderer='pygame'):
        """ Init the world with boundaries and gravity, and init colors.
        
            Parameters:
              screen_size .. (w, h) -- screen size in pixels [int]
              gravity ...... (x, y) in m/s^2  [float] default: (0.0, -9.0)
              ppm .......... pixels per meter [float] default: 100.0
              renderer ..... which drawing method to use (str) default: 'pygame'

            Return: class Elements()
        """
        self.set_screenSize(screen_size)
        self.set_drawingMethod(renderer)

        # Create Subclasses
        self.add = add_objects.Add(self)
        self.callbacks = callbacks.CallbackHandler(self)
        self.camera = camera.Camera(self)

        # Set Boundaries
        self.worldAABB = box2d.b2AABB()
        self.worldAABB.lowerBound.Set(-100.0, -100.0)
        self.worldAABB.upperBound.Set(100.0, 100.0)

        # Gravity + Bodies will sleep on outside
        gx, gy = gravity
        self.gravity = box2d.b2Vec2(gx, gy)
        self.doSleep = True

        # Create the World
        self.world = box2d.b2World(self.worldAABB, self.gravity, self.doSleep)

        # Init Colors
        self.init_colors()

        # Set Pixels per Meter
        self.ppm = ppm
コード例 #14
0
ファイル: elements.py プロジェクト: FOSSRIT/fortunehunter
    def __init__(self, screen_size, gravity=(0.0,-9.0), ppm=100.0, renderer='pygame'):
        """ Init the world with boundaries and gravity, and init colors.
        
            Parameters:
              screen_size .. (w, h) -- screen size in pixels [int]
              gravity ...... (x, y) in m/s^2  [float] default: (0.0, -9.0)
              ppm .......... pixels per meter [float] default: 100.0
              renderer ..... which drawing method to use (str) default: 'pygame'

            Return: class Elements()
        """
        self.set_screenSize(screen_size)
        self.set_drawingMethod(renderer)
        
        # Create Subclasses
        self.add = add_objects.Add(self)
        self.callbacks = callbacks.CallbackHandler(self)
        self.camera = camera.Camera(self)
        
        # Set Boundaries
        self.worldAABB=box2d.b2AABB()
        self.worldAABB.lowerBound.Set(-100.0, -100.0)
        self.worldAABB.upperBound.Set(100.0, 100.0)
        
        # Gravity + Bodies will sleep on outside
        gx, gy = gravity
        self.gravity = box2d.b2Vec2(gx, gy);
        self.doSleep = True
    
        # Create the World
        self.world = box2d.b2World(self.worldAABB, self.gravity, self.doSleep)

        # Init Colors        
        self.init_colors()
        
        # Set Pixels per Meter
        self.ppm = ppm
コード例 #15
0
ファイル: xolympics.py プロジェクト: sugarlabs/XOlympics
    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             
#            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    
コード例 #16
0
ファイル: RollCats.py プロジェクト: bcjordan/rollcats
    def __init__(self, game, location, bottomGraphicFile, topGraphicFile):
        self.jumpTimer = RollCat.initialJumpTimer
        self.game = game
        self.world = game.world
        self.cachedOnGround = False
        
        self.jumpState = RollCat.NO_GROUND_TOUCH
        
        bodyDef = box2d.b2BodyDef()
        bodyDef.position.Set(location[0], location[1]+1)
        #bodyDef.fixedRotation = True
        body = self.world.CreateBody(bodyDef)
        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(1, 1)
        shapeDef.density = 1
        shapeDef.restitution = 0.3
        shapeDef.friction = 1.0
        shape = body.CreateShape(shapeDef)
        shape.SetUserData(-1)
        body.SetMassFromShapes()
        self.torso = body
        self.torso.SetUserData(ROLLCAT_BODY)


        bodyDef3 = box2d.b2BodyDef()
        bodyDef3.position.Set(location[0],location[1])
        body3 = self.world.CreateBody(bodyDef3)

        shapeDef3 = box2d.b2CircleDef()
        shapeDef3.radius = 0.95
        shapeDef3.density = 1
        shapeDef3.restitution = 0.1
        shapeDef3.friction = 3.0
        shape3 = body3.CreateShape(shapeDef3)
        shape3.SetUserData(-1) #don't need
        body3.SetMassFromShapes()
        self.wheel = body3
        self.wheel.SetUserData(ROLLCAT_WHEEL)
        
        sensorDef = box2d.b2CircleDef()
        sensorDef.radius = 0.9
        sensorDef.isSensor = True
        sensorDef.localPosition = box2d.b2Vec2(0,-1.1)
        sensorShape = self.torso.CreateShape(sensorDef)
        sensorShape.SetUserData(ROLLCAT_JUMP_SENSOR)
        
        sensorDef2 = box2d.b2PolygonDef()
        sensorDef2.SetAsBox(1.1,1.6,box2d.b2Vec2(0,.5),0)
        sensorDef2.isSensor = True
        sensorShape2 = self.torso.CreateShape(sensorDef2)
        sensorShape2.SetUserData(ROLLCAT_WIN_SENSOR) 
        
        jd = box2d.b2RevoluteJointDef()
        jd.Initialize(body, body3, box2d.b2Vec2(location[0],location[1]))
        revJoint = self.world.CreateJoint(jd).getAsType()
        self.axle = revJoint
        
        revJoint.EnableMotor(True)
        revJoint.SetMotorSpeed(0)
        revJoint.SetMaxMotorTorque(4000)
        self.motorSpeed = 0
        
        self.targetAngle = 0
        
        BodySprite(body3,bottomGraphicFile,2, 2, 0, 0, self.game.worldToScreen, self.game.screenToWorld)
        BodySprite(body,topGraphicFile,2, 2, 0, 0,self.game.worldToScreen, self.game.screenToWorld)
コード例 #17
0
def stopBody(body):        
    z = box2d.b2Vec2(0,0)
    body.SetLinearVelocity(z)
    body.SetAngularVelocity(0)
コード例 #18
0
ファイル: xolympics.py プロジェクト: yashagrawal3/XOlympics
    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