You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.2 KiB

# 开发日期: 2024/4/26
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""一个对飞船发射的子弹进行管理的类"""
def __init__(self, ai_settings, screen, ship):
"""在飞船所处的位置创建一个子弹对象"""
super(Bullet, self).__init__()
self.screen = screen
# 加载子弹图像,并获取其外接矩形
self.image = pygame.image.load('images/bullet.png')
self.sound = pygame.mixer.Sound('bgm/bullet.wav')
self.rect = self.image.get_rect()
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
# 存储用小数表示的子弹位置
self.y = float(self.rect.y)
"""设置子弹的伤害"""
self.damage = ai_settings.bullet_damage
# 从游戏设置对象中获取子弹的速度
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
"""向上移动子弹"""
# 更新表示子弹位置的小数值
self.y -= self.speed_factor
# 更新表示子弹的rect的位置
self.rect.y = self.y
def draw_bullet(self):
"""在屏幕上绘制子弹"""
self.screen.blit(self.image, self.rect)