AttributeError: ‘EnemyTank‘ object has no attribute ‘live‘

运行之后会有短暂的游戏运行界面,之后就会报错退出,求大佬指导

import random

import pygame, time

_display = pygame.display # 使后面名称简化
COLOR_BLACK = pygame.Color(0, 0, 0) # 意味着窗口填充颜色是黑色,此为合成色设置
COLOR_RED = pygame.Color(255, 0, 0) # 意味着字体颜色是红色,三个参数分别对应红绿蓝


class MainGame():
window = None # 游戏主窗口
SCREEN_HEIFHT = 500 # 窗口高度(变量)
SCREEN_WIDTH = 800 # 窗口宽度(变量)
TANK_P1 = None # 创建我方坦克
EnemyTank_list = [] # 存储所有敌方坦克
EnemTank_count = 5 # 创建敌方坦克数量
Bullet_list = [] # 存储我方子弹列表
Enemy_bullet_list = [] # 存储敌方子弹列表

def __init__(self):
pass

def startGame(self):
_display.init()
# 初始化
MainGame.window = _display.set_mode([MainGame.SCREEN_WIDTH, MainGame.SCREEN_HEIFHT])
# 创建游戏窗口(借鉴官方文档)。解释:创建出来的窗口存进window里
_display.set_caption("坦克大战") # 创建游戏标题
# 创建我方坦克
MainGame.TANK_P1 = Tank(400, 300)
# 创建敌方坦克
self.creatEnemyTank()
while True:
MainGame.window.fill(COLOR_BLACK)
# 给窗口填充颜色
# 在循环中持续完成事件的获取
self.getEvent()
# 将绘制文字得到的小画布,粘贴到窗口中
MainGame.window.blit(self.getTextSurface("敌方坦克剩余%d辆" % len(MainGame.EnemyTank_list)), (5, 10))
# 将我方坦克加入到窗口中
MainGame.TANK_P1.displayTank()
# 根据坦克的开关状态调用坦克的移动方法
# 循环展示敌方坦克
self.blitEnemyTank()
if MainGame.TANK_P1 and not MainGame.TANK_P1.stop:
MainGame.TANK_P1.move()
# 调用渲染我方子弹列表的方法
self.blitBullet()
# 调用渲染敌方子弹列表的方法
self.blitEnemyBullet()
time.sleep(0.02)
_display.update()
# 窗口持续刷新操作(长久展示)

# 创建敌方坦克
def creatEnemyTank(self):
top = 100
for i in range(MainGame.EnemTank_count):
speed = random.randint(3, 6)
# 每次都随机生成一个lift值
left = random.randint(1, 7)
eTank = EnemyTank(left * 100, top, speed)
MainGame.EnemyTank_list.append(eTank)

# 将坦克加入到窗口中
def blitEnemyTank(self):
for eTank in MainGame.EnemyTank_list:
eTank.displayTank()
eTank.randMove() # 调用坦克的移动方法
# 调用敌方坦克的射击
eBullet =eTank.shot()
# 如果子弹为None,不加入到列表
if eBullet:
# 将子弹存储敌方子弹列表
MainGame.Enemy_bullet_list.append(eTank)

# 将我方子弹加入到窗口中
def blitBullet(self):
for bullet in MainGame.Bullet_list:
# 如果子弹还活着,绘制出来,否则,直接从列表中移除该子弹
if bullet.live:
bullet.displayBullet()
# 让子弹移动
bullet.bulletMove()
else:
MainGame.Bullet_list.remove(bullet)

