Advertisement
Guest User

Jungle Battle!

a guest
Dec 4th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6.  
  7. // System objects
  8. Scanner in = new Scanner(System.in);
  9. Random rand = new Random();
  10.  
  11. // Game variables
  12. String[] enemies = { "Gromp", "Raptor", "Wolf", "Golem" };
  13. int maxEnemyHealth = 150; //enemy health
  14. int enemyAttackDamage = 25; //enemy attack damage
  15.  
  16. //Player variables
  17. int health = 100; //player health
  18. int attackDamage = 50; //player attack damage
  19. int numHealthPotions = 3; //number of potions
  20. int healthPotionHealAmount = 25; //potion heal amount
  21. int healthPotionDropChance = 33; //percentage
  22.  
  23. boolean running = true;
  24.  
  25. System.out.println("Welcome to the jungle!");
  26.  
  27. GAME:
  28. while(running) {
  29. System.out.println("---------------------------------------------------");
  30.  
  31. int enemyHealth = rand.nextInt(maxEnemyHealth); //sets enemy's health
  32. String enemy = enemies[rand.nextInt(enemies.length)]; //chooses 1 of 4 enemies
  33. System.out.println("\t# " + enemy + " has appeared! #\n"); //enemy appearance
  34. // # Skeleton had appeared! #
  35.  
  36. while(enemyHealth > 0) { //when enemy is alive
  37. System.out.println("\tYour HP: " + health); //display your HP
  38. System.out.println("\t" + enemy + "'s HP: " + enemyHealth); //display enemy HP
  39. System.out.println("\n\tWhat would you like to do?"); //commands
  40. System.out.println("\t1. Attack");
  41. System.out.println("\t2. Drink health potion");
  42. System.out.println("\t3. Run!");
  43.  
  44. String input = in.nextLine(); //whatever you respond with
  45. if(input.equals("1")) { //1
  46. int damageDealt = rand.nextInt(attackDamage); //damage dealt to enemy
  47. int damageTaken = rand.nextInt(enemyAttackDamage); //damage taken
  48.  
  49. enemyHealth -= damageDealt; //subtracts health
  50. health -= damageTaken; //subtracts health
  51.  
  52. System.out.println("\t> You strike the " + enemy + " for " + damageDealt + " damage."); //explains turn
  53. System.out.println("\t> You recieve " + damageTaken + " in retaliation!");
  54.  
  55. if(health < 1){ //if health is 0
  56. System.out.println("\t You have taken too much damage, you are too weak to go on!"); //die
  57. break; //code ends
  58. }
  59. }
  60. else if(input.equals("2")) { //2
  61. if(numHealthPotions > 0) { //if number of heal potions is greater than 0
  62. health += healthPotionHealAmount; //uses potion
  63. numHealthPotions--; //subtracts a potion from inventory
  64. System.out.println("\t You drink a health potion, healing yourself for " + healthPotionHealAmount + "." //explains turn
  65. + "\n\t You now have " + health + " HP."
  66. + "\n\t You have " + numHealthPotions + " health potions left.\n");
  67. }
  68. else { //if there are no potions
  69. System.out.println("\t> You have no health potions left! Defeat enemies for a chance to get one!");
  70. }
  71. }
  72. else if(input.equals("3")) { //3
  73. System.out.println("\tYou run away from the " + enemy + "!"); //runs from enemy
  74. continue GAME; //game continues
  75. }
  76. else { //if input is other than 1, 2, or 3
  77. System.out.println("\tInvalid command!"); //invalid command
  78. }
  79. }
  80.  
  81. if(health < 1) { //if health is less than 1
  82. System.out.println("You limp out of the dungeon, weak from battle.");
  83. break; //end of game
  84. }
  85. if(health > 1) { //if heath is greater than one
  86. System.out.println("---------------------------------------------------"); //explanation of what happened
  87. System.out.println(" # " + enemy + " was defeated! #");
  88. System.out.println(" # You have " + health + " HP left. #"); //current health
  89. if(rand.nextInt(100) < healthPotionDropChance) { //health potion drop chance
  90. numHealthPotions++; //adds one potion
  91. System.out.println(" # The " + enemy + " dropped a health potion! # "); //explanation
  92. System.out.println(" # You have " + numHealthPotions + " health potion(s). #");
  93. }
  94. System.out.println("---------------------------------------------------"); //what would you like to do
  95. System.out.println("What would you like to do now?");
  96. System.out.println("1. Continue fighting");
  97. System.out.println("2. Exit dungeon");
  98.  
  99. String input = in.nextLine();
  100.  
  101. while(!input.equals("1") && !input.equals("2")) { //if input is both = invalid
  102. System.out.println("Invalid Command!");
  103. input = in.nextLine(); //retry
  104. }
  105.  
  106. if(input.equals("1")) { //continues adventure
  107. System.out.println("You continue on your adventure!");
  108. }
  109. else if(input.equals("2")) { //exits dungeon
  110. System.out.println("You exit the dungeon, successful from your adventures!");
  111. break; //end of game
  112. }
  113. }
  114. }
  115. System.out.println("#######################"); //GG
  116. System.out.println("# THANKS FOR PLAYING! #"); //GG
  117. System.out.println("#######################"); //GG
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement