导语

哈喽!哈喽——我是木木子

今天来升级下之前写的坦克大战游戏嘛,哈哈哈 其实也不算是修改,就是稍微的调试一下!​​

因为之前写的界面都是英文的 ,有的小伙伴儿英文一点儿都不会的可能看着别扭,今天来一款中

文版的给大家嘛!

俗话说的好:“雨露均沾”。哈哈哈.jpg

小简介:

《坦克大战》,1985年由日本开发商南梦宫(Namco)开发,是第一款可以双打的红白机游戏。

当时使用的还是小霸王。

很多小朋友以学习的名义买了以后偷偷打的打游戏还被家长发现了有 没得!

《坦克大战》红白机原版共有35关,每一关的地形和障碍都不同。图为原版坦克大战最后一关,你

有没有打通关过?(小声bb,我这个游戏水平可能达不到!)

正文​

1)游戏规则:

游戏过程是这样的,玩家操作坦克消灭电脑控制的坦克,并保护自己基地。基地图标是一只傲娇的张着翅膀的老鹰。小时候自

己失手把飞鹰轰成烧鸡的惨案经常发生。

双打的时候,为了看谁刷得分高,都争着打坦克,大本营的老鹰被烤熟了都不管。。

坦克大战中的宝贝有战车、星星、时钟等,小编当时最喜欢的是时钟,敌不能动我能动的感觉妙极了。图中的坦克图标吃了是

可以加一条命的,当时为了抢宝贝都抢先把队友的坦克打晕。。。

2)环境安装

Python3、Pycharm、Pygame、以及自带或自定义的模块。

pip install +模块名 或pip install -i https://pypi.douban.com/simple/ +模块名

3)代码演示​​

(之前不是写过的嘛,今天的话就是修改下的,这种小游戏代码肯定都很多的,所以这里直接贴主程序了。

需要完整的打包好的代码跟素材哪些的话 直接滴滴我即可或者看我主页左侧哪里有源码基地的哈!)

主程序:

