我一直在收到错误:
line 102, in <module>
shot[2] += shoty
IndexError: list index out of range
在我的代码中,我试图编写要抛出的匕首,如果它超出界限则删除。我明白列表错误意味着什么;我不确定它是如何影响我的代码的,我们将非常感谢任何帮助!
这是我的代码中与问题相关的部分:
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]))
答案 0 :(得分:0)
您附加到匕首的列表只有两个元素,但通过调用shot[2]
您尝试访问第三个元素,这就是您收到该错误的原因。这两个元素应该被拍摄[0]和拍摄[1]。我相信你也会在screen.blit(daggerOne, (shoot[1], shoot[2]))
中犯同样的错误。