WASD
プレーヤーからの入力を得るために、ゲームプロセスを停止せずにプレーヤーがキーを押す必要があるスネークゲームを作成しています。したがってinput()
、ゲームが入力を取得するためにティックを停止するため、この状況には使用できません。
getch()
エンターを押さなくてもすぐに入力できる機能を見つけたのですが、この機能もゲームのカチカチ音を止めて のような入力をしてくれますinput()
。getch()
threading モジュールを使用して、別のスレッドで入力を取得することにしました。問題は、別のスレッドで getch() が機能しないことです。その理由はわかりません。
import threading, time
from msvcrt import getch
key = "lol" #it never changes because getch() in thread1 is useless
def thread1():
while True:
key = getch() #this simply is almost ignored by interpreter, the only thing it
#gives is that delays print() unless you press any key
print("this is thread1()")
threading.Thread(target = thread1).start()
while True:
time.sleep(1)
print(key)
では、なぜgetch()
にあると役に立たないのthread1()
でしょうか。