前言
嘿!我是栗子同学,欢迎大家阅读这篇《坦克大战双人版游戏编程》。
我相信这个游戏的话对于大家来说可能会比较难,刚开始学习的话这个代码就直接发给大家玩一玩的哈,但是大家可以看看这个代码啦!
当然基础好的话可以考虑看完后尝试做一个类似的游戏,然后遇到不懂的地方可以来我的粉丝裙,
没有基础的同学也不用担心:偶尔有Python免费的在线直播互动课哟,大家可以跟着我一起学习,我带大家入门。
好啦,总之我所有的文章源码+素材都是可以免费拿的,需要的滴滴我哈!也欢迎大家阅读往期的文章。
《坦克大战双人版》
游戏介绍:
双人版的《坦克大战》的基本规则是玩家消灭出现的敌方坦克保卫我方基地。
中间还会随机出现很多特殊道具吸收可获得相应的功能,消灭完即可进入下一关。
方向键:上下左右移动即可。另一个方向键则是:WSAD。
环境配置:
Python3、 Pycharm 、Pygame。
第三方库的安装:pip install pygame
效果展示:
开始界面一一
开始即可上手玩游戏,还有背景音乐辅助!游戏玩起来更带感!
游戏界面——
代码演示:
1)游戏主程序
import pygame
import sys
import traceback
import wall
import myTank
import enemyTank
import food
def main():
()
()
resolution = 630, 630
screen = (resolution)
("Tank War ")
# 加载图片,音乐,音效.
background_image = pygame.image.load(r"..\image\background.png")
home_image = pygame.image.load(r"..\image\;)
home_destroyed_image = pygame.image.load(r"..\image\;)
bang_sound = (r"..\music\bang.wav")
bang_(1)
fire_sound = (r"..\music\Gun;)
start_sound = (r"..\music\;)
()
# 定义精灵组:坦克,我方坦克,敌方坦克,敌方子弹
allTankGroup = ()
mytankGroup = ()
allEnemyGroup = ()
redEnemyGroup = ()
greenEnemyGroup = ()
otherEnemyGroup = ()
enemyBulletGroup = ()
# 创建地图
bgMap = wall.Map()
# 创建食物/道具 但不显示
prop = ()
# 创建我方坦克
myTank_T1 = myTank.MyTank(1)
allTankGroup.add(myTank_T1)
my(myTank_T1)
myTank_T2 = myTank.MyTank(2)
allTankGroup.add(myTank_T2)
my(myTank_T2)
# 创建敌方 坦克
for i in range(1, 4):
enemy = enemyTank.EnemyTank(i)
allTankGroup.add(enemy)
allEnemyGroup.add(enemy)
if enemy.isred == True:
redEnemyGroup.add(enemy)
continue
if enemy.kind == 3:
greenEnemyGroup.add(enemy)
continue
o(enemy)
# 敌军坦克出现动画
appearance_image = pygame.image.load(r"..\image\a;).convert_alpha()
appearance = []
a(appearance_image.subsurface(( 0, 0), (48, 48)))
a(appearance_image.subsurface((48, 0), (48, 48)))
a(appearance_image.subsurface((96, 0), (48, 48)))
# 自定义事件
# 创建敌方坦克延迟200
DELAYEVENT =
(DELAYEVENT, 200)
# 创建 敌方 子弹延迟1000
ENEMYBULLETNOTCOOLINGEVENT = + 1
(ENEMYBULLETNOTCOOLINGEVENT, 1000)
# 创建 我方 子弹延迟200
MYBULLETNOTCOOLINGEVENT = + 2
(MYBULLETNOTCOOLINGEVENT, 200)
# 敌方坦克 静止8000
NOTMOVEEVENT = + 3
(NOTMOVEEVENT, 8000)
delay = 100
moving = 0
movdir = 0
moving2 = 0
movdir2 = 0
enemyNumber = 3
enemyCouldMove = True
switch_R1_R2_image = True
homeSurvive = True
running_T1 = True
running_T2 = True
clock = ()
while True:
for event in ():
if event.type == :
()
()
# 我方子弹冷却事件
if event.type == MYBULLETNOTCOOLINGEVENT:
myTank_T1.bulletNotCooling = True
# 敌方子弹冷却事件
if event.type == ENEMYBULLETNOTCOOLINGEVENT:
for each in allEnemyGroup:
each.bulletNotCooling = True
# 敌方坦克静止事件
if event.type == NOTMOVEEVENT:
enemyCouldMove = True
# 创建敌方坦克延迟
if event.type == DELAYEVENT:
if enemyNumber < 4:
enemy = enemyTank.EnemyTank()
if (enemy, allTankGroup, False, None):
break
allEnemyGroup.add(enemy)
allTankGroup.add(enemy)
enemyNumber += 1
if enemy.isred == True:
redEnemyGroup.add(enemy)
elif enemy.kind == 3:
greenEnemyGroup.add(enemy)
else:
o(enemy)
if event.type == :
if event.key == and :
()
()
if event.key == :
myTank_T1.levelUp()
if event.key == :
myTank_T1.levelDown()
if event.key == :
myTank_T1.levelUp()
myTank_T1.levelUp()
myTank_T1.level = 3
if event.key == :
if myTank_T1.speed == 3:
myTank_T1.speed = 24
else:
myTank_T1.speed = 3
if event.key == :
for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
bgMap.brick = wall.Brick()
bgMap.brick.rect.left, bgMap.brick.rect.top = 3 + x * 24, 3 + y * 24
bgMap.brickGroup.add)
if event.key == :
for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
bgMap.iron = wall.Iron()
bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24
bgMap.ironGroup.add)
# 检查用户的键盘操作
key_pressed = ()
# 玩家一的移动操作
if moving:
moving -= 1
if movdir == 0:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if movdir == 1:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if movdir == 2:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if movdir == 3:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if not moving:
if key_pressed[]:
moving = 7
movdir = 0
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
elif key_pressed[]:
moving = 7
movdir = 1
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
elif key_pressed[]:
moving = 7
movdir = 2
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
elif key_pressed[]:
moving = 7
movdir = 3
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
if key_pressed[]:
if not myTank_T1.bullet.life and myTank_T1.bulletNotCooling:
()
myTank_T1.shoot()
myTank_T1.bulletNotCooling = False
# 玩家二的移动操作
if moving2:
moving2 -= 1
if movdir2 == 0:
allTankGroup.remove(myTank_T2)
myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if movdir2 == 1:
allTankGroup.remove(myTank_T2)
myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if movdir2 == 2:
allTankGroup.remove(myTank_T2)
myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if movdir2 == 3:
allTankGroup.remove(myTank_T2)
myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if not moving2:
if key_pressed[]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 0
running_T2 = True
elif key_pressed[]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 1
running_T2 = True
elif key_pressed[]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 2
running_T2 = True
elif key_pressed[]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 3
running_T2 = True
if key_pressed[]:
if not myTank_T2.bullet.life:
# ()
myTank_T2.shoot()
# 画背景
(background_image, (0, 0))
# 画砖块
for each in bgMap.brickGroup:
(each.image, each.rect)
# 花石头
for each in bgMap.ironGroup:
(each.image, each.rect)
# 画home
if homeSurvive:
(home_image, (3 + 12 * 24, 3 + 24 * 24))
else:
(home_destroyed_image, (3 + 12 * 24, 3 + 24 * 24))
# 画我方坦克1
if not (delay % 5):
switch_R1_R2_image = not switch_R1_R2_image
if switch_R1_R2_image and running_T1:
, , myTank_T1.rect.top))
running_T1 = False
else:
, , myTank_T1.rect.top))
# 画我方坦克2
if switch_R1_R2_image and running_T2:
, , myTank_T2.rect.top))
running_T2 = False
else:
, , myTank_T2.rect.top))
# 画敌方坦克
for each in allEnemyGroup:
# 判断5毛钱特效是否播放
if each.flash:
# 判断画左动作还是右动作
if switch_R1_R2_image:
, , each.rect.top))
if enemyCouldMove:
allTankGroup.remove(each)
each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(each)
else:
, , each.rect.top))
if enemyCouldMove:
allTankGroup.remove(each)
each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(each)
else:
# 播放5毛钱特效
if each.times > 0:
each.times -= 1
if each.times <= 10:
(appearance[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 20:
(appearance[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 30:
(appearance[0], (3 + each.x * 12 * 24, 3))
elif each.times <= 40:
(appearance[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 50:
(appearance[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 60:
(appearance[0], (3 + each.x * 12 * 24, 3))
elif each.times <= 70:
(appearance[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 80:
(appearance[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 90:
(appearance[0], (3 + each.x * 12 * 24, 3))
if each.times == 0:
each.flash = True
# 绘制我方子弹1
if myTank_T1.bullet.life:
myTank_T1.bullet.move()
, myTank_T1.bullet.rect)
# 子弹 碰撞 子弹
for each in enemyBulletGroup:
if each.life:
if , each):
myTank_T1.bullet.life = False
each.life = False
, enemyBulletGroup, True, None)
# 子弹 碰撞 敌方坦克
if , redEnemyGroup, True, None):
()
bang_()
enemyNumber -= 1
myTank_T1.bullet.life = False
elif ,greenEnemyGroup, False, None):
for each in greenEnemyGroup:
if , each):
if each.life == 1:
,greenEnemyGroup, True, None)
bang_()
enemyNumber -= 1
elif each.life == 2:
each.life -= 1
each.tank = each.enemy_3_0
elif each.life == 3:
each.life -= 1
each.tank = each.enemy_3_2
myTank_T1.bullet.life = False
elif , otherEnemyGroup, True, None):
bang_()
enemyNumber -= 1
myTank_T1.bullet.life = False
#if , allEnemyGroup, True, None):
# bang_()
# enemyNumber -= 1
# myTank_T1.bullet.life = False
# 子弹 碰撞 brickGroup
if , bgMap.brickGroup, True, None):
myTank_T1.bullet.life = False
myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 子弹 碰撞 brickGroup
if myTank_T1.bullet.strong:
if , bgMap.ironGroup, True, None):
myTank_T1.bullet.life = False
myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
else:
if , bgMap.ironGroup, False, None):
myTank_T1.bullet.life = False
myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 绘制我方子弹2
if myTank_T2.bullet.life:
myTank_T2.bullet.move()
, myTank_T2.bullet.rect)
# 子弹 碰撞 敌方坦克
if , allEnemyGroup, True, None):
bang_()
enemyNumber -= 1
myTank_T2.bullet.life = False
# 子弹 碰撞 brickGroup
if , bgMap.brickGroup, True, None):
myTank_T2.bullet.life = False
myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 子弹 碰撞 brickGroup
if myTank_T2.bullet.strong:
if , bgMap.ironGroup, True, None):
myTank_T2.bullet.life = False
myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
else:
if , bgMap.ironGroup, False, None):
myTank_T2.bullet.life = False
myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 绘制敌人子弹
for each in allEnemyGroup:
# 如果子弹没有生命,则赋予子弹生命
if not each.bullet.life and each.bulletNotCooling and enemyCouldMove:
enemyBulle)
each.shoot()
enemyBulle)
each.bulletNotCooling = False
# 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹
if each.flash:
if each.bullet.life:
# 如果敌人可以移动
if enemyCouldMove:
each.bullet.move()
.bullet, each.bullet.rect)
# 子弹 碰撞 我方坦克
if , myTank_T1):
bang_()
myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24
each.bullet.life = False
moving = 0 # 重置移动控制参数
for i in range):
myTank_T1.levelDown()
if , myTank_T2):
bang_()
myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24
each.bullet.life = False
# 子弹 碰撞 brickGroup
if , bgMap.brickGroup, True, None):
each.bullet.life = False
# 子弹 碰撞 ironGroup
if each.bullet.strong:
if , bgMap.ironGroup, True, None):
each.bullet.life = False
else:
if , bgMap.ironGroup, False, None):
each.bullet.life = False
# 最后画食物/道具
if :
(prop.image, )
# 我方坦克碰撞 食物/道具
if (myTank_T1, prop):
if == 1: # 敌人全毁
for each in allEnemyGroup:
if (each, allEnemyGroup, True, None):
bang_()
enemyNumber -= 1
= False
if == 2: # 敌人静止
enemyCouldMove = False
= False
if == 3: # 子弹增强
myTank_T1.bullet.strong = True
= False
if == 4: # 家得到保护
for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
bgMap.iron = wall.Iron()
bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24
bgMap.ironGroup.add)
= False
if == 5: # 坦克无敌
= False
pass
if == 6: # 坦克升级
myTank_T1.levelUp()
= False
if == 7: # 坦克生命+1
myTank_T1.life += 1
= False
# 延迟
delay -= 1
if not delay:
delay = 100
()
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
()
()
input()
2)随机出现的特殊道具
import pygame
import random
class Food):
def __init__(self):
= pygame.image.load(r"..\image\;).convert_alpha()
= pygame.image.load(r"..\image\;).convert_alpha()
= pygame.image.load(r"..\image\;).convert_alpha()
= pygame.image.load(r"..\image\;).convert_alpha()
= pygame.image.load(r"..\image\;).convert_alpha()
= pygame.image.load(r"..\image\;).convert_alpha()
= pygame.image.load(r"..\image\;).convert_alpha()
= random.choice([1, 2, 3, 4, 5, 6, 7])
if == 1:
self.image =
elif == 2:
self.image =
elif == 3:
self.image =
elif == 4:
self.image =
elif == 5:
self.image =
elif == 6:
self.image =
elif == 7:
self.image =
= self.image.get_rect()
.left = .top = random.randint(100, 500)
= False
def change(self):
= random.choice([1, 2, 3, 4, 5, 6, 7])
if == 1:
self.image =
elif == 2:
self.image =
elif == 3:
self.image =
elif == 4:
self.image =
elif == 5:
self.image =
elif == 6:
self.image =
elif == 7:
self.image =
.left = .top = random.randint(100, 500)
= True
3)地图界面
import pygame
brickImage = r"..\image\brick.png"
ironImage = r"..\image\iron.png"
class Brick):
def __init__(self):
(self)
self.image = pygame.image.load(brickImage)
= self.image.get_rect()
class Iron):
def __init__(self):
(self)
self.image = pygame.image.load(ironImage)
= self.image.get_rect()
class Map():
def __init__(self):
= ()
= ()
# 数字代表地图中的位置
# 画砖块
X1379 = [2, 3, 6, 7, 18, 19, 22, 23]
Y1379 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 22, 23]
X28 = [10, 11, 14, 15]
Y28 = [2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16, 17, 18, 19, 20]
X46 = [4, 5, 6, 7, 18, 19, 20, 21]
Y46 = [13, 14]
X5 = [12, 13]
Y5 = [16, 17]
X0Y0 = [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]
for x in X1379:
for y in Y1379:
= Brick()
.rect.left, .rect.top = 3 + x * 24, 3 + y * 24
.add()
for x in X28:
for y in Y28:
= Brick()
.rect.left, .rect.top = 3 + x * 24, 3 + y * 24
.add()
for x in X46:
for y in Y46:
= Brick()
.rect.left, .rect.top = 3 + x * 24, 3 + y * 24
.add()
for x in X5:
for y in Y5:
= Brick()
.rect.left, .rect.top = 3 + x * 24, 3 + y * 24
.add()
for x, y in X0Y0:
= Brick()
.rect.left, .rect.top = 3 + x * 24, 3 + y * 24
.add()
# 画石头
for x, y in [(0,14),(1,14),(12,6),(13,6),(12,7),(13,7),(24,14),(25,14)]:
= Iron()
.rect.left, .rect.top = 3 + x * 24, 3 + y * 24
.add()
结尾
《坦克大作战》游戏小程序好玩吗?《坦克大作战》小游戏怎么玩?怎么进入《坦克大作战》?
点点关注,直接找我拿小游戏源码即可上手开完儿了吖~私信我也可以!
源码基地——
私信小编06或者点击这行蓝色字体即可免费获取哈!
往期推荐推荐——
项目1.1 GIF制作神奇(斗罗大陆为例)
【Python神器】推荐这款傻瓜式GIF制作工具,以后别再说不会了(好用到爆~)
项目2.7 圣诞快乐你的已送达!
【圣诞快乐】叮咚 没人送你圣诞礼物没有关系,可以找我啊?只要你开口 我都没有~
项目2.8 圣诞快乐!
【圣诞快乐】闻到圣诞的气息了,喜欢的节日在慢慢来临~祝大家Merry Christmas。
项目1.0 超级玛丽
程序员自制游戏:超级玛丽100%真实版,能把你玩哭了~【附源码】
项目1.1 扫雷
Pygame实战:据说这是史上最难扫雷游戏,没有之一,你们感受下......
文章汇总——
项目1.0 Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了
(更多内容+源码都在文章汇总哦!!欢迎阅读~)