python开发的坦克大战游戏

1 importpygame2 from pygame.sprite importSprite3 importsys4 importtime5 importrandom6

7 SCREEN_WIDTH = 780

8 SCREEN_HEIGHT = 500

9 BG_COLOR =pygame.Color(0,0,0)10 TEXT_COLOR = pygame.Color(255,0,0)11 #复活次数

12

13 #定义一个精灵类

14 classBaseItem(Sprite):15 def __init__(self, color, width, height):16 #Call the parent class (Sprite) constructor

17 pygame.sprite.Sprite.__init__(self)18

19 classMainGame ():20

21 window =None22 my_tank =None23 REC_TIME=024 #敌方坦克初始化

25 EnemyTankList =[]26 EnemyTankCount = 5

27 #存储我方子弹列表

28 myBulleList =[]29 #存储敌方子弹

30 EnemyBulletList =[]31 #创建爆炸对象列表

32 explodeList =[]33

34 #创建墙壁列表

35 wallList =[]36 def __init__(self):37 pass

38

39 defstarGame(self):40 #初始化,设置标题,屏幕大小

41 pygame.display.init()42 MainGame.window =pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))43 pygame.display.set_caption("坦克大战1.03")44 #初始化我方坦克

45 self.creatMyTank()46 #初始化敌方坦克

47 self.creatEnemyTank()48 #初始化墙壁

49 self.creatWall()50 whileTrue:51 #填充颜色

52 MainGame.window.fill(BG_COLOR)53 #获取事件

54 self.getEvent()55 #绘制文字

56 MainGame.window.blit(self.getTextSuface("敌方坦克剩余数量%d"%MainGame.EnemyTankCount),(SCREEN_HEIGHT/2,20))57 #调用我方坦克进行显示

58 if MainGame.my_tank andMainGame.my_tank.live:59 MainGame.my_tank.displayTank()60 else:61 delMainGame.my_tank62 MainGame.my_tank =None63 #显示我方子弹

64 self.blitMyBullet()65 #敌方坦克进行显示

66 self.blitEnenyTank()67 #显示墙壁

68 self.blitWall()69 #显示敌方子弹

70 self.blitEnenyBullet()71 #显示爆炸效果

72 self.blitExplode()73 if MainGame.my_tank andMainGame.my_tank.live:74 if notMainGame.my_tank.stop:75 MainGame.my_tank.move()76 MainGame.my_tank.hitWall()77 MainGame.my_tank.myTank_hit_enemyTank()78 if MainGame.EnemyTankCount ==0:79 MainGame.window.blit(self.getTextSuface('恭喜你,顺利通过'), (SCREEN_HEIGHT/2, SCREEN_WIDTH/2))80 time.sleep(2)81 self.endGame()82 pygame.display.update()83 time.sleep(0.02)84

85

