Exemplo n.º 1
0
    def __init__(self,name):
        self.allObjects = pygame.sprite.LayeredDirty()
        bgimage = ImageHandler()[name]
        self.physicalSize = Vect(float(bgimage.get_width())/PIXELS_PER_METER,float(bgimage.get_height())/PIXELS_PER_METER)
        self.drawDebug = False

        world_bounds = Box2D.b2AABB()
        world_bounds.lowerBound = (0,0)
        world_bounds.upperBound = (self.physicalSize[0],self.physicalSize[1])
        doSleep = True
        self.physicsWorld = Box2D.b2World(world_bounds, GRAVITY, doSleep)
        self.listener = MyListener()
        self.listener.test = self
        self.physicsWorld.SetContactListener(self.listener)
        self.points=[]

        self.background = None
Exemplo n.º 2
0
class RenderableObject(pygame.sprite.DirtySprite):

    def __init__(self,physicalPosition,physicsWorld,imageName,hasPhysics=True,isStatic=False,canRotate=True,userData=""):
        pygame.sprite.DirtySprite.__init__(self)
        self.image = ImageHandler()[imageName] # Returns a pygame surface

        width = self.image.get_width()
        height = self.image.get_height()
        self.rect = pygame.Rect(0,0,width,height)

        if not isinstance(physicalPosition, Vect):
            physicalPosition = Vect(*physicalPosition)
        self.physicalPosition = physicalPosition

        self.physicsWorld = physicsWorld
        self.hasPhysics = hasPhysics
        if self.hasPhysics:
            self._buildPhysics(width,height,canRotate,isStatic)
            self.body.SetUserData(userData)

    def _buildPhysics(self,width,height,canRotate,isStatic,friction=FRICTION):
        bodyDef = Box2D.b2BodyDef()
        bodyDef.position = (self.physicalPosition[0],self.physicalPosition[1])
        bodyDef.fixedRotation = not canRotate
        bodyDef.linearDamping = 0.15
        self.body = self.physicsWorld.CreateBody(bodyDef)
        #self.body.SetUserData(self)

        shapeDef = Box2D.b2PolygonDef()
        shapeDef.SetAsBox((width / 2.0) /  PIXELS_PER_METER, (height / 2.0) / PIXELS_PER_METER)
        if isStatic:
            shapeDef.density = 0
        else:
            shapeDef.density = DENSITY
        shapeDef.linearDamping = AIR_RESISTANCE
        shapeDef.friction = friction
        
        self.body.CreateShape(shapeDef)
        self.body.SetMassFromShapes()

    def update(self, msSinceLast):
        '''
        Overrides Sprite update
        '''
        if Viewport().hasMoved:
            self.rect.center = Viewport().convertPhysicalToPixelCoords(self.__physicalPosition)
            self.dirty = 1
            
        if self.hasPhysics:
            newPhysicalPosition = Vect(self.body.position.x,self.body.position.y)
            if newPhysicalPosition != self.physicalPosition:
                self.physicalPosition = newPhysicalPosition

    def __setPhysicalPosition(self,value):
        self.__physicalPosition = value
        self.rect.center = Viewport().convertPhysicalToPixelCoords(value)
        self.dirty = 1

    def __getPhysicalPosition(self):
        return self.__physicalPosition

    physicalPosition = property(__getPhysicalPosition,__setPhysicalPosition)
    
    def __setPixelPosition(self,value):
        self.rect.move_ip(*value)
        self.__physicalPosition = Viewport().convertPixelsToPhysicalCoords(self.rect.center)

    def __getPixelPosition(self):
        return (self.rect.left,self.rect.top)

    pixelPosition = property(__getPixelPosition,__setPixelPosition)