python3坦克大战

# coding = utf-8
       import pygame, sys, time
       from pygame.locals import *
       from random import randint
       '''坦克大战主窗口'''
       class TankMain(object):
           width = 700
           height = 600
           wall_list = []
           my_tank = None
           home = None
           lives = 3
           # enemy_list = []     # 敌方坦克列表
           enemy_list = pygame.sprite.Group()  # 敌方坦克(精灵)族群
           my_tank_missile = []    # 我方子弹列表
           enemy_tank_missile = pygame.sprite.Group()  # 敌方子弹(精灵)族群
           explode_list = []       # 爆炸效果列表
           # 开始游戏
           def startGame(self):    # 开始游戏
               pygame.init()   # pygame模块初始化,加载系统资源
               # 设置一个窗口,第一个参数:(宽,高),第二个参数:窗口可变{0,RESIZABLE,FULLSCREEM,..},第三个参数:颜色位数{32,64,..}
               # screem是一个Surface表面对象
               screem = pygame.display.set_mode((TankMain.width, TankMain.height), 0, 32)
               # 给窗口设置标题
               pygame.display.set_caption("坦克大战")
               # 生成我方坦克,敌方坦克,障碍物等
               self.creat(screem)
               # 循环显示屏幕内容,形成动态效果
               while True:
                   screem.fill((0, 0, 0))    # 设置背景颜色 RGB (255,255,255):白色 (0,0,0):黑色
                   # 显示
                   self.show(screem)
                   # 事件处理
                   self.get_event(TankMain.my_tank, screem)  # 获取事件进行处理
                   time.sleep(0.05)    # 每次休息0.05秒跳到下一帧
                   pygame.display.update()     # 显示重置
           # 在屏幕上显示文字内容
           def write_text(self):
               font = pygame.font.SysFont('simhei', 13)  # 定义一个字体 参数: 字体 大小 粗细
               text_sf0 = font.render("我方坦克生命:%d"%TankMain.lives, True, (255, 0, 0))  # 根据字体创建一个文字的图像 参数:内容 抗锯齿 颜色 背景
               text_sf1 = font.render("敌方坦克数量:%d"%len(TankMain.enemy_list), True, (255, 0, 0))
   # 根据字体创建一个文字的图像 参数:内容 抗锯齿 颜色 背景
               text_sf2 = font.render("我方坦克屏幕子弹数量:%d"%len(TankMain.my_tank_missile), True,
   (255, 0, 0))  # 根据字体创建一个文字的图像 参数:内容 抗锯齿 颜色 背景
               return text_sf0, text_sf1, text_sf2
           # 生产,创建我方坦克,敌方坦克
           def creat(self, screem):
               # 生产墙列表
               for i in range(1, 14):
                   if i % 3 == 0:
                       TankMain.wall_list.append(GrassBarrier(screem, i * Barrier.width, TankMain.height - 400))
                       TankMain.wall_list.append(CobWallBarrier(screem, i * Barrier.width, 300))
                   elif i % 4 == 0:
                       pass
                   elif i % 5 == 0:
                       TankMain.wall_list.append(GrassBarrier(screem, i * Barrier.width, TankMain.height-400))
                       TankMain.wall_list.append(IronWallBarrier(screem, i * Barrier.width, 300))
                   elif i % 7 == 0:
                       TankMain.wall_list.append(CobWallBarrier(screem, i * Barrier.width, 300))
                   else:
                       TankMain.wall_list.append(IronWallBarrier(screem, i * Barrier.width, 300))
                       TankMain.wall_list.append(WaterBarrier(screem, i * Barrier.width, TankMain.height - 400))
                       TankMain.wall_list.append(GrassBarrier(screem, i * Barrier.width, i*Barrier.height))
                       TankMain.wall_list.append(WaterBarrier(screem, TankMain.width-i * Barrier.width, i * Barrier.height))
               # 生成我方坦克的家
               for i in range(0, 3):
                   if i == 1:
                       TankMain.home = HomeBarrier(screem, TankMain.width/2-HomeBarrier.width/2,
   TankMain.height-HomeBarrier.width)
                       TankMain.wall_list.append(CobWallBarrier(screem, (TankMain.width+(2*i-3)*HomeBarrier.width)/2,
   TankMain.height-HomeBarrier.width*2))
                   else:
                       TankMain.wall_list.append(CobWallBarrier(screem, (TankMain.width+(2*i-3)*HomeBarrier.width)/2,
   TankMain.height-HomeBarrier.width))
                       TankMain.wall_list.append(CobWallBarrier(screem, (TankMain.width+(2*i-3)*HomeBarrier.width)/2,
   TankMain.height-HomeBarrier.width*2))
               # 生产我方坦克
               TankMain.my_tank = MyTank(screem)
               # 生产敌方坦克
               for i in range(1, 6):
                   if i % 3 == 0:
                       TankMain.enemy_list.add(EnemyTankQ(screem))  # 在族群中添加3个普通坦克,就会有一个强坦克
                   TankMain.enemy_list.add(EnemyTank(screem))  # 在族群中添加一个精灵sprite(坦克)
           # 在屏幕上显示(矩形rect, sprite, image,等)
           def show(self, screem):
               # 显示文字
               for i, text in enumerate(self.write_text(), 0):
                   screem.blit(text, (0, 5 + (13 * i)))  # 在左上角显示文字 参数:源 位置(x,y)
               # 显示我方坦克的家
               if TankMain.home and TankMain.home.live:
                   TankMain.home.display()
                   TankMain.home.hit_other()
               else:
                   TankMain.home = None
                   TankMain.lives = 0
                   TankMain.my_tank = None
               # 显示敌方坦克
               for enemy_tank in TankMain.enemy_list:
                   if enemy_tank.live:
                       enemy_tank.display()
                       # enemy_tank.hit_ourself()
                       enemy_tank.random_move()
                       enemy_tank.random_fire()
                   else:
                       TankMain.enemy_list.remove(enemy_tank)
               # 显示我方坦克
               if TankMain.my_tank and TankMain.my_tank.live:
           
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值