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

python使用pygame模塊實(shí)現(xiàn)坦克大戰(zhàn)游戲

 更新時(shí)間:2020年03月25日 09:20:41   作者:CO-MI  
這篇文章主要為大家詳細(xì)介紹了python使用pygame模塊實(shí)現(xiàn)坦克大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

首先,第一步,游戲簡(jiǎn)單素材的準(zhǔn)備。

炮彈,炮彈,坦克移動(dòng)。音樂(lè)-開(kāi)火素材。

其次,思路整理。

我們需要幾個(gè)類,分別是玩家類,敵人類,炮彈類及地圖類,開(kāi)始游戲界面以及結(jié)束界面,血條等等。

開(kāi)始coding。

主函數(shù),new一個(gè)對(duì)象(java亂入emmm),聲明一個(gè)對(duì)象。

# encoding : utf-8
# anthor : comi
from gameloop import *
from pygame import *
import pygame,sys,time
 
if __name__ == '__main__':
 player = game() # 聲明一個(gè)類對(duì)象
 player.game_start('KEEP-GOING') # 調(diào)用開(kāi)始函數(shù)
 while player.playing: # 進(jìn)入游戲運(yùn)行
  player.new() # 開(kāi)始游戲
 player.screen.fill(black)
 player.game_start('GAME-OVER') # 游戲結(jié)束
 time.sleep(1.5) # 可以不要

這里可以根據(jù)自己的需要進(jìn)行更改相關(guān)代碼

接下來(lái) 游戲的主循環(huán)

