Ocaml 图形事件

Ocaml Graphics event

我在这里创建了这个函数来从键盘获取事件:

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;;

我正在制作俄罗斯方块,所以我希望在该函数等待事件时 bloc 可以关闭。

目前,当它等待事件时,它会“暂停”循环。 我该怎么办?

我自己从未使用过 Graphics,只是通过阅读 the documentation I would suggest using either loop_at_exitPoll 事件:

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