示例#1
0
    def __init__(self, verts, direction, lin_speed=0.0, rot_speed=0.0,
                 pos=Vector(0, 0), rot=0.0, scale=1.0):
        """Creates a new Entity

        Args:
            verts: the vertices defining the shape of the entity
            direction: the direction of the entity
            lin_speed: the linear speed of the entity
            rot_speed: the rotational speed of the entity
            pos: the position of the entity
            rot: the rotation of the entity (in degrees)
            scale: the scale of the entity
        Returns:
            a new Entity

        """
        self._shape = Shape(verts, pos, rot, scale)
        self.pos = pos
        self.rot = rot
        self.scale = scale
        self.lin_speed = lin_speed
        self.rot_speed = rot_speed
        self._direction = direction.normalize()
示例#2
0
class Entity(object):
    """Base class for defining an entity in the
       game (such as a ship, asteroid, etc.)

       The Entity class is a base class for all the objects in
       the game, including the player, the asteroids, and anything
       else. It is defined graphically by a shape. The class also
       stores the current position, rotation, and scale of the objects
       which are used when it is drawn to the screen, as well as a
       direction vector which defines where the entity is pointing.

       Attributes:
            _shape: the shape graphically defining the entity
            pos: the position of the entity
            rot: the rotation of the entity (in degrees)
            scale: the scale of the entity
            lin_speed: the linear speed of the entity
            rot_speed: the rotational speed of the entity
            _direction: the direction of the entity (used for movement)

    """
    def __init__(self, verts, direction, lin_speed=0.0, rot_speed=0.0,
                 pos=Vector(0, 0), rot=0.0, scale=1.0):
        """Creates a new Entity

        Args:
            verts: the vertices defining the shape of the entity
            direction: the direction of the entity
            lin_speed: the linear speed of the entity
            rot_speed: the rotational speed of the entity
            pos: the position of the entity
            rot: the rotation of the entity (in degrees)
            scale: the scale of the entity
        Returns:
            a new Entity

        """
        self._shape = Shape(verts, pos, rot, scale)
        self.pos = pos
        self.rot = rot
        self.scale = scale
        self.lin_speed = lin_speed
        self.rot_speed = rot_speed
        self._direction = direction.normalize()

    def update(self, dt):
        """Updates the entity's shape

        Args:
            dt: the amount of time since the last update
            window: the game window
        """
        # Move and rotate the entity
        self.pos += self._direction * self.lin_speed
        self.rot += self.rot_speed
        self.rot = wrap_angle(self.rot)

        # Set new values
        self._shape.update(self.pos, self.rot, self.scale)

        # Reflect across the screen, if necessary
        self._reflect_across_screen()

    def draw(self):
        """Draws the entity onto the screen

        """
        # Defer this to the shape
        self._shape.draw()

    def collides(self, other):
        """Checks whether this entity collides with another one

        Args:
            other: the other entity to test collision with
        Returns:
            True if the entities collide, False if they do not
        """
        return self._shape.collides(other._shape)

    def _reflect_across_screen(self):
        """Checks if the entity needs to be reflected across the
        screen and generates a new position if it does
        """
        # Start with the current position
        new_pos = Vector(self.pos.x, self.pos.y)

        # For each side of the screen, check if the entity has
        # exceeded the bounds using the approximate length

        # Check horizontal
        # Left-side
        if (self.pos.x + self._shape.effective_length < 0):
            dist = abs(0 - (self.pos.x + self._shape.effective_length))
            new_pos.x = WINDOW_WIDTH + dist
        # Right-side
        elif (self.pos.x - self._shape.effective_length > WINDOW_WIDTH):
            dist = abs((self.pos.x - self._shape.effective_length) -
                       WINDOW_WIDTH)
            new_pos.x = -dist

        # Check vertical
        # Bottom
        if (self.pos.y + self._shape.effective_length < 0):
            dist = abs(0 - (self.pos.y + self._shape.effective_length))
            new_pos.y = WINDOW_HEIGHT + dist
        # Top
        elif (self.pos.y - self._shape.effective_length > WINDOW_HEIGHT):
            dist = abs((self.pos.y - self._shape.effective_length) -
                       WINDOW_HEIGHT)
            new_pos.y = -dist

        # Set the new position of the entity.
        # If no reflection was needed, it's just the original position
        self.pos = new_pos

    # Direction stored as a property to ensure normalization
    @property
    def direction(self):
        return self._direction

    @direction.setter
    def direction(self, value):
        self._direction = value.normalize()
示例#3
0
文件: circle.py 项目: tanxuezhi/Aegis
 def __init__(self, size, datum=Datum()):
     Shape.__init__(self, size, datum)
     self.shape = 'circle'
     self.radius = self.radius()