欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)簡單的飛機大戰(zhàn)

 更新時間:2022年05月08日 08:09:49   作者:我是狗汪汪汪  
這篇文章主要為大家詳細介紹了python實現(xiàn)簡單的飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)簡單的飛機大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下

制作初衷

這幾天閑來沒事干,就想起來好長時間沒做過游戲了,于是就想做一個游戲練練手,為了起到一個練習(xí)的目的就使用了自己不是太熟練的python這門語言來編寫,代碼都有備注,大家可以直接看代碼,這個代碼我是在python3.1的環(huán)境下寫的,大家需要可以直接下載我這里的資源,圖片和代碼打包到了一起的,因為是第一次使用python做游戲,有什么不足的地方望大佬斧正。

游戲思路

首先我們分析飛機大戰(zhàn)這個游戲?qū)儆谝粋€平面的2d游戲,那這個游戲的所有都是建立在x,y的基礎(chǔ)上進行的運算,為了加快我們的開發(fā)工作,秉承時間就是金錢的原則,我們采用的是python 的 pygame第三方庫來實現(xiàn)的。
言歸正傳!?。?/p>

背景

仔細分析飛機大戰(zhàn)我們不難發(fā)現(xiàn),這個游戲的背景實際上就是一張圖片一直循環(huán)滾動所造成的一種視覺上的效果,那布滿我們一個場景并且讓他無縫連接實際上我們只需要兩張圖片滾動,然后當(dāng)?shù)诙垐D片滾動完,再讓他從第一張圖片開始滾動如此循環(huán)便可以給用戶視覺上一種無縫滾動的效果。

飛機

我們分析飛機的行為可以發(fā)現(xiàn),不管是我方的飛機還是敵人的飛機都是有幾種他們共有的動作。

1,運動,敵我雙方都是在一直的運動。
2,發(fā)射子彈,雙方都是在發(fā)射子彈,但具體的數(shù)量和速度我們可以通過類的屬性來進行修改。
3,死亡,敵人和我們自身都會死亡,死亡會觸發(fā)一個爆炸的動畫,敵人死亡加分,我方死亡游戲結(jié)束。

子彈

為了節(jié)省時間此處的子彈都只會走直線,封裝到自己的類方法里面,后期可以修改軌跡。

話不多說,直接上代碼,每個重要的代碼都是有注釋。大家可以自己觀看。

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的時鐘
? ? self.fpsClock = pygame.time.Clock()

? ? # 設(shè)置窗口大小
? ? self.screen = pygame.display.set_mode((500, 800), 0, 32)

? ? # 設(shè)置標(biāo)題
? ? pygame.display.set_caption('飛機大戰(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')
? ? #加載開始游戲按鈕
? ? self.startGameImage=pygame.image.load('start.png')

