【发布时间】: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: main.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: main.projectile object at 0x0000012B51DFE2E8> is not in list
【问题讨论】:
-
@FatihAkici 当我运行它时,它会打印出
-
请在运行
hitbaricade()之前与我们分享子弹和路障的外观。另请粘贴您的完整错误消息。 -
@FatihAkici 你好,我添加了完整的错误消息,你说分享“在你运行 hitbaricade() 之前子弹和路障是什么样子”是什么意思,你是说打印列表还是索引.对不起。
标签: python list object valueerror