我制作一个基于文本的冒险游戏,当玩家的健康状况达到0时,我在退出游戏时遇到问题。我希望它退出方法和主要方法并通过消息给游戏但是我甚至不能在没有主循环的情况下退出方法。这是我的主要方法和战斗代码:
我有统计数据并作为静态变量运行
boolean running = true;
printProgramDescription();
if(running != false)
{
combat();
}
done(); //Displays Game-over and thank you for playing
} //End of main method
public static int[] combat() throws Exception {
Random rand = new Random();
String[] enemies = {"Skeleton", "Zombie", "Ghost", "Ghoul"};
String[] attackOptions = {"\t1. Attack", "\t2. Drink health potion", "\t3.Run away!"};
boolean run = false;
int healthPotionDropChance = 30; //Percentage
String enemy, strCombat, lowerStr;
int enemyHealth, damageTaken, damageDealt;
int maxEnemyHealth = 60;
int enemyAttackDamage = 40;
System.out.println("");
System.out.println("============================");
enemyHealth = rand.nextInt(maxEnemyHealth) + 5;
enemy = enemies[rand.nextInt(enemies.length)];
System.out.println("");
System.out.println("\t# " + enemy + " has appeared! #\n");
while(enemyHealth > 0)
{
System.out.println("\tYour HP: " + health);
System.out.println("\t" + enemy + "'s HP: " + enemyHealth);
System.out.println("\tWhat would you like to do?");
for(int i = 0; i < attackOptions.length; i ++)
{
System.out.println(attackOptions[i]);
}
strCombat = strInput.nextLine();
lowerStr = strCombat.toLowerCase();
if(lowerStr.equals("1") || lowerStr.equals("attack"))
{
damageDealt = rand.nextInt(attackDamage);
damageTaken = rand.nextInt(enemyAttackDamage);
enemyHealth -= damageDealt;
health -= (damageTaken - armor);
System.out.println("\tYou attack the " + enemy + " for " + damageDealt + " damage.");
System.out.println("\tYou recieve " + damageTaken + " damage in retaliation!");
System.out.println("");
if(health < 1)
{
break;
}
}
else if(lowerStr.equals("2") || lowerStr.equals("drink health potion"))
{
healthPotion();
}
else if(lowerStr.equals("3") || lowerStr.equals("run away"))
{
damageTaken = rand.nextInt(enemyAttackDamage);
System.out.println("\tYou run away but while doing so, you take " + damageTaken + " damage!");
run = true;
break;
}
else
{
printWithDelays("Invalid command!", TimeUnit.MILLISECONDS, 50);
System.out.println("");
}
}
if(health < 1)
{
printWithDelays("\tYou have taken too much damage... You cannot sustain your wounds... You drift off into unconsciousness...", TimeUnit.MILLISECONDS, 50);
running = false;
}
else if(run == true)
{
System.out.println("\tThe " + enemy + " looses sight of you and you continue on with your (boring) adventure");
}
else
{
System.out.println("====================");
System.out.println(enemy + " was defeated!");
System.out.println("You have " + health + " HP left.");
stats[0] = health;
if(rand.nextInt(100) < healthPotionDropChance )
{
numHealthPotions ++;
System.out.println(enemy + " has dropped a health potion!");
System.out.println("You now have " + numHealthPotions + " health potion(s) in your inventory");
System.out.println("");
stats[1] = numHealthPotions;
}
}
return stats;