Java – Swing Applications 02
TEXT-BASED ADVENTURE GAME IN A GUI

separator-blank.png

TEXT-BASED ADVENTURE GAME IN GUI – VERSION 3

 

VERSION 3 DESCRIPTION

 

This is a basic GUI program that has a start screen and then a playable (though simple) text-based game. 

KNOWN ISSUE

 

The current version of the code doesn’t allow the user to reach the Game Over screen.  If this is needed, the code will have to be altered to allow for this.  This was not done yet because it would potentially complicate the expansion of the actual game.

 

VERSION 3 FILES

 


The following seven images are needed in your Eclipse project.

   gameOfGames.jpg

   gameOver.jpg

   00-forest.png

   01-path.png

   02-swamp.png

   03-croc.png

   04-field.png

 

 

VERSION 3 CODE (3 FILES)

 

The first two files are new.  They are fairly simple and are used to store the current state of the game as well as all of the possible locations in the game.  It is these two files that you must edit to alter the “story” of the game.

The third file is very similar to the file from Version 2.  It simply updates the GUI based on the state of the game.

 

//This file holds the name of each possible location.  The order

//of the names has to be the same as the order of the info in the

//arrays in the GameState class.

 

public enum Loc

{

   FOREST,

   PATH,

   SWAMP,

   CROC,

   FIELD

}

/* This class simply stores the current location of your character.  It

 * also has the information (location, title, image file name and

 * description) on all of the possible locations.

 */

 

public class GameState

{

         //Note: The order of the elements has to match the order of the values in

         //the Loc enum.  So, Forest is first in all of them.  And Path is next in

         //all of them.  And so on...

        

         public Loc location;

         public String[] titles = {"The Forest", "Path", "Swamp", "Game Over via Croc", "Field"};

         public String[] imgFiles = {"00-forest.png", "01-path.png", "02-swamp.png", "03-croc.png", "04-field.png"};

 

         public GameState()

         {

                 location = Loc.FOREST;

         }

 

         public String getImageFileName()

         {

                 return imgFiles[location.ordinal()];  //ordinal() gives index value of enum

         }

 

         public String getTitle()

         {

                 return titles[location.ordinal()];

         }

 

         public String getDescription()

         {

                 if (location == Loc.FOREST)

                 {

                          return "You wake up in a forest.  Wood you believe it?  Get it?  Get it?  You can't remember who you are.  Where you are from.  Or what happened.  But at least you know you have a good sense of humour.  Or the voice in your head does.  You see a PATH leading north and what seems to be a SWAMP to the east.";

                 }

                 if (location == Loc.PATH)

                 {

                          return "In one direction you see a FOREST with big trees.  In the other, you see a FIELD.";

                 }

                 if (location == Loc.SWAMP)

                 {

                          return "You can see a cabin far in the swamp.  You can SWIM to it.  Or you can go back into the FOREST.";

                 }

                 if (location == Loc.CROC)

                 {

                          return "Death by Crocodile!  :(";

                 }

                 if (location == Loc.FIELD)

                 {

                          return "You are in a field.  There is a PATH leading into the forrest and an old red house.  Unfortunately, the creators of this world have not yet made the house accessible to you.";

                 }

                 return "";

         }

 

         public void updateState(String input)

         {

                 input = input.toUpperCase();

                 //=================================================

                 //FOREST

                 //=================================================

                 if (location == Loc.FOREST && input.equals("PATH"))

                 {

                          location = Loc.PATH;

                          return;

                 }

                 if (location == Loc.FOREST && input.equals("SWAMP"))

                 {

                          location = Loc.SWAMP;

                          return;

                 }

                 //================================================

                 //PATH

                 //================================================

                 if (location == Loc.PATH && input.equals("FOREST"))

                 {

                          location = Loc.FOREST;

                          return;

                 }

                 if (location == Loc.PATH && input.equals("FIELD"))

                 {

                          location = Loc.FIELD;

                          return;

                 }

                 //================================================

                 //SWAMP

                 //================================================

                 if (location == Loc.SWAMP && input.equals("SWIM"))

                 {

                          location = Loc.CROC;

                          return;

                 }

                 if (location == Loc.SWAMP && input.equals("FOREST"))

                 {

                          location = Loc.FOREST;

                          return;

                 }

                 //================================================

                 //FIELD

                 //================================================

                 if (location == Loc.FIELD && input.equals("PATH"))

                 {

                          location = Loc.PATH;

                          return;

                 }        

         }

}

 

//Warning: Seven images are required.

//

//Known issue: There is no way to get to the Game Over screen at

//the moment.  This can easily be added.

 

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

 

