import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
def init(self, ai_settings, screen, ship):
super().init()
self.screen = screen
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
ai_settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
self.y = float(self.rect.y)
# self.ai_settings = ai_settings # 这一行和下一行可以不写,也可以写。这是为什么呢?
# self.ship = ship # 为什么在子类重写__init__()方法,可以不初始化这两个参数呢?(ai_settings, ship)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
self.y = self.y - self.speed_factor
self.rect.y = self.y
def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect)