为基于 Java 文本的游戏制作控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27883860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:34:40  来源:igfitidea点击:

Making A Console For A Java Text Based Game

javaconsole

提问by joejoethemonkey

So, Iv'e been trying to make a simple Text Based RPG with Java, and it is going quite well, and my friends want to play it too but they aren't very tech savvy, so it's hard to tell them to get an IDE or use the CMD, so I wanted to know if there was a way to make my text-based game into a window, like using JFrame or something. All i need is a window that displays the text, and a bar on the bottom that lets the user input what they want (Kind of like a CMD). I do not know how to do this as of yet, because I am quite new at coding, can anyone help me? And it could be like a simple console because im not looking for a complex GUI or anything at the moment.

所以,我一直在尝试用 Java 制作一个简单的基于文本的 RPG,而且进展顺利,我的朋友也想玩它,但他们不太懂技术,所以很难告诉他们得到一个IDE 或使用 CMD,所以我想知道是否有一种方法可以将我的基于文本的游戏变成一个窗口,例如使用 JFrame 之类的。我所需要的只是一个显示文本的窗口,以及底部的一个栏,让用户输入他们想要的内容(有点像 CMD)。我还不知道该怎么做,因为我在编码方面很新,有人可以帮助我吗?它可能就像一个简单的控制台,因为我目前不寻找复杂的 GUI 或任何东西。

import java.util.Scanner;

public class Client {

private static String command, commandAttack, commandShop;
private static int monsterRat = 100;
private static int monsterWeasel = 110;
private static int monsterBear = 120;
private static int hp = 100;
private static int level = 0;
private static int mana = 10;
private static int expTotal = 0;
private static int goldTotal = 0;
private static int commandItem, commandBuy, arrow,
        shuriken, bomb, hpPotion, mpPotion, potion, items;
public Skills skillObject = new Skills();

public static void title(){
    System.out.println("Welcome To My First Game!");
}
    //BEGINNING OF MONSTERS
    //First Monster - Easy
public static void huntRat(){
    Scanner console = new Scanner(System.in);
    System.out.println("A Monster Appeared! Its a Wild Rat! ");
    do
    {
    System.out.print("What Skill do you want to use?"
        + "\nBasic,Heal,Item,Run: ");
    commandAttack = console.next();
    System.out.println("");
    if(commandAttack.equalsIgnoreCase("Run")){
        game();
        }
    if(commandAttack.equalsIgnoreCase("Basic")){
        int attack = (int)(15 + Math.random()*40-15+1);
        System.out.println("-You have done: "
            + attack + " damage");
        monsterRat = monsterRat - attack;
        System.out.println("-The Wild Rat has: "
            + monsterRat + " HP left!\n");
        }
    if(commandAttack.equalsIgnoreCase("Heal") && mana >= 2){
        int healing = (int)(10 + Math.random()*35-10+1);
        hp = Math.min(hp + healing, 100);
        mana = Math.max(mana - 2, 0);
        System.out.println("-You have healed: " + healing 
            + "\n-You have now: " + hp + " hp!\n"
                + "Mana: " + mana + "/10\n");
        }
    if(commandAttack.equalsIgnoreCase("Item") && items >= 1){
        System.out.println("Choose what to use::\n");
        System.out.println("1. Arrow: " + arrow + "\n"
                + "2. Shurkiken: " + shuriken + "\n"
                + "3. Bombs: " + bomb + "\n"
                + "4. Small HP-Potion: " + hpPotion + "\n"
                + "5. Mana-Potion: " + mpPotion + "\n"
                + "6. Large HP-Potion: " + potion + "\n");
        commandItem = console.nextInt();
        if(commandItem == 1 && arrow >= 1){
            System.out.println("You fire an arrow at the monster!");
            int attack = 20;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterRat = monsterRat - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterRat + " HP left!\n");
            arrow--;
        }
        if(commandItem == 2 && shuriken >= 1){
            System.out.println("You throw a shuriken at the monster!");
            int attack = 40;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterRat = monsterRat - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterRat + " HP left!\n");
            shuriken--;
        }
        if(commandItem == 3 && bomb >= 1){
            System.out.println("-You throw a bomb at the monster!");
            int attack = 60;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterRat = monsterRat - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterRat + " HP left!\n");
            bomb--;
        }
        if(commandItem == 4 && hpPotion >= 1){
            int healing = 20;
            hp = hp + healing;
            System.out.println("-You have healed: " + healing 
                    + "\n-You have now: " + hp + " hp!\n");
            hpPotion--;
        }
        if(commandItem == 5 && mpPotion >= 1){
            mana = 10;
            System.out.println("-You now have 10/10 mana!");
            mpPotion--;
        }
        if(commandItem == 6 && potion >= 1){
            int healing = 40;
            hp = hp + healing;
            System.out.println("-You have healed: " + healing 
                    + "\n-You have now: " + hp + " hp!\n");
            potion--;
        }
    }
    if(monsterRat > 0){
        int dmg = (int)(10 + Math.random()*25-10+1);
        hp = hp - dmg; 
        System.out.println("-The Wild Rat has done: " + dmg 
                + " HP of damage to you!\n" + "-You have: "
                + hp + "hp left!\n");
        }
    if(hp <= 0){ //Die = GameOver
        System.out.println("-You have died! Game Over!");
        System.exit(0);
    }
    mana = Math.min(mana + 1, 10);
        }
    while(monsterRat >= 1);
    //after battle if wins
    int expRat = (int)(5 + Math.random()*50-5+1);
    int goldRat = (int)(5 + Math.random()*10-5+1);
    goldTotal = goldTotal + goldRat;
    expTotal = expTotal + expRat;
    System.out.println("You have gotten: " + expRat
        + " EXP!\nYou picked up: "
        + goldRat + " gold!\n");
    monsterRat = 100;
    }
    //Second Monster - Intermediate
public static void huntWeasel(){
    Scanner console = new Scanner(System.in);
    System.out.println("A Monster Appeared! Its a Wild Weasel! ");
    do
    {
    System.out.print("What Skill do you want to use?"
        + "\nBasic,Heal,Item,Run: ");
    commandAttack = console.next();
    System.out.println("");
    if(commandAttack.equalsIgnoreCase("Run")){
        game();
        }
    if(commandAttack.equalsIgnoreCase("Basic")){
        int attack = (int)(15 + Math.random()*40-15+1);
        System.out.println("-You have done: "
            + attack + " damage");
        monsterWeasel = monsterWeasel - attack;
        System.out.println("-The Wild Weasel has: "
            + monsterWeasel + " HP left!\n");
        }
    if(commandAttack.equalsIgnoreCase("Heal") && mana >= 2){
        int healing = (int)(10 + Math.random()*35-10+1);
        hp = Math.min(hp + healing, 100);
        mana = Math.max(mana - 2, 0);
        System.out.println("-You have healed: " + healing 
            + "\n-You have now: " + hp + " hp!\n"
                + "Mana: " + mana + "/10\n");
        }
    if(commandAttack.equalsIgnoreCase("Item") && items >= 1){
        System.out.println("Choose what to use::\n");
        System.out.println("1. Arrow: " + arrow + "\n"
                + "2. Shurkiken: " + shuriken + "\n"
                + "3. Bombs: " + bomb + "\n"
                + "4. Small HP-Potion: " + hpPotion + "\n"
                + "5. Mana-Potion: " + mpPotion + "\n"
                + "6. Large HP-Potion: " + potion + "\n");
        commandItem = console.nextInt();
        if(commandItem == 1 && arrow >= 1){
            System.out.println("You fire an arrow at the monster!");
            int attack = 20;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterWeasel = monsterWeasel - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterWeasel + " HP left!\n");
            arrow--;
        }
        if(commandItem == 2 && shuriken >= 1){
            System.out.println("You throw a shuriken at the monster!");
            int attack = 40;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterWeasel = monsterWeasel - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterWeasel + " HP left!\n");
            shuriken--;
        }
        if(commandItem == 3 && bomb >= 1){
            System.out.println("You throw a bomb at the monster!");
            int attack = 60;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterWeasel = monsterWeasel - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterWeasel + " HP left!\n");
            bomb--;
        }
        if(commandItem == 4 && hpPotion >= 1){
            int healing = 20;
            hp = hp + healing;
            System.out.println("-You have healed: " + healing 
                    + "\n-You have now: " + hp + " hp!\n");
            hpPotion--;
        }
        if(commandItem == 5 && mpPotion >= 1){
            mana = 10;
            System.out.println("-You now have 10/10 mana!");
            mpPotion--;
        }
        if(commandItem == 6 && potion >= 1){
            int healing = 40;
            hp = hp + healing;
            System.out.println("-You have healed: " + healing 
                    + "\n-You have now: " + hp + " hp!\n");
            potion--;
        }
    }
    if(monsterWeasel > 0){
        int dmg = (int)(10 + Math.random()*30-10+1);
        hp = hp - dmg; 
        System.out.println("-The Wild Weasel has done: " + dmg 
                + " HP of damage to you!\n" + "-You have: " + hp + "hp left!\n");
        }
    if(hp <= 0){ //Die = GameOver
        System.out.println("-You have died! Game Over!");
        System.exit(0);
    }
    mana = Math.min(mana + 1, 10);
        }
    while(monsterWeasel >= 1);
    //after battle if wins
    int expWeasel = (int)(20 + Math.random()*75-20+1);
    int goldWeasel = (int)(10 + Math.random()*30-10+1);
    goldTotal = goldTotal + goldWeasel;
    expTotal = expTotal + expWeasel;
    System.out.println("You have gotten: " + expWeasel
        + " EXP!\nYou picked up: "
        + goldWeasel + " gold!\n");
    monsterWeasel = 110;
    }
    //Third Monster - Hardest
public static void huntBear(){
    Scanner console = new Scanner(System.in);
    System.out.println("A Monster Appeared! Its a Wild Bear! ");
    do
    {
    System.out.print("What Skill do you want to use?"
        + "\nBasic,Heal,Item,Run: ");
    commandAttack = console.next();
    System.out.println("");
    if(commandAttack.equalsIgnoreCase("Run")){
        game();
        }
    if(commandAttack.equalsIgnoreCase("Basic")){
        int attack = (int)(15 + Math.random()*40-15+1);
        System.out.println("-You have done: "
            + attack + " damage");
        monsterBear = monsterBear - attack;
        System.out.println("-The Wild Bear has: "
            + monsterBear + " HP left!\n");
        }
    if(commandAttack.equalsIgnoreCase("Heal") && mana >= 2){
        int healing = (int)(10 + Math.random()*35-10+1);
        hp = Math.min(hp + healing, 100);
        mana = Math.max(mana - 2, 0);
        System.out.println("-You have healed: " + healing 
            + "\n-You have now: " + hp + " hp!\n"
                + "Mana: " + mana + "/10\n");
        }
    if(commandAttack.equalsIgnoreCase("Item") && items >= 1){
        System.out.println("Choose what to use::\n");
        System.out.println("1. Arrow: " + arrow + "\n"
                + "2. Shurkiken: " + shuriken + "\n"
                + "3. Bombs: " + bomb + "\n"
                + "4. Small HP-Potion: " + hpPotion + "\n"
                + "5. Mana-Potion: " + mpPotion + "\n"
                + "6. Large HP-Potion: " + potion + "\n");
        commandItem = console.nextInt();
        if(commandItem == 1 && arrow >= 1){
            System.out.println("You fire an arrow at the monster!");
            int attack = 20;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterBear = monsterBear - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterBear + " HP left!\n");
            arrow--;
        }
        if(commandItem == 2 && shuriken >= 1){
            System.out.println("You throw a shuriken at the monster!");
            int attack = 40;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterBear = monsterBear - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterBear + " HP left!\n");
            shuriken--;
        }
        if(commandItem == 3 && bomb >= 1){
            System.out.println("You throw a bomb at the monster!");
            int attack = 60;
            System.out.println("-You have done: "
                    + attack + " damage");
            monsterBear = monsterBear - attack;
            System.out.println("-The Wild Rat has: "
                    + monsterBear + " HP left!\n");
            bomb--;
        }
        if(commandItem == 4 && hpPotion >= 1){
            int healing = 20;
            hp = hp + healing;
            System.out.println("-You have healed: " + healing 
                    + "\n-You have now: " + hp + " hp!\n");
            hpPotion--;
        }
        if(commandItem == 5 && mpPotion >= 1){
            mana = 10;
            System.out.println("-You now have 10/10 mana!");
            mpPotion--;
        }
        if(commandItem == 6 && potion >= 1){
            int healing = 40;
            hp = hp + healing;
            System.out.println("-You have healed: " + healing 
                    + "\n-You have now: " + hp + " hp!\n");
            potion--;
        }
    }
    if(monsterBear > 0){
        int dmg = (int)(14 + Math.random()*35-14+1);
        hp = hp - dmg; 
        System.out.println("-The Wild Bear has done: " + dmg 
                + " HP of damage to you!\n" 
                + "-You have: " + hp + "hp left!\n");
        }
    if(hp <= 0){ //Die = GameOver
        System.out.println("-You have died! Game Over!");
        System.exit(0);
    }
    mana = Math.min(mana + 1, 10);
        }
    while(monsterBear >= 1);
    //after battle if wins
    int expBear = (int)(40 + Math.random()*120-40+1);
    int goldBear = (int)(30 + Math.random()*50-30+1);
    goldTotal = goldTotal + goldBear;
    expTotal = expTotal + expBear;
    System.out.println("You have gotten: " + expBear
        + " EXP!\nYou picked up: "
        + goldBear + " gold!\n");
    monsterBear = 120;
    }
    //END OF MONSTERS

