正在学python 按照教学的坦克大战编码学习,学习到改变坦克方向的时候出错。各种查找解决不了。
import pygame
_display = pygame.display
Color_Black = pygame.Color(0, 0, 0)
Color_Red = pygame.Color(255, 0, 0)
version = 'v1.6'
class MainGame():#主模块
window = None # 游戏主窗口
Screen_Height = 700#屏幕高度、宽度
Screen_width = 1000
#创建我方坦克
TANK_P1 = None#
def __init__(self):
pass
def startGame(self):#开始游戏
pygame.display.init()
MainGame.window = _display.set_mode([MainGame.Screen_width,MainGame.Screen_Height])#创建窗口加载窗口
# 创建我方坦克
MainGame.TANK_P1 = Tank(450,350) #为啥这里引用了Tank类的语句没有出错。why???
#设置游戏标题
_display.set_caption("坦克大战"+version)#设置一下游戏标题
#self.getTextSurface()
#持续刷新窗口
while True:
#给窗口完成一个填充颜色
MainGame.window.fill(Color_Black)
#在循环中持续完成事件的获取
self.getEvent()
#将绘制文字得到的小画布,粘贴到窗口中
MainGame.window.blit(self.getTextSurface("剩余敌方坦克%d辆"%5),(5,5))
#将我方坦克加入到窗口中
MainGame.TANK_P1.displayTank() #这里引用了下面的出错语句
#窗口的刷新
_display.update()
#获取程序运行期间所有时间(鼠标事件、键盘时间)
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.move()
elif event.key == pygame.K_RIGHT:
print("坦克向右调头,移动")
MainGame.TANK_P1.direction = 'R'
MainGame.TANK_P1.move()
elif event.key == pygame.K_UP:
print("坦克向上调头,移动")
MainGame.TANK_P1.direction = 'U'
MainGame.TANK_P1.move()
elif event.key == pygame.K_DOWN:
print("坦克向下调头,移动")
MainGame.TANK_P1.direction = 'D'
MainGame.TANK_P1.move()
#左上角文字绘制的功能
def endGame(self):#结束游戏
print("谢谢使用")
exit()#结束python解释器
class Tank():#坦克模块
def __init__(self,left,top):
self.image = {
'U':pygame.image.load('E:/python程序学习/pythonProject25/图片库/tank-U.gif'),
'D':pygame.image.load('E:/python程序学习/pythonProject25/图片库/tank-D.gif'),
'L':pygame.image.load('E:/python程序学习/pythonProject25/图片库/tank-L.gif'),
'R':pygame.image.load('E:/python程序学习/pythonProject25/图片库/tank-R.gif')
}
** self.direction = 'U'
self.image = self.image[self.direction] #为啥在这块就正常执行了?**
#坦克所在的区域 Rect->
self.rect = self.image.get_rect()
#指定坦克初始化位置 分别距X,Y轴的位置
self.rect.left = left
self.rect.top = top
def displayTank(self):#展示坦克(将坦克这个surface绘制到窗口中 blit()
#1.重新设置坦克的图片
try:
** self.image = self.image[self.direction] #就是这里执行不下去**
except BaseException as e:
print(e.args)
print(type(e))
#2.将坦克加入到窗口中
MainGame.window.blit(self.image,self.rect)
MainGame().startGame()
("'pygame.Surface' object is not subscriptable",)
<class 'TypeError'>
("'pygame.Surface' object is not subscriptable",)
<class 'TypeError'>
("'pygame.Surface' object is not subscriptable",)
<class 'TypeError'>
我一度以为是图片格式的问题,将图片格式转换成jpg、gif、jfif等。显然不是。。
查了查再想了想难道是图片太大了?我把图片像素调整到现在的100,40,还是不行。
在这里我直接把self.direction改成'R' : self.image = self.image['R']
结果仍然是报出来同样的错误。
甚至我照抄上面的语句。给self.direction赋值。。还是出错。同样的错误循环
self.direction = 'R'
self.image = self.image[self.direction]
为什么这个语句在while循环中引用就出错。头大
正常转向
self.image定义太乱了,上面定义成字典,下面又将字典中的value对象赋给self.image
try里出错就是self.image中值发生了改变,不再是字典了
#try里改成这样:
if not isinstance(self.image, dict) :
self.image
else:
self.image = self.image[self.direction]