Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表索引超出pygame中项目符号的范围_Python_List_Pygame - Fatal编程技术网

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

Python 列表索引超出pygame中项目符号的范围,python,list,pygame,Python,List,Pygame,所以我是pygame的新手,我一直在做的项目是制作古老的外星入侵街机游戏。我知道我需要清理我的图片、显示、尺寸等等,所以不用担心,但我现在面临的问题是发射子弹,我将它们存储在列表中,然后删除它们。但我出现了错误“列表索引超出范围”。此错误显示在此代码的第50行。哦,这个错误只会在我一次有两个项目处于活动状态时出现。例如,我可以每秒或每秒射击1次,但如果我快速射击一点,两次同时在屏幕上移动,那么我就会得到错误,真正要看的是第42-52行、第88-90行和最后3行。此外,任何关于提高我的代码效率的建

所以我是pygame的新手,我一直在做的项目是制作古老的外星入侵街机游戏。我知道我需要清理我的图片、显示、尺寸等等,所以不用担心,但我现在面临的问题是发射子弹,我将它们存储在列表中,然后删除它们。但我出现了错误“列表索引超出范围”。此错误显示在此代码的第50行。哦,这个错误只会在我一次有两个项目处于活动状态时出现。例如,我可以每秒或每秒射击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])
导入pygame
pygame.init()
#键状态变量
keystate={'left':False,'right':False,'up':False,'down':False}
红色=(255,0,0)
黑色=(0,0,0)
绿色=(0255,0)
shipX=0
shipY=445
bulletsX=[]
bulletsY=[]
ship=pygame.image.load(“ship.png”)
ship=pygame.transform.scale(ship,(35,35))
bullet\u pic=pygame.image.load(“bullet.png”)
bullet_pic=pygame.transform.scale(bullet_pic,(25,25))
background=pygame.image.load(“background.png”)
background=pygame.transform.scale(background,(640400))
clock=pygame.time.clock()
screen=pygame.display.set_模式((640480))
屏幕光点(背景(0,0))
#船舶运动功能
def moveship_Xneg():
全球shipX
如果shipX>0:
shipX-=1
def moveship_Xpos():
全球shipX

如果shipX实际上,您可以在错误消息中找到错误<代码>列表索引超出范围

比如说

a = [1]
a.remove(1)
>>>a[0]
`IndexError: list index out of range`
在代码中

if 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
def del_bullet():
对于射程内的子弹(len(bulletsX)):
#下面是第50行,意识到它没有显示数字,我的错
尝试:

如果bulletsY[bullet]尝试了它,但它不起作用,请注意,当我调用del_bullet函数时,它的参数是list not emptyError,不仅list empty,而且如果没有元素在位置
bullet
中,它是如何工作的?bulletsY中的bulletsY不只是遍历每个位置吗?我知道我让它遍历bulletsX的len,但是每个列表的长度总是相同的。那么,如果项目符号只在列表的长度内工作,那么它是如何在列表之外遍历的呢?哦,我也做了更详细的编辑。我只有在一次有两颗子弹的时候才会犯这个错误这很奇怪,我发誓我完全按照你说的做了,只是再试一次。好吧,很酷,我想了些办法。我误解了如何使用len,然后使用号码调用元素。我需要从透镜中减去1。。。好了,这就停止了错误,还有一些奇怪的事情,我在调试语句中添加了打印子弹线的语句。即使子弹已经通过最小y轴并被删除,它仍会不断打印出跳线仍在减少有效的briliant。抱歉在阅读你的更新lol之前发表评论。谢谢你使用它也停止了坐标改变,因为它超过了我的最小y,所以我假设它现在完全删除了。非常感谢你,现在让我把外星入侵者带进来
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