91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python使用pygame模塊實現坦克大戰游戲

發布時間:2020-10-10 13:35:21 來源:腳本之家 閱讀:227 作者:CO-MI 欄目:開發技術

本文實例為大家分享了pygame模塊實現坦克大戰游戲的具體代碼,供大家參考,具體內容如下

首先,第一步,游戲簡單素材的準備。

python使用pygame模塊實現坦克大戰游戲炮彈,python使用pygame模塊實現坦克大戰游戲python使用pygame模塊實現坦克大戰游戲python使用pygame模塊實現坦克大戰游戲python使用pygame模塊實現坦克大戰游戲炮彈,坦克移動。音樂-開火素材。

其次,思路整理

我們需要幾個類,分別是玩家類,敵人類,炮彈類及地圖類,開始游戲界面以及結束界面,血條等等。

開始coding。

主函數,new一個對象(java亂入emmm),聲明一個對象。

# encoding : utf-8
# anthor : comi
from gameloop import *
from pygame import *
import pygame,sys,time
 
if __name__ == '__main__':
 player = game() # 聲明一個類對象
 player.game_start('KEEP-GOING') # 調用開始函數
 while player.playing: # 進入游戲運行
  player.new() # 開始游戲
 player.screen.fill(black)
 player.game_start('GAME-OVER') # 游戲結束
 time.sleep(1.5) # 可以不要

這里可以根據自己的需要進行更改相關代碼

接下來 游戲的主循環

# encoding : utf-8
# author : comi
from setting import *
from pygame import *
from Sprite import *
import pygame,sys
vec = pygame.math.Vector2
 
 
class game: # 游戲類 包含循環等
 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() # 設置游戲的刷新率
  self.playing = True # 進入游戲的狀態
  self.running = True # 游戲運行的狀態
  self.Waiting = True # 游戲等待的狀態
  self.Pblood = 100 # 玩家血量
  self.Eblood = 100 # 敵人血量
  self.player = Player() # 聲明一個游戲玩家對象
  self.enemy = Enemy() # 聲明一個敵人對象
  self.all_groups = pygame.sprite.Group() # 通過pygame自帶的 group 來判斷碰撞檢測
  self.player_groups = pygame.sprite.Group()
  self.Map_groups = pygame.sprite.Group()
  self.Enemy_groups = pygame.sprite.Group()
 
 def new(self): # 開始一個游戲
  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() # 調用函數運行游戲
 
 def game_start(self,text):  # 游戲的開始界面
  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: # 實現 按鍵等待開始效果
   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): # 畫面更新
  self.Map_groups.update() 
  self.player_groups.update()
  self.enemy.Bullet_groups.update(self.enemy.flag) # 通過按鍵判斷子彈方向
  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) # 設置幀率
   self.events() # 獲取事件
   self.draw_pic() # 畫出圖片
   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): # 文本展示函數
  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) # 畫出圖片
  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): # 血條函數
  # 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

在主循環內實現了很多功能,文本窗口展示,血條展示,以及整個游戲循環的定義都是可以拿來借鑒的,我也是從油管主上學來的。

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

# 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 # 運用向量
 
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) # 設置忽略白色
  self.rect = self.image.get_rect()
  self.rect.midbottom = (115, 130) 
 
  self.pos = vec(115, 130)
 
  self.last_time = time.time() #記錄上一次時間 用來設置子彈頻率等
 
 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 # 位置移動
   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): # 開火
  self.now = time.time() # 獲取現在時間
  if self.now - self.last_time > 0.8: # 子彈時間間隔
   # 這里顯示錯誤了,應該在if 語句內 包含以下部分
  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.last_time = self.now
 
 
class Platform(pygame.sprite.Sprite): # 地圖創建
 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): # 炮彈該有的位置 玩家周圍
  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 # 這里是準確的位置,未進行準確更改
  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

最后,便是相關的設置文件了

# 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)

這個坦克大戰還有一些小的bug,比如說,當你按下SPACE 開火是,第一發炮彈已經出去了,當你按下相反方向,子彈會按你第二次按下的方向移動。(不打算解決,開始學習游戲ai,和橫板游戲的制作)

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

最后,運行效果如下 ,雙人操作 p1:w a s d space 開火 p2: 上 下左 右  p 開火

python使用pygame模塊實現坦克大戰游戲python使用pygame模塊實現坦克大戰游戲

更多關于python游戲的精彩文章請點擊查看以下專題:

python俄羅斯方塊游戲集合

python經典小游戲匯總

python微信跳一跳游戲集合

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

榕江县| 青铜峡市| 松溪县| 庐江县| 增城市| 宾川县| 隆昌县| 永丰县| 棋牌| 江孜县| 蕲春县| 平山县| 麦盖提县| 沂南县| 巧家县| 都昌县| 防城港市| 平泉县| 内乡县| 和田县| 浦江县| 京山县| 惠水县| 玛多县| 鹿泉市| 湖州市| 浮梁县| 玉山县| 余干县| 郧西县| 新乡县| 寻乌县| 临泽县| 新田县| 神池县| 乌什县| 黄梅县| 宁夏| 磴口县| 台江县| 金溪县|