Roguelike 游戏的纯 Java 文本界面
好吧,这听起来像是一个疯狂的想法 - 但我有兴趣模仿 1980 年代风格roguelike 游戏< /a> 纯Java 中的文本界面,即使用Swing 或类似的。
大致如下是它需要做的:
- 提供固定大小字符的固定大小网格作为“屏幕”(例如 100*75)
- 使用适当的等宽字体,最好有许多有趣的符号
- 允许为每个字符设置前景色和背景颜色单独的字符位置
- 允许在屏幕中的任何位置打印字符串或单个字符(这应该覆盖这些位置的屏幕缓冲区中已有的内容)
有人知道可以实现此目的的现有解决方案吗?或者我是否只能从头开始将它们组合在一起?
ps我想要纯Java的原因是它可以在沙盒小程序中运行。因此,像 jcurses 这样的 JNI 解决方案不幸被排除在外......
OK, this is going to sound like a crazy idea - but I'm interested in emulating a 1980s style roguelike game text interface in pure Java, i.e. using Swing or similar.
Here's roughly what it needs to do:
- Provide a fixed size grid of fixed size characters as the "screen" (e.g. 100*75)
- Use an appropriate monospaced font, ideally with lots of interesting symbols
- Allow setting of foreground and background character colours for each character position individually
- Allow printing of strings or individual characters at any place in the screen (which should overwrite whatever is already in the screen buffer in those locations)
Anyone know of a good existing solution that would enable this? Or am I stuck with hacking one together from scratch?
p.s. the reason I want pure Java is so that it can run in a sandboxed applet. So JNI solutions like jcurses are sadly ruled out.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一点也不疯狂,这是我在 Legerdemain 中实现的方法:http://roguelikefiction.com
我使用了一个二维字符数组 (char[ ][]) 和相应的 java.awt.Color[][] 对象数组来跟踪颜色。您可以将这些数组放入继承自 JPanel 的类(JPanel 又是 JFrame 的一部分),并在面板的 PaintComponent() 回调中执行所有绘制操作。
Curses/JNI 方法也没有什么问题,尽管如果您采用 Swing 路线,您会得到各种很棒的 Unicode 字形。 Legerdemain 使用了五六百个。
Not crazy at all, this is the approach I implemented in Legerdemain:http://roguelikefiction.com
I used a two dimensional array of characters (char[][]) with a corresponding array of java.awt.Color[][] objects to track the colors. You can shove these arrays into a class that inherits from JPanel (which in turn is part of a JFrame) and do all of the drawing in the panel's paintComponent() callback.
Nothing wrong with the Curses/JNI approach either, although you get all sorts of great Unicode glyphs if you go the Swing route. Legerdemain uses five or six hundred of them.
对于此类项目,我发现严格分离游戏模型和视图至关重要。这个简单的示例建议了整体架构,而这个更复杂的game 扩展了这个概念。好处是视图可以独立于游戏本身而发展,而游戏并不关心监听视图是什么样子。
对于符号来说,Unicode 字形可能是一个很有吸引力的选择,正如示例中所建议的那样。
For projects of this sort, I found it essential to rigorously separate the game model and view. This simple example suggest the overall architecture, and this more complex game expands on the notion. The benefit is that the view can evolve separately from the game itself, which doesn't care what the listening view(s) look like.
For symbols, Unicode glyphs may be an appealing option, as suggested in this example.
string[] screen = new string[75],然后每个都填充 100 个空格:)。
请参阅以下链接了解一些好的符号:http://cg.scs.carleton。 ca/~luc/mono.html
您可以通过使用允许呈现 HTML 的控件(如 JEditorPane)来获得这些文本效果。这样您就可以将特殊关键字定义为“特殊关键字”。 (好吧,这有点不推荐使用,但应该适合您的情况。如果您将“游戏状态”存储为普通字符串(数组),但在输出之前渲染 html,那将是最简单的。
的建议,而不是将游戏状态作为普通字符串数组,那么您只需找到该行的字符串,找到使用 string.substring(length) + "A" + string。 substring(startindex: 长度 + 2, string.length - (长度 + 2));构建你的新游戏状态。
string[] screen = new string[75], then just fill each one with 100 spaces :).
See this links for a few good ones: http://cg.scs.carleton.ca/~luc/mono.html
You could have those text effects by using a control that allows rendering of HTML like the JEditorPane. That way you could just define special keywords as "special keyword". (Ok that is a bit deprecated but should work fine for your case. It would be easiest if you store your 'game state' as just a normal string (array), but have the html rendered just before you output it.
If you followed my advice in the previous question than you have your gamestate as a normal string array, then you just find the string for your line, find use string.substring(length) + "A" + string.substring(startindex: length + 2, string.length - (length + 2)); to construct your new gamestate.
我曾经(几年前)开始编写类似的代码(Swing 中的终端实现)。
我已经达到了这样的程度:我可以显示带有 ANSI 转义序列的文本,用于光标移动和其中的颜色,但没有实现任何输入。如果你有兴趣,我就挖出来。
I once (years ago) started to code something like this (a terminal implementation in Swing).
I got to the point that I could display text with ANSI escape sequences for cursor movement and colors in it, but didn't implement any input. If you are interested, I'll dig it out.
既然你正在谈论流氓和基于字符的界面,以便让过去的旅程变得完整,为什么你不谷歌搜索 Curses C 实现并使用 JNI 进行视图。您的模型和控制器是常规的 Java。几乎每个平台都有一个 Curses 实现。
Since you are talking rogue and character based interfaces in order to make the trip to the past complete, why don't you google for a Curses C implementation and do the View with JNI. Your Model and Controller are regular Java. There is a Curses implementation for almost every platform.
我最终实现了一个简单的 Swing 控制台,受到这里许多答案的启发(谢谢大家!)
对于那些感兴趣的人,可以在这里找到它:
基于 Swing 的 Java 文本控制台
I ended up implementing a simple Swing console, inspired by many of the answers here (thanks everyone!)
For those who are interested, it's available here:
Swing based Java text console