暂无描述

bullet.py 1.1KB

12345678910111213141516171819202122232425262728293031323334
  1. import pygame
  2. from pygame.sprite import Sprite
  3. class Bullet(Sprite):
  4. """A class to manage bullets fired from the ship."""
  5. def __init__(self, ai_settings, screen, ship):
  6. """Create a bullet object, at the ship's current position."""
  7. super(Bullet, self).__init__()
  8. self.screen = screen
  9. # Create bullet rect at (0, 0), then set correct position.
  10. self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
  11. ai_settings.bullet_height)
  12. self.rect.centerx = ship.rect.centerx
  13. self.rect.top = ship.rect.top
  14. # Store a decimal value for the bullet's position.
  15. self.y = float(self.rect.y)
  16. self.color = ai_settings.bullet_color
  17. self.speed_factor = ai_settings.bullet_speed_factor
  18. def update(self):
  19. """Move the bullet up the screen."""
  20. # Update the decimal position of the bullet.
  21. self.y -= self.speed_factor
  22. # Update the rect position.
  23. self.rect.y = self.y
  24. def draw_bullet(self):
  25. """Draw the bullet to the screen."""
  26. pygame.draw.rect(self.screen, self.color, self.rect)