diff --git a/settings.py b/settings.py index 38548aa..8b40277 100644 --- a/settings.py +++ b/settings.py @@ -7,3 +7,6 @@ def __init__(self): self.screen_width = 1200 self.screen_height = 800 self.bg_color = (230, 230, 230) + + # Ship settings + self.ship_speed = 5 \ No newline at end of file diff --git a/ship.py b/ship.py index 4e905e5..f32fc8a 100644 --- a/ship.py +++ b/ship.py @@ -7,6 +7,7 @@ def __init__(self, ai_game): """Initialize the ship and set its starting position.""" self.screen = ai_game.screen self.screen_rect = ai_game.screen.get_rect() + self.settings = ai_game.settings # Load the ship image and get its rect. self.image = pygame.image.load('images/ship.bmp') @@ -15,16 +16,23 @@ def __init__(self, ai_game): # Start each new ship at the bottom center of the screen. self.rect.midbottom = self.screen_rect.midbottom - # Movement flag; start with a ship that's not moving. + # Store a float for the ship's exact horizontal position. + self.x = float(self.rect.x) + + # Movement flags; start with a ship that's not moving. self.moving_right = False self.moving_left = False def update(self): """Update the ship's position based on the movement flag.""" - if self.moving_right: - self.rect.x += 1 - if self.moving_left: - self.rect.x -= 1 + # Update the ship's x value, not the rect. + if self.moving_right and self.rect.right < self.screen_rect.right: + self.x += self.settings.ship_speed + if self.moving_left and self.rect.left > 0: + self.x -= self.settings.ship_speed + + # Update rect object from self.x. + self.rect.x = self.x def blitme(self):