Tips to create a labyrinth with lanterna – Part I

Lanterna is a console text GUI library written in Java. Coding a simple game without having to worry about details in the UI makes lanterna a very good choice. But while lanterna was easy to use, there were still some pitfalls considering the UI.

The gaming concept

The concept behind the labyrinth is pretty easy. Initially a user has 3 lives. The user has to collect a key before he can hit the exit successfully. But there are some stumbling blocks. They are dangerous as they are able to move throughout the game. If the user hit such a stumbling block, he will lose one of his lives. And if he has no more lives left – game over.

The architecture

Consider to design the game very flexible, as you may decide to extend the above concept of the labyrinth and the existing functionality. For example the user will be allowed to collect lives instead of just losing them. A flexible architecture can be achieved by using the Model View Controller pattern.

UI- the terminal

The game itself has to be presented in the terminal- which will be the first window the user can see.  In order to write anything in your terminal you have to call terminal.enterPrivateMode(). It will give your terminal a private area to use, separate from what was there before. Before ending the game completly consider calling terminal.exitPrivateMode().

SwingTerminal terminal = TerminalFacade.createSwingTerminal();
Screen screen = TerminalFacade.createScreen(terminal);
screen.refresh();
terminal.enterPrivateMode();

After calling terminal.enterPrivateMode() you can start to draw your labyrinth. Depending on the characters you use you may get something like this:

Unbenannt

figure 1– labyrinth by using lanterna

UI-Providing a menu

All good games provide menus. The same goes with this labyrinth. So we may create serval windows during the game.

If the user requires help, wants to save the current game or start a new game he will need to call the menu. Let’s say the menu appears when hitting Esc. As the user may open and close the menu during the game, it has to be ensured that the game will be suspended during that time. In order to achieve that you need to consider serval things

The Controller shall open the new window when needed. The new Window has to be started in a new screen, in order to not disturb the running game. Consider the code snippet below:

public void openMenu(Spiel spiel, String titel) {
GUIScreen guiScreen = TerminalFacade.createGUIScreen();
MenueWindow menueWindow = new MenueWindow(guiScreen, “This is the menu”);
guiScreen.getScreen().startScreen();
guiScreen.showWindow(menueWindow, GUIScreen.Position.CENTER);
}

The new Window (MenueWindow) is a class which extends com.googlecode.lanterna.gui.Window. If the user wants to return to its game you have to close the active screen. The active screen is in our example the menu itself.

public class SpielMenueWindow extends Window {
public SpielMenueWindow(GUIScreen guiScreen,String title) {
super(title);
}
addComponent(new Button("continue game", new Action() {
@Override
public void doAction() {
guiScreen.getActiveWindow().close();
guiScreen.getScreen().stopScreen();
}}))
//more code...

If you want to end the whole game close all active screens and don’t forget to call terminal.exitPrivateMode(). You can even use System.exit(0) to shut the whole UI down.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s