def blitEnemyBullet(self):
for eBullet in MainGame.Enemy_bullet_list:
if eBullet.live:
eBullet.displayBullet()
# 让子弹移动
eBullet.bulletMove()
# 如果子弹还活着,绘制出来,否则,直接从列表中移除该子弹
else:
MainGame.Bullet_list.remove(eBullet)
def getEvent(self): # 获取程序期间所有事件(鼠标事件,键盘事件)
# 1.获取所有事件
eventList = pygame.event.get()
# 2.对事件进行判断处理(1.点击关闭按钮 2.按下键盘上的摸个按键)
for event in eventList:
# 判断event.type是否为退出(quit),如果是退出,直接调用程序结束方法
if event.type == pygame.QUIT:
self.endGame()
# 判断事件类型是否为按键按下,如果是,继续判断按键是哪一个按键,来进行对应的处理
if event.type == pygame.KEYDOWN:
# 具体是哪一个按键进行处理
if event.key == pygame.K_LEFT:
print("坦克向左移动")
# 修改坦克方向
MainGame.TANK_P1.direction = "L"
MainGame.TANK_P1.stop = False
# 完成移动操作(调用坦克的移动方法)
# MainGame.TANK_P1.move()
elif event.key == pygame.K_RIGHT:
print("坦克向右移动")
# 修改坦克方向
MainGame.TANK_P1.direction = "R"
MainGame.TANK_P1.stop = False
# 完成移动操作(调用坦克的移动方法)
# MainGame.TANK_P1.move()
elif event.key == pygame.K_UP:
print("坦克向上移动")
# 修改坦克方向
MainGame.TANK_P1.direction = "U"
MainGame.TANK_P1.stop = False
# 完成移动操作(调用坦克的移动方法)
# MainGame.TANK_P1.move()
elif event.key == pygame.K_DOWN:
print("坦克向下移动")
# 修改坦克方向
MainGame.TANK_P1.direction = "D"
MainGame.TANK_P1.stop = False
# 完成移动操作(调用坦克的移动方法)
# MainGame.TANK_P1.move()
elif event.key == pygame.K_SPACE:
print("射击")
if len(MainGame.Bullet_list) < 3:
# 产生一颗子弹
m = Bullet(MainGame.TANK_P1)
# 将子弹加入到子弹列表
MainGame.Bullet_list.append(m)
else:
print("子弹数量不足")
print("当前屏幕中的子弹数量为:%d" % len(MainGame.Bullet_list))
if event.type == pygame.KEYUP:
# 松开的如果是方向键,才更改移动开关状态
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
# 修改坦克的移动状态
MainGame.TANK_P1.stop = True

# 左上角绘制文字的功能
def getTextSurface(self, text):
# 字体初始化(初始化字体模块)
pygame.font.init()
# 以下两步为为了知道包含了哪些字体而设置,避免设置字体而程序里不包含
# fontList=pygame.font.get_fonts() #获取所有字体
# print(fontList) #此步骤为了知道包含了哪些字体而设置打印,避免设置字体而程序里不包含
# 选中一个合适的字体
font = pygame.font.SysFont("隶书", 20)
# 使用对应的字符完成相关内容的绘制
textSurface = font.render(text, True, COLOR_RED)
return textSurface

def endGame(self):
print("谢谢使用")
exit() # 结束python解释器


class Tank(): # 坦克父类
def __init__(self, left, top):
self.images = {
"U": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\我方坦克-上.png"),
"D": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\我方坦克-下.png"),
"L": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\我方坦克-左.png"),
"R": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\我方坦克-右.png")
}
self.direction = "U"
self.image = self.images[self.direction]
# 坦克所在的区域
self.rect = self.image.get_rect()
# 指定坦克初始化位置
self.rect.left = left
self.rect.top = top
# 新增速度属性
self.speed = 5
# 新增属性:坦克的移动开关
self.stop = True

def move(self):
if self.direction == "L":
if self.rect.left > 0:
self.rect.left -= self.speed
elif self.direction == "R":
if self.rect.left + self.rect.height < MainGame.SCREEN_WIDTH:
self.rect.left += self.speed
elif self.direction == "U":
if self.rect.top > 0:
self.rect.top -= self.speed
elif self.direction == "D":
if self.rect.top + self.rect.height < MainGame.SCREEN_HEIFHT:
self.rect.top += self.speed

