Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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
TypeError:列表索引必须是整数,而不是pygame.Surface-Python,pygame错误_Python_List_Pygame - Fatal编程技术网

TypeError:列表索引必须是整数,而不是pygame.Surface-Python,pygame错误

TypeError:列表索引必须是整数,而不是pygame.Surface-Python,pygame错误,python,list,pygame,Python,List,Pygame,我试图用Pygame(使用Python)制作一个破砖游戏,但我遇到了一个错误 代码: 导入pygame、sys、time、random 从pygame.locals导入* pygame.init() fpscall=pygame.time.Clock() 窗宽=450 窗高=650 mainwindow=pygame.display.set_模式((窗口宽度,窗口高度),0,32) pygame.display.set_标题('Luzion-Brick Breaker') blade=pygam

我试图用Pygame(使用Python)制作一个破砖游戏,但我遇到了一个错误

代码:

导入pygame、sys、time、random
从pygame.locals导入*
pygame.init()
fpscall=pygame.time.Clock()
窗宽=450
窗高=650
mainwindow=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
pygame.display.set_标题('Luzion-Brick Breaker')
blade=pygame.image.load('Brick Breaker-blade.png')
pallerect=palle.get_rect()
pableRect.topleft=(190575)
ball=pygame.image.load('ball.png')
ballrect=ball.get_rect()
ballrect.topleft=(195565)
cooltext=pygame.image.load('cooltext1.png')
cooltextrect=cooltext.get_rect()
cooltextrect.topleft=(0,0)
黑色=(0,0,0)
白色=(255,255,255)
红色=(255,0,0)
绿色=(0,128,0)
蓝色=(0,0255)
石灰=(0,255,0)
text颜色=白色
font=pygame.font.SysFont(无,48)
def displaytext(文本、字体、表面、x、y):
text=font.render(text,1,TEXTCOLOR)
textrect=text.get_rect()
textrect.topleft=(x,y)
surface.blit(text,textrect)
def waitforplayer():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
如果event.key==K_转义:
pygame.quit()
sys.exit()
返回
rb=pygame.image.load('redblock.png')
rb2=rb1=rb
级别1块=[rb、rb1、rb2]
rbrect=rb.get_rect().topleft=(0,0)
rb1rect=rb1.get_rect().topleft=(40,0)
rb2rect=rb2.get_rect().topleft=(80,0)
level1rects=[rbrect,rb1rect,rb2rect]
moveleft=False
moveright=False
速度=7
bmoveup=bmovedown=bmoveleft=bmoverlight=False
球速=8
mainwindow.blit(cooltext,cooltextrect)
pygame.display.update()
时间。睡眠(1)
displaytext('Level 1',字体,主窗口,150,100)
pygame.display.update()
时间。睡眠(1)
displaytext('按任意键开始…',字体,主窗口,22,200)
pygame.display.update()
waitforplayer()
尽管如此:
number=random.randint(0,1)
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
如果event.key==ord('a')或event.key==K_左:
moveleft=True
moveright=False
如果event.key==ord('d')或event.key==K_RIGHT:
moveleft=False
moveright=True
如果event.key==ord('g'):
bmoveup=True
如果数字==1:
bmoveleft=True
其他:
bOverlight=正确
如果event.type==KEYUP:
如果event.key==ord('a')或event.key==K_左:
moveleft=False
如果event.key==ord('d')或event.key==K_RIGHT:
moveright=False
如果moveleft和PackerRect.left>0:
左桨直行-=速度
如果moveright和PackerRect.right<窗口宽度:
右桨+=速度
如果BMOVERDOWN和ballrect.bottom<窗口高度:
ballrect.top+=球速
如果bmoveup和ballrect.top>0:
ballrect.top-=球速
如果bmoveleft和ballrect.left>0:
ballrect.left-=球速
如果bOverlight和ballrect.right<窗宽:
ballrect.right+=球速
如果ballrect.top=窗口高度:
bmovedown=不是bmovedown
b向上移动=不向上移动
主窗口。填充(白色)
主窗口。blit(拨片,拨片矩形)
主窗口。blit(ball,ballrect)
对于1级块[:]中的b:
main window.blit(level1block[b],level1rects[b])
对于Level1Block[:]中的x:
如果ballrect.colliderect(x.get_rect()):
1级锁。拆下(x)
bmovedown=不是bmovedown
b向上移动=不向上移动
bmoveleft=不是bmoveleft
bmoverlight=不bmoverlight
如果ballrect.Collide Rect(桨式Rect):
bmovedown=不是bmovedown
b向上移动=不向上移动
bmoveleft=不是bmoveleft
bmoverlight=不bmoverlight
pygame.display.update()
时钟滴答(35)
错误:

Traceback (most recent call last):
  File "C:/Python32/brick breaker", line 136, in <module>
    mainwindow.blit(level1blocks[b], level1rects[b])
TypeError: list indices must be integers, not pygame.Surface
回溯(最近一次呼叫最后一次):
文件“C:/Python32/brick breaker”,第136行,在
main window.blit(level1block[b],level1rects[b])
TypeError:列表索引必须是整数,而不是pygame.Surface
我有很多不同的方法来解决这个问题,但我还没有成功。 请帮忙。

当你接到电话时

for b in level1blocks[:]:
您正在浏览
level1blocks
中的每个项目,而不是浏览所有可能的索引。将此行替换为

for b in range(len(level1blocks)):

这应该可以解决您的问题。

level1blocks
是先前加载的图像列表,因此当您在其中循环时,
b
将成为每个图像,因此会出现错误。你以为这会怎么样?我也不知道你为什么在这里循环一份清单。
for b in range(len(level1blocks)):