最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于python實(shí)現(xiàn)坦克大戰(zhàn)游戲

 更新時(shí)間:2020年10月27日 08:43:27   作者:嚴(yán)肅溫柔  
這篇文章主要為大家詳細(xì)介紹了基于python實(shí)現(xiàn)坦克大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)坦克大戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下

游戲界面

pygame游戲引擎的安裝

pip安裝

windows + R --> cmd --> 命令行輸入 pip install 模塊名==版本號(hào)

pycharm中安裝

File --> setting --> Project --> Project Interpreter --> 右側(cè) + install --> 搜索框輸入pygame --> 下方
installPackage

面向?qū)ο蠓治?/strong>

分析組成類(lèi)

  • 實(shí)現(xiàn)框架的搭建(類(lèi)的設(shè)計(jì))
  • 主邏輯類(lèi)
  • 坦克類(lèi)
  • 我方坦克
  • 敵方坦克 子彈類(lèi)
  • 爆炸類(lèi)
  • 墻壁類(lèi)
  • 音效類(lèi)

框架搭建

import pygame 
#主邏輯類(lèi)
class MainGame():
 def startGame(self):
 pass
 def gameOver(self):
 pass
#基本坦克類(lèi)
class BaseTank():
 pass
#我方坦克類(lèi)
class MyTank():
 pass
#敵方坦克類(lèi)
class EnemyTank():
 pass
#子彈類(lèi)
class Bullet():
 pass
#爆炸類(lèi)
class Explode():
 pass
#墻壁類(lèi)
class Wall():
 pass
#音效類(lèi)
class Audio():
 pass
game = MainGame()
game.startGame()

展示主窗口

import pygame
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 550
#主邏輯類(lèi)
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)聽(tīng)

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ì)列中所有事件,遍歷事件,對(duì)type為QUIT以及KEYDOWN兩種事件類(lèi)型處理
 eventList = pygame.event.get()
 #遍歷所有事件
 for e in eventList:
 if e.type == pygame.QUIT:
 #調(diào)用游戲結(jié)束的方法
 self.gameOver()
 #如果事件類(lèi)型為按下鍵盤(pán)按鍵
 elif e.type == pygame.KEYDOWN:
 #根據(jù)具體按鍵做對(duì)應(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()

顯示我方坦克

#我方坦克類(lèi)
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)

坦克類(lèi)中,實(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ì)列中所有事件,遍歷事件,對(duì)type為QUIT以及KEYDOWN兩種事件類(lèi)型處理
 eventList = pygame.event.get()
 #遍歷所有事件
 for e in eventList:
 if e.type == pygame.QUIT:
 #調(diào)用游戲結(jié)束的方法
 self.gameOver()
 #如果事件類(lèi)型為按下鍵盤(pán)按鍵
 elif e.type == pygame.KEYDOWN:
 #根據(jù)具體按鍵做對(duì)應(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)開(kāi)關(guān)控制

#所有事件處理的方法
 def getEvents(self):
 #獲取隊(duì)列中所有事件,遍歷事件,對(duì)type為QUIT以及KEYDOWN兩種事件類(lèi)型處理
 eventList = pygame.event.get()
 #遍歷所有事件
 for e in eventList:
 if e.type == pygame.QUIT:
 #調(diào)用游戲結(jié)束的方法
 self.gameOver()
 #如果事件類(lèi)型為按下鍵盤(pán)按鍵
 elif e.type == pygame.KEYDOWN:
 #根據(jù)具體按鍵做對(duì)應(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)敵方坦克的加載

完善敵方坦克類(lèi)

#敵方坦克類(lèi)
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('游戲開(kāi)始')
 #初始化展示模塊
 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)擊鍵盤(pá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)建音效對(duì)象,播放音效文件
  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
 #新增屬性用來(lái)記錄上一步的坐標(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')
 ]
 #用來(lái)記錄圖片為圖片集中的第幾張
 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游戲的精彩文章請(qǐng)點(diǎn)擊查看以下專題:

python俄羅斯方塊游戲集合

python經(jīng)典小游戲匯總

python微信跳一跳游戲集合

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

