【问题标题】:How to fix ValueError: <__main__.projectile object at 0x0000019B405CE128> is not in list如何修复 ValueError: <__main__.projectile object at 0x0000019B405CE128> 不在列表中
【发布时间】:2019-12-02 04:23:56
【问题描述】:

我对 python 还很陌生,我正在尝试使用 pygame 制作一个简单的太空入侵者游戏,但是每当我尝试弹出列表中的一个项目符号时,我都会不断收到此错误。我已经能够在其他碰撞中弹出子弹,但无法让这个工作。

def hitbaricade():
    global bullets, barricades, enemybullets
    for bullet in bullets:
        for barricade in barricades:
            if abs(barricade.x + (barricade.width//2) - bullet.x) < barricade.width//2 + 2 and abs(barricade.y + barricade.height//2 - bullet.y) < barricade.height//2 + 2:
                bullets.pop(bullets.index(bullet)) #this one breaks
                barricades.pop(barricades.index(barricade)) #this one works


    for ebullet in enemybullets:
        for barricade in barricades:
            if abs(barricade.x + (barricade.width//2) - ebullet.x) < barricade.width//2 + 5 and abs(barricade.y + barricade.height - ebullet.y) < defender.height:
                enemybullets.pop(enemybullets.index(ebullet)) #this one breaks
                barricades.pop(barricades.index(barricade)) #this one works

这里是设置项目符号列表的地方,该列表在此之前声明为空列表

if keys[pygame.K_SPACE] and bulletDelay > 10 and len(bullets) < 1:
            bullets.append(projectile(defender.x + (defender.width//2), 460, -1, 10))
            bulletDelay = 0

在这里我设置了路障列表,并且该列表之前也被定义为空列表

def baricadeSetup():
    global barricades
    x = 45
    y = 410
    x2 = x
    width = 5
    height = 5
    loop = 0
    for i in range(0,4):
        for i in range(0,30):
            barricades.append(shield(x,y,width,height))
            loop += 1
            x += 5
            if loop >= 10:
                loop = 0
                x = x2
                y += 5
        x2 += 125
        x = x2
        y = 410
        loop = 0

我试图获取列表中的项目会弹出的输出,但我会收到错误:ValueError: ma​​in.projectile object at 0x000002D6982A5F28> is not in list

以下是完整的错误消息: pygame 1.9.6 来自 pygame 社区的您好。 https://www.pygame.org/contribute.html 回溯(最近一次通话最后): 文件“E:\Python\Scripts\Projects\Login System\spaceInvaders.py”,第 317 行,在 主要的() 文件“E:\Python\Scripts\Projects\Login System\spaceInvaders.py”,第 298 行,在 main 击中路障() 文件“E:\Python\Scripts\Projects\Login System\spaceInvaders.py”,第 250 行,在 hitbaricade bullets.pop(bullets.index(bullet)) #这个破 ValueError: ma​​in.projectile object at 0x0000012B51DFE2E8> is not in list

【问题讨论】:

  • @FatihAkici 当我运行它时,它会打印出
  • 请在运行 hitbaricade() 之前与我们分享子弹和路障的外观。另请粘贴您的完整错误消息。
  • @FatihAkici 你好,我添加了完整的错误消息,你说分享“在你运行 hitbaricade() 之前子弹和路障是什么样子”是什么意思,你是说打印列表还是索引.对不起。

标签: python list object valueerror


【解决方案1】:

原因是你一遍又一遍地弹出同一个项目。您应该将其从内循环中移出。

并且您应该避免在迭代时修改列表。

def hitbaricade():
    global bullets, barricades, enemybullets
    bullets_removed = set()
    barricades_removed = set()
    for bullet in bullets:
        for barricade in barricades:
            if abs(barricade.x + (barricade.width//2) - bullet.x) < barricade.width//2 + 2 and abs(barricade.y + barricade.height//2 - bullet.y) < barricade.height//2 + 2:
                bullets_removed.add(bullet)
                barricades_removed.add(barricade)
    bullets = [bullet for bullet in bullets if bullet not in bullets_removed]
    barricades = [barricade for barricade in barricades if barricade not in barricades_removed]

    ebullets_removed = set()
    barricades_removed = set()
    for ebullet in enemybullets:
        for barricade in barricades:
            if abs(barricade.x + (barricade.width//2) - ebullet.x) < barricade.width//2 + 5 and abs(barricade.y + barricade.height - ebullet.y) < defender.height:
                ebullets_removed(ebullet)
                barricades_removed(barricade)
    enemybullets = [ebullet for ebullet in enemybullets if ebullet not in ebullets_removed]
    barricades = [barricade for barricade in barricades if barricade not in barricades_removed]

【讨论】:

  • 只是从编程的角度来看,请告诉我你的真正目的,以便我给你一些算法建议。
  • 您好,我正在尝试在路障对象和子弹对象相互碰撞时破坏它们。感谢您的帮助
  • @JoshC。一颗子弹会击中多个路障吗?换句话说,如果只有一颗子弹击中所有路障,你会移除所有路障吗?
  • 我有多个路障聚集在一起,当我只创建一个路障时,错误不再存在。有没有办法保持多个路障?如果没有,谢谢:)
  • 感谢您的回复。我的意思是,一个路障会与其他路障重叠吗?我顺便添加了代码。如果它不起作用,请告诉我,因为没有真实数据我无法对其进行测试。
猜你喜欢
  • 1970-01-01
  • 2018-12-09
  • 2018-04-22
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 2021-10-13
  • 2014-10-14
  • 1970-01-01
相关资源
最近更新 更多