Using the keyboard to move the character in a maze

拈花ヽ惹草 提交于 2019-12-11 17:30:15

问题


I have created a simple maze and I have no way to move the character to the objective. The maze is on a TKinter window so I don't know if that is stopping the code from running.

I have tried using pynput, which has worked in a seperate program without a tkinter window running, so I dont get whats wrong at all.

  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()

I hoped that the tkinter window was open and the code still ran so it would detect live keyboard events.

来源:https://stackoverflow.com/questions/55832371/using-the-keyboard-to-move-the-character-in-a-maze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!