pygame飞机大战用精灵组层编写英雄系列(八)英雄的终极技能

最近下载了全民飞机大战的素材库,里面有很多动态的图,又玩了下这个游戏,里面打飞机都有大绝技,觉得我们的英雄也得有个大绝技。

利用手上资源,做了下。

新建一个heroskill.py。和supply的实现原理差不多,把层改为heroskillgroup,用于碰撞检查

代码如下:

from setting import *
from animation import *

class HeroSkill(pygame.sprite.Sprite):
    def __init__(self,index=0):
        self._layer = 8
        self.groups = allgroup,heroskillgroup
        pygame.sprite.Sprite.__init__(self,self.groups)
        self.animation = Animation()
        if index == 0:
            self.images = self.animation.load_image('images/bomb1.png',512,256,2,4)
        elif index == 1:
            self.images = self.animation.load_image('images/bomb.png',1024,1024,3,4)
        elif index ==2:
            self.images = self.animation.load_image('images/whirlwind.png',1024,1024,3,3)
        else:
            self.images = self.animation.load_image('images/dasheng002.png',1024,1024,4,3)

        #必不可少的两条代码
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
        #初设速度、角度
        self.x_speed = 2.00
        self.y_speed = 2.00
        self.speed = 2
        self.duration = 20000
        self.motion_type = 0
        self.start_time = pygame.time.get_ticks()
        # self.clock.get_timer()
     

    #会自动调用
    def update(self):
        current_time = pygame.time.get_ticks()
        if current_time - self.start_time > self.duration:
            # pass
            self.kill()
        self.image = self.animation.get_current_image()
        if random.randint(0, 100) == 1:
            self.x_speed = random.randint(-2, 2)
            self.y_speed = random.randint(-2, 2)
        self.image = self.animation.get_current_image()
        # self.rect = self.image.get_rect()
  
        #镜面反射移动,供给出现的位置可能为负数或者超过屏幕边界,因此要留有判断的余量,
        #不能直接 self.rect.x < 0
        if self.rect.x > SCENEWIDTH-self.rect.width  or self.rect.x < -50:
            self.x_speed = -self.x_speed
            # print(self.rect.x,self.rect.y)
        if self.rect.y  > SCENEHEIGHT-self.rect.height  or self.rect.y < -50:
            self.y_speed = -self.y_speed
        self.rect.x += self.x_speed
        self.rect.y += self.y_speed
        

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

在main.py里调用下

        h = HeroSkill(0)
        h.set_pos(SCENEWIDTH//2,SCENEHEIGHT//2)
        for v in range(0,4):
            a = HeroSkill(v)
            a.set_pos(random.randint(0,SCENEWIDTH-340),random.randint(0,SCENEHEIGHT-340)

这是测试用的代码

https://gitee.com/hailler/hero_skill_test/tree/master

要实现英雄的技能效果,只要在碰撞检查中加入 精灵组的碰撞检查就好。

skill_shot= pygame.sprite.groupcollide(heroskillgroup,enemygroup,False,True)

大招一出,天下任我行。

这里没有对BOSS进行碰撞检查,BOSS不是一碰就死的,所以应用

skill_shot= pygame.sprite.groupcollide(heroskillgroup,enemygroup,False,False)

然后给BOSS减血量就行。

英雄的系列写的也差不多了。

猜你喜欢

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