Nav apraksta

alien.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import pygame
  2. from pygame.sprite import Sprite
  3. class Alien(Sprite):
  4. """A class to represent a single alien in the fleet."""
  5. def __init__(self, ai_settings, screen):
  6. """Initialize the alien, and set its starting position."""
  7. super(Alien, self).__init__()
  8. self.screen = screen
  9. self.ai_settings = ai_settings
  10. # Load the alien image, and set its rect attribute.
  11. self.image = pygame.image.load('images/alien.bmp')
  12. self.rect = self.image.get_rect()
  13. # Start each new alien near the top left of the screen.
  14. self.rect.x = self.rect.width
  15. self.rect.y = self.rect.height
  16. # Store the alien's exact position.
  17. self.x = float(self.rect.x)
  18. def check_edges(self):
  19. """Return True if alien is at edge of screen."""
  20. screen_rect = self.screen.get_rect()
  21. if self.rect.right >= screen_rect.right:
  22. return True
  23. elif self.rect.left <= 0:
  24. return True
  25. def update(self):
  26. """Move the alien right or left."""
  27. self.x += (self.ai_settings.alien_speed_factor *
  28. self.ai_settings.fleet_direction)
  29. self.rect.x = self.x
  30. def blitme(self):
  31. """Draw the alien at its current location."""
  32. self.screen.blit(self.image, self.rect)