eels11
eels11

Reputation: 13

How to get users input in a text adventure game (Java/Applet)

import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;


    class DD {
    public static void main(String[] args){

    JFrame myFrame = new JFrame("#########");
    myFrame.setSize(640,480);
    myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    JTextArea textArea = new JTextArea();
    myFrame.add(textArea);
    new JTextArea();
    textArea.setEditable(false);

    //System objects
    Scanner in = new Scanner(System.in);
    boolean running = true;
    textArea.append("\t\n########################################");
    textArea.append("\t\n>#############");
    textArea.append("\t\n>Th#############11!");
    textArea.append("\t\n>Typ#############enture!");
    textArea.append("\t\n########################################");
    String input = in.nextLine();
    if(input.equals("start")){
            { ///beginning of story.
        if(running)
        textArea.append("\t\nYo#############.");
        textArea.append("\t\n#############");
        textArea.append("\t\n1.#############t.");
        textArea.append("\t\n2.G#############t.");
        String input1 = in.nextLine();
        if(input1.equals("1")){
            textArea.append("\n>Y#############");
            textArea.append("\n>#############");
            textArea.append("\n>A#############");
            textArea.append("\n>1.#############");
            textArea.append("\n>2.#############");
        if(input.equals("1")){
                textArea.append("\n>#############");
                textArea.append("\n>#############");
                textArea.append("\n>Ga#############d.");
                    }
        if(input.equals("2")){
                textArea.append("\n>#############");
                textArea.append("\n>#############");
                textArea.append("\n>Y#############ars");
                textArea.append("\n>Y#############");
                    }
        }
        else if(input1.equals("2")){
            textArea.append("\n>Y#############.");
            textArea.append("\n>Y#############.");
            }
        }
    }
}
}

This is my text adventure game, i'm stuck on how to get users input. i have read about 'action listener' i don't know how to use it, but i really want the input to be entered like its in a console or terminal/cmd like program. the user just needs to enter 1, 2 or 3 to preform an action.

Upvotes: 1

Views: 748

Answers (2)

Mechanic
Mechanic

Reputation: 172

For starters there is a leak here:

myFrame.add(textArea);
**new JTextArea();**
textArea.setEditable(false);

(The line in bold located in the first lines of main, and it needs to be modified or removed)

Regarding your actual Q, I think that since it is meant to be a text based game, you could do something like this to accept user input:

(...)

public static void main(String[] args)
{
   (...)
   // create a scanner to read the command-line input
   Scanner scanner = new Scanner(System.in);
   bool running = true;

   init();

   //game loop
   while (running)
   {
    if(scanner.next() == 1)
     {
        // do this
     }else if ( == 2)
     {
        // do that
     }//etc
    //and simply change running to false, when you want the game to end
   }
   (...)
}

//this is simply a function to present the initial choices and general info to the user.
   private void init(){System.out.println("Add instructions for the user here, e.g. telling them that they have to press 1,2, or 3");}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347332

Dump the Scanner to start with. Use a JTextField. You need to understand that you're operating in an event driven environment, this means that you need to make use of the Observer Pattern to be notified when something changes

Start by taking a look at Creating a GUI With JFC/Swing, How to Use Text Fields, How to Write an Action Listeners for more details

Upvotes: 2

Related Questions