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

时间:2020-07-09 15:35:52

标签: python python-3.x list 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


    def draw(self, win):
        if self.walkcount + 1 >= 60:
            self.walkcount = 0

        if not (self.standing):
            if self.left:
                win.blit(walkleft[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
            elif self.right:
                win.blit(walkright[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
            elif self.up:
                win.blit(walkup[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
            elif self.down:
                win.blit(walkdown[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
        else:
            if self.up:
                win.blit(walkup[0], (self.x, self.y))
            elif self.down:
                win.blit(walkdown[0], (self.x, self.y))
            elif self.right:
                win.blit(walkright[0], (self.x, self.y))
            elif self.left:
                win.blit(walkleft[0], (self.x, self.y))
        self.hitbox = (self.x, self.y, self.width, self.height)
        #pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
    def hit(self):
        self.x = 0
        self.y = 0
        self.life -= 1
        self.walkcount = 0
        font1 = pygame.font.SysFont("comicsans", 100)
        text = font1.render("-1 out of 3 lifes.", 1, (255,0,0))
        win.blit(text, (400 - (text.get_width()/2),150))
        pygame.display.update()
        pygame.time.delay(1000)
        if self.life == 0:
            pygame.quit()

class Bullet(object):
    def __init__(self, x, y, radius, color, vertfacing, hortfacing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.speed = 8
        self.vertfacing = vertfacing
        self.hortfacing = hortfacing

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)

    def move(self):
        if self.hortfacing == -1:
            self.x -= self.speed
        elif self.hortfacing == 1:
            self.x += self.speed
        elif self.vertfacing == 1:
            self.y += self.speed
        elif self.vertfacing == -1:
            self.y -= self.speed

class enemy(object):
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.walkcount = 0
        self.vel = 2
        self.life = 3
        self.hitbox = (self.x, self.y, self.width, self.height)
        self.health = 15
        self.visible = True

    def draw(self, win):
        self.move()
        if self.visible:
            if self.walkcount + 1 >= 60:
                self.walkcount = 0
            if self.vel > 0:
                win.blit(walkvirus1[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
            else:
                win.blit(walkvirus1[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
            pygame.draw.rect(win, (250, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65, 10))
            pygame.draw.rect(win, (0, 250, 0), (self.hitbox[0], self.hitbox[1] - 20, 65 - ((65/15) * (15 - self.health)), 10))
            self.hitbox = (self.x, self.y, self.width, self.height)
            #pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

    def move(self):
        dx, dy = man.x - self.x, man.y - self.y
        dist = math.hypot(dx, dy)
        dx, dy = dx / dist, dy / dist
        self.x += dx * self.vel
        self.y += dy * self.vel
    def hit(self):
        self.walkcount = 0
        if self.health > 0:
            self.health -= 1
        elif self.life > 0:
            self.x = random.randint(400, 600)
            self.y = random.randint(800, 1000)
            self.health = 15
            self.life -= 1
        else:
            self.visible = False

        print("hit")

class enemy1(object):
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.walkcount = 0
        self.vel = 2
        self.hitbox = (self.x, self.y, self.width, self.height)
        self.health = 30
        self. life = 3
        self.visible = True

    def draw(self, win):
        self.move()
        if self.visible:
            if self.walkcount + 1 >= 60:
                self.walkcount = 0
            if self.vel > 0:
                win.blit(walkvirus2[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
            else:
                win.blit(walkvirus2[self.walkcount // 20], (self.x, self.y))
                self.walkcount += 1
            pygame.draw.rect(win, (250, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 65, 10))
            pygame.draw.rect(win, (0, 250, 0), (self.hitbox[0], self.hitbox[1] - 20, 65 - ((65/30) * (30 - self.health)), 10))
            self.hitbox = (self.x, self.y, self.width, self.height)
            #pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

    def move(self):
        dx, dy = man.x - self.x, man.y - self.y
        dist = math.hypot(dx, dy)
        dx, dy = dx / dist, dy / dist
        self.x += dx * self.vel
        self.y += dy * self.vel
    def hit(self):
        self.walkcount = 0
        if self.health > 0:
            self.health -= 1
        elif self.life > 0:
            self.x = random.randint(400, 600)
            self.y = random.randint(800, 1000)
            self.health = 30
            self.life -= 1
        else:
            self.visible = False

        print("hit1")




def RedrawGame():
    win.blit(bg, (0, 0))
    text = font.render("score: " + str(score), 1, (0,0,0))
    win.blit(text, (380, 10))
    man.draw(win)
    virus1.draw(win)
    virus2.draw(win)
    for bullet in bullets:
        bullet.draw(win)
    pygame.display.update()

#Loop
font = pygame.font.SysFont("comicsans",30,True)
man = player(400, 100, 64, 64)
virus1 = enemy(40, 110, 64, 64, 450)
virus2 = enemy1(40, 10, 64, 64, 450)
shoot = 0
bullets = []
run = True
menu = True
while run:

    while menu:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    menu = False
        win.fill((0, 0, 0))
        clock.tick(60)
        win.blit(background1, (0, 0))

        pygame.display.update()


    clock.tick(60)

    if virus1.visible == True:
        if man.hitbox[1] < virus1.hitbox[1] + virus1.hitbox[3] and man.hitbox[1] + man.hitbox[3] > virus1.hitbox[1]:
            if man.hitbox[0] + man.hitbox[2] > virus1.hitbox[0] and man.hitbox[0] < virus1.hitbox[0] + virus1.hitbox[2]:
                man.hit()

    if virus2.visible == True:
        if man.hitbox[1] < virus2.hitbox[1] + virus2.hitbox[3] and man.hitbox[1] + man.hitbox[3] > virus2.hitbox[1]:
            if man.hitbox[0] + man.hitbox[2] > virus2.hitbox[0] and man.hitbox[0] < virus2.hitbox[0] + virus2.hitbox[2]:
                man.hit()


    if shoot > 0:
        shoot += 1
    if shoot > 3:
        shoot = 0

    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             run = False

    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)
        if virus2.visible == True:
            if bullet.y - bullet.radius < virus2.hitbox[1] + virus2.hitbox[3] and virus2.y + bullet.radius > virus2.hitbox[1]:
                if bullet.x + bullet.radius > virus2.hitbox[0] and bullet.x - bullet.radius < virus2.hitbox[0] + \
                        virus2.hitbox[2]:
                    virus2.hit()
                    score += 1
                    bullets.pop(0)

        if bullet.x < 800 and bullet.x > 0 and bullet.y < 400 and bullet.y > 0:
            bullet.move()
        else:
            bullets.remove(bullet)

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE] and shoot == 0:
        hortfacing, vertfacing = 0, 0
        if man.left:
            hortfacing = -1
        elif man.right:
            hortfacing = 1
        elif man.up:
            vertfacing = -1
        elif man.down:
            vertfacing = 1

        if len(bullets) < 5:
            bullet = Bullet(round(man.x + man.width // 2),
                                round(man.y + man.height // 2), 6, (255, 165, 0),
                                vertfacing, hortfacing)
            bullets.append(bullet)
            shoot = 1

    if keys[pygame.K_a] and man.x >= man.vel:
        man.x -= man.vel
        man.right = False
        man.left = True
        man.up = False
        man.down = False
        man.standing = False

    elif keys[pygame.K_d] and man.x < 800 - man.width:
        man.x += man.vel
        man.right = True
        man.left = False
        man.up = False
        man.down = False
        man.standing = False

    elif keys[pygame.K_s] and man.y < 400 - man.height:
        man.y += man.vel
        man.right = False
        man.left = False
        man.up = False
        man.down = True
        man.standing = False

    elif keys[pygame.K_w] and man.y >= man.vel:
        man.y -= man.vel
        man.right = False
        man.left = False
        man.up = True
        man.down = False
        man.standing = False

    else:
        man.standing = True
        man.walkcount = 0

    RedrawGame()

pygame.quit()

1 个答案:

答案 0 :(得分:1)

添加一条if语句,以便该程序仅在列表中有 are 个项目符号时执行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)