Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 类型错误:';int';对象没有属性'__获取项目';错误_Python_List_Pygame - Fatal编程技术网

Python 类型错误:';int';对象没有属性'__获取项目';错误

Python 类型错误:';int';对象没有属性'__获取项目';错误,python,list,pygame,Python,List,Pygame,我有一个列表来存储游戏中子弹的X和Y,当按下空格键时,它会在列表中添加一个X和Y,然后调用这两个值并绘制它们。我知道我需要在他们离开屏幕后删除他们,但是当我点击空格时,这个错误会弹出 TypeError:'int' object has no attribute '__getitem__' 这是我的密码: #start system import pygame, sys, random from pygame.locals import* pygame.init() fps=50 Clo

我有一个列表来存储游戏中子弹的X和Y,当按下空格键时,它会在列表中添加一个X和Y,然后调用这两个值并绘制它们。我知道我需要在他们离开屏幕后删除他们,但是当我点击空格时,这个错误会弹出

   TypeError:'int' object has no attribute '__getitem__'
这是我的密码:

#start system
import pygame, sys, random
from pygame.locals import*
pygame.init()
fps=50
Clock=pygame.time.Clock()#use fps as system time
#set up a list of STUFF
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 100, 0)
RED   = ( 100, 0, 0)
YELLOW= (255,255,0)
ORANGE= (255,165,0)
screensizex=800
screensizey=600
bullets=[]
screenarea=pygame.display.set_mode((screensizex,screensizey))
pygame.display.set_caption("space invaders")
player_x=250
# end of variable init, begin main loop!
while 1==1:
    #key roster
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_d and player_x<screensizex-25:
                player_x+=25
            if event.key==K_a:
                player_x-=25
            if event.key==K_SPACE:
                bullets.insert(player_x,665)
    #drawing to screen             
    screenarea.fill(WHITE)
    for bullet in bullets:
        pygame.draw.rect(screenarea,ORANGE,Rect(bullet[0],bullet[1],5,10))
    pygame.draw.rect(screenarea,GREEN,(0,screensizey-100,screensizex,100))
    pygame.draw.rect(screenarea,RED,(player_x,screensizey-125,25,25))
    pygame.display.update()
    Clock.tick(fps)
#启动系统
导入pygame、sys、random
从pygame.locals导入*
pygame.init()
fps=50
Clock=pygame.time.Clock()#使用fps作为系统时间
#建立一个物品清单
黑色=(0,0,0)
白色=(255,255,255)
绿色=(0,100,0)
红色=(100,0,0)
黄色=(255255,0)
橙色=(255165,0)
屏幕尺寸=800
屏幕尺寸=600
项目符号=[]
screenarea=pygame.display.set_模式((screensizex,screensizey))
pygame.display.set_标题(“太空入侵者”)
玩家x=250
#变量init结束,开始主循环!
而1==1:
#关键名册
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:

如果event.key==K_d和player_x看起来您想将元组放入列表中,那么:

bullets.insert(player_x,665)
应该是:

bullets.append((player_x,665))

这抛出了另一个错误代码,因为子弹.insert需要一个参数?@user3423925可能您实际需要的是
子弹.append(…)
?这些似乎是比较初级的问题,所以我建议您通读一下,例如,免费介绍基本python的所有内容。通常我没有这些问题,但我已经为此奋斗了几个小时。我得到了各种各样的错误代码,我尝试了各种不同的组合。我要读一读谢谢:)当你尝试
bullet[0]
时,它会引发这个异常,因为bullet是一个
int
对象,尝试对它进行索引会产生你看到的异常(对于python3,它是
'int'对象不是可下标的
)。