ocaml - Ocaml 图形事件
问题描述
我在这里创建了这个函数来从键盘获取事件:
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
自己,但仅通过阅读文档,我建议将其loop_at_exit
与Poll
事件一起使用:
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
推荐阅读
- node.js - 为什么 npm 不安装 angular/dev-kit
- python - Python:闰年 - 奥运会
- python - IndexError:在 Tensorflow 训练期间列出超出范围的索引
- react-native - React 原生纯图表动态数据问题
- javascript - 如何在 Windows 的电子应用程序中授予音频权限?
- reactjs - 分析 Gatsby Bundle 文件的慢速加载
- amazon-web-services - 设置 Tableau Server 以在 AWS GovCloud 上运行
- axios - axios 返回 __ob__: Observer... 当从 Nuxt 下的 Vuex 操作调用时
- python - 需要帮助处理 python 2.7 和 python 3 上的 beautifulsoup 错误
- python - 使用 keras 模型 .hdf5 预测图像时出错