列表索引超出pygam中项目符号的范围

2024-09-28 22:37:15 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我是pygame的新手,我一直从事的项目是制作旧的外星人入侵街机游戏。我知道我需要清理我的图片、显示、尺寸等等,所以不用担心,但我现在面临的问题是发射子弹,我把它们存储在一个列表中,然后删除它们。但是我出现了错误“列表索引超出范围”。这个错误出现在代码的第50行。哦,这个错误只在我一次有两颗子弹的时候出现。举个例子,我可以每一秒或两秒拍一次,但如果我快速射击一点,两次同时在屏幕上移动,那么我就会得到一个错误,真正要看的是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])

Tags: keyeventfalsetrueifdefpygameship
1条回答
网友
1楼 · 发布于 2024-09-28 22:37:15

实际上,您可以在错误消息中找到错误。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

相关问题 更多 >