在DrJava中遇到基于文本的Java游戏的问题

时间:2016-10-24 04:27:34

标签: java text-based drjava

菜单没有链接,在第一个菜单之后,它会回到开头,也试图找出如何将它变成一个波浪系统,一旦只有少数敌人来到这里,一旦这些敌人被击败你可以进入下一波和一个保存功能,它允许你保存到你的角色,然后在返回游戏时加载它们。已经挣扎了一个星期了。

import java.util.Random;
import java.util.Scanner;

public class Main3 
{


  public static void main (String[] args)
  {

    menu();  
  }


  static void menuDisp()
  {
    System.out.println("welcome");
    System.out.println("1. Start Game"); 
    System.out.println("2. Character select");
    System.out.println("3. Quit");
  }

  static void menu()
  {
    Scanner sc = new Scanner(System.in);

    int ch=0;
    for( ; ; )
    {
      menuDisp();
      ch = sc.nextInt();
      sc.nextLine();

      switch(ch)
      {
        case 1:
          game();
          break;

        case 2:
          System.out.println("Coming Soon");
          break;

        case 3:
          return;

        default:
           System.out.println("invalid input");

      }
    }
  }

  static Scanner sc = new Scanner(System.in);

  static Random ran = new Random();

  //Game variables 

  //enemies stat 
  static String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin"};
  static int maxEnemyHealth = 125;
  static int enemyAttackDamage = 25;

  // Player stat
  static int health = 100;
  static int attackDamage = 50;
  static int fireball = 100;
  static int numHealthPotions = 3;
  static int healthPotionHealAmount = 30;
  static int healthPotionDropChance = 50; 
  static int experience = 100;
  static int playerLvl = 0;
  static int playerExp = 0; 
  static int score = 0;
  //static String GAME;

  static int enemyHealth = ran.nextInt(maxEnemyHealth);
  static String enemy = enemies[ran.nextInt(enemies.length)];


  public static void game()
  {

    boolean running = true; 

    System.out.println("Welcome to the game");

    /**ID*/
    GAME:
      while (running){
      //System.out.println("---------------------------------------------");
      //System.out.println("\t# " + enemy + " has appeared! #\n");

      gamemenu();


        if (health <1)
        {
          System.out.println("You limp out of the doungeon, weak from battle.");
          break;
        }

        System.out.println("---------------------------------------------");
        System.out.println(" # " + enemy + " was defeated! # ");
        playerExp += ran.nextInt(experience);
        System.out.println(" # You recieve " + playerExp + " xp from battle #");

        if (playerExp >= 100)
        {
          playerLvl += 1 ;
          health += 100;
          attackDamage += 10;
          playerExp -= 100;
          System.out.println(" # You have grown to level " + playerLvl + "#");
          System.out.println(" # You have gained 100hp # \n # Your max damage and increase +10 dmg");
        }

        System.out.println(" # You have " + health + " HP left. #");
        if(ran.nextInt(100) < healthPotionDropChance ){
          numHealthPotions++;
          System.out.println(" # the " + enemy + "Dropped a health postion!# ");
          System.out.println(" # You now have " + numHealthPotions + " health postion(s). # ");
        }
       leavemenu(); 
       /* System.out.println("---------------------------------------------");
        System.out.println("What would you like to do now?");
        System.out.println("1. Continue fighting");
        System.out.println("2. Exit Dungeon");

        String input = sc.nextLine();

        while(!input.equals("1") && !input.equals("2")){
          System.out.println("invalid command!");
          input = sc.nextLine(); 
        }


        if(input.equals("1"))
        {
          System.out.println("You continue on your adventrue!");
          gamemenu();
        }


        else if(input.equals("2"))
        {
          System.out.println("You exit the doungeon, successful from your adventures!");
          break;
        }*/

      }

      System.out.println("#######################");
      System.out.println("# THANKS FOR PLAYING! #");
      System.out.println("#######################");
      return;

    }

    public static void experience()
    {

    }

