gpt4 book ai didi

python - 这种类型错误 :list indices must be integers or slices, not str 的解决方案是什么?

转载 作者:行者123 更新时间:2023-12-04 08:34:31 25 4
gpt4 key购买 nike

我是初学者,没有按照标准提问请见谅
几天前,我创建了一个程序,但它显示以下错误,我做了一些研究,但没有一个答案不是此类问题,错误如下:

if apple["x"]==snakeCoords[head]["x"] and apple["y"]==snakeCoords[head]["y"]:

TypeError: list indices must be integers or slices, not str
我的代码是:
def run2(score,run):
global appleX,appleY,snakeCoords
startX=random.randint(20,cellWidth)
startY=random.randint(20,cellHeight)
apple=randomlocation()
appleX=apple['x']*cell_s
appleY=apple['y']*cell_s
snakeCoords=[{"x":startX,"y":startY},
{"x":startX-1,"y":startY},
{"x":startX-2,"y":startY}]
direction=RIGHT
assert win_w % cell_s==0
assert win_h % cell_s==0


while run:

if snakeCoords[head]['x']==19 or snakeCoords[head]['y']==19:
gameover(window)
pygame.time.wait(500)
run=False
terminate()
sys.exit()
if snakeCoords[head]['x']==win_w-20 or snakeCoords[head]['y']==win_h-20:
gameover(window)
pygame.time.wait(500)
run=False
terminate()
sys.exit()

for body in snakeCoords[1:]:
if snakeCoords[head]['x']==body['x'] and snakeCoords[head]['y']==body['y']:
gameover(window)
pygame.time.wait(500)
terminate()
sys.exit()


if direction==UP:
move={'x':snakeCoords[head]['x']-1,'y':snakeCoords[head]['y']}
if direction==DOWN:
move={'x':snakeCoords[head]['x']+1,'y':snakeCoords[head]['y']}
if direction==RIGHT:
move={'x':snakeCoords[head]['x'],'y':snakeCoords[head]['y']+1}
if direction==LEFT:
move={'x':snakeCoords[head]['x'],'y':snakeCoords[head]['y']-1}
snakeCoords.insert(0,move)



if apple['x']==snakeCoords[head]['x'] and apple['y']==snakeCoords[head]['y']:
apple=randomlocation()
drawgame.drawapple(red)
score+=1
if appleX==snakeCoords[head]['x'] and direction==RIGHT:
newhead=[{'x':startX-3,'y':startY}]
snakeCoords+=newhead
if appleX==snakeCoords[head]['x'] and direction==LEFT:
newhead=[{'x':startX+3,'y':startY}]
snakeCoords+=newhead
if appleY==snakeCoords[head]['y'] and direction==UP:
newhead=[{'x':startX,'y':startY+3}]
snakeCoords+=newhead
if appleY==snakeCoords[head]['y'] and direction==DOWN:
newhead=[{'x':startX,'y':startY-3}]
snakeCoords+=newhead
pygame.display.update()


if score==10:
gameover(window)
pygame.time.wait(500)



for event in pygame.event.get():
if event.type==pygame.QUIT:
run=False
terminate()
sys.exit()



if event.type==KEYDOWN:
if event.key==K_RIGHT and direction!=LEFT:
direction=RIGHT
elif event.key==K_LEFT and direction!=RIGHT:
direction=LEFT
elif event.key==K_UP and direction!=DOWN:
direction=UP
elif event.key==K_DOWN and direction!=UP:
direction=DOWN
elif event.key==K_ESCAPE :
terminate()
sys.exit()
else:
print("Invalid Key Pressed")


if __name__=="__main__":
main(run)
在苹果中,代码是这样的:
apple=randomlocation()
def randomlocation():
return {"x":random.randint(20,cellWidth),
"y":random.randint(20,cellHeight)}
在蛇形中,代码是这样的:
startX=random.randint(20,cellWidth)
startY=random.randint(20,cellHeight)
snakeCoords=[{"x":startX,"y":startY},
{"x":startX-1,"y":startY},
{"x":startX-2,"y":startY}]
和单元格宽度和高度是:
win_w  =640
win_h =640
cell_s =20
cellWidth=int(win_w/cell_s)-1
cellHeight=int(win_h/cell_s)-1
请指导我。

最佳答案

我建议像这样编码

run = True
while run :
for event in pygame.event.get():
if event.type==pygame.QUIT:
run = False

关于python - 这种类型错误 :list indices must be integers or slices, not str 的解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64863278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com