def __init__(self, *args, **kwargs): ## super(Entity, self).__init__(x,y,w,h) super(Entity, self).__init__(*args, **kwargs) # The maximum velocity of this object (as a vector) in pixels/second self.maxVelocity = None # The maximum angular speed of this object in degrees/second self.maxAngularVelocity = None # motion properties # (Be careful setting these directly, since they're used as if they # were Vector objects!) self._velocity = point.Vector(0,0) self._accel = point.Vector(0,0) self._drag = point.Vector(0,0) # We define angular velocity as a scalar, not a vector, but we still # need a getter and setter, so that we can clamp to maxAngularVelocity. self._angular = 0.0 # The angular acceleration of this object in degrees/second^2 # (we don't really need a property for this, since it's a scalar) self.angularAcceleration = 0.0 # rotating sprites use more CPU, so leave this cleared if this sprite # isn't rotating, leaving more time for other rendering self.rotating = False # does this sprite move? self.fixed = False
def _set_velocity(self, val): if self.maxVelocity: _vx, _vy = val if abs(self.maxVelocity.x) < abs(_vx): _vx = self.maxVelocity.x * util.sign(_vx) if abs(self.maxVelocity.y) < abs(_vy): _vy = self.maxVelocity.y * util.sign(_vy) self._velocity = point.Vector(_vx, _vy) else: self._velocity = point.Vector(val) self.redraw()
def vectorFromAngle(theta): """Returns a vector pointing away from the origin at a specific angle (in degrees). This is essentially the same as converting the polar coordinates (1,theta) to rectangular. @param theta: The angle in degrees. """ rad = math.radians(theta) # polar-to-rectangular is (r cos \theta, r sin \theta) # pygame's y-coordinate grows downward, hence the minus sign return point.Vector(math.cos(rad), -math.sin(rad))
def effect(self): """Update the effect by moving sprites around. Non-scrolling sprites (such as overlays) are unaffected, since it is assumed that those sprites are meant to be stationary. """ # the "quake" effect is simply moving the camera by a random amount # each frame, the intensity value is just how big the random numbers # are relative to the screen size rx = random.uniform(-self.intensity * Game.width, self.intensity * Game.width) ry = random.uniform(-self.intensity * Game.height, self.intensity * Game.height) Game.scroll = self.__origscroll + point.Vector(rx, ry)
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 _doCameraFollow(self): """Helper function to move the camera to follow an object.""" if self.focus is not None: _target = point.Vector(self.focus.position - self.getScreenCenter()) if self._focusLead: _target += self.velocity * self._focusLead Game.scroll += (_target - Game.scroll ) * self._followSpeed * Game.elapsed / 1000.0 # _followMin/_followMax are the camera boundaries if self._followMin is not None: if Game.scroll.x < self._followMin.x: Game.scroll.x = self._followMin.x if Game.scroll.y < self._followMin.y: Game.scroll.y = self._followMin.y if self._followMax is not None: if Game.scroll.x > self._followMax.x: Game.scroll.x = self._followMax.x if Game.scroll.y > self._followMax.y: Game.scroll.y = self._followMax.y
def __get_size(self): return point.Vector(self._w, self._h)
def _get_pos(self): return point.Vector(self.x, self.y)
def _set_drag(self, val): self._drag = point.Vector(val)
def _set_accel(self, val): self._accel = point.Vector(val) self.redraw()