OCaml读取了没有图形窗口的按键

OCaml读取了没有图形窗口的按键

本文介绍了OCaml读取了没有图形窗口的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑.我在Windows环境中工作.

Edit. I am working in a Windows environment.

我想用OCaml在控制台输出中编写一个简单的游戏.我需要能够使用"read_key"功能.但是:

I want to write a simple game in console output with OCaml. I need to be able to use a 'read_key' function. But :

  • 图形模块引发错误:异常:Graphics.Graphic_failure图形屏幕未打开".但我不想打开图形窗口.
  • 函数 read_line 强制用户在每次按键后按下返回"键.
  • The graphics module throws an error : Exception: Graphics.Graphic_failure "graphic screen not opened". But I do not want to open the graphic window.
  • The function read_line forces the user to press "return" after every key press...

推荐答案

无法使用纯OCaml实现此类功能.您将需要调用平台特定的库.这不是OCaml的问题,其他语言(包括python,java,c等)也是如此.实际上,以便携式方式与终端进行交互是一种火箭科学.主要是出于历史原因.

It is not possible to implement such function using pure OCaml. You will need to call to platform specific libraries. It is not an OCaml problem, this is the same for other languages, including python, java, c, etc. Actually, interacting with a terminal in a portable way is kind of rocket science. Mostly for historical reasons.

我怀疑您不想陷入此类麻烦,因此我只能提出一些简单的解决方案:

I suspect, that you don't want to get into such troubles, so I can suggest few easy solutions:

  1. 使用 OCamlSDL 库-您正在开发游戏,而SDL用于游戏

  1. Use OCamlSDL library, - you're developing a game, and SDL is for games

使用图形"模块,是的,您将需要打开图形窗口,并且您的游戏将不是一个纯粹的控制台...但是,这可能不是一个大问题.另外,您可以将背景设为黑色并模拟终端:)

Use Graphics module, yes you will need to open the graphic window, and your game will not be a pure console... but maybe this is not a big issue. Also, you can make a black background and emulate a terminal :)

安装OCaml的Cygwin版本.您将获得一个类似Unix的环境,并且 Unix.tc _ * 将开始工作.

Install Cygwin version of OCaml. You will get a unix-like environment, and Unix.tc_* will start to work.

切换到正常操作系统.在正常情况下,我指的是类Unix.您可以第一次使用虚拟机或容器,例如Docker.

Switch to a normal operating system. Where by normal I mean Unix-like. You can use virtual machine, or containers, like Docker, for a first time.

这篇关于OCaml读取了没有图形窗口的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:37