86 defcreatWall(self):87 for i in range(0,SCREEN_WIDTH,SCREEN_WIDTH//6):88 top = SCREEN_HEIGHT//3

89 left =i90 wall =Wall(left,top)91 MainGame.wallList.append(wall)92

93 defcreatMyTank(self):94 MainGame.my_tank = MyTank(SCREEN_WIDTH/2,SCREEN_HEIGHT/3*2)95 music = Music("img/start.wav")96 music.play()97

98 defcreatEnemyTank(self):99 top = 100

100 for i inrange(MainGame.EnemyTankCount):101 left = random.randint(0,600)102 speed = random.randint(1,4)103 enmy =EnemyTank(left,top,speed)104 MainGame.EnemyTankList.append(enmy)105

106 defblitWall(self):107 for wall inMainGame.wallList:108 ifwall.live:109 wall.displayWall()110 else:111 MainGame.wallList.remove(wall)112

113 defblitEnenyTank(self):114 "循环遍历敌方坦克,展示敌方坦克"

115 for enmeyTank inMainGame.EnemyTankList:116 ifenmeyTank.live:117 enmeyTank.displayTank()118 enmeyTank.randomMove()119 EnemyBullet =enmeyTank.shot()120 enmeyTank.hitWall()121 enmeyTank.enemyTank_hit_MyTank()122 #存储敌方子弹

123 ifEnemyBullet:124 MainGame.EnemyBulletList.append(EnemyBullet)125 else:126 MainGame.EnemyTankList.remove(enmeyTank)127 MainGame.EnemyTankCount -=1

128 defblitMyBullet(self):129 for bullet inMainGame.myBulleList:130 ifbullet.live:131 bullet.displayBullet()132 bullet.move()133 bullet.myBullet_hit_enemyTank()134 bullet.wall_bullet()135 else:136 MainGame.myBulleList.remove(bullet)137

138 defblitEnenyBullet(self):139 for bullet inMainGame.EnemyBulletList:140 if bullet.live: #随机会产生None

141 bullet.displayBullet()142 bullet.move()143 bullet.enemyBullet_hit_myTank()144 bullet.wall_bullet()145 else:146 MainGame.EnemyBulletList.remove(bullet)147

148

149 defblitExplode(self):150 for explode inMainGame.explodeList:151 ifexplode.live:152 explode.displayExplode()153 else:154 MainGame.explodeList.remove(explode)155

156

157 defendGame(self):158 print("欢迎下次游戏")159 sys.exit()160 defgetTextSuface(self,text):161 pygame.font.init()162 #print(pygame.font.get_fonts())

163 font = pygame.font.SysFont('kaiti',18)164 textSurface =font.render(text,True,TEXT_COLOR)165 returntextSurface166

167

168 defgetEvent(self):169 '''获取所有事件'''

170 eventList=pygame.event.get()171 for event ineventList:172 if event.type ==pygame.QUIT:173 self.endGame()174 if event.type ==pygame.KEYDOWN:175 if notMainGame.my_tank:176 if event.key ==pygame.K_ESCAPE:177 if MainGame.REC_TIME < 3:178 self.creatMyTank()179 MainGame.REC_TIME +=1

180 if MainGame.my_tank andMainGame.my_tank.live:181 if event.key ==pygame.K_DOWN:182 MainGame.my_tank.direction ='D'

183 #MainGame.my_tank.move()

184 MainGame.my_tank.stop =False185 print("按下向下的键,向下移动",)186 elif event.key ==pygame.K_UP:187 MainGame.my_tank.direction = 'U'

188 #MainGame.my_tank.move()

189 MainGame.my_tank.stop =False190 print("按下向上的键,向上移动")191 elif event.key ==pygame.K_LEFT:192 MainGame.my_tank.direction = 'L'

193 #MainGame.my_tank.move()

194 MainGame.my_tank.stop =False195 print("按下向左的键,向左移动", MainGame.my_tank.direction)196 elif event.key ==pygame.K_RIGHT:197 MainGame.my_tank.direction = 'R'

198 #MainGame.my_tank.move()

199 MainGame.my_tank.stop =False200 print("按下向右的键,向右移动", MainGame.my_tank.direction)201

202 elif event.key ==pygame.K_SPACE:203 print("发射子弹")204 #创建我方子弹

205 if len(MainGame.myBulleList) < 3:206 myBullet =Bullet(MainGame.my_tank)207 MainGame.myBulleList.append(myBullet)208

209 #发射子弹音效

210 ShotMusic = Music('img/hit.wav')211 ShotMusic.play()212

213 elif event.type ==pygame.KEYUP:214 if event.key ==pygame.K_UP or event.key == pygame.K_DOWN or event.key ==pygame.K_LEFT or event.key ==pygame.K_RIGHT:215 if MainGame.my_tank andMainGame.my_tank.live:216 MainGame.my_tank.stop=True217

218

219

220 classTank(BaseItem):221 def __init__(self,left,top):222 self.images={223 "U":pygame.image.load('img/p1tankU.gif'),224 "D": pygame.image.load('img/p1tankD.gif'),225 "L": pygame.image.load('img/p1tankL.gif'),226 "R": pygame.image.load('img/p1tankR.gif'),227 }228 self.direction ='U'

229 self.image =self.images[self.direction]230 self.rect =self.image.get_rect()231 #设置放置的位置

232 self.rect.left=left233 self.rect.top =top234 self.speed = 4

235 #控制坦克开关

236 self.stop =True237 self.live =True238

239 #保持原来的位置

240 self.oldLeft =self.rect.left241 self.oldTop =self.rect.top242

243 defhitWall(self):244 for wall inMainGame.wallList:245 ifpygame.sprite.collide_rect(wall, self):246 self.stay()247

248 defstay(self):249 self.rect.left =self.oldLeft250 self.rect.top =self.oldTop251

252 defshot(self):253 returnBullet(self)254 defmove(self):255 #保持原来的状态

256 self.oldLeft =self.rect.left257 self.oldTop =self.rect.top258 #判断坦克的方向进行移动

259 if self.direction =='U':260 if self.rect.top>0:261 self.rect.top -=self.speed262 elif self.direction =='L':263 if self.rect.left>0:264 self.rect.left -=self.speed265 elif self.direction =='D':266 if self.rect.top +self.rect.height<267 self.rect.top elif self.direction="=" if self.rect.left>

272 defdisplayTank(self):273 self.image =self.images[self.direction]274 MainGame.window.blit(self.image,self.rect)275

276 classMyTank(Tank):277 def __init__(self,left,top):278 super().__init__(left,top)279

280 defmyTank_hit_enemyTank(self):281 for enemyTank inMainGame.EnemyTankList:282 ifpygame.sprite.collide_rect(enemyTank, self):283 self.stay()284

285 classEnemyTank(Tank):286

287

288 def __init__(self,left,top,speed):289

290 super(EnemyTank,self).__init__(left,top)291 self.images ={292 "U": pygame.image.load('img/enemy1U.gif'),293 "D": pygame.image.load('img/enemy1D.gif'),294 "L": pygame.image.load('img/enemy1L.gif'),295 "R": pygame.image.load('img/enemy1R.gif'),296 }297 self.direction =self.RandomDirction()298

299 self.image =self.images[self.direction]300 self.rect =self.image.get_rect()301 self.rect.left =left302 self.rect.top =top303 self.speed =speed304 self.EnemyFlag =False305

306 self.step = 60

307

308 defRandomDirction(self):309 num = random.randint(1,4)310 if num ==1:311 return "U"

312 elif num == 2:313 return "L"

314 elif num == 3:315 return "D"

316 elif num == 4:317 return "R"

318 defrandomMove(self):319 if self.step <0:320 self.direction =self.RandomDirction()321 self.step = 60

322 else:323 self.move()324 self.step -= 1

325 defshot(self):326 num =random.randint(1,100)327 if num < 4:328 returnBullet(self)329

330 defenemyTank_hit_MyTank(self):331 for enemy inMainGame.EnemyTankList:332 if MainGame.my_tank andMainGame.my_tank.live :333 ifpygame.sprite.collide_rect(MainGame.my_tank,enemy):334 self.stay()335

336

337

338

339 classBullet(BaseItem):340 def __init__(self,tank):341 self.image=pygame.image.load('img/enemymissile.gif')342 self.direction =tank.direction343 self.rect =self.image.get_rect()344 if self.direction == 'U':345 self.rect.left = tank.rect.left + tank.rect.width/2 - self.rect.width/2

346 self.rect.top = tank.rect.top -self.rect.height347 elif self.direction == 'D':348 self.rect.left = tank.rect.left + tank.rect.width/2 - self.rect.width/2

349 self.rect.top = tank.rect.top +tank.rect.height350 elif self.direction == 'L':351 self.rect.left = tank.rect.left - self.rect.width/2 -self.rect.width/2

352 self.rect.top = tank.rect.top +tank.rect.height/2 - self.rect.width/2

353 elif self.direction == 'R':354 self.rect.left = tank.rect.left +tank.rect.width355 self.rect.top = tank.rect.top + tank.rect.height/2 -self.rect.width/2

356 #子弹的速度

357 self.speed =6

358 #子弹状态

359 self.live =True360

361 defmove(self):362 if self.direction == 'U':363 if self.rect.top>0:364 self.rect.top -=self.speed365 else:366 self.live =False367 elif self.direction == 'R':368 if self.rect.left + self.rect.width<369 self.rect.left else:371 self.live="False372" elif self.direction="=" if self.rect.top self.rect.height else:376>0:379 self.rect.left -=self.speed380 else:381 self.live =False382369>

383 defdisplayBullet(self):384 MainGame.window.blit(self.image,self.rect)385

386 defmyBullet_hit_enemyTank(self):387 for enemyTank inMainGame.EnemyTankList:388 ifpygame.sprite.collide_rect(enemyTank,self):389 enemyTank.live =False390 self.live =False391

392 #创建爆炸对象

393 explode =Explode(enemyTank)394 MainGame.explodeList.append(explode)395 defenemyBullet_hit_myTank(self):396 if MainGame.my_tank andMainGame.my_tank.live:397 ifpygame.sprite.collide_rect(MainGame.my_tank,self):398 MainGame.my_tank.live =False399 self.live =False400

401 explode =Explode(MainGame.my_tank)402 MainGame.explodeList.append(explode)403

404 defwall_bullet(self):405 for wall inMainGame.wallList:406 ifpygame.sprite.collide_rect(wall,self):407 wall.hg -=1

408 self.live =False409 if wall.hg<=0:410 wall.live =False411

412

413 classWall():414 def __init__(self,left,top):415 self.image = pygame.image.load('img/steels.gif')416 self.rect =self.image.get_rect()417 self.rect.top =top418 self.rect.left =left419 self.live =True420 self.hg = 8

421

422 defdisplayWall(self):423 if self.live ==True:424 MainGame.window.blit(self.image,self.rect)425

426

427 classExplode():428 def __init__(self,tank):429 #爆炸的位置有坦克决定

430 self.rect =tank.rect431 self.images = [pygame.image.load('img/blast0.gif'),432 pygame.image.load('img/blast1.gif'),433 pygame.image.load('img/blast2.gif'),434 pygame.image.load('img/blast3.gif')]435 self.step =0436 self.image =self.images[self.step]437 self.live =True438

439 defdisplayExplode(self):440 if self.step

443 MainGame.window.blit(self.image,self.rect)444 else:445 self.live =False446 self.step =0447

448 classMusic():449 def __init__(self,filename):450 self.filename =filename451 #初始化音乐混合器

452 pygame.mixer.init()453 pygame.mixer.music.load(filename)454 defplay(self):455 pygame.mixer.music.play()456

457

458 if __name__ == '__main__':459 c =MainGame()460 c.starGame()

267>

python坦克大战游戏_python实现坦克大战游戏相关推荐

  1. python写飞机大战游戏_python实现飞机大战游戏

    飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工 ...

  2. python轰炸敌机小游戏_python实现飞机大战微信小游戏

    0.前言 我学一种语言,可以说学任何东西都喜欢自己动手实践,总感觉自己动手一遍,就可以理解的更透彻,学python也一样,自己动手写代码,但更喜欢做点小东西出来,一边玩一边学.下面我就展示一下我最近做 ...

  3. python做飞机大战游戏_python实现飞机大战游戏

    飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工 ...

  4. python实现飞机大战游戏_python实现飞机大战小游戏

    本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下 初学Python,写了一个简单的Python小游戏. 师出bilibili某前辈 pycharm自带了第三方库pyga ...

  5. python扫雷游戏_python实现扫雷小游戏

    前面我们用python实现了贪吃蛇.坦克大战.飞船大战.五子棋等游戏 今天我们用python来实现一下扫雷游戏 本游戏代码量和源文件较多 可以从我的GitHub地址中获取 构建地雷区 import r ...

  6. python做的大型游戏_Python有做大型游戏的潜力吗?

    由于最近我要考试忙不过来没修改回答,谢谢评论区大们的指正,我认识到的pygame库确实可以做游戏,但是pygame限制很多,特别是做大型游戏方面,3D还没有相关的优化方法等(3D游戏,pygame表示 ...

  7. 如何用python制作五子棋游戏_Python制作打地鼠小游戏

    原文链接 Python制作小游戏(二十一)​mp.weixin.qq.com 效果展示 打地鼠小游戏https://www.zhihu.com/video/1200492442610450432 简介 ...

  8. python编游戏_python编的著名游戏

    python编的著名游戏 2019-03-18 15:59:33 3323 0 没有找到相关结果 已邀请: 目前使用Python编写的游戏有文明4.星际迷航:舰桥指挥官.战地2等,小游戏有俄罗斯方 ...

  9. python设计棋牌游戏_python开发棋牌类游戏

    pycharm专门针对用户打造的一种可以进行编辑的工具,它的功能设置比较强大,而且具有跨平台的使用特性,能方便用户通过跨平台的方式使用该软件,有效节省的使用时间,那么这款详情>> 阅读: ...

  10. python制作飞机大战代码_python实现飞机大战完整代码,可运行

    我发现很多python代码飞机大战在互联网上,但几乎没有一个是完整的.所以我做了一个完整的人.python代码分为两个文件,工具类和主类.python版本,pygame模块需要安装.完整的代码如下.1 ...

最新文章

  1. extjs用iframe的问题
  2. php-dev离线安装,局域网 pm2 离线安装
  3. 互联网1分钟 |1109
  4. 《Java 高并发》04 线程的基本操作
  5. P4849 寻找宝藏(模板:四维偏序)
  6. 工作371-javascript判断数组为空
  7. 基于AliOS Things玩转智能语音
  8. 用Python玩转统计数据:取样、计算相关性、拆分训练模型和测试
  9. 使用pandas 按同一列名称合并,并解决concat() got an unexpected keyword argument ‘join_axes‘报错
  10. maxwell监控mysql_利用Maxwell组件实时监听Mysql的binlog日志
  11. NodeJs之npm
  12. Android中启动页ViewPager和ViewFlipper带指示器
  13. 如何自定义快捷方式图标为自己的图片(保姆级教程)
  14. 从数据库导出数据到EXCEL换行的问题解决方法
  15. 中国数字乳房断层合成(DBT)设备市场趋势报告、技术动态创新及市场预测
  16. 下载并安装vue-devtools(详细步骤)
  17. php万年历源代码!源代码![上一年、上一月、下一月、下一年、附加当天日期加背景颜色]-私聊源码
  18. 2022年度保密教育线上培训考试参考答案
  19. 英语进阶系列-A05-英语升级练习三
  20. Azure-900【定义云模型】

热门文章

  1. Linux学习之路-Linux-at及cron命令【7】---20171215
  2. 优盘里文件夹变成html,U盘里面的文件跟文件夹突然乱码了怎么办
  3. ★40个经典的社交心理学现象
  4. 5.3.2 Counter对象
  5. Java算法:牛客网哔哩哔哩bilibili笔试真题算法Java版1-14题
  6. 苹果删除照片不释放内存_原来苹果手机这样清理内存,可以释放大量空间,真是太好用了...
  7. 商业网站建设和运营的四度:Approachability、Usability、 Accessibility 和 Profitability...
  8. 应对Flash中国版流氓后台偷窥的解决方法
  9. Linux下护眼软件
  10. Linux unison 效率,Linux下inotify+unison双向同步环境部署