基于python實(shí)現(xiàn)坦克大戰(zhàn)游戲
本文實(shí)例為大家分享了python實(shí)現(xiàn)坦克大戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲界面
pygame游戲引擎的安裝
pip安裝
windows + R --> cmd --> 命令行輸入 pip install 模塊名==版本號
pycharm中安裝
File --> setting --> Project --> Project Interpreter --> 右側(cè) + install --> 搜索框輸入pygame --> 下方
installPackage
面向?qū)ο蠓治?/strong>
分析組成類
- 實(shí)現(xiàn)框架的搭建(類的設(shè)計(jì))
- 主邏輯類
- 坦克類
- 我方坦克
- 敵方坦克 子彈類
- 爆炸類
- 墻壁類
- 音效類
框架搭建
import pygame #主邏輯類 class MainGame(): def startGame(self): pass def gameOver(self): pass #基本坦克類 class BaseTank(): pass #我方坦克類 class MyTank(): pass #敵方坦克類 class EnemyTank(): pass #子彈類 class Bullet(): pass #爆炸類 class Explode(): pass #墻壁類 class Wall(): pass #音效類 class Audio(): pass game = MainGame() game.startGame()
展示主窗口
import pygame SCREEN_WIDTH = 900 SCREEN_HEIGHT = 550 #主邏輯類 class MainGame(): #游戲主窗口 window = None def startGame(self): #調(diào)用窗口初始化方法 pygame.display.init() MainGame.window = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) pygame.display.set_caption('坦克大戰(zhàn)v1.02') while True: #填充窗口背景色 MainGame.window.fill((0,0,0)) #刷新 pygame.display.update() def gameOver(self): pass
事件監(jiān)聽
class MainGame(): #游戲主窗口 window = None def startGame(self): #調(diào)用窗口初始化方法 pygame.display.init() MainGame.window = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) pygame.display.set_caption('坦克大戰(zhàn)'+VERSION) while True: #填充窗口背景色 MainGame.window.fill((0,0,0)) #調(diào)用事件處理方法 self.getEvents() #刷新 pygame.display.update() #所有事件處理的方法 def getEvents(self): #獲取隊(duì)列中所有事件,遍歷事件,對type為QUIT以及KEYDOWN兩種事件類型處理 eventList = pygame.event.get() #遍歷所有事件 for e in eventList: if e.type == pygame.QUIT: #調(diào)用游戲結(jié)束的方法 self.gameOver() #如果事件類型為按下鍵盤按鍵 elif e.type == pygame.KEYDOWN: #根據(jù)具體按鍵做對應(yīng)處理 if e.key == pygame.K_UP: print("向上移動(dòng)") elif e.key == pygame.K_DOWN: print("向下移動(dòng)") elif e.key == pygame.K_LEFT: print("向左移動(dòng)") elif e.key == pygame.K_RIGHT: print("向右移動(dòng)") elif e.key == pygame.K_SPACE: print("biubiu~~~") # elif e.type == pygame.MOUSEMOTION: # print(e.pos) def gameOver(self): #結(jié)束程序 exit()
顯示我方坦克
#我方坦克類 class MyTank(BaseTank): def __init__(self,x,y): super(MyTank, self).__init__() #設(shè)置具體的圖片集 self.images = { 'U':pygame.image.load('img/p1tankU.gif'), 'D':pygame.image.load('img/p1tankD.gif'), 'L':pygame.image.load('img/p1tankL.gif'), 'R':pygame.image.load('img/p1tankR.gif') } #我方坦克的初始方向 self.direction = 'U' #設(shè)置坦克的圖片 self.image = self.images[self.direction] #先基于圖像獲取坦克的位置以及大小 self.rect = self.image.get_rect() #修改坦克坐標(biāo),改成自定義位置 self.rect.centerx = x self.rect.centery = y def displayTank(self): self.image = self.images[self.direction] MainGame.window.blit(self.image,self.rect)
實(shí)現(xiàn)我方坦克的移動(dòng)
坦克類中,實(shí)現(xiàn)移動(dòng)方法
def move(self): #移動(dòng),基于速度在指定的方向進(jìn)行移動(dòng) if self.direction == 'U': if self.rect.centery > self.rect.height/2: self.rect.centery ‐= self.speed elif self.direction == 'D': if self.rect.centery < SCREEN_HEIGHT ‐ self.rect.height/2: self.rect.centery += self.speed elif self.direction == 'L': if self.rect.centerx > self.rect.height/2: self.rect.centerx ‐= self.speed elif self.direction == 'R': if self.rect.centerx < SCREEN_WIDTH ‐ self.rect.height/2: self.rect.centerx += self.speed
事件處理方法中新增移動(dòng)處理
#所有事件處理的方法 def getEvents(self): #獲取隊(duì)列中所有事件,遍歷事件,對type為QUIT以及KEYDOWN兩種事件類型處理 eventList = pygame.event.get() #遍歷所有事件 for e in eventList: if e.type == pygame.QUIT: #調(diào)用游戲結(jié)束的方法 self.gameOver() #如果事件類型為按下鍵盤按鍵 elif e.type == pygame.KEYDOWN: #根據(jù)具體按鍵做對應(yīng)處理 if e.key == pygame.K_UP: print("向上移動(dòng)") MainGame.tank1.direction = 'U' MainGame.tank1.move() elif e.key == pygame.K_DOWN: print("向下移動(dòng)") MainGame.tank1.direction = 'D' MainGame.tank1.move() elif e.key == pygame.K_LEFT: print("向左移動(dòng)") MainGame.tank1.direction = 'L' MainGame.tank1.move() elif e.key == pygame.K_RIGHT: print("向右移動(dòng)") MainGame.tank1.direction = 'R' MainGame.tank1.move() elif e.key == pygame.K_SPACE: print("biubiu~~~")
優(yōu)化移動(dòng)方式
事件中新增坦克移動(dòng)開關(guān)控制
#所有事件處理的方法 def getEvents(self): #獲取隊(duì)列中所有事件,遍歷事件,對type為QUIT以及KEYDOWN兩種事件類型處理 eventList = pygame.event.get() #遍歷所有事件 for e in eventList: if e.type == pygame.QUIT: #調(diào)用游戲結(jié)束的方法 self.gameOver() #如果事件類型為按下鍵盤按鍵 elif e.type == pygame.KEYDOWN: #根據(jù)具體按鍵做對應(yīng)處理 if e.key == pygame.K_UP: print("向上移動(dòng)") MainGame.tank1.direction = 'U' #修改坦克運(yùn)動(dòng)狀態(tài) MainGame.tank1.stop = False elif e.key == pygame.K_DOWN: print("向下移動(dòng)") MainGame.tank1.direction = 'D' MainGame.tank1.stop = False elif e.key == pygame.K_LEFT: print("向左移動(dòng)") MainGame.tank1.direction = 'L' MainGame.tank1.stop = False elif e.key == pygame.K_RIGHT: print("向右移動(dòng)") MainGame.tank1.direction = 'R' MainGame.tank1.stop = False elif e.key == pygame.K_SPACE: print("biubiu~~~")
主邏輯的循環(huán)中優(yōu)化移動(dòng)
def startGame(self): #調(diào)用窗口初始化方法 pygame.display.init() MainGame.window = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) pygame.display.set_caption('坦克大戰(zhàn)'+VERSION) #調(diào)用創(chuàng)建我方坦克的方法 self.creatMyTank() while True: #填充窗口背景色 MainGame.window.fill((0,0,0)) #調(diào)用事件處理方法 self.getEvents() #展示我方坦克 self.showMyTank() if not MainGame.tank1.stop: # 我方坦克移動(dòng) MainGame.tank1.move() #刷新 pygame.display.update() #新增,程序休眠,優(yōu)化坦克移動(dòng)速度 time.sleep(0.020)
實(shí)現(xiàn)敵方坦克的加載
完善敵方坦克類
#敵方坦克類 class EnemyTank(BaseException): #v1.07 def __init__(self,x,y): self.images = { 'U': pygame.image.load('img/enemy1U.gif'), 'D': pygame.image.load('img/enemy1D.gif'), 'L': pygame.image.load('img/enemy1L.gif'), 'R': pygame.image.load('img/enemy1R.gif') } # 敵方坦克的初始方向?yàn)殡S機(jī)方向 self.direction = self.randomDirection() # 設(shè)置坦克的圖片 self.image = self.images[self.direction] # 先基于圖像獲取坦克的位置以及大小 self.rect = self.image.get_rect() # 修改坦克坐標(biāo),改成自定義位置 self.rect.centerx = x self.rect.centery = y #v1.07生成坦克的隨機(jī)方向 def randomDirection(self): num = random.randint(1,4) if num == 1: return 'U' elif num == 2: return 'D' elif num == 3: return 'L' elif num == 4: return 'R' #v1.07將敵方坦克加入到窗口中 def displayTank(self): self.image = self.images[self.direction] MainGame.window.blit(self.image, self.rect)
主邏輯中實(shí)現(xiàn)敵方坦克的加載
class MainGame(): #游戲主窗口 window = None tank1 = None #v1.07 新增存儲(chǔ)敵方坦克的列表 enemy_tanklist = [] def startGame(self): #調(diào)用窗口初始化方法 pygame.display.init() MainGame.window = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) pygame.display.set_caption('坦克大戰(zhàn)'+VERSION) #調(diào)用創(chuàng)建我方坦克的方法 self.creatMyTank() #v1.07調(diào)用創(chuàng)建敵方坦克的方法 self.creatEnemyTank() while True: #填充窗口背景色 MainGame.window.fill((0,0,0)) #調(diào)用事件處理方法 self.getEvents() #展示我方坦克 self.showMyTank() #v1.07調(diào)用展示敵方坦克的方法 self.showEnemyTank() if not MainGame.tank1.stop: # 我方坦克移動(dòng) MainGame.tank1.move() #刷新 pygame.display.update() #v1.06新增,程序休眠,優(yōu)化坦克移動(dòng)速度 time.sleep(0.020) #創(chuàng)建我方坦克 def creatMyTank(self): MainGame.tank1 = MyTank(SCREEN_WIDTH/2,SCREEN_HEIGHT/3*2) #展示我方坦克 def showMyTank(self): MainGame.tank1.displayTank() #v1.07創(chuàng)建敵方坦克 def creatEnemyTank(self): for i in range(ENEMM_TANK_COUNT): num = random.randint(1,7) etank = EnemyTank(100*num,150) MainGame.enemy_tanklist.append(etank) #v1.07展示敵方坦克 def showEnemyTank(self): for etank in MainGame.enemy_tanklist: etank.displayTank()
運(yùn)行效果
完整代碼
# from pygame import * import pygame,time,random SCREEN_WIDTH = 900 SCRREN_HEIGHT = 600 COLOR_BLACK = pygame.Color(0,0,0) VERSION = 'v2.5' class MainGame(): #游戲窗口 window = None P1 = None #敵方坦克列表 enemyTankList = [] #我方子彈列表 myBulletList = [] #存儲(chǔ)敵方子彈 enemyBulletList = [] #存儲(chǔ)爆炸效果的列表 bombList = [] #存儲(chǔ)墻壁的列表 wallList = [] def __init__(self): self.version = VERSION def startGame(self): print('游戲開始') #初始化展示模塊 pygame.display.init() #調(diào)用自定義的創(chuàng)建窗口的方法 self.creatWindow() #設(shè)置游戲標(biāo)題 pygame.display.set_caption('坦克大戰(zhàn)'+self.version) #調(diào)用創(chuàng)建墻壁的方法 self.creatWalls() #調(diào)用創(chuàng)建坦克方法 self.creatMyTank() #調(diào)用創(chuàng)建敵方坦克 self.creatEnemyTank() while True: #設(shè)置游戲背景的填充色 MainGame.window.fill(COLOR_BLACK) #調(diào)用展示墻壁的方法 self.showAllWalls() # 調(diào)用展示我方坦克的方法 self.showMyTank() #調(diào)用展示我方子彈的方法 self.showAllMyBullet() #調(diào)用展示所有爆炸效果的方法 self.showAllBombs() #調(diào)用展示敵方坦克的方法 self.showEnemyTank() #調(diào)用展示敵方子彈的方法 self.showAllEnemyBullet() #調(diào)用獲取事件,處理事件的方法 self.getAllEvents() #窗口持續(xù)刷新以即時(shí)顯示 pygame.display.update() time.sleep(0.02) def creatWindow(self): MainGame.window = pygame.display.set_mode((SCREEN_WIDTH,SCRREN_HEIGHT)) def getAllEvents(self): #獲取所有的事件 event_list = pygame.event.get() for e in event_list: if e.type == pygame.QUIT: #關(guān)閉窗口,結(jié)束游戲,調(diào)用gameOver方法 self.gameOver() elif e.type == pygame.KEYDOWN: print('點(diǎn)擊鍵盤按鍵') if e.key == pygame.K_SPACE: bullet = MainGame.P1.shot() #控制子彈發(fā)射的數(shù)量 if len(MainGame.myBulletList) < 4: print('發(fā)射子彈') MainGame.myBulletList.append(bullet) print('當(dāng)前我方子彈數(shù)量為:',len(MainGame.myBulletList)) #創(chuàng)建音效對象,播放音效文件 audio = Audio('tank-images/boom.wav') audio.play() #創(chuàng)建墻壁的方法 def creatWalls(self): for i in range(1,8): wall = Wall(i*120,380,'tank-images/steels.gif') MainGame.wallList.append(wall) #展示墻壁的方法 def showAllWalls(self): for w in MainGame.wallList: w.displayWall() def creatMyTank(self): MainGame.P1 = MyTank(SCREEN_WIDTH/2,SCRREN_HEIGHT/4*3) def showMyTank(self): MainGame.P1.displayTank() MainGame.P1.move() MainGame.P1.hitWalls() #展示我方子彈 def showAllMyBullet(self): for b in MainGame.myBulletList: if b.live: b.displayBullet() #調(diào)用子彈的移動(dòng)方法 b.move() #調(diào)用是否打中敵方坦克的方法 b.hitEnemyTank() #調(diào)用是否打中墻壁的方法 b.hitWalls() else: MainGame.myBulletList.remove(b) #展示敵方子彈 def showAllEnemyBullet(self): for b in MainGame.enemyBulletList: if b.live: b.displayBullet() b.move() #調(diào)用是否打中墻壁的方法 b.hitWalls() else: MainGame.enemyBulletList.remove(b) def creatEnemyTank(self): for i in range(5): etank = EnemyTank(random.randint(1,8)*100,150) MainGame.enemyTankList.append(etank) def showEnemyTank(self): for etank in MainGame.enemyTankList: etank.displayTank() etank.move() etank.hitWalls() #調(diào)用射擊方法 etank.shot() #展示所有爆炸效果 def showAllBombs(self): for bomb in MainGame.bombList: if bomb.live: bomb.displayBomb() else: MainGame.bombList.remove(bomb) def gameOver(self): print('游戲結(jié)束') exit() class Tank(): def __init__(self,x,y): #圖片集(存儲(chǔ)4個(gè)方向的所有圖片) self.images = { 'U':pygame.image.load('tank-images/tankU.gif'), 'D':pygame.image.load('tank-images/tankD.gif'), 'L':pygame.image.load('tank-images/tankL.gif'), 'R':pygame.image.load('tank-images/tankR.gif'), } self.direction = 'U' #從圖片集中根據(jù)方向獲取圖片 self.image = self.images[self.direction] self.rect = self.image.get_rect() self.rect.centerx = x self.rect.centery = y self.speed = 3 self.isDead = False #新增屬性用來記錄上一步的坐標(biāo) self.oldx = self.rect.centerx self.oldy = self.rect.centery def stay(self): self.rect.centerx = self.oldx self.rect.centery = self.oldy def hitWalls(self): index = self.rect.collidelist(MainGame.wallList) if index != -1: self.stay() def move(self): #記錄移動(dòng)之前的坐標(biāo) self.oldx = self.rect.centerx self.oldy = self.rect.centery if self.direction == 'U': if self.rect.centery > self.rect.height/2: self.rect.centery -= self.speed elif self.direction == 'D': if self.rect.centery < SCRREN_HEIGHT - self.rect.height/2: self.rect.centery += self.speed elif self.direction == 'L': if self.rect.centerx > self.rect.height/2: self.rect.centerx -= self.speed elif self.direction == 'R': if self.rect.centerx < SCREEN_WIDTH - self.rect.height/2: self.rect.centerx += self.speed def shot(self): return Bullet(self) def displayTank(self): # 重新設(shè)置坦克圖片 self.image = self.images[self.direction] # 將坦克加載的到窗口 MainGame.window.blit(self.image, self.rect) class MyTank(Tank): def __init__(self,x,y): super(MyTank, self).__init__(x,y) def move(self): #pygame.key pressed_list = pygame.key.get_pressed() #分別判斷上下左右四個(gè)方向的按鍵,按下的狀態(tài) if pressed_list[pygame.K_LEFT]: #修改坦克的方向 self.direction = 'L' super(MyTank, self).move() elif pressed_list[pygame.K_RIGHT]: self.direction = 'R' super(MyTank, self).move() elif pressed_list[pygame.K_UP]: self.direction = 'U' super(MyTank, self).move() elif pressed_list[pygame.K_DOWN]: self.direction = 'D' super(MyTank, self).move() class EnemyTank(Tank): def __init__(self,x,y): super(EnemyTank, self).__init__(x,y) #隨機(jī)速度 self.speed = self.randSpeed(2,5) #隨機(jī)方向 self.direction = self.randDirection() #圖片 # self.image = self.images[self.direction] #坐標(biāo)位置 self.rect = self.image.get_rect() self.rect.centerx = x self.rect.centery = y #記錄坦克移動(dòng)步數(shù)的變量 self.step = random.randint(25,50) #生成隨機(jī)速度值 def randSpeed(self,from_,to_): return random.randint(from_,to_) def randDirection(self): list1 = ['U','D','L','R'] return list1[random.randint(0,3)] def move(self): if self.step > 0: super(EnemyTank, self).move() self.step -= 1 else: #1.生成新的方向 self.direction = self.randDirection() #2.步數(shù)還原 self.step = random.randint(25,50) def shot(self): num = random.randint(1,40) if num == 1: b = Bullet(self) MainGame.enemyBulletList.append(b) class Bullet(): def __init__(self,tank): #圖片 if isinstance(tank,MyTank): self.image = pygame.image.load('tank-images/tankmissile.gif') else: self.image = pygame.image.load('tank-images/enemymissile.gif') #方向 self.direction = tank.direction #坐標(biāo)位置 self.rect = self.image.get_rect() #子彈的具體位置 if self.direction == 'U': self.rect.centerx = tank.rect.centerx self.rect.centery = tank.rect.centery - tank.rect.height/2 - self.rect.height/2 elif self.direction == 'D': self.rect.centerx = tank.rect.centerx self.rect.centery = tank.rect.centery + tank.rect.height / 2 + self.rect.height / 2 elif self.direction == 'L': self.rect.centery = tank.rect.centery self.rect.centerx = tank.rect.centerx - tank.rect.height/2 - self.rect.height/2 elif self.direction == 'R': self.rect.centery = tank.rect.centery self.rect.centerx = tank.rect.centerx + tank.rect.height / 2 + self.rect.height / 2 #移動(dòng)速度 self.speed = 8 #子彈的狀態(tài)(live) self.live = True def move(self): if self.direction == 'U': #邊界控制 if self.rect.centery > 0: self.rect.centery -= self.speed else: self.live = False elif self.direction == 'D': if self.rect.centery < SCRREN_HEIGHT: self.rect.centery += self.speed else: self.live = False elif self.direction == 'L': if self.rect.centerx > 0: self.rect.centerx -= self.speed else: self.live = False elif self.direction == 'R': if self.rect.centerx < SCREEN_WIDTH: self.rect.centerx += self.speed else: self.live = False def displayBullet(self): MainGame.window.blit(self.image, self.rect) #子彈與墻壁的碰撞 def hitWalls(self): index = self.rect.collidelist(MainGame.wallList) if index != -1: self.live = False # 我方子彈是否碰撞到敵方坦克 def hitEnemyTank(self): index = self.rect.collidelist(MainGame.enemyTankList) if index != -1: # 打中敵方坦克后的業(yè)務(wù)邏輯 # 修改子彈的live屬性 self.live = False tank = MainGame.enemyTankList.pop(index) # 打中敵方坦克之后產(chǎn)生一個(gè)爆炸效果,裝進(jìn)爆炸效果列表中 bomb = Bomb(tank) MainGame.bombList.append(bomb) class Bomb(): def __init__(self,tank): #存儲(chǔ)多張爆炸效果的圖片 self.images = [ pygame.image.load('tank-images/0.gif'), pygame.image.load('tank-images/1.gif'), pygame.image.load('tank-images/2.gif'), pygame.image.load('tank-images/3.gif'), pygame.image.load('tank-images/4.gif'), pygame.image.load('tank-images/5.gif'), pygame.image.load('tank-images/6.gif') ] #用來記錄圖片為圖片集中的第幾張 self.index = 0 self.image = self.images[self.index] self.live = True self.rect = self.image.get_rect() self.rect.centerx = tank.rect.centerx self.rect.centery = tank.rect.centery def displayBomb(self): if self.index < len(self.images): self.image = self.images[self.index] self.index += 1 MainGame.window.blit(self.image, self.rect) else: self.index = 0 self.live = False class Audio(): def __init__(self,musicpath): pygame.mixer.init() pygame.mixer.music.load(musicpath) def play(self): pygame.mixer.music.play() class Wall(): def __init__(self,x,y,imagepath): self.image = pygame.image.load(imagepath) self.rect = self.image.get_rect() self.rect.centerx = x self.rect.centery = y def displayWall(self): MainGame.window.blit(self.image,self.rect) game = MainGame() game.startGame()
更多關(guān)于python游戲的精彩文章請點(diǎn)擊查看以下專題:
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python?Celery定時(shí)任務(wù)詳細(xì)講解
這篇文章主要介紹了Python?Celery定時(shí)任務(wù)詳細(xì)講解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08Python將string轉(zhuǎn)換到float的實(shí)例方法
在本篇文章中小編給大家分享的是關(guān)于Python將string轉(zhuǎn)換到float的實(shí)例方法以及相關(guān)知識點(diǎn),需要的朋友們參考下。2019-07-07python實(shí)現(xiàn)圖片數(shù)據(jù)增強(qiáng)的示例詳解
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖片數(shù)據(jù)增強(qiáng)的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-10-10Keras神經(jīng)網(wǎng)絡(luò)efficientnet模型搭建yolov3目標(biāo)檢測平臺
這篇文章主要為大家介紹了Keras利用efficientnet系列模型搭建yolov3目標(biāo)檢測平臺的過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05舉例講解Python中的list列表數(shù)據(jù)結(jié)構(gòu)用法
這篇文章主要介紹了Python中的list列表數(shù)據(jù)結(jié)構(gòu)用法,列表是Python內(nèi)置的六種集合類數(shù)據(jù)類型中最常見的之一,需要的朋友可以參考下2016-03-03