Няма описание

ship.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import pygame
  2. class Ship():
  3. def __init__(self, ai_settings, screen):
  4. """Initialize the ship, and set its starting position."""
  5. self.screen = screen
  6. self.ai_settings = ai_settings
  7. # Load the ship image, and get its rect.
  8. self.image = pygame.image.load('images/ship.bmp')
  9. self.rect = self.image.get_rect()
  10. self.screen_rect = screen.get_rect()
  11. # Start each new ship at the bottom center of the screen.
  12. self.rect.centerx = self.screen_rect.centerx
  13. self.rect.bottom = self.screen_rect.bottom
  14. # Store a decimal value for the ship's center.
  15. self.center = float(self.rect.centerx)
  16. # Movement flags.
  17. self.moving_right = False
  18. self.moving_left = False
  19. def center_ship(self):
  20. """Center the ship on the screen."""
  21. self.center = self.screen_rect.centerx
  22. def update(self):
  23. """Update the ship's position, based on movement flags."""
  24. # Update the ship's center value, not the rect.
  25. if self.moving_right and self.rect.right < self.screen_rect.right:
  26. self.center += self.ai_settings.ship_speed_factor
  27. if self.moving_left and self.rect.left > 0:
  28. self.center -= self.ai_settings.ship_speed_factor
  29. # Update rect object from self.center.
  30. self.rect.centerx = self.center
  31. def blitme(self):
  32. """Draw the ship at its current location."""
  33. self.screen.blit(self.image, self.rect)