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

时间:2015-01-11 04:07:47

标签: java console

所以,我一直在尝试用Java制作一个简单的基于文本的RPG,而且它很顺利,而且我的朋友也想玩它,但它们并不是非常精通技术,所以它和#39;很难告诉他们获得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();
}   

}

3 个答案:

答案 0 :(得分:1)

如果您尝试使用this example,那么您需要在游戏类中执行此操作

private ChanDown frame;

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

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

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

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
            }
        }
    });

但显而易见的是,您需要进行大量调整才能使所有工作按预期工作......

答案 1 :(得分:0)

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

@echo off
java -jar TextBasedRPG.jar

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

答案 2 :(得分:0)

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

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