我试着做一个简单的太空入侵者的版本,我一直遇到同样的错误
ValueError: list.remove(x): x not in list
试图在入侵者被击中后将其清除。
这是密码。
def killEnemies(bullets, enemies):
for bullet in bullets:
for x in enemies:
for y in x:
if bullet.top <= y.bottom and bullet.top >= y.top:
if bullet.left >= y.left and bullet.right <= y.right:
x.remove(y)
bullets.remove(bullet)
只有当if语句为True,并且控制台说错误发生在最后一行时,问题才会出现
下面是代码的其余部分
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((800, 660), 0, 32)
pygame.display.set_caption('Space Invaders')
white = (255, 255, 255)
black = (0, 0, 0)
shipx = 50
shipy = 630
DISPLAYSURF.fill(black)
timer = fpsClock.tick()
time = 0
direction = ''
bullets = []
bulletx = shipx + 25
bullety = shipy - 50
enemies = [[], [], [], [], [], [], []]
shields = []
def drawEnemies(enemies):
y = 0
for n in enemies:
x = 0
for f in range(7):
enemy = pygame.draw.rect(DISPLAYSURF, white, (30 + x, 40 + y, 75, 20))
n.append(enemy)
x += 110
y += 30
return enemies
def killEnemies(bullets, enemies):
for bullet in bullets:
for x in enemies:
for y in x:
if bullet.top <= y.bottom and bullet.top >= y.top:
if bullet.left >= y.left and bullet.right <= y.right:
x.remove(y)
bullets.remove(bullet)
def moveBullets(bullets):
for bullet in bullets:
bullet.top -= 15
for b in bullets:
pygame.draw.rect(DISPLAYSURF, white, b)
while True:
if direction == 'left':
shipx -= 8
bulletx -= 8
elif direction == 'right':
shipx += 8
bulletx += 8
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
key = pygame.key.get_pressed()
if key[K_LEFT]:
direction = 'left'
elif key[K_RIGHT]:
direction = 'right'
elif key[K_SPACE]:
bullet = pygame.draw.line(DISPLAYSURF, white, (bulletx, bullety), (bulletx, bullety - 25), 2)
bullets.append(bullet)
if event.type == KEYUP:
direction = ''
time += timer
DISPLAYSURF.fill(black)
pygame.draw.polygon(DISPLAYSURF, white, ((shipx, shipy), (shipx + 25, shipy - 50), (shipx + 50, shipy)), 1)
drawEnemies(enemies)
moveBullets(bullets)
killEnemies(bullets, enemies)
pygame.display.update()
fpsClock.tick(FPS)
目前没有回答
相关问题 更多 >
编程相关推荐