python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)
本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
制作初衷
這幾天閑來(lái)沒(méi)事干,就想起來(lái)好長(zhǎng)時(shí)間沒(méi)做過(guò)游戲了,于是就想做一個(gè)游戲練練手,為了起到一個(gè)練習(xí)的目的就使用了自己不是太熟練的python這門語(yǔ)言來(lái)編寫(xiě),代碼都有備注,大家可以直接看代碼,這個(gè)代碼我是在python3.1的環(huán)境下寫(xiě)的,大家需要可以直接下載我這里的資源,圖片和代碼打包到了一起的,因?yàn)槭堑谝淮问褂胮ython做游戲,有什么不足的地方望大佬斧正。
游戲思路
首先我們分析飛機(jī)大戰(zhàn)這個(gè)游戲?qū)儆谝粋€(gè)平面的2d游戲,那這個(gè)游戲的所有都是建立在x,y的基礎(chǔ)上進(jìn)行的運(yùn)算,為了加快我們的開(kāi)發(fā)工作,秉承時(shí)間就是金錢的原則,我們采用的是python 的 pygame第三方庫(kù)來(lái)實(shí)現(xiàn)的。
言歸正傳?。?!
背景
仔細(xì)分析飛機(jī)大戰(zhàn)我們不難發(fā)現(xiàn),這個(gè)游戲的背景實(shí)際上就是一張圖片一直循環(huán)滾動(dòng)所造成的一種視覺(jué)上的效果,那布滿我們一個(gè)場(chǎng)景并且讓他無(wú)縫連接實(shí)際上我們只需要兩張圖片滾動(dòng),然后當(dāng)?shù)诙垐D片滾動(dòng)完,再讓他從第一張圖片開(kāi)始滾動(dòng)如此循環(huán)便可以給用戶視覺(jué)上一種無(wú)縫滾動(dòng)的效果。
飛機(jī)
我們分析飛機(jī)的行為可以發(fā)現(xiàn),不管是我方的飛機(jī)還是敵人的飛機(jī)都是有幾種他們共有的動(dòng)作。
1,運(yùn)動(dòng),敵我雙方都是在一直的運(yùn)動(dòng)。
2,發(fā)射子彈,雙方都是在發(fā)射子彈,但具體的數(shù)量和速度我們可以通過(guò)類的屬性來(lái)進(jìn)行修改。
3,死亡,敵人和我們自身都會(huì)死亡,死亡會(huì)觸發(fā)一個(gè)爆炸的動(dòng)畫(huà),敵人死亡加分,我方死亡游戲結(jié)束。
子彈
為了節(jié)省時(shí)間此處的子彈都只會(huì)走直線,封裝到自己的類方法里面,后期可以修改軌跡。
話不多說(shuō),直接上代碼,每個(gè)重要的代碼都是有注釋。大家可以自己觀看。
main.py
# -*- coding: UTF-8 -*- # animation.py # 導(dǎo)入需要的模塊 import pygame, sys from pygame.locals import * import airplane import random class Game: ? def __init__(self): ? ? # 初始化pygame ? ? pygame.init() ? ? #初始化字體文件 ? ? pygame.font.init() ? ? #初始化游戲分?jǐn)?shù) ? ? self.mark=0 ? ? # 設(shè)置幀率(屏幕每秒刷新的次數(shù)) ? ? self.FPS = 60 ? ? # 獲得pygame的時(shí)鐘 ? ? self.fpsClock = pygame.time.Clock() ? ? # 設(shè)置窗口大小 ? ? self.screen = pygame.display.set_mode((500, 800), 0, 32) ? ? # 設(shè)置標(biāo)題 ? ? pygame.display.set_caption('飛機(jī)大戰(zhàn)') ? ? # 定義顏色 ? ? self.WHITE = (255, 255, 255) ? ? #用于存放爆炸圖片 ? ? self.boom=[pygame.image.load("boom_1.gif") ? ? ? ,pygame.image.load("boom_2.gif") ? ? ? ,pygame.image.load("boom_3.gif") ? ? ? ,pygame.image.load("boom_4.gif") ? ? ? ,pygame.image.load("boom_5.gif") ? ? ? ,pygame.image.load("boom_6.gif") ? ? ? , pygame.image.load("boom_7.gif") ? ? ? , pygame.image.load("boom_8.gif") ? ? ? , pygame.image.load("boom_9.gif")] ? ? # 加載一張背景圖片 ? ? self.img = pygame.image.load('bg.jpg') ? ? #加載開(kāi)始游戲按鈕 ? ? self.startGameImage=pygame.image.load('start.png') ? ? # 初始化背景圖片的位置 ? ? self.imgx = 0 ? ? self.imgy = 0 ? ? # 全局的時(shí)間變量 ? ? self.time = 0; ? ? #敵人子彈的圖片數(shù)據(jù) ? ? self.allEnemyButtlueImg=[pygame.image.load('buttle3.png')] ? ? # 存放所有敵人圖片數(shù)據(jù)的一個(gè)列表 ? ? self.allEnemyImg = [pygame.image.load('em1.png'), pygame.image.load('em2.png')] ? ? # 存放所有敵人變量的一個(gè)列表 ? ? self.allEnemy = []; ? ? #是否開(kāi)始了游戲 開(kāi)始游戲狀態(tài)為0 死亡狀態(tài)為1 為開(kāi)始為2 ? ? self.isStart=2 ? ? #最后一個(gè)爆炸動(dòng)畫(huà) ? ? self.lastTime=0 ? ? #開(kāi)始執(zhí)行動(dòng)畫(huà) ? ? self.run() ? def initGame(self): ? ? # 創(chuàng)建一個(gè)自己角色 ? ? self.myself = airplane.airplane(self.screen, 30, pygame.image.load('airplane.png'), 250, 800, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pygame.image.load('buttle.png'), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0, 120, 79, self.boom) ? ? self.isStart=0 ? def run(self): ? ? # 程序主循環(huán) ? ? while True: ? ? ? # 每次都要重新繪制背景白色 ? ? ? self.screen.fill(self.WHITE) ? ? ? # 背景的循環(huán)滾動(dòng) ? ? ? self.imgy += 2 ? ? ? if self.imgy == 854: ? ? ? ? self.imgy = 0 ? ? ? # 背景的繪制,繪制兩張圖片實(shí)現(xiàn)循環(huán)滾動(dòng)的效果 ? ? ? self.screen.blit(self.img, (self.imgx, self.imgy)) ? ? ? self.screen.blit(self.img, (self.imgx, self.imgy - 854)) ? ? ? #如果游戲isstart為flase則游戲不會(huì)繼續(xù)刷新 ? ? ? if self.isStart==0: ? ? ? ? self.startGame(); ? ? ? elif self.isStart == 1: ? ? ? ? self.clearAll(); ? ? ? else: ? ? ? ? self.showstartGame() ? ? ? for event in pygame.event.get(): ? ? ? ? if event.type == QUIT: ? ? ? ? ? pygame.quit() ? ? ? ? ? sys.exit() ? ? ? ? if event.type==MOUSEBUTTONDOWN: ? ? ? ? ? x, y = pygame.mouse.get_pos() ? ? ? ? ? if(x>170 and x<170+161and y>300 and y<300+43): ? ? ? ? ? ? self.isStart=0 ? ? ? ? ? ? self.initGame(); ? ? ? # 刷新屏幕 ? ? ? pygame.display.update() ? ? ? # 設(shè)置pygame時(shí)鐘的間隔時(shí)間 ? ? ? self.fpsClock.tick(self.FPS) ? def showMark(self): ? ? a = pygame.font.SysFont('幼圓', 50) ? ? text = a.render(str(self.mark),True,(255,0,0)) ? ? b=len(str(self.mark)) ? ? self.screen.blit(text, [500-b*30,10]) ? def showstartGame(self): ? ? self.screen.blit(self.startGameImage,(170,300)) ? def addMark(self,m): ? ? self.mark+=m; ? def clearMark(self): ? ? self.mark=0; ? def startGame(self): ? ? # 獲取鼠標(biāo)位置點(diǎn) ? ? x, y = pygame.mouse.get_pos() ? ? # 飛機(jī)的運(yùn)行函數(shù) ? ? self.myself.run(x, y); ? ? # 所有子彈每一幀的位置 ? ? for item in self.myself.allbullet: ? ? ? if item.y <= 0: ? ? ? ? # 當(dāng)子彈行駛出地圖邊界之后移除列表中的當(dāng)前元素并且銷毀對(duì)象 ? ? ? ? self.myself.allbullet.remove(item) ? ? ? ? del item ? ? ? else: ? ? ? ? # 如果沒(méi)有行駛出邊界范圍則繼續(xù)行駛 ? ? ? ? item.advance() ? ? ? ? for enemy in self.allEnemy: ? ? ? ? ? try: ? ? ? ? ? ? # 判斷我方子彈是否與敵人相撞,并且相撞的時(shí)候敵人還活著 ? ? ? ? ? ? if ( ? ? ? ? ? ? ? ? ? ? item.x > enemy.x and item.x < enemy.x + enemy.width and item.y >= enemy.y and item.y < enemy.y + enemy.height and enemy.isDeath == 0): ? ? ? ? ? ? ? self.myself.allbullet.remove(item) ? ? ? ? ? ? ? del item ? ? ? ? ? ? ? enemy.death() ? ? ? ? ? ? ? self.addMark(enemy.myselfMark) ? ? ? ? ? except: ? ? ? ? ? ? continue; ? ? self.showMark() ? ? # 發(fā)射子彈的函數(shù)具體的射速由對(duì)象內(nèi)部決定 ? ? self.myself.attack(); ? ? # 外部計(jì)算時(shí)間的一個(gè)變量沒(méi)time % FPS == 0 時(shí)為1秒鐘 ? ? self.time += 1 ? ? if self.time==self.lastTime: ? ? ? self.isStart=1 ? ? # 飛機(jī)隨機(jī)生成時(shí)間 ? ? if self.time % (self.FPS * 2) == 0: ? ? ? # 每過(guò)一秒鐘新生成一架飛機(jī),添加到allEnemy這個(gè)列表之中,y從0開(kāi)始 x在窗口大小隨機(jī)生成 ? ? ? self.allEnemy.append( ? ? ? ? airplane.airplane(self.screen, 15, self.allEnemyImg[random.randint(0, 1)], random.randint(0, 500 - 179), -134, ? ? ? ? ? ? ? ? ? ? ? ? ? self.allEnemyButtlueImg[0], 1, 179, 134, self.boom)) ? ? # 循環(huán)執(zhí)行該列表,敵人飛機(jī)的運(yùn)動(dòng)軌跡 ? ? for item in self.allEnemy: ? ? ? # 飛機(jī)超出下邊界后去除 ? ? ? if item.y > 800 + item.height: ? ? ? ? self.allEnemy.remove(item) ? ? ? ? del item ? ? ? # 飛機(jī)死亡并且存在子彈數(shù)為0后去除 ? ? ? elif item.isDeath == 1 and len(item.allbullet) == 0: ? ? ? ? self.allEnemy.remove(item) ? ? ? ? del item ? ? ? # 否則飛機(jī)繼續(xù)運(yùn)行 ? ? ? else: ? ? ? ? item.ai() ? ? ? ? item.attack() ? ? ? ? for i in item.allbullet: ? ? ? ? ? if i.y <= -96 or i.y >= 896: ? ? ? ? ? ? # 當(dāng)子彈行駛出地圖邊界之后移除列表中的當(dāng)前元素并且銷毀對(duì)象 ? ? ? ? ? ? item.allbullet.remove(i) ? ? ? ? ? ? del i ? ? ? ? ? else: ? ? ? ? ? ? # 如果沒(méi)有行駛出邊界范圍則繼續(xù)行駛 ? ? ? ? ? ? if ( ? ? ? ? ? ? ? ? ? ? i.x > self.myself.x and i.x < self.myself.x + self.myself.width and i.y >= self.myself.y and i.y < self.myself.y + self.myself.height and self.myself.isDeath == 0): ? ? ? ? ? ? ? self.myself.death() ? ? ? ? ? ? ? self.lastTime=self.time+50; ? ? ? ? ? ? i.advance() ? def clearAll(self): ? ? self.mark=0 ? ? for i in self.allEnemy: ? ? ? for j in i.allbullet: ? ? ? ? del j; ? ? ? del i; ? ? for j in self.myself.allbullet: ? ? ? del j ? ? del self.myself ? ? # 存放所有敵人變量的一個(gè)列表 ? ? self.allEnemy = []; ? ? # 是否開(kāi)始了游戲 ? ? self.isStart = 2 ? ? self.lastTime=0 game=Game()
airplane.py
#飛機(jī)子彈的圖片路徑 ? ? ? ? self.bullet=bullet; ? ? ? ? #飛機(jī)的類型,0代表自己,1代表敵人 ? ? ? ? self.type=type; ? ? ? ? #飛機(jī)的寬度 ? ? ? ? self.width=w; ? ? ? ? #飛機(jī)的高度 ? ? ? ? self.height=h; ? ? ? ? #存放子彈的數(shù)組 ? ? ? ? self.allbullet=[]; ? ? ? ? #用于判斷子彈多長(zhǎng)時(shí)間發(fā)射一次和fps相關(guān) ? ? ? ? self.time=0; ? ? ? ? #當(dāng)前這個(gè)飛機(jī)的速度 ? ? ? ? self.speedx=1; ? ? ? ? self.speedy=1; ? ? ? ? # 攻擊速度,值越小攻擊越快最小為1 ? ? ? ? self.advancespeed = advancespeed; ? ? ? ? #每個(gè)飛機(jī)自己攜帶的分?jǐn)?shù),目前隨機(jī) ? ? ? ? self.myselfMark=random.randint(1,3) ? ? ? ? #運(yùn)動(dòng)軌跡 ? ? ? ? self.runType=random.randint(0, 3) ? ? ? ? #是否死亡 1表示死亡 0表示活著 ? ? ? ? self.isDeath=0 ? ? ? ? #爆炸圖片播放那一張 ? ? ? ? self.showboomTime=0 ? ? ? ? #爆炸數(shù)據(jù) ? ? ? ? self.boom=boom ? ? ? ? #開(kāi)始攻擊 ? ? ? ? self.attack(); ? ? def death(self): ? ? ? ? #死亡狀態(tài) ? ? ? ? self.isDeath=1; ? ? def showboom(self): ? ? ? ? #顯示boom的動(dòng)畫(huà) ? ? ? ? if self.showboomTime==len(self.boom)-1: ? ? ? ? ? ? return ; ? ? ? ? if self.time%5==0: ? ? ? ? ? ? self.showboomTime+=1; ? ? ? ? self.screen.blit(self.boom[self.showboomTime], (self.x, self.y)) ? ? def run(self,x,y): ? ? ? ? if self.isDeath==1: ? ? ? ? ? ? self.showboom(); ? ? ? ? ? ? return 1; ? ? ? ? self.x = x - self.width / 2; ? ? ? ? self.y = y - self.height / 2; ? ? ? ? self.screen.blit(self.img, (self.x, self.y)) ? ? def attack(self): ? ? ? ? #飛機(jī)發(fā)射子彈的函數(shù) ? ? ? ? if self.time%self.advancespeed==0 and self.isDeath==0: ? ? ? ? ? ? self.allbullet.append(bullet(self.screen,self.bullet,self.x-self.width/2,self.y,self.width,self.height,128,128,self.type)) ? ? ? ? self.time+=1 ? ? def ai(self): ? ? ? ? if self.isDeath==1: ? ? ? ? ? ? self.showboom(); ? ? ? ? ? ? return ? ? ? ? #這里分為四種的運(yùn)動(dòng)軌跡隨機(jī)決定 ? ? ? ? if self.type==1: ? ? ? ? ? ? if self.runType==0: ? ? ? ? ? ? ? ? self.y+=self.speedy*2; ? ? ? ? ? ? elif self.runType==1: ? ? ? ? ? ? ? ? self.y+=self.speedy; ? ? ? ? ? ? ? ? if self.x<=0-self.width/2 or self.x>=500-self.width/2: ? ? ? ? ? ? ? ? ? ? self.speedx=0-self.speedx ? ? ? ? ? ? ? ? self.x+=self.speedx ? ? ? ? ? ? elif self.runType==2: ? ? ? ? ? ? ? ? self.y += self.speedy; ? ? ? ? ? ? ? ? if self.x<=0-self.width/2 or self.x>=500-self.width/2: ? ? ? ? ? ? ? ? ? ? self.speedx = 0 - self.speedx ? ? ? ? ? ? ? ? self.x -= self.speedx ? ? ? ? ? ? elif self.runType==3: ? ? ? ? ? ? ? ? self.y += self.speedy; ? ? ? ? ? ? ? ? if self.x<=0-self.width/2 or self.x>=500-self.width/2: ? ? ? ? ? ? ? ? ? ? self.speedx = 0 - self.speedx ? ? ? ? ? ? ? ? ? ? self.speedy=0-self.speedy ? ? ? ? ? ? ? ? self.x -= self.speedx ? ? ? ? ? ? self.screen.blit(self.img, (self.x, self.y)) ? ? ? ? ? ? self.attack() ? ? def __del__(self): ? ? ? ? return class bullet: ? ? def __init__(self,screen,img,x,y,airplanex,airplaney,w,h,type): ? ? ? ? #游戲場(chǎng)景傳參 ? ? ? ? self.screen=screen; ? ? ? ? #子彈的圖片的路徑 ? ? ? ? self.img=img; ? ? ? ? if type==0: ? ? ? ? ? ? # 子彈的y坐標(biāo) ? ? ? ? ? ? self.y=y-airplaney; ? ? ? ? ? ? # 子彈的x坐標(biāo) ? ? ? ? ? ? self.x = x + airplanex / 2; ? ? ? ? else: ? ? ? ? ? ? self.y=y+airplaney-20; ? ? ? ? ? ? self.x = x+airplanex/2+w/2-12; ? ? ? ? #子彈圖片的寬度 ? ? ? ? self.width=w ? ? ? ? #子彈圖片的高度 ? ? ? ? self.height=h ? ? ? ? #子彈需要一直往前飛 ? ? ? ? self.run() ? ? ? ? #子子彈是誰(shuí)的 ? ? ? ? self.type = type ? ? ? ? if self.type==0: ? ? ? ? ? ? self.speed=5 ? ? ? ? else: ? ? ? ? ? ? self.speed=3 ? ? def advance(self): ? ? ? ? if self.type==0: ? ? ? ? ? ? self.y-=self.speed; ? ? ? ? else: ? ? ? ? ? ? self.y+=self.speed; ? ? ? ? self.run(); ? ? def run(self): ? ? ? ? self.screen.blit(self.img, (self.x, self.y)) ? ? def __del__(self): ? ? ? ? return
給大家看看游戲運(yùn)行的效果
一個(gè)簡(jiǎn)單的開(kāi)始按鈕,死亡后又返回這個(gè)界面
上方有一個(gè)簡(jiǎn)潔明了的計(jì)分板,游戲結(jié)束歸零
敵人有四種運(yùn)動(dòng),隨機(jī)產(chǎn)生!?。?/p>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)(面向過(guò)程)
- python版飛機(jī)大戰(zhàn)代碼分享
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
- 用Python寫(xiě)飛機(jī)大戰(zhàn)游戲之pygame入門(4):獲取鼠標(biāo)的位置及運(yùn)動(dòng)
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(pygame版)
- python pygame模塊編寫(xiě)飛機(jī)大戰(zhàn)
- python飛機(jī)大戰(zhàn)pygame碰撞檢測(cè)實(shí)現(xiàn)方法分析
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲
- python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)游戲
相關(guān)文章
Pytorch用Tensorboard來(lái)觀察數(shù)據(jù)
這篇文章主要介紹了Pytorch用Tensorboard來(lái)觀察數(shù)據(jù),上一篇文章我們講解了關(guān)于Pytorch?Dataset的數(shù)據(jù)處理,這篇我們就來(lái)講解觀察數(shù)據(jù),下面具體相關(guān)資料,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-12-12使用Python快速搭建HTTP服務(wù)和文件共享服務(wù)的實(shí)例講解
今天小編就為大家分享一篇使用Python快速搭建HTTP服務(wù)和文件共享服務(wù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06使用python實(shí)現(xiàn)男神女神顏值打分系統(tǒng)(推薦)
這篇文章主要介紹了用python做一個(gè)男神女神顏值打分系統(tǒng)(程序分析見(jiàn)注釋),需要的朋友可以參考下2019-10-10python控制臺(tái)中實(shí)現(xiàn)進(jìn)度條功能
這篇文章主要介紹了python控制臺(tái)中實(shí)現(xiàn)進(jìn)度條功能的方法,想要了解的朋友可以參考一下2015-11-11通過(guò)python實(shí)現(xiàn)隨機(jī)交換禮物程序詳解
這篇文章主要介紹了通過(guò)python實(shí)現(xiàn)隨機(jī)交換禮物程序詳解的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07python 求某條線上特定x值或y值的點(diǎn)坐標(biāo)方法
今天小編就為大家分享一篇python 求某條線上特定x值或y值的點(diǎn)坐標(biāo)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07Sublime Text3最新激活注冊(cè)碼分享適用2020最新版 親測(cè)可用
這篇文章主要介紹了Sublime Text3最新激活注冊(cè)碼分享親測(cè)3211可用2020-11-11