Skip to content

Commit

Permalink
speed adjustment p244
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkfi committed Feb 6, 2024
1 parent aa00682 commit a171763
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 13 additions & 5 deletions ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
Expand Down

0 comments on commit a171763

Please sign in to comment.