javascript經(jīng)典小游戲匯總

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

相關(guān)文章

  • python如何批量讀取.mat文件并保存成.npy

    python如何批量讀取.mat文件并保存成.npy

    這篇文章主要介紹了python如何批量讀取.mat文件并保存成.npy問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • PYQT5開(kāi)啟多個(gè)線程和窗口,多線程與多窗口的交互實(shí)例

    PYQT5開(kāi)啟多個(gè)線程和窗口,多線程與多窗口的交互實(shí)例

    今天小編就為大家分享一篇PYQT5開(kāi)啟多個(gè)線程和窗口,多線程與多窗口的交互實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • 詳解Python 中的容器 collections

    詳解Python 中的容器 collections

    這篇文章主要介紹了Python 中的容器 collections的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-08-08
  • python推導(dǎo)式的使用方法實(shí)例

    python推導(dǎo)式的使用方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于python推導(dǎo)式的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python實(shí)現(xiàn)下載指定網(wǎng)址所有圖片的方法

    python實(shí)現(xiàn)下載指定網(wǎng)址所有圖片的方法

    這篇文章主要介紹了python實(shí)現(xiàn)下載指定網(wǎng)址所有圖片的方法,涉及Python針對(duì)頁(yè)面的讀取、遍歷及文件操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • python中數(shù)字列表轉(zhuǎn)化為數(shù)字字符串的實(shí)例代碼

    python中數(shù)字列表轉(zhuǎn)化為數(shù)字字符串的實(shí)例代碼

    先前學(xué)習(xí)過(guò),數(shù)字和字符串都可以存儲(chǔ)到變量當(dāng)中,下面這篇文章主要給大家介紹了關(guān)于python中數(shù)字列表轉(zhuǎn)化為數(shù)字字符串的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • 教你利用Python+Turtle繪制簡(jiǎn)易版愛(ài)心表白

    教你利用Python+Turtle繪制簡(jiǎn)易版愛(ài)心表白

    這篇文章主要介紹了教你利用Python+Turtle繪制簡(jiǎn)易版愛(ài)心表白,文中有非常詳細(xì)的代碼示例,對(duì)想要和男朋友或者女朋友表白的小伙伴們有很大幫助喲,需要的朋友可以參考下
    2021-04-04
  • Python筆記之a(chǎn) = [0]*x格式的含義及說(shuō)明

    Python筆記之a(chǎn) = [0]*x格式的含義及說(shuō)明

    這篇文章主要介紹了Python筆記之a(chǎn) = [0]*x格式的含義及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解Python如何檢查一個(gè)數(shù)字是否為科技數(shù)

    詳解Python如何檢查一個(gè)數(shù)字是否為科技數(shù)

    科技數(shù)(Tech?Number)是一種在數(shù)學(xué)上具有一定特殊性質(zhì)的數(shù)字,這篇文章主要為大家詳細(xì)介紹了如何使用Python檢查一個(gè)數(shù)字是否為科技數(shù),感興趣的可以了解下
    2024-03-03
  • Flask框架使用DBUtils模塊連接數(shù)據(jù)庫(kù)操作示例

    Flask框架使用DBUtils模塊連接數(shù)據(jù)庫(kù)操作示例

    這篇文章主要介紹了Flask框架使用DBUtils模塊連接數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了flask框架使用DBUtils模塊連接數(shù)據(jù)庫(kù)的常見(jiàn)操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2018-07-07

最新評(píng)論

翁源县| 赤城县| 茂名市| 广宗县| 分宜县| 邵阳县| 阿合奇县| 五华县| 大名县| 银川市| 方城县| 武宁县| 勃利县| 西乌珠穆沁旗| 周口市| 华安县| 梧州市| 泰州市| 博爱县| 遵义市| 乐陵市| 道真| 盘山县| 柘荣县| 泰安市| 泰兴市| 玉环县| 巴彦淖尔市| 本溪市| 古交市| 忻州市| 丹阳市| 左权县| 荥阳市| 屯门区| 民县| 剑川县| 昭通市| 鸡西市| 玉田县| 湖口县|