# encoding : utf-8
# author : comi
from setting import *
from pygame import *
from Sprite import *
import pygame,sys
vec = pygame.math.Vector2
 
 
class game: # 游戲類 包含循環(huán)等
 def __init__(self): # 初始化
  pygame.init() # pygame 初始化
  pygame.display.set_caption("Keep-Going") # 游戲窗口 左上角名稱
  self.screen = pygame.display.set_mode((width, height)) # 游戲窗口的大小
  self.FpsClock = pygame.time.Clock() # 設(shè)置游戲的刷新率
  self.playing = True # 進(jìn)入游戲的狀態(tài)
  self.running = True # 游戲運(yùn)行的狀態(tài)
  self.Waiting = True # 游戲等待的狀態(tài)
  self.Pblood = 100 # 玩家血量
  self.Eblood = 100 # 敵人血量
  self.player = Player() # 聲明一個(gè)游戲玩家對(duì)象
  self.enemy = Enemy() # 聲明一個(gè)敵人對(duì)象
  self.all_groups = pygame.sprite.Group() # 通過(guò)pygame自帶的 group 來(lái)判斷碰撞檢測(cè)
  self.player_groups = pygame.sprite.Group()
  self.Map_groups = pygame.sprite.Group()
  self.Enemy_groups = pygame.sprite.Group()
 
 def new(self): # 開(kāi)始一個(gè)游戲
  self.player_groups.add(self.player) # 將玩家添加到玩家組
  self.all_groups.add(self.player) # 將玩家添加到 所有組
 
  self.Enemy_groups.add(self.enemy)
  self.all_groups.add(self.enemy)
 
  for platfroms in Map1: # 地圖
   p = Platform(*platfroms)  # 取出所有值
   self.Map_groups.add(p)
   self.all_groups.add(p)
 
  self.run() # 調(diào)用函數(shù)運(yùn)行游戲
 
 def game_start(self,text):  # 游戲的開(kāi)始界面
  self.text_draw(width / 2, height / 4, 64, text) # 文本
  self.text_draw(width / 2, height * 3 / 4, 25,'Press any key to continue',) # 文本
  pygame.display.update() # 更行展示
  while self.Waiting: # 實(shí)現(xiàn) 按鍵等待開(kāi)始效果
   for event in pygame.event.get():
    if event.type == pygame.QUIT:
     pygame.quit()
     sys.exit()
    if event.type == pygame.KEYDOWN: 
     self.Waiting = False
 
 def update(self): # 畫(huà)面更新
  self.Map_groups.update() 
  self.player_groups.update()
  self.enemy.Bullet_groups.update(self.enemy.flag) # 通過(guò)按鍵判斷子彈方向
  self.player.Bullet_groups.update(self.player.flag)
  self.Enemy_groups.update()
 
  hit = pygame.sprite.groupcollide(self.player.Bullet_groups, self.Map_groups, True,False) # 子彈碰墻消失
  hit = pygame.sprite.groupcollide(self.enemy.Bullet_groups, self.Map_groups, True, False)
 
  PMC = pygame.sprite.spritecollide(self.player,self.Map_groups,False,False)  # 撞墻
  if PMC:
   key_pressed = pygame.key.get_pressed()
   if key_pressed[pygame.K_a]:
    self.player.pos.x = self.player.pos.x + gap
   if key_pressed[pygame.K_d]:
    self.player.pos.x = self.player.pos.x - gap
   if key_pressed[pygame.K_w]:
    self.player.pos.y = self.player.pos.y + gap
   if key_pressed[pygame.K_s]:
    self.player.pos.y = self.player.pos.y - gap
 
  EMC = pygame.sprite.spritecollide(self.enemy,self.Map_groups,False,False)  # 撞墻
  if EMC:
   key_pressed = pygame.key.get_pressed()
   if key_pressed[pygame.K_LEFT]:
    self.enemy.pos.x = self.enemy.pos.x + gap
   if key_pressed[pygame.K_RIGHT]:
    self.enemy.pos.x = self.enemy.pos.x - gap
   if key_pressed[pygame.K_UP]:
    self.enemy.pos.y = self.enemy.pos.y + gap
   if key_pressed[pygame.K_DOWN]:
    self.enemy.pos.y = self.enemy.pos.y - gap
 
 def run(self): 
  while self.running:
   self.FpsClock.tick(Fps) # 設(shè)置幀率
   self.events() # 獲取事件
   self.draw_pic() # 畫(huà)出圖片
   self.update() 
 
   f self.Eblood <= 0: # enemy 
    self.screen.fill(black)
    self.game_start('P1 WIN!')
    time.sleep(1.5)
    self.running = False
    self.playing = False
 
   if self.Pblood <= 0: # Player
    self.screen.fill(black)
    self.game_start('P2 WIN!')
    time.sleep(1.5)
    self.running = False
    self.playing = False
 
 def text_draw(self, x, y, size, text): # 文本展示函數(shù)
  self.font = pygame.font.Font('freesansbold.ttf', size) # 字體,大小
  self.text_surf = self.font.render(text, True, red) # 顏色
  self.text_rect = self.text_surf.get_rect() # 矩形
  self.text_rect.center = (x, y) # 位置
  self.screen.blit(self.text_surf, self.text_rect) # 覆蓋展示
 
 def draw_pic(self): 
  self.screen.fill(white) # 背景
  self.text_draw(900,50,30,"KEEP") # 文本
  self.text_draw(900, 100, 30, "GOING")
 
  self.text_draw(820, 150, 20, "P1:")
  self.text_draw(820, 200, 20, "P2:")
  
  self.text_draw(900, 250, 20, "Attention!")
  self.text_draw(900,300,20,"The Bullet Can")
  self.text_draw(900, 350, 20, "Be Control!")
  self.bar_draw(850, 145, self.Pblood) # 血條
  hit = pygame.sprite.groupcollide(self.enemy.Bullet_groups, self.player_groups, True, False) # 血條減少
  if hit:
   self.Pblood = self.Pblood - randint(10, 15)
   self.bar_draw(850, 145, self.Pblood)
 
  self.bar_draw(850, 195, self.Eblood)
  hit = pygame.sprite.groupcollide(self.player.Bullet_groups, self.Enemy_groups, True, False)
  if hit:
   self.Eblood = self.Eblood - randint(10, 15)
   self.bar_draw(850, 195, self.Eblood)
 
  self.Map_groups.draw(self.screen) # 畫(huà)出圖片
  self.player_groups.draw(self.screen)
  self.Enemy_groups.draw(self.screen)
  self.player.Bullet_groups.draw(self.screen)
  self.enemy.Bullet_groups.draw(self.screen)
 
  pygame.display.update() 
 
 def bar_draw(self, x, y, pct): # 血條函數(shù)
  # draw a bar 
  if pct <= 0:
   pct = 0
  Bar_Lenth = 100
  Bar_Height = 10
  Fill_Lenth = (pct / 100) * Bar_Lenth
  Out_rect = pygame.Rect(x, y, Bar_Lenth, Bar_Height)
  Fill_rect = pygame.Rect(x, y, Fill_Lenth, Bar_Height)
  pygame.draw.rect(self.screen, green, Fill_rect)
  pygame.draw.rect(self.screen, red, Out_rect, 2)
 
 def events(self): # 事件
  for events in pygame.event.get():
   if events.type == pygame.QUIT:
    self.running = False
    self.playing = False

