python实现双人版坦克大战游戏

游戏介绍: 双人版的《坦克大战》的基本规则是玩家消灭出现的敌方坦克保卫我方基地。
中间还会随机出现很多特殊道具吸收可获得相应的功能,消灭玩即可进入下一关。
方向键:上下左右移动即可。另一个方向键则是:WSAD。
环境配置: Python3、 Pycharm 、Pygame。
第三方库的安装:pipinstall pygame
效果展示: 开始界面一一
开始即可上手玩游戏,还有背景音乐辅助!游戏玩儿起来更带感!
python实现双人版坦克大战游戏
文章图片

游戏界面——
python实现双人版坦克大战游戏
文章图片

代码演示: 1)游戏主程序

import pygameimport sysimport tracebackimport wallimport myTankimport enemyTankimport food def main():pygame.init()pygame.mixer.init()resolution = 630, 630screen = pygame.display.set_mode(resolution)pygame.display.set_caption("Tank War ")# 加载图片,音乐,音效.background_image= pygame.image.load(r"..\image\background.png")home_image= pygame.image.load(r"..\image\home.png")home_destroyed_image = pygame.image.load(r"..\image\home_destroyed.png")bang_sound= pygame.mixer.Sound(r"..\music\bang.wav")bang_sound.set_volume(1)fire_sound= pygame.mixer.Sound(r"..\music\Gunfire.wav")start_sound= pygame.mixer.Sound(r"..\music\start.wav")start_sound.play()# 定义精灵组:坦克,我方坦克,敌方坦克,敌方子弹allTankGroup= pygame.sprite.Group()mytankGroup= pygame.sprite.Group()allEnemyGroup= pygame.sprite.Group()redEnemyGroup= pygame.sprite.Group()greenEnemyGroup= pygame.sprite.Group()otherEnemyGroup= pygame.sprite.Group()enemyBulletGroup = pygame.sprite.Group()# 创建地图 bgMap = wall.Map()# 创建食物/道具 但不显示prop = food.Food()# 创建我方坦克myTank_T1 = myTank.MyTank(1)allTankGroup.add(myTank_T1)mytankGroup.add(myTank_T1)myTank_T2 = myTank.MyTank(2)allTankGroup.add(myTank_T2)mytankGroup.add(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)continueif enemy.kind == 3:greenEnemyGroup.add(enemy)continueotherEnemyGroup.add(enemy)# 敌军坦克出现动画appearance_image = pygame.image.load(r"..\image\appear.png").convert_alpha()appearance = []appearance.append(appearance_image.subsurface(( 0, 0), (48, 48)))appearance.append(appearance_image.subsurface((48, 0), (48, 48)))appearance.append(appearance_image.subsurface((96, 0), (48, 48)))# 自定义事件# 创建敌方坦克延迟200DELAYEVENT = pygame.constants.USEREVENTpygame.time.set_timer(DELAYEVENT, 200)# 创建 敌方 子弹延迟1000ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1pygame.time.set_timer(ENEMYBULLETNOTCOOLINGEVENT, 1000)# 创建 我方 子弹延迟200MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2pygame.time.set_timer(MYBULLETNOTCOOLINGEVENT, 200)# 敌方坦克 静止8000NOTMOVEEVENT = pygame.constants.USEREVENT + 3pygame.time.set_timer(NOTMOVEEVENT, 8000)delay = 100moving = 0movdir = 0moving2 = 0movdir2 = 0enemyNumber = 3enemyCouldMove= Trueswitch_R1_R2_image= TruehomeSurvive= Truerunning_T1= Truerunning_T2= Trueclock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()# 我方子弹冷却事件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 pygame.sprite.spritecollide(enemy, allTankGroup, False, None):breakallEnemyGroup.add(enemy)allTankGroup.add(enemy)enemyNumber += 1if enemy.isred == True:redEnemyGroup.add(enemy)elif enemy.kind == 3:greenEnemyGroup.add(enemy)else:otherEnemyGroup.add(enemy)if event.type == pygame.KEYDOWN:if event.key == pygame.K_c and pygame.KMOD_CTRL:pygame.quit()sys.exit()if event.key == pygame.K_e:myTank_T1.levelUp()if event.key == pygame.K_q:myTank_T1.levelDown()if event.key == pygame.K_3:myTank_T1.levelUp()myTank_T1.levelUp()myTank_T1.level = 3if event.key == pygame.K_2:if myTank_T1.speed == 3:myTank_T1.speed = 24else:myTank_T1.speed = 3if event.key == pygame.K_1: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 * 24bgMap.brickGroup.add(bgMap.brick)if event.key == pygame.K_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 * 24bgMap.ironGroup.add(bgMap.iron) # 检查用户的键盘操作key_pressed = pygame.key.get_pressed()# 玩家一的移动操作if moving:moving -= 1if movdir == 0:allTankGroup.remove(myTank_T1)if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving += 1allTankGroup.add(myTank_T1)running_T1 = Trueif movdir == 1:allTankGroup.remove(myTank_T1)if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving += 1allTankGroup.add(myTank_T1)running_T1 = Trueif movdir == 2:allTankGroup.remove(myTank_T1)if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving += 1allTankGroup.add(myTank_T1)running_T1 = Trueif movdir == 3:allTankGroup.remove(myTank_T1)if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving += 1allTankGroup.add(myTank_T1)running_T1 = Trueif not moving:if key_pressed[pygame.K_w]:moving = 7movdir = 0running_T1 = TrueallTankGroup.remove(myTank_T1)if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving = 0allTankGroup.add(myTank_T1)elif key_pressed[pygame.K_s]:moving = 7movdir = 1running_T1 = TrueallTankGroup.remove(myTank_T1)if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving = 0allTankGroup.add(myTank_T1)elif key_pressed[pygame.K_a]:moving = 7movdir = 2running_T1 = TrueallTankGroup.remove(myTank_T1)if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving = 0allTankGroup.add(myTank_T1)elif key_pressed[pygame.K_d]:moving = 7movdir = 3running_T1 = TrueallTankGroup.remove(myTank_T1)if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):moving = 0allTankGroup.add(myTank_T1)if key_pressed[pygame.K_j]:if not myTank_T1.bullet.life and myTank_T1.bulletNotCooling:fire_sound.play()myTank_T1.shoot()myTank_T1.bulletNotCooling = False# 玩家二的移动操作if moving2:moving2 -= 1if movdir2 == 0:allTankGroup.remove(myTank_T2)myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)running_T2 = Trueif movdir2 == 1:allTankGroup.remove(myTank_T2)myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)running_T2 = Trueif movdir2 == 2:allTankGroup.remove(myTank_T2)myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)running_T2 = Trueif movdir2 == 3:allTankGroup.remove(myTank_T2)myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)running_T2 = Trueif not moving2:if key_pressed[pygame.K_UP]:allTankGroup.remove(myTank_T2)myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)moving2 = 7movdir2 = 0running_T2 = Trueelif key_pressed[pygame.K_DOWN]:allTankGroup.remove(myTank_T2)myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)moving2 = 7movdir2 = 1running_T2 = Trueelif key_pressed[pygame.K_LEFT]:allTankGroup.remove(myTank_T2)myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)moving2 = 7movdir2 = 2running_T2 = Trueelif key_pressed[pygame.K_RIGHT]:allTankGroup.remove(myTank_T2)myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(myTank_T2)moving2 = 7movdir2 = 3running_T2 = Trueif key_pressed[pygame.K_KP0]:if not myTank_T2.bullet.life:# fire_sound.play()myTank_T2.shoot()# 画背景screen.blit(background_image, (0, 0))# 画砖块for each in bgMap.brickGroup:screen.blit(each.image, each.rect)# 花石头for each in bgMap.ironGroup:screen.blit(each.image, each.rect)# 画homeif homeSurvive:screen.blit(home_image, (3 + 12 * 24, 3 + 24 * 24))else:screen.blit(home_destroyed_image, (3 + 12 * 24, 3 + 24 * 24))# 画我方坦克1if not (delay % 5):switch_R1_R2_image = not switch_R1_R2_imageif switch_R1_R2_image and running_T1:screen.blit(myTank_T1.tank_R0, (myTank_T1.rect.left, myTank_T1.rect.top))running_T1 = Falseelse:screen.blit(myTank_T1.tank_R1, (myTank_T1.rect.left, myTank_T1.rect.top))# 画我方坦克2if switch_R1_R2_image and running_T2:screen.blit(myTank_T2.tank_R0, (myTank_T2.rect.left, myTank_T2.rect.top))running_T2 = Falseelse:screen.blit(myTank_T2.tank_R1, (myTank_T2.rect.left, myTank_T2.rect.top))# 画敌方坦克for each in allEnemyGroup:# 判断5毛钱特效是否播放if each.flash:# 判断画左动作还是右动作if switch_R1_R2_image:screen.blit(each.tank_R0, (each.rect.left, each.rect.top))if enemyCouldMove:allTankGroup.remove(each)each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)allTankGroup.add(each)else:screen.blit(each.tank_R1, (each.rect.left, 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 -= 1if each.times <= 10:screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))elif each.times <= 20:screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))elif each.times <= 30:screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))elif each.times <= 40:screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))elif each.times <= 50:screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))elif each.times <= 60:screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))elif each.times <= 70:screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))elif each.times <= 80:screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))elif each.times <= 90:screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))if each.times == 0:each.flash = True# 绘制我方子弹1if myTank_T1.bullet.life:myTank_T1.bullet.move()screen.blit(myTank_T1.bullet.bullet, myTank_T1.bullet.rect)# 子弹 碰撞 子弹for each in enemyBulletGroup:if each.life:if pygame.sprite.collide_rect(myTank_T1.bullet, each):myTank_T1.bullet.life = Falseeach.life = Falsepygame.sprite.spritecollide(myTank_T1.bullet, enemyBulletGroup, True, None)# 子弹 碰撞 敌方坦克if pygame.sprite.spritecollide(myTank_T1.bullet, redEnemyGroup, True, None):prop.change()bang_sound.play()enemyNumber -= 1myTank_T1.bullet.life = Falseelif pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, False, None):for each in greenEnemyGroup:if pygame.sprite.collide_rect(myTank_T1.bullet, each):if each.life == 1:pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, True, None)bang_sound.play()enemyNumber -= 1elif each.life == 2:each.life -= 1each.tank = each.enemy_3_0elif each.life == 3:each.life -= 1each.tank = each.enemy_3_2myTank_T1.bullet.life = Falseelif pygame.sprite.spritecollide(myTank_T1.bullet, otherEnemyGroup, True, None):bang_sound.play()enemyNumber -= 1myTank_T1.bullet.life = False#if pygame.sprite.spritecollide(myTank_T1.bullet, allEnemyGroup, True, None):#bang_sound.play()#enemyNumber -= 1#myTank_T1.bullet.life = False# 子弹 碰撞 brickGroupif pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.brickGroup, True, None):myTank_T1.bullet.life = FalsemyTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24# 子弹 碰撞 brickGroupif myTank_T1.bullet.strong:if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, True, None):myTank_T1.bullet.life = FalsemyTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24else:if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, False, None):myTank_T1.bullet.life = FalsemyTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24# 绘制我方子弹2if myTank_T2.bullet.life:myTank_T2.bullet.move()screen.blit(myTank_T2.bullet.bullet, myTank_T2.bullet.rect)# 子弹 碰撞 敌方坦克if pygame.sprite.spritecollide(myTank_T2.bullet, allEnemyGroup, True, None):bang_sound.play()enemyNumber -= 1myTank_T2.bullet.life = False# 子弹 碰撞 brickGroupif pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.brickGroup, True, None):myTank_T2.bullet.life = FalsemyTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24# 子弹 碰撞 brickGroupif myTank_T2.bullet.strong:if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, True, None):myTank_T2.bullet.life = FalsemyTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24else:if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, False, None):myTank_T2.bullet.life = FalsemyTank_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:enemyBulletGroup.remove(each.bullet)each.shoot()enemyBulletGroup.add(each.bullet)each.bulletNotCooling = False# 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹if each.flash:if each.bullet.life:# 如果敌人可以移动if enemyCouldMove:each.bullet.move()screen.blit(each.bullet.bullet, each.bullet.rect)# 子弹 碰撞 我方坦克if pygame.sprite.collide_rect(each.bullet, myTank_T1):bang_sound.play()myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24 each.bullet.life = Falsemoving = 0# 重置移动控制参数for i in range(myTank_T1.level+1):myTank_T1.levelDown()if pygame.sprite.collide_rect(each.bullet, myTank_T2):bang_sound.play()myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24 each.bullet.life = False# 子弹 碰撞 brickGroupif pygame.sprite.spritecollide(each.bullet, bgMap.brickGroup, True, None):each.bullet.life = False# 子弹 碰撞 ironGroupif each.bullet.strong:if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, True, None):each.bullet.life = Falseelse:if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, False, None):each.bullet.life = False# 最后画食物/道具if prop.life:screen.blit(prop.image, prop.rect)# 我方坦克碰撞 食物/道具if pygame.sprite.collide_rect(myTank_T1, prop):if prop.kind == 1:# 敌人全毁for each in allEnemyGroup:if pygame.sprite.spritecollide(each, allEnemyGroup, True, None):bang_sound.play()enemyNumber -= 1prop.life = Falseif prop.kind == 2:# 敌人静止enemyCouldMove = Falseprop.life = Falseif prop.kind == 3:# 子弹增强myTank_T1.bullet.strong = Trueprop.life = Falseif prop.kind == 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 * 24bgMap.ironGroup.add(bgMap.iron)prop.life = Falseif prop.kind == 5:# 坦克无敌prop.life = Falsepassif prop.kind == 6:# 坦克升级myTank_T1.levelUp()prop.life = Falseif prop.kind == 7:# 坦克生命+1myTank_T1.life += 1prop.life = False# 延迟delay -= 1if not delay:delay = 100pygame.display.flip()clock.tick(60)if __name__ == "__main__":try:main()except SystemExit:passexcept:traceback.print_exc()pygame.quit()input()

