简体   繁体   中英

How do I add a keypress action in my program?

I am making a very basic Java based RPG game with many of options as you go along and I want to make it so when you get the ability to type, if you press "x" it will automatically quit the game. I don't want to continuously add the "if-then" statement every time the user progresses.

What I don't want to do: (I would have to do this over 50 times for: inventory, quit game, character information and more)

switch (choice1)
  {
     case "x":
        System.out.println("\nYou quit the game!");
        System.exit(0);
        break;  
     }    

What I have: (Doesn't work)

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;


public class TheDungeon extends KeyAdapter
{
    public void keyPressed(KeyEvent e) {
        char ch = e.getKeyChar();
        if (ch == 'a')
        {
        System.out.println("You pressed A"); 
        }    
    }   
    public static void main(String[] args)
    {
    /* My variables...

    */
    System.out.println("e: Check experience and level");
    System.out.println("c: Character Information");
    System.out.println("i: Inventory");
    System.out.println("x: Quit Game");
    choice1 = keyboard.nextLine();
    switch (choice1)
        {
        case "x":                                         //This section
            System.out.println("\nYou quit the game!");   //here works
            System.exit(0);                               //but I don't
        break;                                            //want to add this section
    }                                                     //every time the user
                                                          //progresses.

To use KeyAdapters and/or KeyListeners you will need to construct a Gui in which to add these Adapters/Listners too.

The way you are currently reading in the users action is a valid way to do it for a console app.

Edit Extending on BlakeP's Answer if you have your determineAction method you could have a Map of the text your print out like so then you only need to add the special actions for the keys.

Map<Character, String> actionText = new HashMap<Character, String>();

actionText.put('x', "\nYou quit the game!");
actionText.put('i', "\nInventory Items:\n  Things are here");


private void determineAction(char choice) {
   System.out.println(actionText.get(choice));
   switch (choice1)
   {
     case "x":                                        
         System.exit(0);                               
     break;                                    
   }
}

Or you should have another method to do each special action, this will keep your switch shorter and easier to read. Like so

private void determineAction(char choice) {
   System.out.println(actionText.get(choice));
   switch (choice1)
   {
     case "x":                                        
         doExit();                              
         break;                                    
     case "i":
         printInventory();
         break;

   }
}

private void doExit()
{
    System.out.println("\nYou quit the game!");
    System.exit(0);
}

private void printInventory()
{
    System.out.println("\nInventory Items:");
    System.out.println("\n  Things are here");
}

The KeyAdapter class is meant for GUI apps (notice the AWT package that it is in), and it sounds like you are doing just a console app. As far as I know, there is no "keypress" listener for console apps.

I would suggest putting the switch statement in a method such as "determineAction" and then return what you need (if anything).

Example method:

    private void determineAction(char choice) {
       switch (choice1)
       {
         case "x":                                        
             System.out.println("\nYou quit the game!");   
             System.exit(0);                               
         break;
         case "i":                                        
             System.out.println("\nInventory Items:");   
             System.out.println("\n  Things are here");                                  
         break;                                      
       }
    }

So after every time you ask for the choice, put this line --

    determineAction(choice);

And it will run the switch statement without you having to copy it each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM