列表索引超出 pygame 中的项目符号范围
list index is out of range for bullets in pygame
所以我是 pygame 的新手,我一直在做的项目是制作旧的外星人入侵街机游戏。我知道我需要清理我的图片和显示以及尺寸和其他东西所以不用担心,但我现在正在努力解决的问题是发射我的子弹,我将它们存储在一个列表中然后删除它们。但我想出了错误 "list index out of range"。此错误显示在此代码的第 50 行。哦,这个错误只会在我同时激活 2 颗子弹时出现。例如,我可以每秒或每两秒射击 1 次,但如果我快速射击一点,并且有两个同时在屏幕上移动,那么我会得到错误,真正要看的东西在第 42-52、88-90 行,以及最后 3. 非常感谢任何关于提高我的代码效率的建议
import pygame
pygame.init()
#keystate variables
keystate={'left':False,'right':False,'up':False,'down':False}
red=(255,0,0)
black=(0,0,0)
green=(0,255,0)
shipX=0
shipY=445
bulletsX=[]
bulletsY=[]
ship=pygame.image.load("ship.png")
ship=pygame.transform.scale(ship,(35,35))
bullet_pic=pygame.image.load("bullet.png")
bullet_pic=pygame.transform.scale(bullet_pic,(25,25))
backdrop=pygame.image.load("backdrop.png")
backdrop=pygame.transform.scale(backdrop,(640,400))
clock=pygame.time.Clock()
screen=pygame.display.set_mode((640,480))
screen.blit(backdrop,(0,0))
# ship movement functions
def moveship_Xneg():
global shipX
if shipX>0:
shipX-=1
def moveship_Xpos():
global shipX
if shipX<605:
shipX+=1
def moveship_Yneg():
global shipY
if shipY<445:
shipY+=1
def moveship_Ypos():
global shipY
if shipY>400:
shipY-=1
#gunfire definitions
def move_bullet():
for bullet in range(len(bulletsX)):
bulletsY[bullet]-=2
screen.blit(bullet_pic,(bulletsX[bullet],bulletsY[bullet]))
pygame.display.update()
def del_bullet():
for bullet in range(len(bulletsX)):
#below this is line 50, realized it didn't show numbers, my bad
if bulletsY[bullet]<=-10:
bulletsY.remove(bulletsY[bullet])
bulletsX.remove(bulletsX[bullet])
# ship movement changes
def start_ship(): #draws the starting position of the ship
screen.blit(ship,(0,445))
pygame.display.update()
def draw_newship(): #draws the new ship and updates the screen
screen.blit(ship,(shipX,shipY))
#screen.blit(backdrop,(shipX
#print(shipX,shipY)
pygame.display.update()
#def del_oldship(): #deletes the old ship
start_ship()
#Main Loop
running=True
while running:
clock.tick(350)
#checks keystroke events
for event in pygame.event.get():
#quits the program
if event.type==pygame.QUIT:
running=False
pygame.quit()
#KEYDOWN CHECKS
if event.type==pygame.KEYDOWN:
#Movement variable changes
if event.key==pygame.K_LEFT:
keystate['left']=True
if event.key==pygame.K_RIGHT:
keystate['right']=True
if event.key==pygame.K_DOWN:
keystate['down']=True
if event.key==pygame.K_UP:
keystate['up']=True
#Action per event
if event.key==pygame.K_SPACE:
bulletsX.append(shipX+17.5)
bulletsY.append(shipY)
#KEYUP CHECKS
if event.type==pygame.KEYUP:
#movement variable changes
if event.key==pygame.K_LEFT:
keystate['left']=False
if event.key==pygame.K_RIGHT:
keystate['right']=False
if event.key==pygame.K_DOWN:
keystate['down']=False
if event.key==pygame.K_UP:
keystate['up']=False
# pygame event processing ends
if running==True:
#performs an action per each loop dependant on keystate variables
if keystate['left']==True:
#del_oldship()
moveship_Xneg()
draw_newship()
if keystate['right']==True:
#del_oldship()
moveship_Xpos()
draw_newship()
if keystate['down']==True:
#del_oldship()
moveship_Yneg()
draw_newship()
if keystate['up']==True:
#del_oldship()
moveship_Ypos()
draw_newship()
if bulletsX!=[]:
del_bullet()
move_bullet()
#for coord in range(len(bulletsX)):
#print(bulletsX[coord],bulletsY[coord])
实际上您可以在错误消息中找到错误。 List index out of range
例如
a = [1]
a.remove(1)
>>>a[0]
`IndexError: list index out of range`
在您的代码中,
if bulletsY[bullet] <= -10
这次可能是bulletsY
中没有元素在bullet
位置或者列表bulletsY
可能是空的
所以你可以这样试试
if bulletsY and bulletsY[bullet] <= -10
希望对您有所帮助
已更新
试试这个
def del_bullet():
for bullet in range(len(bulletsX)):
#below this is line 50, realized it didn't show numbers, my bad
try:
if bulletsY[bullet]<=-10:
bulletsY.remove(bulletsY[bullet])
bulletsX.remove(bulletsX[bullet])
except:pass
所以我是 pygame 的新手,我一直在做的项目是制作旧的外星人入侵街机游戏。我知道我需要清理我的图片和显示以及尺寸和其他东西所以不用担心,但我现在正在努力解决的问题是发射我的子弹,我将它们存储在一个列表中然后删除它们。但我想出了错误 "list index out of range"。此错误显示在此代码的第 50 行。哦,这个错误只会在我同时激活 2 颗子弹时出现。例如,我可以每秒或每两秒射击 1 次,但如果我快速射击一点,并且有两个同时在屏幕上移动,那么我会得到错误,真正要看的东西在第 42-52、88-90 行,以及最后 3. 非常感谢任何关于提高我的代码效率的建议
import pygame
pygame.init()
#keystate variables
keystate={'left':False,'right':False,'up':False,'down':False}
red=(255,0,0)
black=(0,0,0)
green=(0,255,0)
shipX=0
shipY=445
bulletsX=[]
bulletsY=[]
ship=pygame.image.load("ship.png")
ship=pygame.transform.scale(ship,(35,35))
bullet_pic=pygame.image.load("bullet.png")
bullet_pic=pygame.transform.scale(bullet_pic,(25,25))
backdrop=pygame.image.load("backdrop.png")
backdrop=pygame.transform.scale(backdrop,(640,400))
clock=pygame.time.Clock()
screen=pygame.display.set_mode((640,480))
screen.blit(backdrop,(0,0))
# ship movement functions
def moveship_Xneg():
global shipX
if shipX>0:
shipX-=1
def moveship_Xpos():
global shipX
if shipX<605:
shipX+=1
def moveship_Yneg():
global shipY
if shipY<445:
shipY+=1
def moveship_Ypos():
global shipY
if shipY>400:
shipY-=1
#gunfire definitions
def move_bullet():
for bullet in range(len(bulletsX)):
bulletsY[bullet]-=2
screen.blit(bullet_pic,(bulletsX[bullet],bulletsY[bullet]))
pygame.display.update()
def del_bullet():
for bullet in range(len(bulletsX)):
#below this is line 50, realized it didn't show numbers, my bad
if bulletsY[bullet]<=-10:
bulletsY.remove(bulletsY[bullet])
bulletsX.remove(bulletsX[bullet])
# ship movement changes
def start_ship(): #draws the starting position of the ship
screen.blit(ship,(0,445))
pygame.display.update()
def draw_newship(): #draws the new ship and updates the screen
screen.blit(ship,(shipX,shipY))
#screen.blit(backdrop,(shipX
#print(shipX,shipY)
pygame.display.update()
#def del_oldship(): #deletes the old ship
start_ship()
#Main Loop
running=True
while running:
clock.tick(350)
#checks keystroke events
for event in pygame.event.get():
#quits the program
if event.type==pygame.QUIT:
running=False
pygame.quit()
#KEYDOWN CHECKS
if event.type==pygame.KEYDOWN:
#Movement variable changes
if event.key==pygame.K_LEFT:
keystate['left']=True
if event.key==pygame.K_RIGHT:
keystate['right']=True
if event.key==pygame.K_DOWN:
keystate['down']=True
if event.key==pygame.K_UP:
keystate['up']=True
#Action per event
if event.key==pygame.K_SPACE:
bulletsX.append(shipX+17.5)
bulletsY.append(shipY)
#KEYUP CHECKS
if event.type==pygame.KEYUP:
#movement variable changes
if event.key==pygame.K_LEFT:
keystate['left']=False
if event.key==pygame.K_RIGHT:
keystate['right']=False
if event.key==pygame.K_DOWN:
keystate['down']=False
if event.key==pygame.K_UP:
keystate['up']=False
# pygame event processing ends
if running==True:
#performs an action per each loop dependant on keystate variables
if keystate['left']==True:
#del_oldship()
moveship_Xneg()
draw_newship()
if keystate['right']==True:
#del_oldship()
moveship_Xpos()
draw_newship()
if keystate['down']==True:
#del_oldship()
moveship_Yneg()
draw_newship()
if keystate['up']==True:
#del_oldship()
moveship_Ypos()
draw_newship()
if bulletsX!=[]:
del_bullet()
move_bullet()
#for coord in range(len(bulletsX)):
#print(bulletsX[coord],bulletsY[coord])
实际上您可以在错误消息中找到错误。 List index out of range
例如
a = [1]
a.remove(1)
>>>a[0]
`IndexError: list index out of range`
在您的代码中,
if bulletsY[bullet] <= -10
这次可能是bulletsY
中没有元素在bullet
位置或者列表bulletsY
可能是空的
所以你可以这样试试
if bulletsY and bulletsY[bullet] <= -10
希望对您有所帮助
已更新
试试这个
def del_bullet():
for bullet in range(len(bulletsX)):
#below this is line 50, realized it didn't show numbers, my bad
try:
if bulletsY[bullet]<=-10:
bulletsY.remove(bulletsY[bullet])
bulletsX.remove(bulletsX[bullet])
except:pass