所以我在tkinter做了一个小小的发光游戏,我遇到了墙。编译器只捕获一个关键事件,如果第二个用户按下键,第一个用户移动ll停止。你们知道如何解决这个问题吗?
这是代码:
<svg class="spinner-container" viewBox="0 0 44 44" style="width: 50px; height: 50px;"><circle class="path" cx="22" cy="22" r="20" fill="none" stroke-width="4"></circle></svg>
答案 0 :(得分:2)
在其他模块中 - 例如PyGame
- 您使用w_pressed = True/False
和up_pressed = True/False
等变量,在按下或释放按键时会更改这些变量。接下来,您创建mainloop
,检查此变量以移动对象。由于tkinter
已经mainloop
,因此您可以使用after()
定期执行自己的功能,该功能会检查w_pressed
/ up_pressed
并移动对象。
简单(工作)示例:
它会检查w
和up
,并为两个键显示True/False
。
import tkinter as tk
# --- functions ---
def pressed(event):
global w_pressed
global up_pressed
if event.keysym == 'w':
w_pressed = True
elif event.keysym == 'Up':
up_pressed = True
def released(event):
global w_pressed
global up_pressed
if event.keysym == 'w':
w_pressed = False
elif event.keysym == 'Up':
up_pressed = False
def game_loop():
# use keys
print(w_pressed, up_pressed)
# run again after 500ms
root.after(500, game_loop)
# --- data ---
w_pressed = False
up_pressed = False
# --- main ---
root = tk.Tk()
root.bind("<KeyPress>", pressed)
root.bind("<KeyRelease>", released)
# start own loop
game_loop()
root.mainloop()