import pygame
from pygame.locals import *
import sys
import scene
import bullet
import food
import tanks
import home# 开始界面显示
def show_start_interface(screen, width, height):tfont = pygame.font.Font('./font/simkai.ttf', width // 4)cfont = pygame.font.Font('./font/simkai.ttf', width // 20)title = tfont.render(u'坦克大战', True, (255, 0, 0))content1 = cfont.render(u'按1键进入单人游戏', True, (0, 244, 222))content2 = cfont.render(u'按2键进入双人人游戏', True, (0, 0, 255))#显示字体pygame.font.Font.render(text文本, antialias是否抗锯齿, color颜色, background=None)trect = title.get_rect()# 默认title左上角的坐标是 (0, 0)trect.midtop = (width / 2, height / 5)crect1 = content1.get_rect()crect1.midtop = (width / 2, height / 1.8)crect2 = content2.get_rect()crect2.midtop = (width / 2, height / 1.6)screen.blit(title, trect)screen.blit(content1, crect1)screen.blit(content2, crect2)# 在指定位置绘制指定文字对象pygame.display.update()# 更新界面while True:for event in pygame.event.get():if event.type == QUIT:sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_1:return 1if event.key == pygame.K_2:return 2# 结束界面显示
def show_end_interface(screen, width, height, is_win):bg_img = pygame.image.load("./images/others/background.png")screen.blit(bg_img, (0, 0))if is_win:font = pygame.font.Font('./font/simkai.ttf', width // 10)content = font.render(u'恭喜通关!', True, (255, 0, 0))rect = content.get_rect()rect.midtop = (width / 2, height / 2)screen.blit(content, rect)else:fail_img = pygame.image.load("./images/others/gameover.png")rect = fail_img.get_rect()rect.midtop = (width / 2, height / 2)screen.blit(fail_img, rect)pygame.display.update()while True:for event in pygame.event.get():if event.type == QUIT:sys.exit()# 关卡切换
def show_switch_stage(screen, width, height, stage):bg_img = pygame.image.load("./images/others/background.png")screen.blit(bg_img, (0, 0))font = pygame.font.Font('./font/simkai.ttf', width // 10)content = font.render(u'第%d关' % stage, True, (0, 255, 0))rect = content.get_rect()rect.midtop = (width / 2, height / 2)screen.blit(content, rect)pygame.display.update()delay_event = pygame.constants.USEREVENTpygame.time.set_timer(delay_event, 1000)#定时器延时while True:for event in pygame.event.get():if event.type == QUIT:sys.exit()if event.type == delay_event:returndef main():pygame.init()pygame.mixer.init()screen = pygame.display.set_mode((630, 630))pygame.display.set_caption('坦克大战')bg_img = pygame.image.load('./images/others/background.png')# 加载音效add_sound = pygame.mixer.Sound("./audios/add.wav")add_sound.set_volume(1)bang_sound = pygame.mixer.Sound("./audios/bang.wav")bang_sound.set_volume(1)blast_sound = pygame.mixer.Sound("./audios/blast.wav")blast_sound.set_volume(1)fire_sound = pygame.mixer.Sound("./audios/fire.wav")fire_sound.set_volume(1)Gunfire_sound = pygame.mixer.Sound("./audios/Gunfire.wav")Gunfire_sound.set_volume(1)hit_sound = pygame.mixer.Sound("./audios/hit.wav")hit_sound.set_volume(1)start_sound = pygame.mixer.Sound("./audios/start.wav")start_sound.set_volume(1)# 开始界面num_player = show_start_interface(screen, 630, 630)# 播放游戏开始的音乐start_sound.play()# 关卡stage = 0num_stage = 2# 游戏是否结束is_gameover = False# 时钟clock = pygame.time.Clock()# 主循环while not is_gameover:# 关卡stage += 1if stage > num_stage:breakshow_switch_stage(screen, 630, 630, stage)# 该关卡坦克总数量enemytanks_total = min(stage * 18, 80)# 场上存在的敌方坦克总数量enemytanks_now = 0# 场上可以存在的敌方坦克总数量enemytanks_now_max = min(max(stage * 2, 4), 8)# 精灵组,独立运行的动画组tanksGroup = pygame.sprite.Group()mytanksGroup = pygame.sprite.Group()enemytanksGroup = pygame.sprite.Group()bulletsGroup = pygame.sprite.Group()mybulletsGroup = pygame.sprite.Group()enemybulletsGroup = pygame.sprite.Group()myfoodsGroup = pygame.sprite.Group()# 自定义事件#   -生成敌方坦克事件genEnemyEvent = pygame.constants.USEREVENTpygame.time.set_timer(genEnemyEvent, 100)#   -敌方坦克静止恢复事件recoverEnemyEvent = pygame.constants.USEREVENTpygame.time.set_timer(recoverEnemyEvent, 8000)#    -我方坦克无敌恢复事件noprotectMytankEvent = pygame.constants.USEREVENTpygame.time.set_timer(noprotectMytankEvent, 8000)# 关卡地图map_stage = scene.Map(stage)# 我方坦克tank_player1 = tanks.myTank(1)tanksGroup.add(tank_player1)mytanksGroup.add(tank_player1)if num_player > 1:tank_player2 = tanks.myTank(2)tanksGroup.add(tank_player2)mytanksGroup.add(tank_player2)is_switch_tank = Trueplayer1_moving = Falseplayer2_moving = False# 为了轮胎的动画效果time = 0# 敌方坦克for i in range(0, 3):if enemytanks_total > 0:enemytank = tanks.enemyTank(i)tanksGroup.add(enemytank)enemytanksGroup.add(enemytank)enemytanks_now += 1enemytanks_total -= 1# 大本营myhome = home.Home()# 出场特效appearance_img = pygame.image.load("./images/others/appear.png").convert_alpha()appearances = []appearances.append(appearance_img.subsurface((0, 0), (48, 48)))appearances.append(appearance_img.subsurface((48, 0), (48, 48)))appearances.append(appearance_img.subsurface((96, 0), (48, 48)))# 关卡主循环while True:if is_gameover is True:breakif enemytanks_total < 1 and enemytanks_now < 1:is_gameover = Falsebreakfor event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == genEnemyEvent:if enemytanks_total > 0:if enemytanks_now < enemytanks_now_max:enemytank = tanks.enemyTank()if not pygame.sprite.spritecollide(enemytank, tanksGroup, False, None):tanksGroup.add(enemytank)enemytanksGroup.add(enemytank)enemytanks_now += 1enemytanks_total -= 1if event.type == recoverEnemyEvent:for each in enemytanksGroup:each.can_move = Trueif event.type == noprotectMytankEvent:for each in mytanksGroup:mytanksGroup.protected = False# 检查用户键盘操作key_pressed = pygame.key.get_pressed()# 玩家一# WSAD -> 上下左右# 空格键射击if key_pressed[pygame.K_w]:tanksGroup.remove(tank_player1)tank_player1.move_up(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player1)player1_moving = Trueelif key_pressed[pygame.K_s]:tanksGroup.remove(tank_player1)tank_player1.move_down(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player1)player1_moving = Trueelif key_pressed[pygame.K_a]:tanksGroup.remove(tank_player1)tank_player1.move_left(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player1)player1_moving = Trueelif key_pressed[pygame.K_d]:tanksGroup.remove(tank_player1)tank_player1.move_right(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player1)player1_moving = Trueelif key_pressed[pygame.K_SPACE]:if not tank_player1.bullet.being:fire_sound.play()tank_player1.shoot()# 玩家二# ↑↓←→ -> 上下左右# 小键盘0键射击if num_player > 1:if key_pressed[pygame.K_UP]:tanksGroup.remove(tank_player2)tank_player2.move_up(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player2)player2_moving = Trueelif key_pressed[pygame.K_DOWN]:tanksGroup.remove(tank_player2)tank_player2.move_down(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player2)player2_moving = Trueelif key_pressed[pygame.K_LEFT]:tanksGroup.remove(tank_player2)tank_player2.move_left(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player2)player2_moving = Trueelif key_pressed[pygame.K_RIGHT]:tanksGroup.remove(tank_player2)tank_player2.move_right(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(tank_player2)player2_moving = Trueelif key_pressed[pygame.K_KP0]:if not tank_player2.bullet.being:fire_sound.play()tank_player2.shoot()# 背景screen.blit(bg_img, (0, 0))# 石头墙for each in map_stage.brickGroup:screen.blit(each.brick, each.rect)# 钢墙for each in map_stage.ironGroup:screen.blit(each.iron, each.rect)# 冰for each in map_stage.iceGroup:screen.blit(each.ice, each.rect)# 河流for each in map_stage.riverGroup:screen.blit(each.river, each.rect)# 树for each in map_stage.treeGroup:screen.blit(each.tree, each.rect)time += 1if time == 5:time = 0is_switch_tank = not is_switch_tank# 我方坦克if tank_player1 in mytanksGroup:if is_switch_tank and player1_moving:screen.blit(tank_player1.tank_0, (tank_player1.rect.left, tank_player1.rect.top))player1_moving = Falseelse:screen.blit(tank_player1.tank_1, (tank_player1.rect.left, tank_player1.rect.top))if tank_player1.protected:screen.blit(tank_player1.protected_mask1, (tank_player1.rect.left, tank_player1.rect.top))if num_player > 1:if tank_player2 in mytanksGroup:if is_switch_tank and player2_moving:screen.blit(tank_player2.tank_0, (tank_player2.rect.left, tank_player2.rect.top))player1_moving = Falseelse:screen.blit(tank_player2.tank_1, (tank_player2.rect.left, tank_player2.rect.top))if tank_player2.protected:screen.blit(tank_player1.protected_mask1, (tank_player2.rect.left, tank_player2.rect.top))# 敌方坦克for each in enemytanksGroup:# 出生特效if each.born:if each.times > 0:each.times -= 1if each.times <= 10:screen.blit(appearances[2], (3 + each.x * 12 * 24, 3))elif each.times <= 20:screen.blit(appearances[1], (3 + each.x * 12 * 24, 3))elif each.times <= 30:screen.blit(appearances[0], (3 + each.x * 12 * 24, 3))elif each.times <= 40:screen.blit(appearances[2], (3 + each.x * 12 * 24, 3))elif each.times <= 50:screen.blit(appearances[1], (3 + each.x * 12 * 24, 3))elif each.times <= 60:screen.blit(appearances[0], (3 + each.x * 12 * 24, 3))elif each.times <= 70:screen.blit(appearances[2], (3 + each.x * 12 * 24, 3))elif each.times <= 80:screen.blit(appearances[1], (3 + each.x * 12 * 24, 3))elif each.times <= 90:screen.blit(appearances[0], (3 + each.x * 12 * 24, 3))else:each.born = Falseelse:if is_switch_tank:screen.blit(each.tank_0, (each.rect.left, each.rect.top))else:screen.blit(each.tank_1, (each.rect.left, each.rect.top))if each.can_move:tanksGroup.remove(each)each.move(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)tanksGroup.add(each)# 我方子弹for tank_player in mytanksGroup:if tank_player.bullet.being:tank_player.bullet.move()screen.blit(tank_player.bullet.bullet, tank_player.bullet.rect)# 子弹碰撞敌方子弹for each in enemybulletsGroup:if each.being:if pygame.sprite.collide_rect(tank_player.bullet, each):tank_player.bullet.being = Falseeach.being = FalseenemybulletsGroup.remove(each)breakelse:enemybulletsGroup.remove(each)# 子弹碰撞敌方坦克for each in enemytanksGroup:if each.being:if pygame.sprite.collide_rect(tank_player.bullet, each):if each.is_red == True:myfood = food.Food()myfood.generate()myfoodsGroup.add(myfood)each.is_red = Falseeach.blood -= 1each.color -= 1if each.blood < 0:bang_sound.play()each.being = FalseenemytanksGroup.remove(each)enemytanks_now -= 1tanksGroup.remove(each)else:each.reload()tank_player.bullet.being = Falsebreakelse:enemytanksGroup.remove(each)tanksGroup.remove(each)# 子弹碰撞石头墙if pygame.sprite.spritecollide(tank_player.bullet, map_stage.brickGroup, True, None):tank_player.bullet.being = False# 子弹碰钢墙if tank_player.bullet.stronger:if pygame.sprite.spritecollide(tank_player.bullet, map_stage.ironGroup, True, None):tank_player.bullet.being = Falseelse:if pygame.sprite.spritecollide(tank_player.bullet, map_stage.ironGroup, False, None):tank_player.bullet.being = False# 子弹碰大本营if pygame.sprite.collide_rect(tank_player.bullet, myhome):tank_player.bullet.being = Falsemyhome.set_dead()is_gameover = True# 敌方子弹for each in enemytanksGroup:if each.being:if each.can_move and not each.bullet.being:enemybulletsGroup.remove(each.bullet)each.shoot()enemybulletsGroup.add(each.bullet)if not each.born:if each.bullet.being:each.bullet.move()screen.blit(each.bullet.bullet, each.bullet.rect)# 子弹碰撞我方坦克for tank_player in mytanksGroup:if pygame.sprite.collide_rect(each.bullet, tank_player):if not tank_player.protected:bang_sound.play()tank_player.life -= 1if tank_player.life < 0:mytanksGroup.remove(tank_player)tanksGroup.remove(tank_player)if len(mytanksGroup) < 1:is_gameover = Trueelse:tank_player.reset()each.bullet.being = FalseenemybulletsGroup.remove(each.bullet)break# 子弹碰撞石头墙if pygame.sprite.spritecollide(each.bullet, map_stage.brickGroup, True, None):each.bullet.being = FalseenemybulletsGroup.remove(each.bullet)# 子弹碰钢墙if each.bullet.stronger:if pygame.sprite.spritecollide(each.bullet, map_stage.ironGroup, True, None):each.bullet.being = Falseelse:if pygame.sprite.spritecollide(each.bullet, map_stage.ironGroup, False, None):each.bullet.being = False# 子弹碰大本营if pygame.sprite.collide_rect(each.bullet, myhome):each.bullet.being = Falsemyhome.set_dead()is_gameover = Trueelse:enemytanksGroup.remove(each)tanksGroup.remove(each)# 家screen.blit(myhome.home, myhome.rect)# 食物for myfood in myfoodsGroup:if myfood.being and myfood.time > 0:screen.blit(myfood.food, myfood.rect)myfood.time -= 1for tank_player in mytanksGroup:if pygame.sprite.collide_rect(tank_player, myfood):# 消灭当前所有敌人if myfood.kind == 0:for _ in enemytanksGroup:bang_sound.play()enemytanksGroup = pygame.sprite.Group()enemytanks_total -= enemytanks_nowenemytanks_now = 0# 敌人静止if myfood.kind == 1:for each in enemytanksGroup:each.can_move = False# 子弹增强if myfood.kind == 2:add_sound.play()tank_player.bullet.stronger = True# 使得大本营的墙变为钢板if myfood.kind == 3:map_stage.protect_home()# 坦克获得一段时间的保护罩if myfood.kind == 4:add_sound.play()for tank_player in mytanksGroup:tank_player.protected = True# 坦克升级if myfood.kind == 5:add_sound.play()tank_player.up_level()# 坦克生命+1if myfood.kind == 6:add_sound.play()tank_player.life += 1myfood.being = FalsemyfoodsGroup.remove(myfood)breakelse:myfood.being = FalsemyfoodsGroup.remove(myfood)pygame.display.flip()clock.tick(60)if not is_gameover:show_end_interface(screen, 630, 630, True)else:show_end_interface(screen, 630, 630, False)if __name__ == '__main__':main()

4)效果展示

视频展示效果——

【Pygame实战】经典的坦克大战游戏,勾起童年无限回忆!

静态截图效果——

游戏界面:

​第一关单人游戏:

双人第一关游戏:

总结

​好啦!中文版的坦克大战都看的懂了哈,想咋玩儿咋玩儿。

​这是程序员的浪漫,这也是我们的浪漫。

往期也有更多好玩儿的游戏,欢迎阅读哦~如有帮助到你,记得三连的啦

完整的素材、安装环境、源码等看文末即可啦!

【Pygame实战】经典的坦克大战游戏,勾起童年无限回忆《坦克大战小霸王版》相关推荐

  1. 基于Unity3D经典消消乐游戏源码,代码详细注释,c#版方块消消乐源代码

    使用Unity2017开发,实现功能有: 基本消除功能 UI动画以及代码控制动画 消除动画以及手势识别 消除判定.连续消除判定 UI是自己独立完成比较简单,打包平台我设置的是webGL平台,有需要改成 ...

  2. python人狗大战游戏_day23 python学习 类 人狗大战

    面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 面向过程 优点是:极大的降低了 ...

  3. java实现潜艇大战游戏_基于Java实现的潜艇大战游戏

    一.需求分析 本次游戏课程设计小组成员团队合作的方式,通过游戏总体分析设计,场景画面的绘制,游戏事件的处理,游戏核心算法的分析实现,游戏的碰撞检测,游戏的反复测试,游戏的打包运行等一个完整的游戏设计编 ...

  4. 雷电飞机大战游戏|基于Java开发实现雷电飞机大战游戏

    作者主页:编程千纸鹤 作者简介:Java.前端.Pythone开发多年,做过高程,项目经理,架构师 主要内容:Java项目开发.毕业设计开发.面试技术整理.最新技术分享 收藏点赞不迷路  关注作者有好 ...

  5. 100行JS代码实现❤坦克大战js小游戏源码 HTML5坦克大战游戏代码(HTML+CSS+JavaScript )

    坦克大战js小游戏源码 HTML5坦克大战游戏代码(HTML+CSS+JavaScript ) HTML5坦克大战网页小游戏,完美还原小霸王学习机效果,以坦克战斗及保卫基地为主题,属于策略型类游戏. ...

  6. Python+pygame飞机大战游戏

    转载自IIronMan 的博客 Python:飞机大战游戏1:前期准备 Python:飞机大战游戏2:pygame 快速入门 Python:飞机大战游戏3:框架搭建 Python:飞机大战游戏4:背景 ...

  7. android 坦克,Android坦克大战游戏《坦克骑士 Tank Riders》

    <坦克骑士 Tank Riders>是一款3D坦克战斗游戏,玩家在游戏当中将操控一个造型非常Q的小坦克在激烈的战斗中寻求出路.与红白机时代的坦克大战玩法完全不同,坦克骑士更具有冒险性质,而 ...

  8. 体感游戏 | 手势识别玩飞机大战游戏(一) 用pygame实现飞机大战小游戏

    Color Space OpenCV与AI深度学习 后面将分四篇文章来介绍实现手势识别控制飞机大战游戏的功能,它们分别是: 使用Pygame实现简易飞机大战小游戏 使用Python+OpenCV实现简 ...

  9. 体感游戏 | 手势识别玩飞机大战游戏(三) 使用OpenCV实现手势识别玩飞机大战游戏

    后面将分四篇文章来介绍实现手势识别控制飞机大战游戏的功能,它们分别是: 使用Pygame实现简易飞机大战小游戏 使用Python+OpenCV实现简单手势识别 使用OpenCV实现手势识别玩飞机大战游 ...

最新文章

  1. ACCP学习旅程之-----使用C#开发数据库应用程序(第二章)
  2. java中实现线程互斥的关键词_简单的互斥同步方式——synchronized关键字详解
  3. Codeforces - 1194C - From S To T - 子序列 - 排序
  4. Java面试之谈谈对CAS的理解
  5. dede首页调用会员积分和头像代码
  6. iptables原理知识
  7. 我的世界空岛生存服务器制作,《我的世界 》10款PC服务器专属空岛玩法大盘点!...
  8. 艾米丽Java游戏_艾米丽玩闹鬼 Emily Wants To Play中文游戏介绍_游戏库_巴士单机游戏...
  9. go time.Ticker与time.Timer使用
  10. pytorch2-gym
  11. 【JavaScript】 对象 Object
  12. mac导出iphone手机上的ipa包
  13. ZYNQ_使用PL-PS的中断
  14. linux CentOS
  15. vc使用hiredis的几个填坑动作
  16. python百度图库爬取
  17. Android中使用又拍云存储来上传文件(包括图片、音频和视频等)
  18. 细思极恐——R语言forestplot包画meta分析群体药动学常用森林图
  19. 视频画中画效果,简单好用快速剪辑视频方法
  20. Latex-三线表(表格问题)

热门文章

  1. 微软反盗版 小心别砸了自己的脚
  2. 把Firfox的缓冲设置到内存盘
  3. 四川企立方电商:拼多多店包爆款打造
  4. 禁用USB设备的脚本
  5. java批量上传文件_Java 批量大文件上传下载
  6. 国内又一款开源软件(Wall),可搭建个人照片墙
  7. 互联网慢病管理-软件架构设计
  8. 给网赚从业者的几点建议
  9. 【MATLAB appdesigner】27_如何在appdesigner中调试,查看变量?(举例+技巧)
  10. AI开发基本流程介绍