pygame飞机大战用精灵组(sprite)的层(layer)编写(七)BOSS想要射的好看些,要动起来,动起来

BOSS治好病后,感觉自己有强大起来,不满足发射这普通的子弹了,它要玩花样,它有的是钱,对吧。

子弹的动态,在cocos2d的python版本里,可以用它的离子效果实现,但在pygame里,只能靠动态图片实现。不过,聊胜于无。

一般这些动态图片会放在一张图里,需要自己切割出来。用到日志里的animation这个类。

烧饭去了,先占个位置。。。。。。。

既然要有特色,那就从bullet类里继承一个吧。

加载动态类的模块,用动态类的load_image,把图片切割成list。

在update()函数里,把当前的self.image设置成动态图片里的当前图片。

添加一个set_bullet_type的函数,根据参数加载不同的子弹图片。

好简单。

from setting import *
from animation import *
from bullet import *

class BossBullet(Bullet):
    def __init__(self):
        self._layer = 5
        self.groups = allgroup, bossbulletgroup
        Bullet.__init__(self)
        self.animation = Animation()
        self.images = self.animation.load_image(
            "images/bullet/bossbullet/bossbullet0.png", 111, 47, 1, 1)
        self.image = self.images[0]
        self.mask = pygame.mask.from_surface(self.image)

    def update(self):
        Bullet.update(self)
        self.image = self.animation.get_current_image()

    def set_bullet_type(self, type=0):
        """设置子弹的类型
           子弹的图片存在同一个目录里,以 enemybullet 加 序号 加 .png
           type 0~8
        """
        self.image_type = type
        if type >10: type =10
        if type == 0:
            self.images = self.animation.load_image(
                "images/bullet/bossbullet/bossbullet0.png", 111, 47, 1, 1)
        elif type == 1:
            self.images = self.animation.load_image(
                "images/bullet/bossbullet/bossbullet1.png", 64, 111, 1, 2)
        elif type == 2:
            self.images = self.animation.load_image(
                "images/bullet/bossbullet/1-0.png", 180, 65, 1, 3)
        elif type == 3:
            self.images = self.animation.load_image(
                "images/bullet/bossbullet/1-1.png", 255, 81, 1, 3)
        elif type == 4:
            self.images = self.animation.load_image(
                "images/bullet/bossbullet/Thunder.png", 490, 157, 1, 3)
        else:
            self.images = self.animation.load_image(
                "images/bullet/bossbullet/0-" + str(type) + ".png", 72, 36, 1,
                2)

BOSS的子弹类有了,让BOSS炫酷一下。

在boss.py里,把子弹类改成刚出炉的子弹类。

演示下效果。代码如下:

from setting import *
from animation import *
from lifebar import *
from bossbullet import *


class BossPlane(pygame.sprite.Sprite):
    def __init__(self):
        self._layer = 2
        self.groups = allgroup, bossgroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        #设置飞机动态图像
        self.animation = Animation()
        self.images = self.animation.load_images('images/boss/', 0, 10, '.png')
        self.image = self.images[1]
        self.mask = pygame.mask.from_surface(self.image)
        # 飞机矩形
        self.rect = self.image.get_rect()
        self.x_speed = 5
        self.y_speed = 5
        #默认移动方式
        self.motion_type = 1
        # 飞机子弹发射间隔 毫秒
        self.interval = 200
        # 飞机子弹发射当前时间
        self.start_time = pygame.time.get_ticks()

        self.HP = 500
        self.HPFULL = 500
        lifebar = Lifebar(self)

    def update(self):
        # self.HP -= 1
        if self.HP <= 0:
            self.kill()
        #更新飞机当前图像
        if random.randint(0, 50) == 1:
            self.motion_type = random.randint(0, 1)
            self.x_speed = random.randint(-5, 6)
            self.y_speed = random.randint(-6, 5)
        self.image = self.animation.get_current_image()
        if self.motion_type == 0:
            if self.rect.x + self.rect.width > SCENEWIDTH or self.rect.x < 0:
                self.x_speed = -self.x_speed
            # self.rect.y += self.y_speed
            self.rect.x += self.x_speed
        elif self.motion_type == 1:
            if self.rect.x + self.rect.width > SCENEWIDTH or self.rect.x < 0:
                self.x_speed = -self.x_speed
            if self.rect.y + self.rect.height > SCENEHEIGHT or self.rect.y < 0:
                self.y_speed = -self.y_speed
            self.rect.x += self.x_speed
            self.rect.y += self.y_speed
        
        self.fire()
    def fire(self):
        #时间判断,不到发射间隔,不射
        current_time = pygame.time.get_ticks()
        pass_time = current_time - self.start_time
        if pass_time < self.interval:
            return
        self.start_time = current_time

        bullet = BossBullet()
        bullet.set_bullet_type(random.randint(0,10))
        pos_x =  self.rect.midbottom[0]-bullet.rect.width//2
        pos_y = self.rect.midbottom[1]
        # print(pos_x,pos_y)
        bullet.set_pos(pos_x,pos_y)

效果如下:

猜你喜欢

转载自blog.csdn.net/hailler1119/article/details/88931029
今日推荐