上一篇已經(jīng)創(chuàng)建了基類讨阻,接下來繼承基類創(chuàng)建動(dòng)態(tài)背景偎蘸、飛機(jī)的基礎(chǔ)類料按。
創(chuàng)建背景基類
class BackGround(Base):
def __init__(self, pygame_screen, image_name,type):
Base.__init__(self,pygame_screen, (0, 0), image_name)
self.movedistance = 852 // 200 #動(dòng)態(tài)背景每次移動(dòng)的距離
self.type = type #背景類型:"dynamic" 動(dòng)態(tài)奄侠,為空或任意字符都為靜態(tài)
def display(self):
if self.type == "dynamic": #動(dòng)態(tài)背景
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
屬性:用來區(qū)分創(chuàng)建的背景是動(dòng)態(tài)的還是靜態(tài),因?yàn)橛螒驎和1尘笆庆o態(tài)的载矿。
動(dòng)態(tài)背景是飛機(jī)俯瞰地面的圖像垄潮,我們會(huì)讓它不停的向下移動(dòng),這樣看上去就像飛機(jī)往上飛闷盔。
加上創(chuàng)建對象及相關(guān)測試腳本
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()
創(chuàng)建背景使用了一個(gè)變量Image_Background
逢勾,來指定圖片文件
Bg = BackGround(screen, Image_Background, 'dynamic')
Image_Background
是已經(jīng)聲明好的變量,在程序的開頭部分牡整,集中聲明了大量這種類似的變量。
實(shí)際開發(fā)中溺拱,也建議大家這樣做逃贝,易于閱讀,也易于維護(hù)迫摔。
#靜態(tài)資源集中聲明
Font_Icmblk = "./res/Interstate Cond Mono - Blk.ttf"
Font_Consola = "consola.ttf"
#暫停背景圖片沐扳,系統(tǒng)保存暫停前的最后一個(gè)畫面作為背景
Image_Pausedbg = "pausedbg.png"
#背景
Image_Background = "./res/background.png"
#顯示當(dāng)前剩余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"
#飛機(jī)的血量(一格)
Image_HeroHealth = "./res/health.png"
#玩家飛機(jī)正常狀態(tài)
Image_HeroPlane = "./res/hero2.png"
#玩家飛機(jī)向前飛行
Image_HeroPlaneUp = "./res/hero1.png"
#玩家飛機(jī)子彈
Image_HeroBullet = "./res/bullet2.png"
#補(bǔ)給包1
Image_Reward1 = "./res/prop_type_0.png"
#補(bǔ)給包2
Image_Reward2 = "./res/prop_type_1.png"
#敵機(jī)子彈
Image_EnemyBullet = "./res/bullet1.png"
#敵機(jī)1
Image_EnemyPlane_1 = "./res/enemy0.png"
#Boss1
Image_BossPlane1 = "./res/enemy2.png"
#Boss1 一半血量
Image_BossPlane1Half = "./res/enemy2_down1.png"
#帶獎(jiǎng)勵(lì)的敵機(jī)
Image_RewardPlane1 = "./res/enemy1.png"
#飛機(jī)爆炸
Sound_Boom = "./music/enemy1_down.wav"
#開火
Sound_HeroFire = "./music/bullet.wav"
#敵機(jī)1刷新
Sound_EnemyPlane_1 = "./music/enemy2_out.wav"
#飛機(jī)中彈
Sound_Hit = "./music/enemy3_down.wav"
#游戲暫停
Sound_Pause = "./music/game_achievement.wav"
#按鈕點(diǎn)擊
Sound_ButtonOnClicked = "./music/enemy4_down.wav"
#背景音樂
Music_backgroud = "./music/game_music.mp3"
#繼續(xù)按鈕
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
創(chuàng)建飛機(jī)的基類:
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 #子彈冷卻時(shí)間
self.bulletCoolDownstate = 0 #子彈冷卻計(jì)時(shí)
self.bulletCdOk = True #子彈冷卻好
self.live = 1 #飛機(jī)有幾條命
self.Health = 4 #當(dāng)前血量
self.MaxHealth = 4 #一條命有幾格血
self.Reward = '' #爆炸后是否有獎(jiǎng)勵(lì)
self.HealthImage = pygame.image.load(Image_HeroHealth)#單格血條圖片
# 爆炸效果
self.hit = False # 被擊中
self.bomb_list = [] # 爆炸分幀圖片組
self.image_num = 0 # 顯示刷新多少次換一張圖片
self.image_index = 0 # 當(dāng)前顯示圖片的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 # 爆炸動(dòng)畫播放完成,再修改標(biāo)記
self.image_num = 1
self.image_index = 1
def display(self):
if self.hit == True:
if self.live == 0 : #被擊中且live=0句占,飛機(jī)爆炸
self.boom()
playsound(Sound_Boom)
else:
self.Health -= 1 #live > 0 ,扣一格血
if self.Health == 0: #Health = 0 ,扣一條命
self.live -= 1
if self.live > 0: #只有玩家飛機(jī)有多條生命沪摄,這部分針對玩家飛機(jī)飛機(jī),扣一條民之后又10秒無敵時(shí)間
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
方法:控制子彈冷卻時(shí)間纱烘,
boom
方法:顯示飛機(jī)爆炸動(dòng)畫
display
方法:繪制顯示的圖像
本篇講解了背景基類的創(chuàng)建及動(dòng)態(tài)背景原理杨拐,另外創(chuàng)建了飛機(jī)的基類。下一篇繼續(xù)講解玩家飛機(jī)類的創(chuàng)建及其控制方法凹炸。