天天看點

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後,碰撞檢查更精确了。

pygame飛機大戰用精靈組(sprite)的層(layer)編寫(十三)BOSS想看煙火了

繼續閱讀