问题描述
let get_move() : string =
let e = Graphics.wait_next_event [Key_pressed] in
let ke = if e.keypressed then Printf.sprintf "%c" e.key else "" in
ke;;
我正在做俄罗斯方块,所以我希望在此函数等待事件时,该团体可以关闭。
当前,当它等待事件时,它将“暂停”循环。 我该怎么办?
解决方法
我自己从未使用过Graphics
,但是从阅读the documentation开始,我建议在Poll
事件中使用loop_at_exit
:
Graphics.loop_at_exit [Key_pressed; Poll] begin fun e ->
if e.keypressed then
...
else
...
end
或者使用自己的循环,使用key_pressed
检查是否可以不阻塞地调用read_key
:
let get_move () : char option =
if Graphics.key_pressed () then
Some (Graphics.read_key ())
else
None