上一篇講解了背景和音效的操作居砖,本篇繼續(xù)添加游戲暫停節(jié)目、游戲信息展示驴娃、游戲開(kāi)始確認(rèn)畫(huà)面奏候,以及敵機(jī)刷新控制。
添加游戲暫停
#暫停游戲 exp9添加
def Paused(screen):
pygame.image.save(screen, Image_Pausedbg)
#保存當(dāng)前游戲圖像作為背景
pygame.mixer.music.pause()
#暫停背景音樂(lè)
PlaySound(Sound_Pause)
isPause = True
pausedBg = BackGround(screen,Image_Pausedbg,"") #創(chuàng)建相關(guān)按鈕
cbContinue = Button(screen, Image_ButtonResume, Image_ButtonResume, (210, 360),"dislocation")
cbRestart = Button(screen, Image_ButtonRestart, Image_ButtonRestart, (180, 410),"dislocation")
cbExit = Button(screen, Image_ButtonQuit, Image_ButtonQuit, (180, 460),"dislocation")
while isPause:
pausedBg.display()
cbContinue.display()
cbRestart.display()
cbExit.display()
pygame.display.update()
for event in pygame.event.get():
# 對(duì)不同按鈕進(jìn)行相應(yīng)
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and cbContinue.isOver():
PlaySound(Sound_ButtonOnClicked)
return 'continue'
elif event.type == pygame.MOUSEBUTTONDOWN and cbRestart.isOver():
PlaySound(Sound_ButtonOnClicked)
return 'restart'
elif event.type == pygame.MOUSEBUTTONDOWN and cbExit.isOver():
PlaySound(Sound_ButtonOnClicked)
return 'exit'
elif event.type == KEYDOWN :
if event.key == K_ESCAPE:
return 'continue'
在MainControl
函數(shù)中調(diào)用 Paused
函數(shù)就可以加載暫停界面了唇敞,運(yùn)行效果如圖:
Paused
函數(shù)把用戶對(duì)按鈕的操作蔗草,返回給MainControl
函數(shù),MainControl
函數(shù)在返回給Main
函數(shù)
#exp9 添加對(duì)暫停界面操作的相應(yīng)
getReturn = MainControl(hero, screen, cbSound, cbMusic)
if getReturn:
if getReturn == 'restart':
break
elif getReturn == 'exit':
exit()
else:
pygame.mixer.music.unpause()
返回的是 'restart' 疆柔,即重新開(kāi)始游戲咒精,程序是直接跳出出循環(huán),那么程序也就停止了旷档,所以要有重啟的效果模叙,要把程序入口改成循環(huán)。也就是調(diào)出循環(huán)鞋屈,再次調(diào)用Main函數(shù)開(kāi)始游戲范咨。
if __name__ == '__main__' :
while True: #exp 9 添加循環(huán),重新開(kāi)始游戲才會(huì)起作用
Main()
展示游戲信息
添加玩家生命數(shù)厂庇、當(dāng)前得分渠啊、飛行里程數(shù)等等信息
添加玩家生命數(shù)展示類
# exp9 添加類 展示玩家生命數(shù)
class ShowHealth(object):
def __init__(self,screen_temp,hero):
self.screen = screen_temp
self.hero = hero
self.image = pygame.image.load(Image_ShowHealth)
self.w, self.h = self.image.get_size()
def display(self):
for i in range(self.hero.live):
self.screen.blit(self.image, (12 + i * 13, 830))
得分、飛行里程數(shù)數(shù)使用 ShowText
類來(lái)創(chuàng)建 (在Main
函數(shù)中創(chuàng)建)权旷。
# ex9 Main 函數(shù) 添加 展示游戲信息
showScores = ShowText(screen,str("%010d" % scores),Font_Icmblk,(80,80,80),(420, 20))
showFlyMileage = ShowText(screen,str("%010d" % flyMileage) + "Km",Font_Icmblk,(80,80,80),(70, 20))
showHealth = ShowHealth(screen,hero)
在Main
函數(shù)循環(huán)部分昭抒,刷新展示的信息,在函數(shù)開(kāi)始部分添加 變量flyMileage
# 刷新戰(zhàn)斗信息顯示 ex9 添加
flyMileage += 1 #刷新飛行里程數(shù)
showScores.text = str("%010d" % scores)
showScores.display()
showFlyMileage.text = str("%010d" % flyMileage) + "Km"
showFlyMileage.display()
showHealth.display()
pygame.display.update()
這樣就完成了游戲信息的展示
程序開(kāi)始直接進(jìn)入游戲炼杖,會(huì)顯得突兀灭返,所以我們添加一個(gè)游戲開(kāi)始確認(rèn)界面。
# 顯示LOGO及開(kāi)始游戲按鈕 游戲開(kāi)始確認(rèn)界面 ex9 添加
cbStart = Button(screen, Image_ButtonStart2, Image_ButtonStart, (233, 400), "replace")
cbLogo = Button(screen, Image_Logo, Image_Logo, (24, 220), "")
showAuthor = ShowText(screen, "Codes By xiaox 2019", Font_Icmblk, (80, 80, 80), (248, 480))
showTel = ShowText(screen, "Tel:18627472125", Font_Icmblk, (80, 80, 80), (250, 510))
pause = True
while pause:
mainBackGround.display()
cbStart.display()
cbLogo.display()
showAuthor.display()
showTel.display()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and cbStart.isOver():
PlaySound(Sound_ButtonOnClicked)
pause = False
del (cbStart)
del (cbLogo)
程序邏輯比較簡(jiǎn)單坤邪,就是在Main
函數(shù)中主循環(huán)前面加一個(gè)循環(huán)熙含,展示一些LOGO信息,添加一個(gè)開(kāi)始確認(rèn)按鈕艇纺,跳出這個(gè)循環(huán)怎静,就達(dá)到開(kāi)始確認(rèn)的效果邮弹。
當(dāng)前腳本只是在游戲開(kāi)始,創(chuàng)建了3種不同敵機(jī)蚓聘,作為演示腌乡。
實(shí)際游戲會(huì)按一定規(guī)率或隨機(jī)創(chuàng)建敵機(jī)對(duì)象,實(shí)現(xiàn)敵機(jī)刷新夜牡。具體的實(shí)現(xiàn)方法就比較多与纽,比如使用列表、文件塘装、或者幾個(gè)變量來(lái)保存刷新規(guī)則都可以急迂。這里講解的,使用3個(gè)變量蹦肴,來(lái)刷新敵機(jī)周期性刷新僚碎。
同樣把刷新控制封裝到一個(gè)函數(shù)中
#刷新敵機(jī) exp9-2 添加
def BuildEnemyPlane(flymileage, enemyplanes, pygame_screen, enemybulltes):
buildEnemyTiming = 200
buildBossTiming = 3000
buildRewardTiming = 1000
if flymileage % buildEnemyTiming == 0 :
enemyplanes.append(EnemyPlane(pygame_screen,enemybulltes))
if flymileage % buildBossTiming == 0 :
enemyplanes.append(BossPlane(pygame_screen, enemybulltes))
if flymileage % buildRewardTiming == 0 :
if flymileage // buildRewardTiming % 2 == 0:
enemyplanes.append(RewardPlane(pygame_screen, enemybulltes,"doublebullet"))
else:
enemyplanes.append(RewardPlane(pygame_screen, enemybulltes,'shotbullet'))
BuildEnemyPlane
函數(shù)講解
函數(shù)通過(guò)buildEnemyTiming
buildBossTiming
buildRewardTiming
3個(gè)變量來(lái)保存敵機(jī)刷新的頻率。
參照的是傳入的flymileage
參數(shù),也就是主循環(huán)的次數(shù)阴幌。通過(guò)判斷是否可以整除頻率變量勺阐,來(lái)刷新對(duì)應(yīng)的敵機(jī)。
比如上面的程序就是矛双,每循環(huán)200次皆看,刷新普通敵機(jī)一架,循環(huán)3000次背零,刷新Boss一架,循環(huán)1000次刷新帶補(bǔ)給飛機(jī)一架无埃。
在Main
函數(shù)中刪除之前創(chuàng)建的3架演示敵機(jī)
添加BuildEnemyPlane
函數(shù)調(diào)用徙瓶,來(lái)刷新敵機(jī)
# 敵機(jī)刷新 exp9添加
BuildEnemyPlane(flyMileage, enemyPlanes, screen, enemyBullets)
注意:flymileage
變量初始值如果是0,那么要在調(diào)用函數(shù)之前給它+1嫉称,不然都能被整除侦镇,游戲開(kāi)始會(huì)刷新3架飛機(jī),也可以給它初始值設(shè)置為1
至此游戲開(kāi)發(fā)已經(jīng)完成织阅,下一篇會(huì)對(duì)Main函數(shù)做一個(gè)梳理壳繁,