This commit is contained in:
parent
53e000a54a
commit
fd4b21232a
|
@ -0,0 +1,33 @@
|
||||||
|
import pygame
|
||||||
|
from pygame.sprite import Sprite
|
||||||
|
|
||||||
|
class Bullet(Sprite):
|
||||||
|
"""一个对飞船发射子弹进行管理的类"""
|
||||||
|
|
||||||
|
def __init__(self, ai_seetings, screen, ship):
|
||||||
|
"""在飞船所处的位置创建一个子弹对象"""
|
||||||
|
super().__init__()
|
||||||
|
self.screen = screen
|
||||||
|
|
||||||
|
# 在(0,0)处创建一个表示子弹的矩形,并将其设置为正确的位置
|
||||||
|
self.rect = pygame.Rect(0, 0, ai_seetings.bullet_width,
|
||||||
|
ai_seetings.bullet_height)
|
||||||
|
self.rect.centerx = ship.rect.centerx
|
||||||
|
self.rect.top = ship.rect.top
|
||||||
|
|
||||||
|
# 存储用小数表示的子弹位置
|
||||||
|
self.y = float(self.rect.y)
|
||||||
|
|
||||||
|
self.color = ai_seetings.bullet_color
|
||||||
|
self.speed_factor = ai_seetings.bullet_speed_factor
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""向上移动子弹"""
|
||||||
|
# 更新表示子弹位置的小数值
|
||||||
|
self.y -= self.speed_factor
|
||||||
|
# 更新表示子弹的rect的位置
|
||||||
|
self.rect.y = self.y
|
||||||
|
|
||||||
|
def draw_bullet(self):
|
||||||
|
"""在屏幕上绘制子弹"""
|
||||||
|
pygame.draw.rect(self.screen, self.color, self.rect)
|
Loading…
Reference in New Issue