在主循環(huán)內(nèi)實(shí)現(xiàn)了很多功能,文本窗口展示,血條展示,以及整個(gè)游戲循環(huán)的定義都是可以拿來(lái)借鑒的,我也是從油管主上學(xué)來(lái)的。

接下來(lái),精靈類,包含玩家,敵人。子彈,地圖四部分。

# encoding : utf-8
# antuor : comi
from setting import *
from pygame import *
import pygame,sys,time
from random import *
from math import *
 
vec = pygame.math.Vector2 # 運(yùn)用向量
 
class Player(pygame.sprite.Sprite): # 玩家類
 Bullet_groups = pygame.sprite.Group()
 flag = 1 # 判斷方向的flag
 def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\down.png').convert() # 圖片的加載
  self.image.set_colorkey(white) # 設(shè)置忽略白色
  self.rect = self.image.get_rect()
  self.rect.midbottom = (115, 130) 
 
  self.pos = vec(115, 130)
 
  self.last_time = time.time() #記錄上一次時(shí)間 用來(lái)設(shè)置子彈頻率等
 
 def update(self): 
  key_pressed = pygame.key.get_pressed() # 按鍵獲取
  if key_pressed[pygame.K_a]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\left.png').convert()
   self.image.set_colorkey(white)
   self.pos.x -= move_space # 位置移動(dòng)
   self.flag = 2
  if key_pressed[pygame.K_d]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\right.png').convert()
   self.image.set_colorkey(white)
   self.pos.x += move_space
   self.flag = 1
  if key_pressed[pygame.K_w]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\up.png').convert()
   self.image.set_colorkey(white)
   self.pos.y -= move_space
   self.flag = 3
  if key_pressed[pygame.K_s]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\down.png').convert()
   self.image.set_colorkey(white)
   self.pos.y += move_space
   self.flag = 4
  if key_pressed[pygame.K_SPACE]:
   self.shoot()
  self.rect.midbottom = self.pos 
 
 def shoot(self): # 開(kāi)火
  self.now = time.time() # 獲取現(xiàn)在時(shí)間
  if self.now - self.last_time > 0.8: # 子彈時(shí)間間隔
   # 這里顯示錯(cuò)誤了,應(yīng)該在if 語(yǔ)句內(nèi) 包含以下部分
  pygame.mixer.music.load(r'C:\Users\Administrator\Desktop\KeepGoing\sounds\expl.wav') 
   pygame.mixer.music.play() # 音樂(lè)加載
   bullet = Bullet(self.pos.x, self.pos.y)
   self.Bullet_groups.add(bullet) 
   self.last_time = self.now
 
 
class Platform(pygame.sprite.Sprite): # 地圖創(chuàng)建
 def __init__(self, x, y, w, h): # x,y,寬,高
  pygame.sprite.Sprite.__init__(self)
  self.image = pygame.Surface((w, h)) # 磚塊大小
  self.image.fill(yellow) # 磚顏色
  self.rect = self.image.get_rect()
  self.rect.x = x
  self.rect.y = y
 
 
