import pygame


class Ship():
    def __init__(self, ai_settings, screen):
        """It initializes the ship and sets its initial position."""

        self.screen = screen
        self.ai_settings = ai_settings

        # Download image of the ship and receiving a rectangle.
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Each new ship appears at the bottom edge of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Saving the real coordinates of the center of the ship.
        self.center = float(self.rect.centerx)

        # Moving flag
        self.moving_right = False
        self.moving_left = False

    def update(self):
        """Updates the ship's position in view of the flag."""

        # For moving right
        if self.moving_right:
            # Ship do not leave screen
            if self.moving_right and self.rect.right < self.screen_rect.right:
                # Change shop position
                self.center += self.ai_settings.ship_speed_factor

        # For moving left
        if self.moving_left:
            # Ship do not leave screen
            if self.moving_left and self.rect.left > 0:
                # Change shop position
                self.center -= self.ai_settings.ship_speed_factor

        # Update rect attribute based on self.center.
        self.rect.centerx = self.center

    def blitme(self):
        """Draws a ship at the current position."""

        self.screen.blit(self.image, self.rect)