pygame中出现意外缩进

时间:2016-05-16 08:59:26

标签: python-2.7 pygame indentation

当我运行以下游戏时,我收到错误“意外缩进”,但是当你查看代码时,它是完全正确的。

错误发生在del evilGuy[-1]。缩进是正确的我仍然得到这个错误。

修改

代码已经改变了一点。即使现在,错误发生在:del evilGuy[-1]显示意外缩进。

def evilMove(evilGuy):
    evilCoords=[]
    #deadZones=[]
    #Returns either -1, 0 or 1
    randomMovex=random.randrange(-1,2)
    randomMovey=random.randrange(-1,2)
    newCell={'x':evilGuy[0]['x']+randomMovex,'y':evilGuy[0]['y']+randomMovey}
    if (newCell['x']<0 or newCell['y']<0 or newCell['x']>cellSize or newCell['y']>display_height/cellSize):
        newCell={'x':display_width/(2*cellSize),'y':display_height/(2*cellSize)

    del evilGuy[-1]

    evilCoords.append(newCell['x'])
    evilCoords.append(newCell['x'])
    deadZones.append(evilCoords)
    evilGuy.insert(0,newCell)

解决

错误是函数evilMove中缺少'}'。 Solutiuon在下面给出。

def evilMove(evilGuy):
    evilCoords=[]
    #deadZones=[]
    #Returns either -1, 0 or 1
    randomMovex=random.randrange(-1,2)
    randomMovey=random.randrange(-1,2)
    newCell={'x':evilGuy[0]['x']+randomMovex,'y':evilGuy[0]['y']+randomMovey}
    if (newCell['x']<0 or newCell['y']<0 or newCell['x']>cellSize or newCell['y']>display_height/cellSize):
        newCell={'x':display_width/(2*cellSize),'y':display_height/(2*cellSize)} # Here It's missing '}'

    del evilGuy[-1]

    evilCoords.append(newCell['x'])
    evilCoords.append(newCell['x'])
    deadZones.append(evilCoords)
    evilGuy.insert(0,newCell)

1 个答案:

答案 0 :(得分:0)

此pb存在于空白区域的数量 在你的代码中删除def之前的空格,并在'''之前添加三个空格,你的代码将是(只是复制和粘贴):

def evilMove(evilGuy): # here delete spaces 
    evilCoords=[]
    #deadZones=[]
    #Returns either -1, 0 or 1
    randomMovex=random.randrange(-1,2)
    randomMovey=random.randrange(-1,2)
    newCell={'x':evilGuy[0]['x']+randomMovex,'y':evilGuy[0]['y']+randomMovey}
    '''  # here add spaces
    if (newCell['x']<0 or newCell['y']<0 or newCell['x']>cellSize or newCell['y']>display_height/cellSize):
        newCell={'x':display_width/(2*cellSize),'y':display_height/(2*cellSize)

    '''
    del evilGuy[-1]                 
    evilGuy.insert(0,newCell)