pygame飞机大战用精灵组(sprite)的层(layer)编写(十三)BOSS想看烟火了

还是有点郁闷,写了那么久,居然没有一个点赞的。

就当写日记了。

接上篇……

BOSS很纳闷,说好的,要看到打中的效果,可为什么子弹消失了,英雄却没反应。

好好的打英雄的游戏,变成了英雄吃子弹的游戏了。不行,我要看到效果,要看到烟花四起。

好吧,加点特性不就完了嘛。

现成的类都有了,稍微改改,BOSS马上就可以过节,看漫天的烟火了。

还没想好,是在HeroPlane类里实现呢,还是MainScene类里实现。

泡脚、睡觉。

接着写吧。

烟花不就是一组动态的图片,在子弹击中的位置显示下,然后消失掉吗?

动态图片,不就是用 Animation这个类嘛,位置,不就是增加一个set_pos(self,x,y)函数嘛,消失,不就是超过时间 self.kill()嘛。

from setting import *
from animation import *


class Blast(pygame.sprite.Sprite):
    def __init__(self):
        self._layer = 9
        self.groups = allgroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.animation = Animation()
        self.images = self.animation.load_image('images/blast/blast0.png', 465,
                                                72, 1, 6)
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.start_time = pygame.time.get_ticks()
        self.interval = 300

    def update(self):
        self.image = self.animation.get_current_image()
        current_time = pygame.time.get_ticks()
        last_time = current_time - self.start_time
        if last_time >self.interval:
            self.kill()

    def set_pos(self, x, y):
        self.rect.centerx = x
        self.rect.centery = y

感觉持续时间不够长,可以直接改下self.interval这个值。

在 main.py的碰撞检查函数里添加

        boss_bullet_hero = pygame.sprite.spritecollide(
            self.hero, bossbulletgroup, True, pygame.sprite.collide_mask)
        if boss_bullet_hero:
            for bullet in boss_bullet_hero:
                blast = Blast()
                blast.set_pos(bullet.rect.centerx,bullet.rect.centery)

碰撞检查改为了单个精灵的检查,因为目前英雄还是比较孤独,没有僚机什么的,没必要用groupcollide(),关于精灵组之间的碰撞检查后续再写,或者可以自己查看以前的日志。

https://blog.csdn.net/hailler1119/article/details/88825255

为了体验效果,换了个大个的飞机,可以看到子弹击中效果,也可以看到 加了collide_mask后,碰撞检查更精确了。

猜你喜欢

转载自blog.csdn.net/hailler1119/article/details/88959268