How to fix ValueError: <__main__.projectile object at 0x0000019B405CE128> is not in list
How to fix ValueError: <__main__.projectile object at 0x0000019B405CE128> is not in list
我是 python 的新手,我正在尝试使用 pygame 制作一个简单的 space 入侵者游戏,但每当我尝试弹出其中一个时,我总是收到此错误列表中的项目符号。我已经能够用其他碰撞弹出子弹,但无法让这个碰撞起作用。
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: <main.projectile object at 0x000002D6982A5F28> is not在列表中
完整的错误信息如下:
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
hitbaricade()
文件 "E:\Python\Scripts\Projects\Login System\spaceInvaders.py",第 250 行,在 hitbaricade 中
bullets.pop(bullets.index(bullet)) #这个断了
ValueError:<main.projectile object at 0x0000012B51DFE2E8> is not in list
原因是您一遍又一遍地弹出相同的项目。您应该将其从内部循环中移出。
并且在迭代列表时应避免修改列表。
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]
我是 python 的新手,我正在尝试使用 pygame 制作一个简单的 space 入侵者游戏,但每当我尝试弹出其中一个时,我总是收到此错误列表中的项目符号。我已经能够用其他碰撞弹出子弹,但无法让这个碰撞起作用。
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: <main.projectile object at 0x000002D6982A5F28> is not在列表中
完整的错误信息如下: 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 hitbaricade() 文件 "E:\Python\Scripts\Projects\Login System\spaceInvaders.py",第 250 行,在 hitbaricade 中 bullets.pop(bullets.index(bullet)) #这个断了 ValueError:<main.projectile object at 0x0000012B51DFE2E8> is not in list
原因是您一遍又一遍地弹出相同的项目。您应该将其从内部循环中移出。
并且在迭代列表时应避免修改列表。
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]