pygame飞机大战用精灵组(sprite)的层(layer)编写(十四)BOSS问,到底谁是BOSS?(终结篇)

BOSS烟花看多了,有点腻了,看到英雄的飞机依然神采奕奕,很生气,问道“到底谁是BOSS,打了半天,怎么不少血,难道对面的才是BOSS?”,不行,你得少血给我看,还得被我打死”!

这个要求,实现起来也很简单。

Lifebar的加个判断,少于一定血量的时候,改个颜色。

至于打死嘛,不就是一个self.kill()解决了。

当然为了逼真,可以重新self.kil(),加入些 动画特性,音效之类的。

留着下次写。。。。。

先给Lifebar加个判断,生命值小于0.25比例时,改为红色。update添加判断,绘制里加两条代码

def update(self):
            self.percent = self.boss.HP / self.boss.HPFULL * 1.0
            #x血量变化后,重绘
            if self.percent != self.oldpercent:
                self.paint() # bOSS变形后,血条尺寸会变的,需要重新计算尺寸
                #设置底色黑色,加上paint的colorkey,黑色就变透明色了。
                pygame.draw.rect(self.image, (0,0,0), (1,1,self.boss.rect.width-2,10)) 
                #绘制
                if self.percent >0.25:
                    pygame.draw.rect(self.image, (0,255,0), (1,1,int(self.boss.rect.width * self.percent),10)) 
                elif self.percent <= 0.25:
                    pygame.draw.rect(self.image, (255,0,0), (1,1,int(self.boss.rect.width * self.percent),10)) 
            self.oldpercent = self.percent
            self.rect.centerx = self.boss.rect.centerx
            self.rect.centery = self.boss.rect.centery - self.boss.rect.height /2 - 10
            if self.boss.HP < 1:   #check if boss is still alive
                self.kill()

hero.py 里添加update里添加判读,生命值少于1时,自毁。

if self.HP < 1:

self.kill()

main.py里,碰撞检查,打中英雄,英雄的血根据子弹的伤害减少。

def collideEvent(self):
        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)
                self.hero.HP -= bullet.damage
        boss_missle_hero = pygame.sprite.spritecollide(
            self.hero, bossmisslegroup,  True,
            pygame.sprite.collide_mask)
        if boss_missle_hero:
            for missle in boss_missle_hero:
                blast = Blast()
                blast.set_pos(missle.rect.centerx,missle.rect.centery)
                self.hero.HP -= missle.damage
        boss_gmissle_hero = pygame.sprite.spritecollide(
            self.hero, bossGMgroup,  True,
            pygame.sprite.collide_mask)
        if boss_gmissle_hero:
            for gmissle in boss_gmissle_hero:
                blast = Blast()
                blast.set_pos(gmissle.rect.centerx,gmissle.rect.centery)
                self.hero.HP -= gmissle.damage

还有个BUG,当英雄死亡后,导弹没有目标了,需要重新给他们设定一个默认的位置,当然是飞到屏幕外面去了。

在boss.py的fire_missle和fire_guided_missle函数里,加入判断

        if herogroup:
            gmissle.set_target_coordinate(hero_pos)
        else:
            gmissle.set_target_coordinate(vect(SCENEWIDTH//2,SCENEHEIGHT+400))


        if herogroup:
             missle.set_target_coordinate(hero_pos)
        else:
             missle.set_target_coordinate(vect(SCENEWIDTH//2,SCENEHEIGHT+400))

可怜的英雄,一出生,就被BOSS蹂躏,到死都没发出一颗子弹,游戏就结束了。

与其说是飞机大战,不如改个名字,叫《是英雄,就挺过100秒》

可以自行增加英雄死亡时的动态图片,可以加入音乐配合气氛。我就不写了

BOSS的故事讲完了。。。。后续要不要写个英雄的故事系列??

既然都写了是BOSS系列,那就给boss点奖励吧。显示BOSS打英雄的信息。

新建一个message.py

from setting import *


class GameMessage(pygame.sprite.Sprite):
    """ display a text"""

    def __init__(self, msg):
        self.groups = allgroup, textgroup
        self._layer = 99
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.start_time = pygame.time.get_ticks()
        self.newmsg(msg)

    def write(self, msg="pygame is cool"):
        """write text into pygame surfaces"""
        myfont = pygame.font.SysFont("None", 32)
        mytext = myfont.render(msg, True, (0, 255, 0))
        mytext = mytext.convert_alpha()
        return mytext

    def update(self):
        current_time = pygame.time.get_ticks()
        last_time = (current_time - self.start_time)//1000
        for hero in herogroup:
            score = 100 - hero.HP
            self.newmsg('boss score:'+ str(score)+'   time passed: ' + str(last_time)+" second")
            if score  >= 100:
                self.newmsg('boss win!!')

    def newmsg(self, msg="i have nothing to say"):
        self.image = self.write(msg)
        self.rect = self.image.get_rect()
        self.rect.center = (SCENEWIDTH//2, 10)

在main.py里初始化函数里添加

GameMessage('BOSS:score')

运行后的效果如下:

全部代码及资源见链接

https://gitee.com/hailler/boss

猜你喜欢

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