以下代碼單純是個(gè)簡(jiǎn)易的沒(méi)有完全完成的代碼,主坦克單純只能進(jìn)行移動(dòng)的操作,其他攻擊類以及敵方坦克的移動(dòng)攻擊均未設(shè)置。
(精靈圖可以到bilibili或者網(wǎng)上找精靈圖,只需要改一下以下代碼的文件位置就行)
import pygame.display
import pygame, time, random
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
COLRE = pygame.Color(0, 0, 0)
TEXT_COLOR = pygame.Color(255, 0, 0)
# 導(dǎo)入模塊
class Maingame():
? ? window = None
? ? my_tank = None
? ? # 創(chuàng)建存儲(chǔ)敵方坦克的列表
? ? enemytanklist = []
? ? enemytankcount = 5
?
? ? def __init__(self):
? ? ? ? pass
?
? ? def startgame(self):
? ? ? ? # 加載一個(gè)主窗口
? ? ? ? pygame.display.init() # 初始化窗口
? ? ? ? # 設(shè)置窗口的大小及其顯示
? ? ? ? Maingame.window = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
? ? ? ? # 初始化坦克的位置
? ? ? ? Maingame.my_tank = Tanke(350, 250)
? ? ? ? # 初始化敵方的坦克,并且添加到列表當(dāng)中
? ? ? ? self.createenemy()
? ? ? ? # 對(duì)標(biāo)題方面進(jìn)行設(shè)置
? ? ? ? pygame.display.set_caption('坦克大作戰(zhàn)')
? ? ? ? while True:
? ? ? ? ? ? # 使用time模塊的時(shí)侯讓坦克速度慢一些
? ? ? ? ? ? time.sleep(0.001)
? ? ? ? ? ? # 給窗口設(shè)置填充的顏色
? ? ? ? ? ? Maingame.window.fill(COLRE)
? ? ? ? ? ? self.getevent()
? ? ? ? ? ? # 繪制文字
? ? ? ? ? ? Maingame.window.blit(self.getText('敵方坦克剩余的數(shù)量%d' % 6), (10, 10))
? ? ? ? ? ? # 調(diào)用坦克的顯示方法
? ? ? ? ? ? Maingame.my_tank.displayTake()
? ? ? ? ? ? #循環(huán)遍歷敵方的坦克的列表,展示敵方的坦克
? ? ? ? ? ? self.blitenemytake()
? ? ? ? ? ? # 調(diào)用移動(dòng)的方法
? ? ? ? ? ? # 如果坦克的開關(guān)是開啟,才可以移動(dòng)
? ? ? ? ? ? if not Maingame.my_tank.stop:
? ? ? ? ? ? ? ? Maingame.my_tank.move()
? ? ? ? ? ? pygame.display.update()
?
? ? def createenemy(self):
? ? ? ? top = 100
? ? ? ? # 循環(huán)生成敵方的坦克
? ? ? ? for i in range(Maingame.enemytankcount):
? ? ? ? ? ? left = random.randint(0, 600)
? ? ? ? ? ? speed = random.randint(1, 4)
? ? ? ? ? ? enemy = EnemyTanke(left, top, speed)
? ? ? ? ? ? Maingame.enemytanklist.append(enemy)
? ? def blitenemytake(self):
? ? ? ? for enemytake in Maingame.enemytanklist:
? ? ? ? ? ? enemytake.displayTake()
?
? ? def endgame(self):
? ? ? ? print('謝謝使,歡迎再一次進(jìn)行使用')
? ? ? ? exit()
?
? ? # 對(duì)左上角的文字進(jìn)行繪制
? ? def getText(self, text):
? ? ? ? # 設(shè)置初始化字體的模塊
? ? ? ? pygame.font.init()
? ? ? ? # 獲取字體FONT對(duì)象
? ? ? ? font = pygame.font.SysFont('kaiti', 18)
? ? ? ? # 繪制文字的信息
? ? ? ? textSurface = font.render(text, True, TEXT_COLOR)
? ? ? ? return textSurface
?
? ? # 獲取時(shí)間
? ? def getevent(self):
? ? ? ? eventlist = pygame.event.get()
? ? ? ? for event in eventlist:
? ? ? ? ? ? # 判斷按下的鍵位是關(guān)閉還是鍵盤按下
? ? ? ? ? ? # 代表的意思是如果按下的鍵位是退出那么就關(guān)閉窗口
? ? ? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? ? ? self.endgame()
? ? ? ? ? ? # 如果是鍵盤的按下鍵位
? ? ? ? ? ? if event.type == pygame.KEYDOWN:
? ? ? ? ? ? ? ? # 用來(lái)判斷是否是上下左右
? ? ? ? ? ? ? ? # 判斷按下的是上下左右鍵位
? ? ? ? ? ? ? ? if event.key == pygame.K_LEFT:
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.direction = 'L'
? ? ? ? ? ? ? ? ? ? # 修改坦克的開關(guān)狀態(tài)
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.stop = False
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.move()
? ? ? ? ? ? ? ? ? ? print('按下左鍵,坦克向左方向進(jìn)行移動(dòng)')
? ? ? ? ? ? ? ? elif event.key == pygame.K_RIGHT:
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.direction = 'R'
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.stop = False
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.move()
? ? ? ? ? ? ? ? ? ? print('按下右鍵,坦克向右移動(dòng)')
? ? ? ? ? ? ? ? elif event.key == pygame.K_UP:
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.direction = 'U'
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.stop = False
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.move()
? ? ? ? ? ? ? ? ? ? print('按下上鍵,坦克向上移動(dòng)')
? ? ? ? ? ? ? ? elif event.key == pygame.K_DOWN:
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.direction = 'D'
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.stop = False
? ? ? ? ? ? ? ? ? ? Maingame.my_tank.move()
? ? ? ? ? ? ? ? ? ? print('按下下鍵,坦克向下移動(dòng)')
? ? ? ? ? ? ? ? elif event.key == pygame.K_SPACE:
? ? ? ? ? ? ? ? ? ? print('發(fā)射子彈')
? ? ? ? ? ? ? ? # 松開方向鍵的時(shí)侯停止坦克的移動(dòng),修改坦克的開關(guān)狀態(tài)
? ? ? ? ? ? if event.type == pygame.KEYUP:
? ? ? ? ? ? ? ? Maingame.my_tank.stop = True
?
?
class Tanke():
? ? # 添加距離左邊left距離上邊top
? ? def __init__(self, left, top):
? ? ? ? self.images = {
? ? ? ? ? ? 'U': pygame.image.load('坦克2/p1tankU.gif'),
? ? ? ? ? ? 'D': pygame.image.load('坦克2/p1tankD.gif'),
? ? ? ? ? ? 'L': pygame.image.load('坦克2/p1tankL.gif'),
? ? ? ? ? ? 'R': pygame.image.load('坦克2/p1tankR.gif'),
? ? ? ? }
? ? ? ? # 加載方向
? ? ? ? self.direction = 'U'
? ? ? ? # 根據(jù)當(dāng)前的圖片的方向獲取圖片 surface
? ? ? ? self.image = self.images[self.direction]
? ? ? ? # 獲取區(qū)域根據(jù)圖片
? ? ? ? self.rect = self.image.get_rect()
? ? ? ? # 設(shè)置區(qū)域的left 和 top
? ? ? ? self.rect.left = left
? ? ? ? self.rect.top = top
? ? ? ? # 調(diào)整速度,決定移動(dòng)速度的快慢
? ? ? ? self.speed = 1
? ? ? ? # 坦克移動(dòng)的開關(guān)
? ? ? ? self.stop = True
?
? ? def move(self):
? ? ? ? # 判斷坦克的方向進(jìn)行移動(dòng)
? ? ? ? if self.direction == 'L':
? ? ? ? ? ? if self.rect.left > 0:
? ? ? ? ? ? ? ? self.rect.left -= self.speed
? ? ? ? elif self.direction == 'U':
? ? ? ? ? ? if self.rect.top > 0:
? ? ? ? ? ? ? ? self.rect.top -= self.speed
? ? ? ? elif self.direction == 'D':
? ? ? ? ? ? if self.rect.top + self.rect.height < SCREEN_HEIGHT:
? ? ? ? ? ? ? ? self.rect.top += self.speed
? ? ? ? elif self.direction == 'R':
? ? ? ? ? ? if self.rect.top + self.rect.height < SCREEN_WIDTH:
? ? ? ? ? ? ? ? self.rect.left += self.speed
?
? ? def shot(self):
? ? ? ? pass
?
? ? # 展示坦克的方法
?
? ? def displayTake(self):
? ? ? ? # 獲取展示的對(duì)象
? ? ? ? self.image = self.images[self.direction]
? ? ? ? # 調(diào)用blit方法
? ? ? ? Maingame.window.blit(self.image, self.rect)
?
?
# 我方的坦克
class MyTanke(Tanke):
? ? def __init__(self):
? ? ? ? pass
?
?
# 敵方的坦克
class EnemyTanke(Tanke):
? ? def __init__(self, left, top, speed):
? ? ? ? # 坦克是需要加載圖片的
? ? ? ? self.images = {
? ? ? ? ? ? 'U': pygame.image.load('坦克2/enemy1U.gif'),
? ? ? ? ? ? 'D': pygame.image.load('坦克2/enemy1D.gif'),
? ? ? ? ? ? 'L': pygame.image.load('坦克2/enemy1L.gif'),
? ? ? ? ? ? 'R': pygame.image.load('坦克2/enemy1R.gif'),
? ? ? ? }
? ? ? ? # 需要定義一個(gè)方向
? ? ? ? self.direction = self.randDirection()
? ? ? ? # 根據(jù)方向獲取image獲取當(dāng)前的圖片
? ? ? ? self.image = self.images[self.direction]
? ? ? ? # 區(qū)域
? ? ? ? self.rect = self.image.get_rect()
? ? ? ? # 對(duì)left以及top進(jìn)行賦值
? ? ? ? self.rect.left = left
? ? ? ? self.rect.top = top
? ? ? ? # 速度
? ? ? ? self.speed = speed
? ? ? ? # 移動(dòng)開關(guān)
? ? ? ? self.flag = True
?
? ? def randDirection(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'
?
?
class Bullet():
? ? def __init__(self):
? ? ? ? pass
?
? ? def move(self):
? ? ? ? pass
?
? ? def displayBullet(self):
? ? ? ? pass
?
?
class Wall():
? ? def __init__(self):
? ? ? ? pass
?
? ? def displayWall(self):
? ? ? ? pass
?
?
class Explode():
? ? def __init__(self):
? ? ? ? pass
?
? ? def displayExplore(self):
? ? ? ? pass
?
?
class Music():
? ? def __init__(self):
? ? ? ? pass
?
? ? # 播放音樂(lè)
? ? def play(self):
? ? ? ? pass
?
?
if __name__ == '__main__':文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-849411.html
? ? Maingame().startgame()文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-849411.html
到了這里,關(guān)于使用python的pygame做一個(gè)簡(jiǎn)易的坦克大戰(zhàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!