pygame飞机游戏运行一段时间后就报错,请大佬们看一下

飞机游戏仍然运行是有报错求大神们帮忙, 谢谢!

这是代码:

import pygame
import random
import math

# 初始化
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("飞机大战2")
icon = pygame.image.load("./resource/ufo.png")
pygame.display.set_icon(icon)
bgimg = pygame.image.load("./resource/bg.png")

# 背景音乐
pygame.mixer.music.load("./resource/bg.wav")
pygame.mixer.music.play(-1)

# 射中音效
bao_sound = pygame.mixer.Sound("./resource/exp.wav")

# 飞机
playerimg = pygame.image.load("./resource/player.png")
playerx = 400
playery = 500
playerStep = 0

# 分数
score = 0
font = pygame.font.Font("freesansbold.ttf", 32)


def show_score():
    text = f"score:    {score}"
    score_render = font.render(text, True, (0, 255, 0))
    screen.blit(score_render, (10, 10))


# 游戏结束
is_over = False
over_font = pygame.font.Font("freesansbold.ttf", 64)


def check_is_over():
    if is_over:
        text = "GAME OVER"
        render = over_font.render(text, True, (255, 0, 0))
        screen.blit(render, (200, 250))


# 添加敌人
number_of_enemies = 6


class Enemy:

    def __init__(self):
        self.img = pygame.image.load("./resource/enemy.png")
        self.x = random.randint(200, 600)
        self.y = random.randint(50, 250)
        self.step = 0.5

    def reset(self):
        self.x = random.randint(200, 600)
        self.y = random.randint(50, 250)


enemies = []
for i in range(number_of_enemies):
    enemies.append(Enemy())


def distance(bx, by, ex, ey):
    a = bx - ex
    b = by - ey
    return math.sqrt(a * a + b * b)


class Bullet:
    def __init__(self):
        self.img = pygame.image.load("./resource/bullet.png")
        self.x = playerx + 16
        self.y = playery + 10
        self.step = 10

    def hit(self):
        global score
        for e in enemies:
            if distance(self.x, self.y, e.x, e.y) < 30:
                # 子弹命中
                bao_sound.play()
                bullets.remove(self)
                e.reset()
                score += 1
                print(score)


bullets = []


def show_bullets():
    bullets_to_remove = []
    for b in bullets:
        screen.blit(b.img, (b.x, b.y))
        b.hit()
        b.y -= b.step
        if b.y < 0:
            bullets_to_remove.append(b)
    for b in bullets_to_remove:
        bullets.remove(b)


def show_enemy():
    global is_over
    for e in enemies:
        screen.blit(e.img, (e.x, e.y))
        e.y += e.step
        if e.y > 450:
            is_over = True
            print("游戏结束啦")
            enemies.clear()


def move_player():
    global playerx
    playerx += playerStep
    # 防止飞机出界
    if playerx > 736:
        playerx = 736
    if playerx < 0:
        playerx = 0


# 游戏主循环
running = True
while running:
    screen.blit(bgimg, (0, 0))
    show_score()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        # 控制飞机移动
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_RIGHT]:
            playerStep = 5
        elif keys_pressed[pygame.K_LEFT]:
            playerStep = -5
        else:
            playerStep = 0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            # 其他事件处理逻辑
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                print("发射子弹")
                # 创建子弹
                b = Bullet()
                bullets.append(b)

    screen.blit(playerimg, (playerx, playery))
    move_player()
    show_enemy()
    show_bullets()
    check_is_over()
    pygame.display.update()

报错信息:

Traceback (most recent call last):
File “H:\python\作品\飞机大战2\planegame.py”, line 158, in
show_bullets()
File “H:\python\作品\飞机大战2\planegame.py”, line 102, in show_bullets
b.hit()
File “H:\python\作品\飞机大战2\planegame.py”, line 90, in hit
bullets.remove(self)
ValueError: list.remove(x): x not in list

讨论数量: 5

根据报错信息,是下面这一行出错了。

bullets.remove(self)
8个月前 评论
徵羽宫 (作者) 8个月前
徵羽宫 (作者) 8个月前
jianggaojun (楼主) 8个月前
jianggaojun (楼主) 8个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!