Nav apraksta

ship.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 update(self):
  20. """Update the ship's position, based on movement flags."""
  21. # Update the ship's center value, not the rect.
  22. if self.moving_right and self.rect.right < self.screen_rect.right:
  23. self.center += self.ai_settings.ship_speed_factor
  24. if self.moving_left and self.rect.left > 0:
  25. self.center -= self.ai_settings.ship_speed_factor
  26. # Update rect object from self.center.
  27. self.rect.centerx = self.center
  28. def blitme(self):
  29. """Draw the ship at its current location."""
  30. self.screen.blit(self.image, self.rect)