嗨,伙计,我的游戏有问题,当你运行它时我收到这个错误,“第 283 行,在 bullets.pop(0)”,“IndexError:从空列表中弹出”,我认为这是因为项目符号列表是空的,它不能弹出它们,但我不知道如何修复它。如果你能帮助我,我将不胜感激。这是代码,谢谢。import pygameimport mathimport randompygame.init()win = pygame.display.set_mode((800, 400))pygame.display.set_caption("Pandemic")clock = pygame.time.Clock()score = 0walkup = [pygame.image.load("Soldier-1up.png"), pygame.image.load("Soldier-2up.png"), pygame.image.load("Soldier-3up.png")]walkdown = [pygame.image.load("Soldier-1down.png"), pygame.image.load("Soldier-2down.png"), pygame.image.load("Soldier-3down.png")]walkright = [pygame.image.load("Soldier-1right.png"), pygame.image.load("Soldier-2right.png"), pygame.image.load("Soldier-3right.png")]walkleft = [pygame.image.load("Soldier-1left.png"), pygame.image.load("Soldier-2left.png"), pygame.image.load("Soldier-3left.png")]bg = pygame.image.load("map.png")ch = pygame.image.load("Soldier-1up.png")bulletimg = pygame.image.load("bullet.png")walkvirus1 = [pygame.image.load("virus1.png"), pygame.image.load("virus2.png"), pygame.image.load("virus3.png")]walkvirus2 = [pygame.image.load("1virus1.png"), pygame.image.load("1virus2.png"), pygame.image.load("1virus3.png")]background1 = pygame.image.load("menu.png")class player(object): def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.vel = 5 self.walkcount = 0 self.right = False self.left = False self.up = False self.down = False self.standing = True self.hitbox = (self.x, self.y, self.width, self.height) self.life = 3
1 回答
月关宝盒
TA贡献1772条经验 获得超5个赞
添加一条if语句,使程序仅在列表中有项目符号时才for执行循环:
if bullets:
for bullet in bullets:
if virus1.visible == True:
if bullet.y - bullet.radius < virus1.hitbox[1] + virus1.hitbox[3] and virus1.y + bullet.radius > virus1.hitbox[1]:
if bullet.x + bullet.radius > virus1.hitbox[0] and bullet.x - bullet.radius < virus1.hitbox[0] + \
virus1.hitbox[2]:
virus1.hit()
score += 1
bullets.pop(0)
添加回答
举报
0/150
提交
取消