class Enemy(pygame.sprite.Sprite): # 與player 相同
 Bullet_groups = pygame.sprite.Group()
 flag = 1
 def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\down.png').convert()
  self.image.set_colorkey(white)
  self.rect = self.image.get_rect()
  self.rect.midbottom = (315, 130)
  self.pos = vec(315, 130)
  self.bar = 100
  self.last_time = time.time()
  self.flag = 1
 
 def update(self):
  key_pressed = pygame.key.get_pressed()
  if key_pressed[pygame.K_LEFT]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\left.png').convert()
   self.image.set_colorkey(white)
   self.pos.x -= move_space
   self.flag = 2
  if key_pressed[pygame.K_RIGHT]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\right.png').convert()
   self.image.set_colorkey(white)
   self.pos.x += move_space
   self.flag = 1
  if key_pressed[pygame.K_UP]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\up.png').convert()
   self.image.set_colorkey(white)
   self.pos.y -= move_space
   self.flag = 3
  if key_pressed[pygame.K_DOWN]:
   self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\down.png').convert()
   self.image.set_colorkey(white)
   self.pos.y += move_space
   self.flag = 4
  if key_pressed[pygame.K_p]:
   self.shoot()
 
  self.rect.midbottom = self.pos
 
 def shoot(self):
  self.now = time.time()
  if self.now - self.last_time > 0.8:
   pygame.mixer.music.load(r'C:\Users\Administrator\Desktop\KeepGoing\sounds\expl.wav')
   pygame.mixer.music.play()
   bullet = Bullet(self.pos.x, self.pos.y)
   self.Bullet_groups.add(bullet)
   self.Bullet_groups.update(self.flag)
   self.last_time = self.now
 
 
class Bullet(pygame.sprite.Sprite): # 炮彈組
 def __init__(self, x, y): # 炮彈該有的位置 玩家周?chē)?
  pygame.sprite.Sprite.__init__(self)
  self.image = pygame.image.load(r'C:\Users\Administrator\Desktop\KeepGoing\img\dot.png ').convert()
  self.image.set_colorkey(white)
  self.rect = self.image.get_rect()
  self.rect.centerx = x + 10 # 這里是準(zhǔn)確的位置,未進(jìn)行準(zhǔn)確更改
  self.rect.bottom = y - 12
  self.speed = 5
 
 def update(self,flag): 
  if flag == 1: # right
   self.rect.x += self.speed
  if flag == 2: # left
   self.rect.x -= self.speed
  if flag == 3: #up
   self.rect.y -= self.speed
  if flag == 4: # down
   self.rect.y += self.speed

最后,便是相關(guān)的設(shè)置文件了

# encoding : utf-8
# author :comi
width = 1000
height = 600
Fps = 60
food = 20
gap = 3
move_space = 1.5
back_space = 5
Map1 = [(0, 0, width*2, 10), (0, 10, 10, height * 2),
  (0, height-10, width * 2, 10), (width - 210, 0, 10, height * 2),
  (50,50,100,20),(250,50,100,20),(150,230,100,20),(100,340,200,20),
  (50, 70, 20, 90), (130, 70, 20, 90),(250,70,20,90),(330,70,20,90),
  (130,280,20,70),(250,300,20,50),
  (80,320,20,20),(300,320,20,20),(185,200,30,30),(185,250,30,30),
  (60,300,20,20),(320,300,20,20),
  (40,280,20,20),(340,280,20,20),
  (490,100,160,40),(650,100,40,200),(425,250,150,40),(425,290,40,80),
  (510,365,160,40),(695,460,95,40),(595,454,40,100),(190,460,30,30),
  (300,450,200,40),(100,425,30,130),(200,520,230,25),(725,70,30,30),
  (725,140,30,30),(725,210,30,30),(725,280,30,30),(725,365,30,30)
  ] # map
# color
 
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255,0)
blue = (0, 0, 255)
yellow = ( 255,200,0)
purple = (128,138,135)

這個(gè)坦克大戰(zhàn)還有一些小的bug,比如說(shuō),當(dāng)你按下SPACE 開(kāi)火是,第一發(fā)炮彈已經(jīng)出去了,當(dāng)你按下相反方向,子彈會(huì)按你第二次按下的方向移動(dòng)。(不打算解決,開(kāi)始學(xué)習(xí)游戲ai,和橫板游戲的制作)

有想法的同學(xué)可以和我交流,歡迎大家留言。

最后,運(yùn)行效果如下 ,雙人操作 p1:w a s d space 開(kāi)火 p2: 上 下左 右  p 開(kāi)火

更多關(guān)于python游戲的精彩文章請(qǐng)點(diǎn)擊查看以下專題:

python俄羅斯方塊游戲集合

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

python微信跳一跳游戲集合

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