? ? # 初始化背景圖片的位置
? ? self.imgx = 0
? ? self.imgy = 0
? ? # 全局的時間變量
? ? self.time = 0;
? ? #敵人子彈的圖片數(shù)據(jù)
? ? self.allEnemyButtlueImg=[pygame.image.load('buttle3.png')]
? ? # 存放所有敵人圖片數(shù)據(jù)的一個列表
? ? self.allEnemyImg = [pygame.image.load('em1.png'), pygame.image.load('em2.png')]
? ? # 存放所有敵人變量的一個列表
? ? self.allEnemy = [];
? ? #是否開始了游戲 開始游戲狀態(tài)為0 死亡狀態(tài)為1 為開始為2
? ? self.isStart=2
? ? #最后一個爆炸動畫
? ? self.lastTime=0
? ? #開始執(zhí)行動畫
? ? self.run()
? def initGame(self):
? ? # 創(chuàng)建一個自己角色
? ? 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)滾動
? ? ? self.imgy += 2
? ? ? if self.imgy == 854:
? ? ? ? self.imgy = 0
? ? ? # 背景的繪制,繪制兩張圖片實現(xiàn)循環(huán)滾動的效果
? ? ? self.screen.blit(self.img, (self.imgx, self.imgy))
? ? ? self.screen.blit(self.img, (self.imgx, self.imgy - 854))
? ? ? #如果游戲isstart為flase則游戲不會繼續(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時鐘的間隔時間
? ? ? 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)位置點
? ? x, y = pygame.mouse.get_pos()
? ? # 飛機的運行函數(shù)
? ? self.myself.run(x, y);
? ? # 所有子彈每一幀的位置
? ? for item in self.myself.allbullet:
? ? ? if item.y <= 0:
? ? ? ? # 當(dāng)子彈行駛出地圖邊界之后移除列表中的當(dāng)前元素并且銷毀對象
? ? ? ? self.myself.allbullet.remove(item)
? ? ? ? del item
? ? ? else:
? ? ? ? # 如果沒有行駛出邊界范圍則繼續(xù)行駛
? ? ? ? item.advance()
? ? ? ? for enemy in self.allEnemy:
? ? ? ? ? try:
? ? ? ? ? ? # 判斷我方子彈是否與敵人相撞,并且相撞的時候敵人還活著
? ? ? ? ? ? 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ù)具體的射速由對象內(nèi)部決定
? ? self.myself.attack();
? ? # 外部計算時間的一個變量沒time % FPS == 0 時為1秒鐘
? ? self.time += 1
? ? if self.time==self.lastTime:
? ? ? self.isStart=1
? ? # 飛機隨機生成時間
? ? if self.time % (self.FPS * 2) == 0:
? ? ? # 每過一秒鐘新生成一架飛機,添加到allEnemy這個列表之中,y從0開始 x在窗口大小隨機生成
? ? ? 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í)行該列表,敵人飛機的運動軌跡
? ? for item in self.allEnemy:
? ? ? # 飛機超出下邊界后去除
? ? ? if item.y > 800 + item.height:
? ? ? ? self.allEnemy.remove(item)
? ? ? ? del item
? ? ? # 飛機死亡并且存在子彈數(shù)為0后去除
? ? ? elif item.isDeath == 1 and len(item.allbullet) == 0:
? ? ? ? self.allEnemy.remove(item)
? ? ? ? del item
? ? ? # 否則飛機繼續(xù)運行
? ? ? else:
? ? ? ? item.ai()
? ? ? ? item.attack()
? ? ? ? for i in item.allbullet:
? ? ? ? ? if i.y <= -96 or i.y >= 896:
? ? ? ? ? ? # 當(dāng)子彈行駛出地圖邊界之后移除列表中的當(dāng)前元素并且銷毀對象
? ? ? ? ? ? item.allbullet.remove(i)
? ? ? ? ? ? del i
? ? ? ? ? else:
? ? ? ? ? ? # 如果沒有行駛出邊界范圍則繼續(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
? ? # 存放所有敵人變量的一個列表
? ? self.allEnemy = [];
? ? # 是否開始了游戲
? ? self.isStart = 2
? ? self.lastTime=0


game=Game()

airplane.py

#飛機子彈的圖片路徑
? ? ? ? self.bullet=bullet;
? ? ? ? #飛機的類型,0代表自己,1代表敵人
? ? ? ? self.type=type;
? ? ? ? #飛機的寬度
? ? ? ? self.width=w;
? ? ? ? #飛機的高度
? ? ? ? self.height=h;
? ? ? ? #存放子彈的數(shù)組
? ? ? ? self.allbullet=[];
? ? ? ? #用于判斷子彈多長時間發(fā)射一次和fps相關(guān)
? ? ? ? self.time=0;
? ? ? ? #當(dāng)前這個飛機的速度
? ? ? ? self.speedx=1;
? ? ? ? self.speedy=1;
? ? ? ? # 攻擊速度,值越小攻擊越快最小為1
? ? ? ? self.advancespeed = advancespeed;
? ? ? ? #每個飛機自己攜帶的分?jǐn)?shù),目前隨機
? ? ? ? self.myselfMark=random.randint(1,3)
? ? ? ? #運動軌跡
? ? ? ? self.runType=random.randint(0, 3)
? ? ? ? #是否死亡 1表示死亡 0表示活著
? ? ? ? self.isDeath=0
? ? ? ? #爆炸圖片播放那一張
? ? ? ? self.showboomTime=0
? ? ? ? #爆炸數(shù)據(jù)
? ? ? ? self.boom=boom
? ? ? ? #開始攻擊
? ? ? ? self.attack();
? ? def death(self):
? ? ? ? #死亡狀態(tài)
? ? ? ? self.isDeath=1;
? ? def showboom(self):
? ? ? ? #顯示boom的動畫
? ? ? ? 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):
? ? ? ? #飛機發(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
? ? ? ? #這里分為四種的運動軌跡隨機決定
? ? ? ? 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):
? ? ? ? #游戲場景傳參
? ? ? ? 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()
? ? ? ? #子子彈是誰的
? ? ? ? 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