public static void shop(){
    do
    {
    Scanner console = new Scanner(System.in);
    System.out.print("----------------------------\nWhat do you want to"
            + " shop for? | Weapons | Items | Magic | Leave |\n"
            + "Choice: ");
    commandShop = console.next();
    System.out.println();
    if(commandShop.equalsIgnoreCase("Weapons")){
        System.out.print("What do you want to buy?\n1. Arrow - 10g"
                + " DMG: 20 (lvl: 0)\n2. Shuriken - 20g DMG: 40 (lvl: 1)"
                + "\n3. Bomb - 40g DMG: 60 (lvl: 2)\n"
                + "Choice: ");
        commandBuy = console.nextInt();
        System.out.println();
        if(commandBuy == 1 && goldTotal >= 10){
            arrow++;
            items++;
            goldTotal = goldTotal - 10;
            System.out.println("You now have: " + arrow + " Arrow(s)!\n"
                    + "You now have: " + goldTotal + " gold!\n");
        }
        if(commandBuy == 2 && goldTotal >= 20 && level >= 1){
            shuriken++;
            items++;
            goldTotal = goldTotal - 20;
            System.out.println("You now have: " + shuriken 
                    + " Shuriken(s)!\n"
                    + "You now have: " + goldTotal + " gold!\n");
        }
        if(commandBuy == 3 && goldTotal >= 40 && level >= 2){
            bomb++;
            items++;
            goldTotal = goldTotal - 40;
            System.out.println("You now have: " + bomb + " Bomb(s)!\n"
                    + "You now have: " + goldTotal + " gold!\n");
        }
        else{
            System.out.println("You do not have the required level or gold"
                    + "need to buy this weapon!");
        }
    }
    if(commandShop.equalsIgnoreCase("Items")){
        System.out.print("What do you want to buy?\n"
                + "1. Small HP-Potion - 10g Heal: 20 (lvl: 0)\n"
                + "2. Mana-Potion - 20g Full-Mana (lvl: 1)\n"
                + "3. Large HP-Potion - 40g Heal: 40 (lvl: 2)\n"
                + "Choice: ");
        commandShop = console.next();
        System.out.println();
        if(commandBuy == 1 && goldTotal >= 10){
            hpPotion++;
            items++;
            goldTotal = goldTotal - 10;
            System.out.println("You now have: "
                    + hpPotion + " Small HP-Potion(s)!\n"
                    + "You now have: " + goldTotal + " gold!\n");
        }
        if(commandBuy == 2 && goldTotal >= 20 && level >= 1){
            mpPotion++;
            items++;
            goldTotal = goldTotal - 20;
            System.out.println("You now have: "
                    + mpPotion + " Mana-Potion(s)!\n"
                    + "You now have: " + goldTotal + " gold!\n");
        }
        if(commandBuy == 3 && goldTotal >= 40 && level >= 2){
            potion++;
            items++;
            goldTotal = goldTotal - 40;
            System.out.println("You now have: "
                    + potion + " Large HP-Potions(s)!\n"
                    + "You now have: " + goldTotal + " gold!\n");
        }
        else{
            System.out.println("You do not have the required level or gold"
                    + "need to buy this item!");
        }
    }
    if(commandShop.equalsIgnoreCase("Magic")){
        System.out.println("Not implemented yet!\n");
    }
    }
    while(!commandShop.equalsIgnoreCase("Leave"));
}

