pygame飞机大战开发实录4

上一篇已经创建了基类,接下来继承基类创建动态背景、飞机的基础类。

创建背景基类

class BackGround(Base):
    def __init__(self, pygame_screen, image_name,type):
        Base.__init__(self,pygame_screen, (0, 0), image_name)
        self.movedistance = 852 // 200            #动态背景每次移动的距离
        self.type         = type                  #背景类型:"dynamic" 动态,为空或任意字符都为静态
    def display(self):
        if self.type == "dynamic":                #动态背景
            self.y += self.movedistance
            self.y2 = self.y - self.h
            if self.y >= 852:
                self.y = 0
            self.screen.blit(self.image, (self.x, self.y2))
        self.screen.blit(self.image, (self.x, self.y))

type属性:用来区分创建的背景是动态的还是静态,因为游戏暂停背景是静态的。

动态背景是飞机俯瞰地面的图像,我们会让它不停的向下移动,这样看上去就像飞机往上飞。

pygame飞机大战开发实录4_第1张图片
加上创建对象及相关测试脚本

def main():
    screen = pygame.display.set_mode((480, 852), 0, 32)
    # 添加背景
    Bg = BackGround(screen, Image_Background, 'dynamic')
    while True:
        Bg.display()
        pygame.display.update()
        for event in pygame.event.get():
            pass
if __name__ == '__main__' :
    main()

pygame飞机大战开发实录4_第2张图片

创建背景使用了一个变量Image_Background,来指定图片文件

Bg = BackGround(screen, Image_Background, 'dynamic')

Image_Background是已经声明好的变量,在程序的开头部分,集中声明了大量这种类似的变量。
实际开发中,也建议大家这样做,易于阅读,也易于维护。

#静态资源集中声明
Font_Icmblk             = "./res/Interstate Cond Mono - Blk.ttf"
Font_Consola            = "consola.ttf"
#暂停背景图片,系统保存暂停前的最后一个画面作为背景
Image_Pausedbg          = "pausedbg.png"
#背景
Image_Background        = "./res/background.png"
#显示当前剩余Live
Image_ShowHealth        = "./res/health2.png"
Image_Logo              = "./res/name.png"
Image_Sound             = "./res/sound.png"
Image_SoundNor          = "./res/sound_nor.png"
Image_Music             = "./res/music.png"
Image_MusicNor          = "./res/music_nor.png"
#飞机的血量(一格)
Image_HeroHealth        = "./res/health.png"
#玩家飞机正常状态
Image_HeroPlane         = "./res/hero2.png"
#玩家飞机向前飞行
Image_HeroPlaneUp       = "./res/hero1.png"
#玩家飞机子弹
Image_HeroBullet        = "./res/bullet2.png"
#补给包1
Image_Reward1           = "./res/prop_type_0.png"
#补给包2
Image_Reward2           = "./res/prop_type_1.png"
#敌机子弹
Image_EnemyBullet       = "./res/bullet1.png"
#敌机1
Image_EnemyPlane_1      = "./res/enemy0.png"
#Boss1
Image_BossPlane1        = "./res/enemy2.png"
#Boss1 一半血量
Image_BossPlane1Half    = "./res/enemy2_down1.png"
#带奖励的敌机
Image_RewardPlane1      = "./res/enemy1.png"

#飞机爆炸
Sound_Boom              = "./music/enemy1_down.wav"
#开火
Sound_HeroFire          = "./music/bullet.wav"
#敌机1刷新
Sound_EnemyPlane_1      = "./music/enemy2_out.wav"
#飞机中弹
Sound_Hit               = "./music/enemy3_down.wav"
#游戏暂停
Sound_Pause             = "./music/game_achievement.wav"
#按钮点击
Sound_ButtonOnClicked   = "./music/enemy4_down.wav"
#背景音乐
Music_backgroud         = "./music/game_music.mp3"
#继续按钮
Image_ButtonResume      = "./res/resume_nor.png"
Image_ButtonRestart     = "./res/restart_nor.png"
Image_ButtonQuit        = "./res/quit_nor.png"
Image_ButtonStart       = "./res/game_resume_pressed.png"
Image_ButtonStart2      = "./res/game_resume_nor.png"
#全局变量
#是否播放音效
Sound_IsPlay            = True

创建飞机的基类:

class BasePlane(Base):
    def __init__(self, pygame_screen, postion, image_name, bulletCoolDownSpeed,bullets):
        Base.__init__(self, pygame_screen, postion, image_name) 
        self.bullet_list = bullets
        self.bulletCoolDownSpeed = bulletCoolDownSpeed  #子弹冷却时间
        self.bulletCoolDownstate = 0                    #子弹冷却计时
        self.bulletCdOk          = True                 #子弹冷却好
        self.live                = 1                    #飞机有几条命
        self.Health              = 4                    #当前血量
        self.MaxHealth           = 4                    #一条命有几格血
        self.Reward              = ''                   #爆炸后是否有奖励
        self.HealthImage         = pygame.image.load(Image_HeroHealth)#单格血条图片
        # 爆炸效果
        self.hit                 = False  # 被击中
        self.bomb_list           = []     # 爆炸分帧图片组
        self.image_num           = 0      # 显示刷新多少次换一张图片
        self.image_index         = 0      # 当前显示图片的index
  
    def bulletCoolDown(self):
        #子弹冷却判断
        if self.bulletCoolDownstate > 0:
            self.bulletCoolDownstate += 1
            if self.bulletCoolDownstate == self.bulletCoolDownSpeed:
                self.bulletCoolDownstate = 0
                self.bulletCdOk = True
    def boom(self):
        self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
        self.image_num += 1
        if self.image_num == 8:  # 刷新10次切换1张图
            self.image_num = 0
            self.image_index += 1
        if self.image_index > 3:
            self.hit         = False  # 爆炸动画播放完成,再修改标记
            self.image_num   = 1
            self.image_index = 1

    def display(self):
        if self.hit == True:
            if self.live == 0 :   #被击中且live=0,飞机爆炸
                self.boom()
                playsound(Sound_Boom)
            else:
                self.Health -= 1         #live   > 0 ,扣一格血
                if self.Health == 0:     #Health = 0 ,扣一条命
                    self.live -= 1
                    if self.live > 0:    #只有玩家飞机有多条生命,这部分针对玩家飞机飞机,扣一条民之后又10秒无敌时间
                        self.Invincible = True
                        self.Health     = self.MaxHealth
                        self.hit        = False
                else:
                    self.hit = False
        else:
            self.screen.blit(self.image, (self.x, self.y))

bulletCoolDown方法:控制子弹冷却时间,
boom方法:显示飞机爆炸动画
display方法:绘制显示的图像

本篇讲解了背景基类的创建及动态背景原理,另外创建了飞机的基类。下一篇继续讲解玩家飞机类的创建及其控制方法。

你可能感兴趣的:(Python-pygame)