我一直在得到错误:line 102, in <module> shot[2] += shotyIndexError: list index out of range
在我尝试编写要抛出的 Dagger 的代码中,如果超出范围则将其删除。我了解列表错误的含义;我不确定它如何影响我的代码,任何帮助将不胜感激!
这是我的代码中与该问题有关的部分:
daggers = []
daggerPlayer = pygame.image.load(daggerImage.png)
for shot in daggers:
index = 0
shotx = math.cos(shot[0])*10
shoty = math.sin(shot[0])*10
shot[1] += shotx
shot[2] += shoty
if (shot[1] < -64) or (shot[1] > 900) or (shot[2] < -64) or (shot[2]> 600):
daggers.pop(index)
index +=1
for shoot in daggers:
daggerOne = pygame.transform.rotate(daggerPlayer, 360 - shoot[0]*57.29)
screen.blit(daggerOne, (shoot[1], shoot[2]))
编辑:这是我的代码的全部
#Load Images
peterPlayer = pygame.image.load('pixelPirateOne.png)'
nightBackground = pygame.image.load ('NightSky.png')
daggerPlayer = pygame.image.load('daggerImage.png')
#Settting Variables for Moving Character
xPlayer = 200
yPlayer = 275
dxPlayer = 0
dyPlayer = 0
playerPosition = (200,275)
accuracyShot = [0,0]
daggers = []
def quitGame():
pygame.quit()
sys.exit()
go = True
while go:
#Blit The Background
screen.blit(nightBackground, (0,0))
#Learned about atan2 from --> https://docs.python.org/2/library/math.html
#Allows To Rotate Player With Mouse
mousePosition = pygame.mouse.get_pos()
angle = math.atan2(mousePosition[1]-(yPlayer+32),mousePosition[0]-(xPlayer+26))
playerRotate = pygame.transform.rotate(peterPlayer, 360-angle*57.29)
playerPositionNew = (xPlayer-playerRotate.get_rect().width/2, yPlayer-playerRotate.get_rect().height/2)
screen.blit(playerRotate, playerPositionNew)
pygame.display.update()
#Quit Game
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
#Move Player- Note: Make this portion fluid; it's choppy as of now
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dxPlayer -= 15
elif event.key == pygame.K_RIGHT:
dxPlayer += 15
elif event.key == pygame.K_UP:
dyPlayer -= 15
elif event.key == pygame.K_DOWN:
dyPlayer += 15
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
dxPlayer = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
dyPlayer = 0
xPlayer = xPlayer + dxPlayer
yPlayer = yPlayer + dyPlayer
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
mousePosition = pygame.mouse.get_pos()
accuracyShot[1] += 1
daggers.append([math.atan2(mousePosition[1]-(playerPositionNew[1]+32), mousePosition[0]-(playerPositionNew[0]+26)), playerPositionNew[1]+32])
#Learned about cos and sin in python from --> https://docs.python.org/2/library/math.html
#Learned about .pop from --> https://docs.python.org/2/tutorial/datastructures.html
for shot in daggers:
index = 0
shotx = math.cos(shot[0])*10
shoty = math.sin(shot[0])*10
shot[1] += shotx
shot[2] += shoty
if (shot[1] < -64) or (shot[1] > 900) or (shot[2] < -64) or (shot[2]> 600):
daggers.pop(index)
index +=1
for shoot in daggers:
daggerOne = pygame.transform.rotate(daggerPlayer, 360 - shoot[0]*57.29)
screen.blit(daggerOne, (shoot[1], shoot[2]))
最佳答案
附加到 Dagger 的列表只有两个元素,但是通过调用shot[2]
试图访问第三个元素,这就是为什么会出现该错误的原因。这两个元素应该是shot [0]和shot [1]。我相信您在screen.blit(daggerOne, (shoot[1], shoot[2]))
中也会犯同样的错误。