为板gam编码有效移动时,列表索引超出范围

2024-04-18 01:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,大家我是新来的,我想做一个游戏叫HiQ现在我得到了董事会绘制和一切,我可以点击其中一块,但当我做的一块确实改变了颜色,我得到了一个错误的外壳以及(列在下面)我不知道为什么我得到这个,我希望你们能给我更好的洞察力。我也会在下面提供我的代码,它是用python3编写的,谢谢

builtins.IndexError: list index out of range

boardcirc =[[0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0],
            [1,1,1,1,1,1,1,1,1],
            [1,1,1,1,2,1,1,1,1],
            [1,1,1,1,1,1,1,1,1],
            [0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0],
            [0,0,0,1,1,1,0,0,0]]


def HiQ():
    splash_screen() 
    make_board()





def make_board():
    make_sqr()
    make_circ()
    get_click()



def get_click():
    global count, boardcirc
    while 1!=0:
        count = count - 1
        displaymessage("Pieces: " + str(count))        
        where = win.getMouse()
        col = where.x//90
        row = where.y//90
        valid_move(row,col)
        make_move(row,col)




def valid_move(row,col):
    if boardcirc[row][col] == 0:
        return False
    if boardcirc[row-1][col] == 1 and boardcirc[row-2][col] == 1:
        return True
    if boardcirc[row+1][col] == 1 and boardcirc[row+2][col] == 1:
        return True
    if boardcirc[row][col-1] == 1 and boardcirc[row][col-2] == 1:
        return True
    if boardcirc[row][col+1] == 1 and boardcirc[row][col+2] == 1:
        return True



def make_move(row,col):
    while valid_move(row,col) == True:

        col = (col*85)+42
        row = (row*85)+42
        circ = Circle(Point(col,row),35)
        circ.setFill("white")
        circ.draw(win)

所有这些都适用于错误


Tags: andtruemovemakereturnifdefcount
1条回答
网友
1楼 · 发布于 2024-04-18 01:22:34

对于你的valid_move(row,col),你不能拥有所有的if语句。 不要这样做,在初始的if语句后面使用elif,不要忘记编写一个else语句

if boardcirc[row][col] == 0:
        return False
    if boardcirc[row-1][col] == 1 and boardcirc[row-2][col] == 1:
        return True
    elif boardcirc[row+1][col] == 1 and boardcirc[row+2][col] == 1:
        return True
    elif boardcirc[row][col-1] == 1 and boardcirc[row][col-2] == 1:
        return True
    elif boardcirc[row][col+1] == 1 and boardcirc[row][col+2] == 1:
        return True
else:
    return False

相关问题 更多 >

    热门问题