python列表溢出_[求助] python3.5.2出现列表索引溢出错误,求指点

本人python初学者,用python3.5.2写了个游戏小程序,出现列表索引溢出错误,可我实在看不出是哪里出了问题,特来请教高人指点。代码如下:

from tkinter import *

import random

import time

……

……

self.images_left = [

PhotoImage(file="figure-L1.gif"),

PhotoImage(file="figure-L2.gif"),

PhotoImage(file="figure-L3.gif")

]

self.images_right = [

PhotoImage(file="figure-R1.gif"),

PhotoImage(file="figure-R2.gif"),

PhotoImage(file="figure-R3.gif")

]

self.image = game.canvas.create_image(200, 470, \

image=self.images_left[0], anchor='nw')

self.x = -2

self.y = 0

self.current_image = 0

self.current_image_add = 1

self.last_time = time.time()

……

……

def animate(self):

if self.x != 0 and self.y == 0:

if time.time() - self.last_time > 0.1:

self.last_time = time.time()

self.current_image += self.current_image_add

if self.current_image == 2:

self.current_image_add = -1

elif self.current_image == 0:

self.current_iamge_add = 1

if self.x < 0:

if self.y != 0:

self.game.canvas.itemconfig(self.image, \

image=self.images_left[2])

else:

self.game.canvas.itemconfig(self.image, \

image=self.images_left[self.current_image])

elif self.x > 0:

if self.y != 0:

self.game.canvas.itemconfig(self.image, \

image=self.images_right[2])

else:

self.game.canvas.itemconfig(self.image, \

image=self.images_right[self.current_image])

……

……

运行结果是,游戏人物跑了几步,然后就停住了,出现如下错误信息:

Traceback (most recent call last):

File "C:\Users\pp\Desktop\stickman\stickman.py", line 350, in

g.mainloop()

File "C:\Users\pp\Desktop\stickman\stickman.py", line 52, in mainloop

sprite.move()

File "C:\Users\pp\Desktop\stickman\stickman.py", line 232, in move

self.animate()

File "C:\Users\pp\Desktop\stickman\stickman.py", line 214, in animate

image=self.images_left[self.current_image])

IndexError: list index out of range

可是代码里面列表索引只是在0,1,2之间循环啊,怎么会溢出呢?

是我的代码哪里写得不对吗?

亟盼指教,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值