2)随机出现的特殊道具
import pygameimport random class Food(pygame.sprite.Sprite):def __init__(self):self.food_boom= pygame.image.load(r"..\image\food_boom.png").convert_alpha()self.food_clock= pygame.image.load(r"..\image\food_clock.png").convert_alpha()self.food_gun= pygame.image.load(r"..\image\food_gun.png").convert_alpha()self.food_iron= pygame.image.load(r"..\image\food_iron.png").convert_alpha()self.food_protect = pygame.image.load(r"..\image\food_protect.png").convert_alpha()self.food_star= pygame.image.load(r"..\image\food_star.png").convert_alpha()self.food_tank= pygame.image.load(r"..\image\food_tank.png").convert_alpha()self.kind = random.choice([1, 2, 3, 4, 5, 6, 7])if self.kind == 1:self.image = self.food_boomelif self.kind == 2:self.image = self.food_clockelif self.kind == 3:self.image = self.food_gunelif self.kind == 4:self.image = self.food_ironelif self.kind == 5:self.image = self.food_protectelif self.kind == 6:self.image = self.food_starelif self.kind == 7:self.image = self.food_tankself.rect = self.image.get_rect()self.rect.left = self.rect.top = random.randint(100, 500)self.life = Falsedef change(self):self.kind = random.choice([1, 2, 3, 4, 5, 6, 7])if self.kind == 1:self.image = self.food_boomelif self.kind == 2:self.image = self.food_clockelif self.kind == 3:self.image = self.food_gunelif self.kind == 4:self.image = self.food_ironelif self.kind == 5:self.image = self.food_protectelif self.kind == 6:self.image = self.food_starelif self.kind == 7:self.image = self.food_tankself.rect.left = self.rect.top = random.randint(100, 500)self.life = True

3)地图界面
import pygame brickImage= r"..\image\brick.png"ironImage= r"..\image\iron.png" class Brick(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load(brickImage)self.rect = self.image.get_rect()class Iron(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load(ironImage)self.rect = self.image.get_rect()class Map():def __init__(self):self.brickGroup = pygame.sprite.Group()self.ironGroup= pygame.sprite.Group()# 数字代表地图中的位置# 画砖块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:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brickGroup.add(self.brick)for x in X28:for y in Y28:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brickGroup.add(self.brick)for x in X46:for y in Y46:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brickGroup.add(self.brick)for x in X5:for y in Y5:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brickGroup.add(self.brick)for x, y in X0Y0:self.brick = Brick()self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24self.brickGroup.add(self.brick)# 画石头for x, y in [(0,14),(1,14),(12,6),(13,6),(12,7),(13,7),(24,14),(25,14)]:self.iron = Iron()self.iron.rect.left, self.iron.rect.top = 3 + x * 24, 3 + y * 24self.ironGroup.add(self.iron)

【python实现双人版坦克大战游戏】到此这篇关于python实现双人版坦克大战游戏的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读