Python 遍历列表(索引超出范围)

Python 遍历列表(索引超出范围),python,python-3.x,pygame,Python,Python 3.x,Pygame,快速总结一下我正在创建的内容:这是一个游戏,其中一艘外星人宇宙飞船以一定的边界在屏幕(如戴尔徽标/加载屏幕)周围反弹,使其保持在屏幕顶部附近。在屏幕底部附近有一艘玩家飞船,它必须以点击的方式射击敌人的太空入侵者,同时左右移动(但目前我仍在使用键盘/鼠标同时工作,因为事件只在队列的顶部进行)。母舰也会从你下方发射母牛。如果你抓住了那头牛,你就会得到分数。如果你没能躲过一次,你将失去分数和生命。如果你用“网”抓住一个,你会得到分数 我遇到的问题是这个错误(cowRect=(cow_x[I],cow_

快速总结一下我正在创建的内容:这是一个游戏,其中一艘外星人宇宙飞船以一定的边界在屏幕(如戴尔徽标/加载屏幕)周围反弹,使其保持在屏幕顶部附近。在屏幕底部附近有一艘玩家飞船,它必须以点击的方式射击敌人的太空入侵者,同时左右移动(但目前我仍在使用键盘/鼠标同时工作,因为事件只在队列的顶部进行)。母舰也会从你下方发射母牛。如果你抓住了那头牛,你就会得到分数。如果你没能躲过一次,你将失去分数和生命。如果你用“网”抓住一个,你会得到分数

我遇到的问题是这个错误(
cowRect=(cow_x[I],cow_y[I],127,76)
IndexError:list index超出范围(我认为这可能是由于程序试图在列表仍然为空时遍历列表造成的,尽管在“扫描”列表时,列表中似乎有项目

我的一些代码片段(大约170行,所以我不会全部发布):

开始-

cowList = []
statusList = []
cow_x = []
cow_y = []
cows = []
主回路内部-

if hits >= hitsNeeded and time.time() >= currentTime + 1:
        cowList.append(cownumber)
        cownumber += 1
        statusList.append(3)
        cow_x.append(random.randint(0, 573))
        cow_y.append(700)
for i in statusList:
        cowRect = (cow_x[i], cow_y[i], 127, 76)
        if cow_y[i] + 111 < 0:
            statusList[i] = 0  #offscreen
        if cowRect.colliderect(missileRect):
            statusList[i] = 1  #exploded
            points -= 15
        netRect = (net_x, net_y, 127, 127)
        if cowRect.colliderect(netRect):
            points += 90
            screen.blit(milkplus, (cow_x[i], cow_y[i]))
            powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
            powerup.play()
            shotNet = 0
            statusList[i] = 2  #caught
        if cowRect.colliderect(playerRect):
            points -= 10
            lives -= 1
            statusList[i] = 4  #player collision

    for i in statusList:
        if statusList[i] == 3:  #alive
            screen.blit(cow, (cow_x[i], cow_y[i]))
            cow_y[i] -= cowSpeed
if hits >= hitsNeeded and time.time() >= currentTime + 1:
    cows.append(Cow(random.randint(0, 573), 700))
for cow in cows:
    cowRect = (cow.x, cow.y, 127, 76)
    if cow.y + 111 < 0:
        cow.status = 'offscreen'
    if cowRect.colliderect(missileRect):
        cow.status = 'exploded'
        points -= 15
    netRect = (net_x, net_y, 127, 127)
    if cowRect.colliderect(netRect):
        points += 90
        screen.blit(milkplus, (cow.x, cow.y))
        powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
        powerup.play()
        shotNet = 0
        cow.status = 'caught'  #caught
    if cowRect.colliderect(playerRect):
        points -= 10
        lives -= 1
        cow.status = 'collision'

for cow in cows:
    if cow.status == 'alive':
        screen.blit(cow_pic, (cow.x, cow.y))
        cow.y -= cowSpeed
也在主回路内-

if hits >= hitsNeeded and time.time() >= currentTime + 1:
        cowList.append(cownumber)
        cownumber += 1
        statusList.append(3)
        cow_x.append(random.randint(0, 573))
        cow_y.append(700)
for i in statusList:
        cowRect = (cow_x[i], cow_y[i], 127, 76)
        if cow_y[i] + 111 < 0:
            statusList[i] = 0  #offscreen
        if cowRect.colliderect(missileRect):
            statusList[i] = 1  #exploded
            points -= 15
        netRect = (net_x, net_y, 127, 127)
        if cowRect.colliderect(netRect):
            points += 90
            screen.blit(milkplus, (cow_x[i], cow_y[i]))
            powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
            powerup.play()
            shotNet = 0
            statusList[i] = 2  #caught
        if cowRect.colliderect(playerRect):
            points -= 10
            lives -= 1
            statusList[i] = 4  #player collision

    for i in statusList:
        if statusList[i] == 3:  #alive
            screen.blit(cow, (cow_x[i], cow_y[i]))
            cow_y[i] -= cowSpeed
if hits >= hitsNeeded and time.time() >= currentTime + 1:
    cows.append(Cow(random.randint(0, 573), 700))
for cow in cows:
    cowRect = (cow.x, cow.y, 127, 76)
    if cow.y + 111 < 0:
        cow.status = 'offscreen'
    if cowRect.colliderect(missileRect):
        cow.status = 'exploded'
        points -= 15
    netRect = (net_x, net_y, 127, 127)
    if cowRect.colliderect(netRect):
        points += 90
        screen.blit(milkplus, (cow.x, cow.y))
        powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
        powerup.play()
        shotNet = 0
        cow.status = 'caught'  #caught
    if cowRect.colliderect(playerRect):
        points -= 10
        lives -= 1
        cow.status = 'collision'

for cow in cows:
    if cow.status == 'alive':
        screen.blit(cow_pic, (cow.x, cow.y))
        cow.y -= cowSpeed
状态列表中的i的
:
cowRect=(cow_x[i],cow_y[i],127,76)
如果cow_y[i]+111<0:
状态列表[i]=0#屏幕外
如果cowRect.colliderect(MisselRect):
状态列表[i]=1#已分解
点数-=15
netRect=(净x,净y,127127)
如果cowRect.collide rect(netRect):
积分+=90
blit(milkplus,(cow_x[i],cow_y[i]))
powerup=pygame.mixer.Sound(“C:/Python35/powerup.mp3”)
通电。播放()
shotNet=0
状态列表[i]=2#已捕获
如果cowRect.collide rect(playerRect):
分数-=10
寿命-=1
状态列表[i]=4#玩家冲突
对于状态列表中的i:
如果状态列表[i]==3:#活动
blit(cow,(cow_x[i],cow_y[i]))
cow_y[i]=cowSpeed
是的,我确实意识到我并不真的需要有4种状态的牛,它只是帮助我保持头脑有条理(这也适用于其他一些事情)


如果我犯了一些错误,我很抱歉,我已经很长时间没有在这里讨论了。

您看到的问题是由于
for
循环在Python中的工作方式造成的。它们遍历列表的内容,而不是列表索引。如评论中所述,您可以通过简单地对范围内的i(len(statusList))
执行
来修复错误,但我想建议您使用稍微不同的策略来编码有关奶牛的信息,方法是拥有一个奶牛列表,而不是四个关于奶牛的列表

class Cow:
    def __init__(x, y):
        self.status = 'alive'
        self.x = x
        self.y = y
开始-

cowList = []
statusList = []
cow_x = []
cow_y = []
cows = []
主回路内部-

if hits >= hitsNeeded and time.time() >= currentTime + 1:
        cowList.append(cownumber)
        cownumber += 1
        statusList.append(3)
        cow_x.append(random.randint(0, 573))
        cow_y.append(700)
for i in statusList:
        cowRect = (cow_x[i], cow_y[i], 127, 76)
        if cow_y[i] + 111 < 0:
            statusList[i] = 0  #offscreen
        if cowRect.colliderect(missileRect):
            statusList[i] = 1  #exploded
            points -= 15
        netRect = (net_x, net_y, 127, 127)
        if cowRect.colliderect(netRect):
            points += 90
            screen.blit(milkplus, (cow_x[i], cow_y[i]))
            powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
            powerup.play()
            shotNet = 0
            statusList[i] = 2  #caught
        if cowRect.colliderect(playerRect):
            points -= 10
            lives -= 1
            statusList[i] = 4  #player collision

    for i in statusList:
        if statusList[i] == 3:  #alive
            screen.blit(cow, (cow_x[i], cow_y[i]))
            cow_y[i] -= cowSpeed
if hits >= hitsNeeded and time.time() >= currentTime + 1:
    cows.append(Cow(random.randint(0, 573), 700))
for cow in cows:
    cowRect = (cow.x, cow.y, 127, 76)
    if cow.y + 111 < 0:
        cow.status = 'offscreen'
    if cowRect.colliderect(missileRect):
        cow.status = 'exploded'
        points -= 15
    netRect = (net_x, net_y, 127, 127)
    if cowRect.colliderect(netRect):
        points += 90
        screen.blit(milkplus, (cow.x, cow.y))
        powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
        powerup.play()
        shotNet = 0
        cow.status = 'caught'  #caught
    if cowRect.colliderect(playerRect):
        points -= 10
        lives -= 1
        cow.status = 'collision'

for cow in cows:
    if cow.status == 'alive':
        screen.blit(cow_pic, (cow.x, cow.y))
        cow.y -= cowSpeed
也在主回路内-

if hits >= hitsNeeded and time.time() >= currentTime + 1:
        cowList.append(cownumber)
        cownumber += 1
        statusList.append(3)
        cow_x.append(random.randint(0, 573))
        cow_y.append(700)
for i in statusList:
        cowRect = (cow_x[i], cow_y[i], 127, 76)
        if cow_y[i] + 111 < 0:
            statusList[i] = 0  #offscreen
        if cowRect.colliderect(missileRect):
            statusList[i] = 1  #exploded
            points -= 15
        netRect = (net_x, net_y, 127, 127)
        if cowRect.colliderect(netRect):
            points += 90
            screen.blit(milkplus, (cow_x[i], cow_y[i]))
            powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
            powerup.play()
            shotNet = 0
            statusList[i] = 2  #caught
        if cowRect.colliderect(playerRect):
            points -= 10
            lives -= 1
            statusList[i] = 4  #player collision

    for i in statusList:
        if statusList[i] == 3:  #alive
            screen.blit(cow, (cow_x[i], cow_y[i]))
            cow_y[i] -= cowSpeed
if hits >= hitsNeeded and time.time() >= currentTime + 1:
    cows.append(Cow(random.randint(0, 573), 700))
for cow in cows:
    cowRect = (cow.x, cow.y, 127, 76)
    if cow.y + 111 < 0:
        cow.status = 'offscreen'
    if cowRect.colliderect(missileRect):
        cow.status = 'exploded'
        points -= 15
    netRect = (net_x, net_y, 127, 127)
    if cowRect.colliderect(netRect):
        points += 90
        screen.blit(milkplus, (cow.x, cow.y))
        powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
        powerup.play()
        shotNet = 0
        cow.status = 'caught'  #caught
    if cowRect.colliderect(playerRect):
        points -= 10
        lives -= 1
        cow.status = 'collision'

for cow in cows:
    if cow.status == 'alive':
        screen.blit(cow_pic, (cow.x, cow.y))
        cow.y -= cowSpeed
对于奶牛中的奶牛:
cowRect=(cow.x,cow.y,127,76)
如果奶牛y+111<0:
cow.status='屏幕外'
如果cowRect.colliderect(MisselRect):
cow.status=‘爆炸’
点数-=15
netRect=(净x,净y,127127)
如果cowRect.collide rect(netRect):
积分+=90
屏幕光点(milkplus,(cow.x,cow.y))
powerup=pygame.mixer.Sound(“C:/Python35/powerup.mp3”)
通电。播放()
shotNet=0
cow.status=“已捕获”#已捕获
如果cowRect.collide rect(playerRect):
分数-=10
寿命-=1
cow.status='collision'
对于奶牛中的奶牛:
如果cow.status=='alive':
屏幕blit(奶牛图片,(奶牛x,奶牛y))
cow.y-=cowSpeed

这将使事情在未来变得更容易。如果您对类不满意,则可以在
集合中获得类似的行为。namedtuple

您看到的问题是由于
for
循环在Python中的工作方式。它们遍历列表的内容,而不是列表索引。如评论中所述,您可以通过简单地对范围内的i(len(statusList))
执行
来修复错误,但我想建议您使用稍微不同的策略来编码有关奶牛的信息,方法是拥有一个奶牛列表,而不是四个关于奶牛的列表

class Cow:
    def __init__(x, y):
        self.status = 'alive'
        self.x = x
        self.y = y
开始-

cowList = []
statusList = []
cow_x = []
cow_y = []
cows = []
主回路内部-

if hits >= hitsNeeded and time.time() >= currentTime + 1:
        cowList.append(cownumber)
        cownumber += 1
        statusList.append(3)
        cow_x.append(random.randint(0, 573))
        cow_y.append(700)
for i in statusList:
        cowRect = (cow_x[i], cow_y[i], 127, 76)
        if cow_y[i] + 111 < 0:
            statusList[i] = 0  #offscreen
        if cowRect.colliderect(missileRect):
            statusList[i] = 1  #exploded
            points -= 15
        netRect = (net_x, net_y, 127, 127)
        if cowRect.colliderect(netRect):
            points += 90
            screen.blit(milkplus, (cow_x[i], cow_y[i]))
            powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
            powerup.play()
            shotNet = 0
            statusList[i] = 2  #caught
        if cowRect.colliderect(playerRect):
            points -= 10
            lives -= 1
            statusList[i] = 4  #player collision

    for i in statusList:
        if statusList[i] == 3:  #alive
            screen.blit(cow, (cow_x[i], cow_y[i]))
            cow_y[i] -= cowSpeed
if hits >= hitsNeeded and time.time() >= currentTime + 1:
    cows.append(Cow(random.randint(0, 573), 700))
for cow in cows:
    cowRect = (cow.x, cow.y, 127, 76)
    if cow.y + 111 < 0:
        cow.status = 'offscreen'
    if cowRect.colliderect(missileRect):
        cow.status = 'exploded'
        points -= 15
    netRect = (net_x, net_y, 127, 127)
    if cowRect.colliderect(netRect):
        points += 90
        screen.blit(milkplus, (cow.x, cow.y))
        powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
        powerup.play()
        shotNet = 0
        cow.status = 'caught'  #caught
    if cowRect.colliderect(playerRect):
        points -= 10
        lives -= 1
        cow.status = 'collision'

for cow in cows:
    if cow.status == 'alive':
        screen.blit(cow_pic, (cow.x, cow.y))
        cow.y -= cowSpeed
也在主回路内-

if hits >= hitsNeeded and time.time() >= currentTime + 1:
        cowList.append(cownumber)
        cownumber += 1
        statusList.append(3)
        cow_x.append(random.randint(0, 573))
        cow_y.append(700)
for i in statusList:
        cowRect = (cow_x[i], cow_y[i], 127, 76)
        if cow_y[i] + 111 < 0:
            statusList[i] = 0  #offscreen
        if cowRect.colliderect(missileRect):
            statusList[i] = 1  #exploded
            points -= 15
        netRect = (net_x, net_y, 127, 127)
        if cowRect.colliderect(netRect):
            points += 90
            screen.blit(milkplus, (cow_x[i], cow_y[i]))
            powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
            powerup.play()
            shotNet = 0
            statusList[i] = 2  #caught
        if cowRect.colliderect(playerRect):
            points -= 10
            lives -= 1
            statusList[i] = 4  #player collision

    for i in statusList:
        if statusList[i] == 3:  #alive
            screen.blit(cow, (cow_x[i], cow_y[i]))
            cow_y[i] -= cowSpeed
if hits >= hitsNeeded and time.time() >= currentTime + 1:
    cows.append(Cow(random.randint(0, 573), 700))
for cow in cows:
    cowRect = (cow.x, cow.y, 127, 76)
    if cow.y + 111 < 0:
        cow.status = 'offscreen'
    if cowRect.colliderect(missileRect):
        cow.status = 'exploded'
        points -= 15
    netRect = (net_x, net_y, 127, 127)
    if cowRect.colliderect(netRect):
        points += 90
        screen.blit(milkplus, (cow.x, cow.y))
        powerup = pygame.mixer.Sound("C:/Python35/powerup.mp3")
        powerup.play()
        shotNet = 0
        cow.status = 'caught'  #caught
    if cowRect.colliderect(playerRect):
        points -= 10
        lives -= 1
        cow.status = 'collision'

for cow in cows:
    if cow.status == 'alive':
        screen.blit(cow_pic, (cow.x, cow.y))
        cow.y -= cowSpeed
对于奶牛中的奶牛:
cowRect=(cow.x,cow.y,127,76)
如果奶牛y+111<0:
cow.status='屏幕外'
如果cowRect.colliderect(MisselRect):
cow.status=‘爆炸’
点数-=15
netRect=(净x,净y,127127)
如果cowRect.collide rect(netRect):
积分+=90
屏幕光点(milkplus,(cow.x,cow.y))
powerup=pygame.mixer.Sound(“C:/Python35/powerup.mp3”)
通电。播放()
shotNet=0
cow.status=“已捕获”#已捕获
如果cowRect.collide rect(playerRect):
分数-=10
寿命-=1
cow.status='collision'
对于奶牛中的奶牛:
如果cow.status=='alive':
屏幕blit(奶牛图片,(奶牛x,奶牛y))
cow.y-=cowSpeed

这将使事情在未来变得更容易。如果您对类不满意,则可以在
集合中获得类似的行为。namedtuple

也许您可以更改为
范围内的i(len(statusList)):
,因为您需要索引而不是值。作为一个非常简单的调试步骤,您可以在循环顶部打印
i
len(cow\x)
镜头(奶牛)
。如果
i
等于或大于
len
值中的任何一个,那么这就是您的问题。对于范围内的i(len(statusList)),您可以更改为
,因为您需要索引而不是值。作为一个非常简单的调试步骤,您可以在循环顶部打印
i
len(cow\x)
镜头(奶牛)
。如果
i
等于或大于
len
值中的任何一个,那么这就是您的问题。这是一个非常有用的解决方案,并且它起到了作用