Nenhuma Descrição

ship.py 1.6KB

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