相關(guān)文章

  • Python通過(guò)UDP傳輸超過(guò)64k的信息

    Python通過(guò)UDP傳輸超過(guò)64k的信息

    在UDP中,單個(gè)數(shù)據(jù)包的最大尺寸通常受到網(wǎng)絡(luò)層的限制,這通常被稱為最大傳輸單元(MTU),在以太網(wǎng)環(huán)境中,標(biāo)準(zhǔn)的MTU大小通常為1500字節(jié),下面是一個(gè)Python示例,展示了如何通過(guò)UDP發(fā)送和接收超過(guò)64KB的數(shù)據(jù),需要的朋友可以參考下
    2024-08-08
  • python urllib和urllib3知識(shí)點(diǎn)總結(jié)

    python urllib和urllib3知識(shí)點(diǎn)總結(jié)

    在本篇內(nèi)容里小編給大家分享了一篇關(guān)于python urllib和urllib3知識(shí)點(diǎn)總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-02-02
  • Python解決N階臺(tái)階走法問(wèn)題的方法分析

    Python解決N階臺(tái)階走法問(wèn)題的方法分析

    這篇文章主要介紹了Python解決N階臺(tái)階走法問(wèn)題的方法,簡(jiǎn)單描述了走臺(tái)階問(wèn)題,并結(jié)合實(shí)例形式分析了Python使用遞歸與遞推算法解決走臺(tái)階問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • Python入門(mén)教程(二)Python快速上手

    Python入門(mén)教程(二)Python快速上手

    這篇文章主要介紹了Python入門(mén)教程(二)Python快速上手,Python是一門(mén)非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門(mén)教程,需要的朋友可以參考下
    2023-04-04
  • Python基于回溯法子集樹(shù)模板解決m著色問(wèn)題示例

    Python基于回溯法子集樹(shù)模板解決m著色問(wèn)題示例

    這篇文章主要介紹了Python基于回溯法子集樹(shù)模板解決m著色問(wèn)題,簡(jiǎn)單描述了m著色問(wèn)題并結(jié)合實(shí)例形式分析了Python使用回溯法子集樹(shù)模板解決m著色問(wèn)題的具體步驟與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2017-09-09
  • python連接sqlite3簡(jiǎn)單用法完整例子

    python連接sqlite3簡(jiǎn)單用法完整例子

    SQLite3是一個(gè)內(nèi)置的Python模塊,可以通過(guò)Python的標(biāo)準(zhǔn)庫(kù)輕松地使用,無(wú)需進(jìn)行額外安裝和配置,這篇文章主要介紹了python連接sqlite3簡(jiǎn)單用法的相關(guān)資料,需要的朋友可以參考下
    2025-08-08
  • tensorflow2.0與tensorflow1.0的性能區(qū)別介紹

    tensorflow2.0與tensorflow1.0的性能區(qū)別介紹

    今天小編就為大家分享一篇tensorflow2.0與tensorflow1.0的性能區(qū)別介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python獲取代碼運(yùn)行時(shí)間的實(shí)例代碼

    python獲取代碼運(yùn)行時(shí)間的實(shí)例代碼

    今天小編就為大家分享一篇python獲取代碼運(yùn)行時(shí)間的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Python數(shù)據(jù)分析入門(mén)之?dāng)?shù)據(jù)讀取與存儲(chǔ)

    Python數(shù)據(jù)分析入門(mén)之?dāng)?shù)據(jù)讀取與存儲(chǔ)

    今天繼續(xù)帶大家學(xué)習(xí)python數(shù)據(jù)分析,下文中有非常詳細(xì)的代碼示例,清楚地解釋了python數(shù)據(jù)讀取與存儲(chǔ)的相關(guān)知識(shí),需要的朋友可以參考下
    2021-05-05
  • Python字符串處理的8招秘籍(小結(jié))

    Python字符串處理的8招秘籍(小結(jié))

    這篇文章主要介紹了Python字符串處理的8招秘籍,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評(píng)論

长白| 双桥区| 浦北县| 辛集市| 开封市| 南漳县| 门源| 开鲁县| 黎平县| 神农架林区| 清远市| 望江县| 如皋市| 包头市| 贺兰县| 华安县| 广平县| 丘北县| 衡南县| 涞水县| 铜梁县| 乌兰察布市| 仁化县| 鄯善县| 泰宁县| 且末县| 大石桥市| 石嘴山市| 蒙城县| 喜德县| 石河子市| 呼伦贝尔市| 江川县| 台安县| 吉水县| 汕尾市| 依安县| 新泰市| 闵行区| 昌邑市| 安宁市|