    /**Game Menu display  */
    static void gamemenuDisp()
    {
      System.out.println("---------------------------------------------");
      System.out.println("\t# " + enemy + " has appeared! #\n");
      System.out.println("\tYour HP: " + health);
      System.out.println("\tYour Exp: " + playerExp);
      System.out.println("\tYour Lvl: " + playerLvl);
      System.out.println("\t" + enemy + "'s HP: " + enemyHealth);
      System.out.println("\n\tWhat would you like to do?");
      System.out.println("\t1. Attack menu");
      System.out.println("\t2. Drink health potion");
      System.out.println("\t3. Run!");
    }

    static void gamemenu()
    {
      Scanner sc = new Scanner(System.in);

      int ch=0;

      while (enemyHealth > 0)
      {
        //for( ; ; )
        //{
          gamemenuDisp();
          ch = sc.nextInt();
          sc.nextLine();


          switch(ch)
          {
            case 1:
              attackmenu();


            case 2:
              potionHealth();
              break;

            case 3:
              run();
              return;

            default:
              System.out.println("invalid input");
          }
        break;
      }
     /* if (enemyhealth <= 0)
      {
        continue GAME;
      }*/
    }

    static void attack()
      {
            int damageDealt = ran.nextInt(attackDamage);
            int damageTaken = ran.nextInt(enemyAttackDamage);

            enemyHealth -= damageDealt;
            health -= damageTaken;

            System.out.println("\t> You strike the " + enemy + " for " +damageDealt + " damage.");
            System.out.println("\t> You recieve " + damageTaken + " in retaliation!");

            if(health <= 0){
              System.out.println("\t Yout have taken too much damage, you are too weak to go on!");
              System.exit(0);
            }
            gamemenu();
      }

    static void fireball()
      {
            int damageDealt = ran.nextInt(fireball);
            int damageTaken = ran.nextInt(enemyAttackDamage);

            enemyHealth -= damageDealt;
            health -= damageTaken;

            System.out.println("\t> You strike the " + enemy + " with a fire ball for " +damageDealt + " damage.");
            System.out.println("\t> You recieve " + damageTaken + " in retaliation!");

            if(health <= 0){
              System.out.println("\t> Yout have taken too much damage, you are too weak to go on!");
              System.exit(0);
            }
      }

    static void potionHealth()
      {
            if(numHealthPotions > 0) {
              health += healthPotionHealAmount;
              numHealthPotions--;
              System.out.println("\t> You drink a health potion, healing yourself for " +healthPotionHealAmount + "." 
                                   + "\n\t> You now have " + health + " HP"
                                   + "\n\t> You have " + numHealthPotions + " healthpotions left.\n");
            }
            else 
            {
              System.out.println("\t> You have no health potions left!");
            }
      }

    static void run()
     {
         System.out.println("\tYou run away from the " +enemy);
         //continue GAME;
     }


    /** fighting option display */  
    static void submenuDisp()
    {
      System.out.println("Attack menu");
      System.out.println("\t1. Attack"); 
      System.out.println("\t2. Fireball");
      System.out.println("\t3. back");
    }
    /** fighting option menu*/
    static void attackmenu()
    {
      Scanner sc = new Scanner(System.in);

      int ch=0;
      for( ; ; )
      {
        submenuDisp();
        ch = sc.nextInt();
        sc.nextLine();


        switch(ch)
        {
          case 1:
            attack();
            //return;
            break;

          case 2:
            fireball();
            return;
            //break;

          case 3:
            return;

          default:
            System.out.println("invalid input");  
        }
      }
    } 


  static void leavemenuDisp()
  {
    System.out.println("what would you like to do?");
    System.out.println("1. Continue fighting"); 
    System.out.println("2. Exit Dungeon");
  }

  static void leavemenu()
  {
    Scanner sc = new Scanner(System.in);

    int ch=0;

    for( ; ; )
    {
      menuDisp();
      ch = sc.nextInt();
      sc.nextLine();

      switch(ch)
      {
        case 1:
          gamemenu();
          break;

        case 2:
          System.out.println("You exit the doungeon, successful from your adventures!");
          break;


        default:
           System.out.println("invalid input");
      }
    }

  }
}

0 个答案:

没有答案