私は学校プロジェクトのためのスペースインベーダーを再作成しています。スペースインベーダーIndexError:リストインデックスが範囲外になっています
敵の1をヒットし、スコアを追加するために弾丸を取得しようとすると、私はエラーを取得する
IndexError: list index out of range.
ここで問題を再現する私のゲームの簡易版です。
import pygame, sys
from pygame.locals import *
import math
pygame.init()
FPS=30
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((628, 602),0,32)
FILL=(0,162,232)
BLACK=(0,0,0)
font=pygame.font.Font('freesansbold.ttf',22)
mothership=pygame.image.load('mothership.png').convert()
mothership.set_colorkey(FILL)
bullet=pygame.image.load('bullet.png').convert()
player=pygame.image.load('player.png').convert()
player.set_colorkey(FILL)
def drawbullet(bulletx,bullety):
screen.blit(bullet,(bulletx,bullety))
def drawplayer():
screen.blit(player,(playerx,playery))
def drawmothership(mothershipx,mothershipy):
screen.blit(mothership,(mothershipx,mothershipy))
bulletx=[]
bullety=[]
score=0
mothershipx=-30
mothershipy=200
playerx=84
playery=483
firing=False
while True:
screen.fill(BLACK)
drawplayer()
drawmothership(mothershipx,mothershipy)
mothershipx=mothershipx+7
message=''+str(score)+''
text=font.render(message,True,FILL)
screen.blit(text,(143,59))
if mothershipx>=628:
mothershipx=-30
for l in range(len(bulletx)):
drawbullet(bulletx[l],bullety[l])
bullety[l]=bullety[l]-10
if math.hypot((bulletx[1]-mothershipx),(bullety[1]-mothershipy))<100:
score=score+10
pygame.display.update()
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
keys=pygame.key.get_pressed()
if keys[K_LEFT] and playerx>0:
playerx=playerx-5
if keys[K_RIGHT] and playerx<583:
playerx=playerx+5
if event.type==KEYDOWN:
if event.key==K_SPACE:
firing=True
firinglocationx=playerx+20
bulletx.append(firinglocationx)
bullety.append(483)
fpsClock.tick(FPS)
コード内のどの行がエラーになっているかを知るために、完全なスタックトレースを投稿できますか? – elveatles