def getOutOfBounds(self, border=10): """Gets a Struct containing Rects that are just outside the camera boundaries.""" bounds = self.getBounds() oobleft = Game.Rect(bounds.left-border, bounds.top-border,\ border,bounds.height+border*2) oobright = Game.Rect(bounds.right, bounds.top-border,\ border,bounds.height+border*2) oobtop = Game.Rect(bounds.left-border, bounds.top-border,\ bounds.width+border*2, border) oobbottom = Game.Rect(bounds.left-border, bounds.bottom,\ bounds.width+border*2, border) l = [r.move(-Game.scroll.x, -Game.scroll.y) for r in \ oobleft, oobright, oobtop, oobbottom] return Struct(left=l[0], right=l[1], top=l[2], bottom=l[3])
def __init__(self, width=640, height=480, fps=60, scale=1): super(World, self).__init__(width, height, fps, scale) # camera position (this is a basis position for all drawing) self.camera = point.Point(0, 0) Game.scroll = Game.camera = self.camera # which object is the focus of the camera self.focus = None # how far away the camera (center of the screen) should be from the focus self._focusLead = None # how fast the camera should "catch up" to the focus self._followSpeed = 1.0 # the boundaries of camera movement self._followMin, self._followMax = None, None # SDL uses 16-bit coordinates for drawing, so these are the maximum bounds # Range of a 16-bit integer = -32,678 to +32,767 (or -2**15 to 2**15 - 1) self._bounds = Game.Rect(-(1 << 15), (1 << 15) - 1, (1 << 16) - 1, (1 << 16) - 1) # the world's stages (game states) self._stages = [] # the currently active stage, or None if no stage is active, # or stages are not being used self._activeStage = None
def update(self): """Update the console.""" if self.textbox.dirty > 0: rect = Game.Rect(0,max(0,self.textbox.height - self.height), self.width,min(self.rect.height,self.textbox.rect.height)) self.pixels = self.textbox.pixels.subsurface(rect) self.redraw() self.textbox.dirty = 0
def __init__(self, items, depth=8, bounds=None): # the four sub trees self.nw = self.ne = self.se = self.sw = None # change a Group to a list # this can be useful when using World.getEntities() if isinstance(items, Game.Sprite.Group): items = items.sprites() depth -= 1 if depth == 0 or not items: self.items = items return if bounds: bounds = Game.Rect(bounds) else: bounds = items[0].rect bounds = bounds.unionall([i.rect for i in items[1:]]) cx = self.cx = bounds.centerx cy = self.cy = bounds.centery self.items = [] nw_items = [] ne_items = [] se_items = [] sw_items = [] for item in items: # Which of the sub-quadrants does the item overlap? in_nw = item.rect.left <= cx and item.rect.top <= cy in_sw = item.rect.left <= cx and item.rect.bottom >= cy in_ne = item.rect.right >= cx and item.rect.top <= cy in_se = item.rect.right >= cx and item.rect.bottom >= cy # If it overlaps all 4 quadrants then insert it at the current # depth, otherwise append it to a list to be inserted under every # quadrant that it overlaps. if in_nw and in_ne and in_se and in_sw: self.items.append(item) else: if in_nw: nw_items.append(item) if in_ne: ne_items.append(item) if in_se: se_items.append(item) if in_sw: sw_items.append(item) # Create the sub-quadrants, recursively. if nw_items: self.nw = QuadTree(nw_items, depth, (bounds.left, bounds.top, cx, cy)) if ne_items: self.ne = QuadTree(ne_items, depth, (cx, bounds.top, bounds.right, cy)) if se_items: self.se = QuadTree(se_items, depth, (cx, cy, bounds.right, bounds.bottom)) if sw_items: self.sw = QuadTree(sw_items, depth, (bounds.left, cy, cx, bounds.bottom))
def followBounds(self, followMin=None, followMax=None): """Sets the range in which the camera is allowed to move. If both parameters, C{followMin} and C{followMax}, are None, then the game world's extents are set to be the same as the screen. @param followMin: The top-left corner of the game world. @param followMax: The bottom-right corner of the game world. """ if followMin is None: followMin = point.Vector(0, 0) if followMax is None: followMax = point.Vector(self.width, self.height) # The minimum follow value is the top-left corner of the game world, # and the maximum is the bottom-right. We can just use followMin as is, # but we have to adjust followMax to take into account the size of the # screen. We do this _after_ setting the world boundaries, though, # because it saves typing and it might be slightly faster. self._followMin = point.Vector(followMin) self._followMax = point.Vector(followMax) self._bounds = Game.Rect(followMin, self._followMax - self._followMin) self._followMax -= (self.width, self.height) self._doCameraFollow()
def getScreenRect(self): """Returns a Rect object containing the bounds of the screen.""" if self.screen is not None: return self.screen.get_rect() else: return Game.Rect(0, 0, self.width, self.height)