給大家看看游戲運行的效果

一個簡單的開始按鈕,死亡后又返回這個界面

上方有一個簡潔明了的計分板,游戲結(jié)束歸零

敵人有四種運動,隨機產(chǎn)生?。?!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • opencv實現(xiàn)圖像縮放效果

    opencv實現(xiàn)圖像縮放效果

    這篇文章主要為大家詳細介紹了opencv實現(xiàn)圖像縮放效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Pytorch用Tensorboard來觀察數(shù)據(jù)

    Pytorch用Tensorboard來觀察數(shù)據(jù)

    這篇文章主要介紹了Pytorch用Tensorboard來觀察數(shù)據(jù),上一篇文章我們講解了關(guān)于Pytorch?Dataset的數(shù)據(jù)處理,這篇我們就來講解觀察數(shù)據(jù),下面具體相關(guān)資料,需要的朋友可以參考一下,希望對你有所幫助
    2021-12-12
  • NumPy 數(shù)組屬性的具體使用

    NumPy 數(shù)組屬性的具體使用

    本文主要介紹了NumPy 數(shù)組屬性的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 使用Python快速搭建HTTP服務(wù)和文件共享服務(wù)的實例講解

    使用Python快速搭建HTTP服務(wù)和文件共享服務(wù)的實例講解

    今天小編就為大家分享一篇使用Python快速搭建HTTP服務(wù)和文件共享服務(wù)的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 使用python實現(xiàn)男神女神顏值打分系統(tǒng)(推薦)

    使用python實現(xiàn)男神女神顏值打分系統(tǒng)(推薦)

    這篇文章主要介紹了用python做一個男神女神顏值打分系統(tǒng)(程序分析見注釋),需要的朋友可以參考下
    2019-10-10
  • Python抽象類應(yīng)用詳情

    Python抽象類應(yīng)用詳情

    這篇文章主要介紹了Python抽象類應(yīng)用詳情,抽象類就是控制子類的方法的名稱,要求子類必須按照父類的要求的實現(xiàn)指定的方法,且方法名要和父類保持一致,下文更多相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • python控制臺中實現(xiàn)進度條功能

    python控制臺中實現(xiàn)進度條功能

    這篇文章主要介紹了python控制臺中實現(xiàn)進度條功能的方法,想要了解的朋友可以參考一下
    2015-11-11
  • 通過python實現(xiàn)隨機交換禮物程序詳解

    通過python實現(xiàn)隨機交換禮物程序詳解

    這篇文章主要介紹了通過python實現(xiàn)隨機交換禮物程序詳解的,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • python 求某條線上特定x值或y值的點坐標(biāo)方法

    python 求某條線上特定x值或y值的點坐標(biāo)方法

    今天小編就為大家分享一篇python 求某條線上特定x值或y值的點坐標(biāo)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Sublime Text3最新激活注冊碼分享適用2020最新版 親測可用

    Sublime Text3最新激活注冊碼分享適用2020最新版 親測可用

    這篇文章主要介紹了Sublime Text3最新激活注冊碼分享親測3211可用
    2020-11-11

最新評論