2016-11-27 12 views
-2

私は学校プロジェクトのためのスペースインベーダーを再作成しています。スペースインベーダー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) 
+0

コード内のどの行がエラーになっているかを知るために、完全なスタックトレースを投稿できますか? – elveatles

答えて

0

ここでエラーが発生しているようです:

if math.hypot((bulletx[1]-mothershipx),(bullety[1]-mothershipy))<100: 
    score=score+10 

両方の配列の2番目のオブジェクトがヒットするかどうかを確認しようとします電子母船の値について、しかし、リストは最初ここ

でゼロの要素を持っているが、私のソリューションです:

for l in range(len(bulletx)): 
    drawbullet(bulletx[l],bullety[l]) 
    bullety[l]=bullety[l]-10 
    if math.hypot((bulletx[l]-mothershipx),(bullety[l]-mothershipy))<100: 
     score=score+10 

あなたが両方のリストに要素を持っていないのであれば、ループイマイチのために実行します。そして、これは2番目の要素だけでなく、すべての要素をチェックします。

なぜ複雑なのですか?リストのリストを作成するのはどうですか?

if event.type==KEYDOWN: 
    if event.key==K_SPACE: 
     firing=True 
     firinglocationx=playerx+20 
     bullets.append([firinglocationx,483}) 

弾丸を描画し、衝突チェック:母船とするために

for bullet in bullets: 
    drawbullet(bullet[0], bullet[1]) 
    bullet[1]=bullet[1]-1; 
    if math.hypot((bullet[0]-mothershipx),(bullet[1]-mothershipy))<100: 
     score=score+10 

をプレイヤーがスペースを押すと

​​

は弾丸を追加します。

は、箇条書きリストを開始します健康、可変健康を作成します。

あなたが1つのリストだけを使用した場合、私はあなたが、変数の数が必要になります「Nそれはオブジェクトカウントするように示唆されているように、または

for l in range(len(bulletx)): 
    drawbullet(bulletx[l],bullety[l]) 
    bullety[l]=bullety[l]-10 
    if math.hypot((bulletx[l]-mothershipx),(bullety[l]-mothershipy))<100: 
     score=score+10 
     mothershiphealth-=10 #whatever damage you want 
     bulletx.pop(l) #Delete bullet 
     bullety.pop(l) #Delete bullet 

:変更のカップルが含まれている必要が3210

mothershiphealth=100 

count=0; 
for bullet in bullets: 
    drawbullet(bullet[0],bullet0[1]) 
    bullet[1]=bullet[1]-10 
    if math.hypot((bullet[0]-mothershipx),(bullet[1]-mothershipy))<100: 
     score=score+10 
     mothershiphealth-=10 #whatever damage you want 
     bullet.pop(count) #Delete bullet 
    count+=1; 

そして、母船をunblitする:

if mothershiphealth > 0: 
    drawmothership(mothershipx,mothershipy) 

これが唯一のマザーズを描きますヒップがゼロよりも健康な場合

+0

ありがとうございましたuser7185318、ほとんどの部分でうまくいたようです。唯一の問題は、弾丸が母船に当たったときに、母船がヒットしたときに母船が消えないために、10ポイント以上追加することです。母船が弾丸に当たった後に母船を「掘り下げる」方法はありますか?もちろん –

+0

。私は私の答えを延長します – user7185318

+0

うん、それはそれをやった。助けてくれてありがとう! –

関連する問題