Ejemplo n.º 1
0
class PlaneSprite(pygame.sprite.Sprite):
    """moves a clenched fist on the screen, following the mouse"""
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('plane_low.png', -1)
        self.plane = Plane(randint(0, DISPLAY_WIDTH), randint(0, DISPLAY_HEIGHT), randint(MIN_ALTITUDE, MAX_ALTITUDE))
        # self.plane.setCourse(randint(496, 500), 400, 600, SPEED)
        self.plane.setCourse(randint(0, DISPLAY_WIDTH), randint(0, DISPLAY_HEIGHT), randint(MIN_ALTITUDE, MAX_ALTITUDE), SPEED)

        self.originalImage = self.image


    def update(self):
        self.image = self.originalImage
        self.rect = self.image.get_rect()

        # Setting the position and saving it before transformations
        self.plane.flyAway()
        self.rect.center = self.plane.int2Dpos()
        center = self.rect.center

        # Scale down the icon
        scale = PLANE_SIZE / self.rect.width

        # Calculate heading and rotate the icon accordingly
        if self.plane.velocity.x == 0:
            heading = 0
        else:
            heading = math.degrees(math.atan(-self.plane.velocity.y/self.plane.velocity.x))
        if self.plane.velocity.x < 0:
            heading += 180
        self.image = pygame.transform.rotozoom(self.image, heading, scale)

        # Restoring the position after transformations
        self.rect = self.image.get_rect()
        self.rect.center = center

        white = pygame.Surface(self.rect.size)
        white.fill(Color('white'))
        self.image.blit(white, (0, 0), None, BLEND_MAX)

        # Change the plane's color according to its height
        heightToColor = int(((self.plane.position.z - MIN_ALTITUDE) / (MAX_ALTITUDE - MIN_ALTITUDE)) * 255)
        if heightToColor > 255:
            heightToColor = 255
        if heightToColor < 0:
            heightToColor = 0
        heightColor = pygame.Surface(self.rect.size)
        heightColor.fill((heightToColor, heightToColor, heightToColor))
        self.image.blit(heightColor, (0, 0), None, BLEND_MIN)