public static void game(){   // last static void used - User input For Home
    Scanner console = new Scanner(System.in);
    do
    {
        System.out.print("-----------------------\n"
                + "What do you want to do?\n"
                + "Hunt | Shop | Sleep | Skills |\nStats | LevelUp"
                + " | Inventory |\n"
                + "Choice: ");
        command=console.next();
        System.out.println();
        if(command.equalsIgnoreCase("Hunt")){
            int monsterChance = (int)(1 + Math.random()*100-1+1);
            if(monsterChance <= 50){
                huntRat();
            }
            if(monsterChance <=90 && monsterChance > 50){
                huntWeasel();
            }
            if(monsterChance <=100 && monsterChance > 90){
                huntBear();
            }
        }
        if(command.equalsIgnoreCase("Sleep")){
            hp = 100;
            mana = 10;
            System.out.println("You HP is now at 100"
                    + " and your Mana is at 10!");
        }
        if(command.equalsIgnoreCase("Stats")){
            System.out.println("You have: " + expTotal 
                    + " exp!\nYou have: " + goldTotal + " gold!\n"
                    + "Your level is: " + level);
        }
        if(command.equalsIgnoreCase("LevelUp")){
            if(expTotal >= 100){
                level++;
                System.out.println("CONGRATS! You have leveled up!"
                        + "\nYour level is: " + level);
                expTotal = expTotal - 100;
            }
            else{
                System.out.println("You don't have enough exp! Hunt more!");
    }
        }
        if(command.equalsIgnoreCase("Shop")){
            System.out.println("Welcome to the shops!");
            shop();
        }
        if(command.equalsIgnoreCase("Inventory")){
            System.out.println("You have: " + arrow + " arrow(s)\n"
                    + "You have: " + shuriken + " shuriken(s)\n"
                    + "You have: " + bomb + " bomb(s)\n"
                    + "You have: " + hpPotion + " Small Hp-Potion(s)\n"
                    + "You have: " + mpPotion + " Mana-Potion(s)\n"
                    + "You have: " + potion + " Large Hp-Potion(s)\n");
        }
        if(command.equalsIgnoreCase("Skills")){
            Skills skillObject = new Skills();
            skillObject.skills();
        }
    }
    while(!command.equalsIgnoreCase("STOP"));         //how to stop game
    System.out.println("You have left the game without saving!");
}
public static void main(String[] args) {
    title();
    game();
}   

}

}

采纳答案by Ubica

If you try to do it with this examplethen you will need to do this in your game class

如果您尝试使用此示例执行此操作,则您需要在游戏类中执行此操作

private ChanDown frame;

public static void main(String[] args) {
    frame = new ChanDown();
    frame.setVisible(true);
    title();
    game();
}

and change all your System.out.println("text here");to frame.textArea.append("\n" + text here);

并将您的所有更改System.out.println("text here");frame.textArea.append("\n" + text here);

remove your Scanner, and just take all the input from this code:

删除您的扫描仪,然后从此代码中获取所有输入:

textField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == 10) { //this is key code for pressing enter
                // do the code you want to do when you get the text entered
            }
        }
    });

but it is obvious that you will need to do a lot of adjustments in order to get all that working as intended...

但很明显,您需要进行大量调整才能按预期进行所有工作......

回答by puzzler7

You can write the following in a Batch file

您可以在批处理文件中编写以下内容

@echo off
java -jar TextBasedRPG.jar

and tell your friends to run the .bat file.

并告诉您的朋友运行 .bat 文件。

回答by Bernardo Sulzbach

Yes, it could be run by double-clicking the JAR if it kicked up a JFrame.

是的,如果它启动了 JFrame,则可以通过双击 JAR 来运行它。

Have a look at https://github.com/mafagafogigante/dungeon, a project of mine that uses a JFrame as a terminal-like window (also a text-based game). You can download a compiled JAR file from the releases tab.

看看https://github.com/mafagafogigante/dungeon,这是我的一个项目,它使用 JFrame 作为类似终端的窗口(也是一个基于文本的游戏)。您可以从发布选项卡下载已编译的 JAR 文件。