使用键盘在迷宫中移动角色

时间:2019-04-24 14:21:49

标签: python python-3.7 maze pynput

我已经创建了一个简单的迷宫,而且无法将角色移动到目标。迷宫在TKinter窗口上,所以我不知道这是否会阻止代码运行。

我尝试使用pynput,它在一个单独的程序中运行,而没有运行tkinter窗口,所以我一点都没有错。

  def level1():      
    x=1
    startx = 1
    objectivex = 10
    y=1
    starty=1
    objectivey=10
    val_in_row = [[0,1,2,3,4,5,6,7,8,9,10,11],[0,3,7,11],[0,1,5,7,8,9,11],[0,3,5,6,11],[0,2,4,7,9,10,11],[0,4,6,10,11],[0,1,3,8,9,11],[0,1,3,5,6,11],[0,5,8,9,11],[0,2,4,6,9,11],[0,1,6,7,9,11],[0,1,2,3,4,5,6,7,8,9,10,11]]
    board = Tk()
    board.title("LEVEL 1")
    for i in range(0,len(val_in_row)):
        for j in val_in_row[i]:
            Button(board, text="     ").grid(row=i, column=j)
    Label(board, text="U").grid(row=y, column=x)
    Label(board, text="O").grid(row=10, column=10)
    Label(board, text="      ").grid(row=6, column=13)
    Button(board, text="restart", command = restart).grid(row=9, column=21)
    def on_press(key):
        if key == Key.right:
            right()
        elif key == Key.left:
            left()
        elif key == Key.up:
            up()
        elif key == Key.down:
            down()
    with Listener(on_press=on_press) as listener:
        listener.join()
    board.mainloop()

我希望tkinter窗口打开并且代码仍然运行,以便可以检测实时键盘事件。

0 个答案:

没有答案