def shot(self):
return Bullet(self)

def displayTank(self): # 展示坦克
# 重新设置坦克图片
self.image = self.images[self.direction]
# 将坦克加入到窗口中
MainGame.window.blit(self.image, self.rect)


class MyTank(Tank): # 子类坦克
def __init__(self):
pass


class EnemyTank(Tank): # 子类坦克
def __init__(self, left, top, speed):
# 图片集
self.images = {
"U": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\敌方坦克-上.png"),
"D": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\敌方坦克-下.png"),
"L": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\敌方坦克-左.png"),
"R": pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\敌方坦克-右.png")
}
self.direction = self.randDirection()
self.image = self.images[self.direction]
# 坦克所在的区域
self.rect = self.image.get_rect()
# 指定坦克初始化位置
self.rect.left = left
self.rect.top = top
# 新增速度属性
self.speed = speed
# 新增属性:坦克的移动开关
self.stop = True
# 新增步数属性,用来控制敌方坦克随机移动
self.step = 35

# 方向
def move(self):
if self.direction == "L":
if self.rect.left > 0:
self.rect.left -= self.speed
elif self.direction == "R":
if self.rect.left + self.rect.height < MainGame.SCREEN_WIDTH:
self.rect.left += self.speed
elif self.direction == "U":
if self.rect.top > 0:
self.rect.top -= self.speed
elif self.direction == "D":
if self.rect.top + self.rect.height < MainGame.SCREEN_HEIFHT:
self.rect.top += self.speed

def randMove(self):
if self.step <= 0:
self.direction = self.randDirection()
self.step = 35
else:
self.move()
self.step -= 1

def shot(self):
num = random.randint(1, 1000)
if num <= 20:
return Bullet(self)

def randDirection(self):
num = random.randint(1, 4)
if num == 1:
return "U"
elif num == 2:
return "D"
elif num == 3:
return "L"
elif num == 4:
return "R"
# def displayEnemyTank(self): #展示坦克
# super().displayTank()


class Bullet():
def __init__(self, tank):
# 图片
self.image = pygame.image.load(r"D:\PyCharm练习文档\坦克大战图片\子弹.png")
# 方向(坦克方向)
self.direction = tank.direction
# 位置
self.rect = self.image.get_rect()
if self.direction == "U":
self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top - self.rect.top
elif self.direction == "D":
self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top + tank.rect.height
elif self.direction == "L":
self.rect.left = tank.rect.left - self.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top + tank.rect.width / 2 - self.rect.width / 2
elif self.direction == "R":
self.rect.left = tank.rect.left + tank.rect.width
self.rect.top = tank.rect.top + tank.rect.width / 2 - self.rect.width / 2
# 速度
self.speed = 7
# 用来记录子弹是否活着
self.live = True

def bulletMove(self):
if self.direction == "U":
if self.rect.top > 0:
self.rect.top -= self.speed
else:
# 修改状态值
self.live = False
elif self.direction == "D":
if self.rect.top < MainGame.SCREEN_HEIFHT - self.rect.height:
self.rect.top += self.speed
else:
# 修改状态值
self.live = False
elif self.direction == "L":
if self.rect.left > 0:
self.rect.left -= self.speed
else:
# 修改状态值
self.live = False
elif self.direction == "R":
if self.rect.left < MainGame.SCREEN_WIDTH - self.rect.width:
self.rect.left += self.speed
else:
# 修改状态值
self.live = False

def displayBullet(self): # 展示子弹
MainGame.window.blit(self.image, self.rect) # 如果是尖头子弹需考虑方向问题


class Explode():
def __init__(self):
pass

def displayExplode(self): # 展示爆炸效果
pass

lass Wall():
def __init__(self):
pass

def displayWall(self): # 展示墙壁
pass


class Music():
def __init__(self):
pass

def play(self):
pass


MainGame().startGame() # 基本实现,运行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值