添加了子弹类
This commit is contained in:
parent
7569f54a58
commit
7ab5f624a5
24
飞机大战.py
24
飞机大战.py
|
@ -66,3 +66,27 @@ class HeroPlane(pygame.sprite.Sprite):
|
|||
def clear_bullets(cls):
|
||||
# 清空子弹
|
||||
cls.bullets.empty()
|
||||
|
||||
|
||||
# 子弹类
|
||||
# 属性 坐标 速度 图片
|
||||
class Bullet(pygame.sprite.Sprite):
|
||||
|
||||
def __init__(self, screen, x, y):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
# 图片
|
||||
self.image = pygame.image.load('./feiji/bullet.png')
|
||||
self.rect = self.image.get_rect() # rect属性:矩形
|
||||
self.rect.topleft = [x + 100 / 2 - 22 / 2, y - 25] # 矩形左上角坐
|
||||
# 窗口
|
||||
self.screen = screen
|
||||
# 速度
|
||||
self.speed = 20
|
||||
|
||||
def update(self):
|
||||
# 修改子弹坐标
|
||||
self.rect.top -= self.speed
|
||||
# 如果子弹移出屏幕上方,则销毁子弹对象
|
||||
if self.rect.top < -22:
|
||||
# 超出界面 干掉自己
|
||||
self.kill()
|
Loading…
Reference in New Issue