猿问

IndexError:从pygame中的空列表中弹出

嗨,伙计,我的游戏有问题,当你运行它时我收到这个错误,“第 283 行,在 bullets.pop(0)”,“IndexError:从空列表中弹出”,我认为这是因为项目符号列表是空的,它不能弹出它们,但我不知道如何修复它。如果你能帮助我,我将不胜感激。这是代码,谢谢。

import pygame

import math

import random

pygame.init()


win = pygame.display.set_mode((800, 400))

pygame.display.set_caption("Pandemic")


clock = pygame.time.Clock()

score = 0



walkup = [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

慕盖茨4494581
浏览 89回答 1
1回答

月关宝盒

添加一条if语句,使程序仅在列表中有项目符号时才for执行循环:if bullets:&nbsp; &nbsp; for bullet in bullets:&nbsp; &nbsp; &nbsp; &nbsp; if virus1.visible == True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if bullet.y - bullet.radius < virus1.hitbox[1] + virus1.hitbox[3] and virus1.y + bullet.radius > virus1.hitbox[1]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if bullet.x + bullet.radius > virus1.hitbox[0] and bullet.x - bullet.radius < virus1.hitbox[0] + \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; virus1.hitbox[2]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; virus1.hit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bullets.pop(0)
随时随地看视频慕课网APP

相关分类

Python
我要回答