import javax.imageio.ImageIO;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.ImageIcon;   //I had to add this import manually.  Why?

 

public class GameFrame extends JFrame implements ActionListener

{

         public Container gc;

 

         //start screen

         public JPanel startPanel;

         public JButton startButton;

         public BufferedImage startImage;

         public JLabel startImageLabel;

 

         //game screen

         public JPanel gamePanel;

         public JTextField gameTextField;

         public JButton gameButton;

         public JTextArea gameTextArea;

         public BufferedImage gameImage;

         public JLabel gameImageLabel;

 

         //game state

         public GameState state;

 

         //end screen

         public JPanel endPanel;

         public JButton restartButton;

         public BufferedImage gameOverImage;

         public JLabel gameOverImageLabel;

 

         public GameFrame()

         {

                 gc = this.getContentPane();

                 showStartScreen();

 

                 this.setTitle("Game of Games - Version 3");

                 this.setSize(500,500);

                 this.setVisible(true);

         }

 

         public void showStartScreen()

         {

                 try

                 {

                          startPanel = new JPanel();

 

                          startImage = ImageIO.read(new File("gameOfGames.jpg"));

                          startImageLabel = new JLabel(new ImageIcon(startImage));

                          startPanel.add(startImageLabel);

 

                          startButton = new JButton("Start");

                          startButton.addActionListener(this);

                          startPanel.add(startButton);

 

                          gc.add(startPanel);

                          revalidate();           //needed after replacing a component

                          repaint();              //needed after replacing a component

                 }

                 catch (IOException err)

                 {

                          System.out.println("Error 1.  Image file missing?");

                 }

         }

 

         public void showGameScreen()

         {

                 try

                 {

                          state = new GameState();

 

                          gamePanel = new JPanel();

 

                          gameImage = ImageIO.read(new File(state.getImageFileName()));

                          gameImageLabel = new JLabel(new ImageIcon(gameImage));

                          gamePanel.add(gameImageLabel);

 

                          gameTextArea = new JTextArea(5, 30);

                          gameTextArea.setText(state.getDescription());

                          gameTextArea.setLineWrap(true);       //make text wraps

                          gameTextArea.setWrapStyleWord(true);  //wrap by word

                          gameTextArea.setEditable(false);

                          gamePanel.add(gameTextArea);

 

                          gameTextField = new JTextField(25);

                          gamePanel.add(gameTextField);

 

                          gameButton = new JButton("Go");

                          gameButton.addActionListener(this);

                          gamePanel.add(gameButton);

 

                          gc.add(gamePanel);

                          revalidate();           //needed after replacing a component

                          repaint();              //needed after replacing a component

                 }

                 catch (IOException err)

                 {

                          System.out.println("Error 2.  Image file missing?");

                 }

         }

 

         public void showEndScreen()

         {

                 try

                 {

                          endPanel = new JPanel();

 

                          gameOverImage = ImageIO.read(new File("gameOver.jpg"));

                          gameOverImageLabel = new JLabel(new ImageIcon(gameOverImage));

                          endPanel.add(gameOverImageLabel);

 

                          restartButton = new JButton("Restart");

                          restartButton.addActionListener(this);

                          endPanel.add(restartButton);

 

                          gc.add(endPanel);

                          revalidate();          //needed after replacing a component

                          repaint();             //needed after replacing a component

                 }

                 catch (IOException err)

                 {

                          System.out.println("Error 3.  Image file missing?");

                 }

         }

 

         public static void main(String[] args)

         {

                 GameFrame game = new GameFrame();

         }

 

         @Override

         public void actionPerformed(ActionEvent e)

         {

                 if (e.getSource() == startButton)

                 {

                          gc.removeAll();

                          showGameScreen();

                 }

                 else if (e.getSource() == gameButton)  //GO button

                 {

                          try

                          {

                                  //We simply need to update the information inside each

                                  //component.  No need to change the GUI around.

 

                                  //1-Update the state.

                                  String data = gameTextField.getText();

                                  state.updateState(data);

 

                                  //2-Update the image.

                                  gameImage = ImageIO.read(new File(state.getImageFileName()));

                                  gameImageLabel.setIcon(new ImageIcon(gameImage));

                                  //gameImageLabel = new JLabel(new ImageIcon(gameImage));

 

                                  //3-Update the text area

                                  gameTextArea.setText(state.getDescription());

                          }

                          catch (IOException err)

                          {

                                  System.out.println("Error 4.  Image file missing?");

                          }

                 }

                 else if (e.getSource() == restartButton)

                 {

                          gc.removeAll();

                          showStartScreen();

                 }

         }

}

 

separator-campeauIsAwesome.png