def __init__ (self, real, within, player, vel, dirn, pos): Entity.__init__(self, vel) self.size = conf.MINE['size'] r = Rect((0, 0), self.size) r.center = pos self.graphics.pos = r.clamp(within).topleft self.real = real self.player = player self.placed = None
class Camera: def __init__(self, x: int, y: int, screen: Surface): """ Creates a camera object @bug scaling not considered @param x: initial x position of the camera in *world coordinate* @param y: initial y position of the camera in *world coordinate* @param screen: screen object used to determine width and height of the camera view """ self.centre = Vector2(x, y) self.size = Vector2(screen.get_size()) self.top_left = self.centre - self.size / 2 self.rect = Rect(self.top_left, self.size) def move_to(self, centre: Vector2): """ Moves the camera to a position in the world. Sets camera's rect and position attributes @param centre: camera's new central position in *world coordinate* @return: None """ self.centre = centre self.top_left = self.centre - self.size / 2 self.rect = Rect(self.top_left, self.size) def clamp(self, area: Rect): """ Bounds the camera view inside a rectangular area @param area: the area where the camera is restricted in @return: None """ self.rect = self.rect.clamp(area) self.centre = Vector2(self.rect.center) self.top_left = Vector2(self.rect.topleft) def from_world(self, world_coords: Vector2) -> Vector2: """ Converts world coordinates to view coordinates based on the camera's position @param world_coords: world coordinates to convert @return: the point in camera coordinate. Can be used to draw on the screen """ return world_coords - self.top_left
class Player(WorldObject): def __init__(self, identifier, nick, color, pos, vel=(0,0)): WorldObject.__init__(self, identifier) self.nick = nick self.color = color self.rect = Rect((0,0), settings.PLAYER_SIZE) self.vx, self.vy = vel self.x, self.y = pos self.rect.center = pos self.local = False dispatcher.connect(self.move, signal=signals.MOVE_PLAYER, sender=self) def move(self, x, y, vx, vy): if vx and vy: vx *= 1 / math.sqrt(2) vy *= 1 / math.sqrt(2) self.x = x self.y = y self.vx = vx * settings.PLAYER_SPEED self.vy = vy * settings.PLAYER_SPEED def update(self, dt): self.x += self.vx * dt self.y += self.vy * dt self.rect.centerx = self.x self.rect.centery = self.y clamp = self.rect.clamp(self.world.bounds) if clamp != self.rect: self.rect = clamp self.x = self.rect.centerx self.y = self.rect.centery
class Player(WorldObject): def __init__(self, identifier, nick, color, pos, vel=(0, 0)): WorldObject.__init__(self, identifier) self.nick = nick self.color = color self.rect = Rect((0, 0), settings.PLAYER_SIZE) self.vx, self.vy = vel self.x, self.y = pos self.rect.center = pos self.local = False dispatcher.connect(self.move, signal=signals.MOVE_PLAYER, sender=self) def move(self, x, y, vx, vy): if vx and vy: vx *= 1 / math.sqrt(2) vy *= 1 / math.sqrt(2) self.x = x self.y = y self.vx = vx * settings.PLAYER_SPEED self.vy = vy * settings.PLAYER_SPEED def update(self, dt): self.x += self.vx * dt self.y += self.vy * dt self.rect.centerx = self.x self.rect.centery = self.y clamp = self.rect.clamp(self.world.bounds) if clamp != self.rect: self.rect = clamp self.x = self.rect.centerx self.y = self.rect.centery