python实现双人版坦克大战游戏
程序员文章站
2022-06-15 16:11:05
游戏介绍:双人版的《坦克大战》的基本规则是玩家消灭出现的敌方坦克保卫我方基地。中间还会随机出现很多特殊道具吸收可获得相应的功能,消灭玩即可进入下一关。方向键:上下左右移动即可。另一个方向键则是:wsa...
游戏介绍:
双人版的《坦克大战》的基本规则是玩家消灭出现的敌方坦克保卫我方基地。
中间还会随机出现很多特殊道具吸收可获得相应的功能,消灭玩即可进入下一关。
方向键:上下左右移动即可。另一个方向键则是:wsad。
环境配置:
python3、 pycharm 、pygame。
第三方库的安装:pip install pygame
效果展示:
开始界面一一
开始即可上手玩游戏,还有背景音乐辅助!游戏玩儿起来更带感!
游戏界面——
代码演示:
1)游戏主程序
import pygame import sys import traceback import wall import mytank import enemytank import food def main(): pygame.init() pygame.mixer.init() resolution = 630, 630 screen = 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) continue if enemy.kind == 3: greenenemygroup.add(enemy) continue otherenemygroup.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))) # 自定义事件 # 创建敌方坦克延迟200 delayevent = pygame.constants.userevent pygame.time.set_timer(delayevent, 200) # 创建 敌方 子弹延迟1000 enemybulletnotcoolingevent = pygame.constants.userevent + 1 pygame.time.set_timer(enemybulletnotcoolingevent, 1000) # 创建 我方 子弹延迟200 mybulletnotcoolingevent = pygame.constants.userevent + 2 pygame.time.set_timer(mybulletnotcoolingevent, 200) # 敌方坦克 静止8000 notmoveevent = pygame.constants.userevent + 3 pygame.time.set_timer(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 = 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): break allenemygroup.add(enemy) alltankgroup.add(enemy) enemynumber += 1 if 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 = 3 if event.key == pygame.k_2: if mytank_t1.speed == 3: mytank_t1.speed = 24 else: mytank_t1.speed = 3 if 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 * 24 bgmap.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 * 24 bgmap.irongroup.add(bgmap.iron) # 检查用户的键盘操作 key_pressed = pygame.key.get_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[pygame.k_w]: 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[pygame.k_s]: 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[pygame.k_a]: 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[pygame.k_d]: 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[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 -= 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[pygame.k_up]: 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[pygame.k_down]: 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[pygame.k_left]: 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[pygame.k_right]: 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[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) # 画home if homesurvive: screen.blit(home_image, (3 + 12 * 24, 3 + 24 * 24)) else: screen.blit(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: screen.blit(mytank_t1.tank_r0, (mytank_t1.rect.left, mytank_t1.rect.top)) running_t1 = false else: screen.blit(mytank_t1.tank_r1, (mytank_t1.rect.left, mytank_t1.rect.top)) # 画我方坦克2 if switch_r1_r2_image and running_t2: screen.blit(mytank_t2.tank_r0, (mytank_t2.rect.left, mytank_t2.rect.top)) running_t2 = false else: 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 -= 1 if 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 # 绘制我方子弹1 if 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 = false each.life = false pygame.sprite.spritecollide(mytank_t1.bullet, enemybulletgroup, true, none) # 子弹 碰撞 敌方坦克 if pygame.sprite.spritecollide(mytank_t1.bullet, redenemygroup, true, none): prop.change() bang_sound.play() enemynumber -= 1 mytank_t1.bullet.life = false elif 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 -= 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 pygame.sprite.spritecollide(mytank_t1.bullet, otherenemygroup, true, none): bang_sound.play() enemynumber -= 1 mytank_t1.bullet.life = false #if pygame.sprite.spritecollide(mytank_t1.bullet, allenemygroup, true, none): # bang_sound.play() # enemynumber -= 1 # mytank_t1.bullet.life = false # 子弹 碰撞 brickgroup if pygame.sprite.spritecollide(mytank_t1.bullet, 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 pygame.sprite.spritecollide(mytank_t1.bullet, 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 pygame.sprite.spritecollide(mytank_t1.bullet, 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() screen.blit(mytank_t2.bullet.bullet, mytank_t2.bullet.rect) # 子弹 碰撞 敌方坦克 if pygame.sprite.spritecollide(mytank_t2.bullet, allenemygroup, true, none): bang_sound.play() enemynumber -= 1 mytank_t2.bullet.life = false # 子弹 碰撞 brickgroup if pygame.sprite.spritecollide(mytank_t2.bullet, 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 pygame.sprite.spritecollide(mytank_t2.bullet, 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 pygame.sprite.spritecollide(mytank_t2.bullet, 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: 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 = false moving = 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 # 子弹 碰撞 brickgroup if pygame.sprite.spritecollide(each.bullet, bgmap.brickgroup, true, none): each.bullet.life = false # 子弹 碰撞 irongroup if each.bullet.strong: if pygame.sprite.spritecollide(each.bullet, bgmap.irongroup, true, none): each.bullet.life = false else: 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 -= 1 prop.life = false if prop.kind == 2: # 敌人静止 enemycouldmove = false prop.life = false if prop.kind == 3: # 子弹增强 mytank_t1.bullet.strong = true prop.life = false if 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 * 24 bgmap.irongroup.add(bgmap.iron) prop.life = false if prop.kind == 5: # 坦克无敌 prop.life = false pass if prop.kind == 6: # 坦克升级 mytank_t1.levelup() prop.life = false if prop.kind == 7: # 坦克生命+1 mytank_t1.life += 1 prop.life = false # 延迟 delay -= 1 if not delay: delay = 100 pygame.display.flip() clock.tick(60) if __name__ == "__main__": try: main() except systemexit: pass except: traceback.print_exc() pygame.quit() input()
2)随机出现的特殊道具
import pygame import 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_boom elif self.kind == 2: self.image = self.food_clock elif self.kind == 3: self.image = self.food_gun elif self.kind == 4: self.image = self.food_iron elif self.kind == 5: self.image = self.food_protect elif self.kind == 6: self.image = self.food_star elif self.kind == 7: self.image = self.food_tank self.rect = self.image.get_rect() self.rect.left = self.rect.top = random.randint(100, 500) self.life = false def change(self): self.kind = random.choice([1, 2, 3, 4, 5, 6, 7]) if self.kind == 1: self.image = self.food_boom elif self.kind == 2: self.image = self.food_clock elif self.kind == 3: self.image = self.food_gun elif self.kind == 4: self.image = self.food_iron elif self.kind == 5: self.image = self.food_protect elif self.kind == 6: self.image = self.food_star elif self.kind == 7: self.image = self.food_tank self.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 * 24 self.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 * 24 self.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 * 24 self.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 * 24 self.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 * 24 self.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 * 24 self.irongroup.add(self.iron)
到此这篇关于python实现双人版坦克大战游戏的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。