0

我正在尝试编写一个简单的程序,该程序将允许我控制数组中数字 5 的位置。但是,当我运行代码时,控件并没有真正起作用。另外,我正在尝试清除终端,以便它只显示当前位置,而不是过去的位置,但即使这样也不起作用。请帮忙。

import numpy as np
import keyboard
from os import system




a = int(input())
c = int(input())
z = 0


isKEYPRESSED = 0



while (True):
    b = np.zeros((a,c))
    for i in range(a):
        for j in range(c):
            b[i][j]=0

    b[z][0] = 5
    print(b)
    
    if keyboard.read_key() =="right" and isKEYPRESSED == 0:
        b[z][0] = 0
        z = z + 1
        isKEYPRESSED = 1
        if keyboard.read_key() =="right" and isKEYPRESSED == 1:
            isKEYPRESSED = 0
    if keyboard.read_key() =="left" and isKEYPRESSED == 0:
        b[z][0] = 0
        z = z - 1
        isKEYPRESSED = 1
        if keyboard.read_key() =="left" and isKEYPRESSED == 1:
            isKEYPRESSED = 0
    
    system('clear')
4

1 回答 1

0

你可以试试这个,它使用更可靠的回调,只在“key down”时触发。但是,请注意,这是安装 GLOBAL 钩子,无论哪个应用程序具有焦点,它都会抓取键。keyboard对于此类应用程序,该模块不是一个好的选择。

import numpy as np
import keyboard
from os import system
import sys


a = int(input())
c = int(input())
z = 0

b = np.zeros((a,c))


def onkey(evt):
    global z
    print(evt.name)
    if evt.name =="right":
        if keyboard.is_pressed('right'):
            b[z][0] = 0
            z = z + 1
    elif evt.name =="left":
        if keyboard.is_pressed('left'):
            b[z][0] = 0
            z = z - 1
    b[z][0] = 5
    print(b)
    

b[z][0] = 5
print(b)
keyboard.on_press(onkey)
keyboard.wait()
于 2021-06-04T21:14:34.030 回答