开发者

Pure Java text interface for a roguelike game

开发者 https://www.devze.com 2023-02-08 09:11 出处:网络
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.

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


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.


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.


Provide a fixed size grid of fixed size characters as the "screen" (e.g. 100*75)

string[] screen = new string[75], then just fill each one with 100 spaces :).

Use an appropriate monospaced font, ideally with lots of interesting symbols

See this links for a few good ones: http://cg.scs.carleton.ca/~luc/mono.html

Allow setting of foreground and background character colours for each character position individually

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.

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)

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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号