资源简介
利用python开发的坦克大战源码加图片文件,直接可以运行。分享给大家自己下载学习和交流.

代码片段和文件信息
import pygametimerandom
_display = pygame.display
COLOR_BLACK = pygame.Color(0 0 0)
COLOR_RED = pygame.Color(255 0 0)
version = ‘v1.25‘
class MainGame():
#游戏主窗口
window = None
SCREEN_HEIGHT = 500
SCREEN_WIDTH = 800
#创建我方坦克
TANK_P1 = None
#存储所有敌方坦克
EnemyTank_list = []
#要创建的敌方坦克的数量
EnemTank_count = 5
#存储我方子弹的列表
Bullet_list = []
#存储敌方子弹的列表
Enemy_bullet_list = []
#爆炸效果列表
Explode_list = []
#墙壁列表
Wall_list = []
#开始游戏方法
def startGame(self):
_display.init()
#创建窗口加载窗口(借鉴官方文档)
MainGame.window = _display.set_mode([MainGame.SCREEN_WIDTHMainGame.SCREEN_HEIGHT])
self.creatMyTank()
self.creatEnemyTank()
self.creatWalls()
#设置一下游戏标题
_display.set_caption(“坦克大战“+version)
#让窗口持续刷新操作
while True:
#给窗口完成一个填充颜色
MainGame.window.fill(COLOR_BLACK)
#在循环中持续完成事件的获取
self.getEvent()
#将绘制文字得到的小画布,粘贴到窗口中
MainGame.window.blit(self.getTextSurface(“剩余敌方坦克%d辆“%len(MainGame.EnemyTank_list))(55))
#调用展示墙壁的方法
self.blitWalls()
if MainGame.TANK_P1 and MainGame.TANK_P1.live:
# 将我方坦克加入到窗口中
MainGame.TANK_P1.displayTank()
else:
del MainGame.TANK_P1
MainGame.TANK_P1 = None
#循环展示敌方坦克
self.blitEnemyTank()
#根据坦克的开关状态调用坦克的移动方法
if MainGame.TANK_P1 and not MainGame.TANK_P1.stop:
MainGame.TANK_P1.move()
#调用碰撞墙壁的方法
MainGame.TANK_P1.hitWalls()
MainGame.TANK_P1.hitEnemyTank()
#调用渲染子弹列表的一个方法
self.blitBullet()
#调用渲染敌方子弹列表的一个方法
self.blitEnemyBullet()
#调用展示爆炸效果的方法
self.displayExplodes()
time.sleep(0.02)
#窗口的刷新
_display.update()
#创建我方坦克的方法
def creatMyTank(self):
# 创建我方坦克
MainGame.TANK_P1 = MyTank(400 300)
#创建音乐对象
music = Music(‘img/start.wav‘)
#调用播放音乐方法
music.play()
#创建敌方坦克
def creatEnemyTank(self):
top = 100
for i in range(MainGame.EnemTank_count):
speed = random.randint(36)
#每次都随机生成一个left值
left = random.randint(1 7)
eTank = EnemyTank(left*100topspeed)
MainGame.EnemyTank_list.append(eTank)
#创建墙壁的方法
def creatWalls(self):
for i in range(6):
wall = Wall(130*i240)
MainGame.Wall_list.append(wall)
def blitWalls(self):
for wall in MainGame.Wall_list:
if wall.live:
wall.displayWall()
else:
MainGame.Wall_list.remove(wall)
#将敌方坦克加入到窗口中
def blitEnemyTank(self):
for eTank in MainGame.EnemyTank_list:
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-06-05 08:52 img\
文件 744 2019-06-05 08:52 img\blast0.gif
文件 1968 2019-06-05 08:52 img\blast1.gif
文件 2683 2019-06-05 08:52 img\blast2.gif
文件 3740 2019-06-05 08:52 img\blast3.gif
文件 5686 2019-06-05 08:52 img\blast4.gif
文件 3716 2019-06-05 08:52 img\enemy1D.gif
文件 3722 2019-06-05 08:52 img\enemy1L.gif
文件 3735 2019-06-05 08:52 img\enemy1R.gif
文件 3707 2019-06-05 08:52 img\enemy1U.gif
文件 377 2019-06-05 08:52 img\enemymissile.gif
文件 69216 2019-06-05 08:52 img\fire.wav
文件 23136 2019-06-05 08:52 img\hit.wav
文件 3512 2019-06-05 08:52 img\p1tankD.gif
文件 3595 2019-06-05 08:52 img\p1tankL.gif
文件 3590 2019-06-05 08:52 img\p1tankR.gif
文件 3506 2019-06-05 08:52 img\p1tankU.gif
文件 883296 2019-06-05 08:52 img\start.wav
文件 1769 2019-06-05 08:52 img\steels.gif
文件 19262 2019-06-05 08:52 tankall.py
相关资源
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
评论
共有 条评论