class Physical(Model): """ @param dispatcher: game.Dispatcher @param physics: physics.Physics @param shape: tuple<physics.Position> | list<physics.Position> @param pos: physics.Position """ def __init__(self, dispatcher, physics, shape, pos): super(Physical, self).__init__(dispatcher) self._body = Body(physics, shape, pos) """ @return physics.Position """ def getPos(self): return self._body.getPos() """ @param pos: physics.Position """ def setPos(self, pos): self._body.setPos(pos) """ @return tuple<physics.Position> | list<physics.Position> """ def getShape(self): return self._body.getShape()
def addBodies(self, world, objectList): for layer in self.tiled_map.layers: if isinstance(layer, pytmx.TiledObjectGroup): for obj in layer: if obj.type == "block": b = Body( Vector2(obj.x - 16 + obj.width / 2, obj.y - 16 + obj.height / 2), 'rect', Vector2(obj.width, obj.height), 'static') world.addBody(b) if obj.image != None and obj.type == "block" or obj.type == "": b = MapProp( Vector2(obj.x - 16 + obj.width / 2, obj.y - 16 + obj.height / 2), obj.image) objectList.append(b) pass
def __init__( self, x, y, width, height, frameHandler): pygame.sprite.Sprite.__init__(self) # first, make a self self.image = pygame.Surface([width, height]) # This will be changed # Dimensions stuff self.rect = self.image.get_rect() self.width = self.rect.width self.height = self.rect.height # Physics. self.body = Body(x, y, width, height) self.rect.x = self.body.x self.rect.y = self.body.y # Sprite frame handling self.frameHandler = frameHandler self.updateImage() self.propLastCollide = None self.fullTime = 0
def __init__(self, dispatcher, physics, shape, pos): super(Physical, self).__init__(dispatcher) self._body = Body(physics, shape, pos)
class GameObject(sprite.Sprite): """An object in the game.""" def __init__( self, x, y, width, height, frameHandler): pygame.sprite.Sprite.__init__(self) # first, make a self self.image = pygame.Surface([width, height]) # This will be changed # Dimensions stuff self.rect = self.image.get_rect() self.width = self.rect.width self.height = self.rect.height # Physics. self.body = Body(x, y, width, height) self.rect.x = self.body.x self.rect.y = self.body.y # Sprite frame handling self.frameHandler = frameHandler self.updateImage() self.propLastCollide = None self.fullTime = 0 def draw(self, pos, surf): """Draw the GameObject on the screen if it is in a visible state.""" if not self.frameHandler.visible: return surf.blit(self.image, self.getScreenPos(pos)) def update(self, dt): self.fullTime += dt self.updateImage() self.body.update(dt) self.rect.x = self.body.x self.rect.y = self.body.y super(GameObject, self).update() def getScreenPos(self, playerPosition): """ Gets the screen-adjusted position of the sprite's rect. Due to sidescrolling, we need to offset by the player's position. The x/yOffs are usually zero but may need to be set for non-standard- sized frames like melee attacks. """ return (self.rect.x - playerPosition[0] + self.image.xOff, self.rect.y - playerPosition[1] + self.image.yOff) def updateImage(self): """Update the current image of our sprite animation.""" self.image = self.frameHandler.getImage(self.body.facing) def isInRect(self, pos): """ Returns True if the sprite is within the displayed screen, False otherwise. """ screenPos = self.getScreenPos(pos) return -self.rect.width < screenPos < \ glob.Screen.WIDTH + self.rect.width and \ -self.rect.width < screenPos < \ glob.Screen.HEIGHT + self.rect.width def setCollided(self, collidesWith): """ Handle collision restitution for the GameObject with another object. Only handles tile collisions at the moment. Return "remove" to remove tile. """ self.propLastCollide = collidesWith.prop self.body.collidePlatform(collidesWith) def reloadSprite(self): """Reloads the GameObject's sprite from memory.""" self.frameHandler.reloadSprite() def pos(self): """Returns a tuple of the position of the GameObject's body.""" return (self.body.x, self.body.y)