Java FX – ASSORTED APPLICATIONS
TOPIC 01 – TEXT-BASED ADVENTURE GAME

 

 

LESSON NOTE

 

 

Lessons in this unit require that you study the code to fully understand how things work. While some explanations may be provided, they are more examples than true lessons.

 

DEMO OF GAME

 

                               

 

CODE EXPLANATION

 

Part 1

 

Part 2

 

 

CODE

 

public enum Loc

{

   FOREST,

   PATH,

   SWAMP,

   CROC,

   FIELD

}

public class GameState

{

     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()];

     }

    

     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;

          }        

     }

}

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.TextArea;

import javafx.scene.control.TextField;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.Border;

import javafx.scene.layout.BorderStroke;

import javafx.scene.layout.BorderStrokeStyle;

import javafx.scene.layout.BorderWidths;

import javafx.scene.layout.CornerRadii;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Pane;

import javafx.scene.layout.VBox;

import javafx.scene.paint.Color;

import javafx.scene.text.Font;

import javafx.scene.text.Text;

import javafx.stage.Stage;

 

public class GameApp extends Application

{

     public Stage stage;

    public Scene currentScene;

     public Button startButton;

    

     public TextField inText;

     public Button goButton;

     public GameState gs;

    

     public static void main(String[] args)

     {

          Application.launch(args);

     }

 

     @Override

     public void start(Stage primaryStage) throws Exception

     {

        gs = new GameState();

        stage = primaryStage;

        currentScene = introScene();

 

       primaryStage.setScene(currentScene);

       primaryStage.setTitle("The game of games");

       primaryStage.show();

     }

    

     public Scene introScene()

     { 

          Image img = new Image(GameApp.class.getResourceAsStream("introScreen.png"));

          ImageView imv = new ImageView();

          imv.setImage(img);

 

          startButton = new Button();

          startButton.setText("Start Game");

          startButton.setOnAction(new MyActionHandler());

 

          VBox vb = new VBox();

          vb.setSpacing(8);

          vb.setAlignment(Pos.CENTER);

          vb.getChildren().add(imv);

          vb.getChildren().add(startButton);

 

          Scene s = new Scene(vb,800,600);

          return s;

     }

    

     public void displayGameScreen()

     {

          Text txt = new Text(gs.getTitle());

          txt.setFont(new Font("Arial",36));

         

          String filename = gs.getImageFileName();

          Image img = new Image(GameApp.class.getResourceAsStream(filename));

          ImageView imv = new ImageView();

          imv.setImage(img);

 

          Text t = new Text();

          t.setFont(new Font("Arial",14));

          t.setWrappingWidth(450);

          String desc = gs.getDescription();

          t.setText(desc);

         

        inText = new TextField();

        inText.setOnAction(new MyActionHandler());

        goButton = new Button();

          goButton.setText("Go");

          goButton.setOnAction(new MyActionHandler());

         

        HBox hb = new HBox();

        hb.setSpacing(8);

          hb.setAlignment(Pos.CENTER);

        hb.getChildren().add(inText);

        hb.getChildren().add(goButton);

       

          VBox vb = new VBox();

          vb.setSpacing(8);

          vb.setAlignment(Pos.CENTER);

          vb.getChildren().add(txt);

          vb.getChildren().add(imv);

          vb.getChildren().add(t);

          vb.getChildren().add(hb);

 

          Scene s = new Scene(vb,800,600);

          stage.setScene(s);

     }

    

     public class MyActionHandler implements EventHandler<ActionEvent>

     {

          @Override

          public void handle(ActionEvent e)

          {

              if (e.getSource()==startButton)

              {

                   displayGameScreen();

              }

              else if (e.getSource()==goButton || e.getSource()==inText)

              {

                   String inp = inText.getText();

                   gs.updateState(inp);

                   displayGameScreen();

              }

          }

     }

}