Jump to content
  • Advertisement

dajiandbun

Member
  • Content Count

    6
  • Joined

  • Last visited

Community Reputation

104 Neutral

About dajiandbun

  • Rank
    Newbie
  1. dajiandbun

    optimizing code for first game

    fixed it. it was a problem with the switch and i forgot to initialize a scanner to get input. it works fine now!
  2. dajiandbun

    optimizing code for first game

    it was a problem with my switch in battleSlime(). but now it wont let me enter a response, it just terminates after i battleSlime starts do i even need the while loops with the fight_tech if im just calling the method again from slimeAttack? i feel like i already have a loop just by repeatedly calling the methods back and forth until the slime dies
  3. dajiandbun

    optimizing code for first game

    when i set battle_slime to false after its health reaches 0 it still gives the infinite loop public static void battleSlime() { boolean battle_slime = true; while (battle_slime) { System.out.println("Type 'F' to fight, 'D' to defend, or 'R' to run away"); switch('F') { case 'F': fightTech(); break; case 'D': defendTech(); break; case 'R': break; } if (s_health == 0 || s_health < 0) { battle_slime = false; Victory(); } } }
  4. dajiandbun

    optimizing code for first game

    huh. didnt show all the code if (ROLLR <= 10) { System.out.println("You successfully escaped"); } else { System.out.println("Couldn't escape"); } } public static void battleSlime() { boolean battle_slime = true; System.out.println("You've encountered a slime!!"); while (battle_slime) { System.out.println("Type 'F' to fight, 'D' to defend, or 'R' to run away); "); switch('F') { case 'F': fightTech(); break; case 'D': defendTech(); break; case 'R': break; } } } public static void slimeAttack() { int low = 1; int high = 10; int roll = (int)(Math.random() * 10); int damage = hero_defense - s_strength; if (roll <= 5) { hero_health = hero_health - damage; System.out.println("Slime dealt " + damage + " damage."); System.out.println("You have " + hero_health + " health left"); fightTech(); } else { System.out.println("Slime missed!"); System.out.println("You have " + hero_health + " health left"); fightTech(); } } public static void slimeDead() { } public static void Victory() { System.out.println("You've defeated the slime!"); } }
  5. Im in the progress of working on a crappy little fight simulator as my first game in java. Ive got most of it done, but whenever i fight the slime i get an infinite loop. can somebody point out to me what i did wrong? also any tips on making my code smoother and easier to read will be appreciated. [source lang="java"]import java.io.*; import java.util.*; public class HelloWorld { static String name; static boolean gameStart = true; static Scanner sc = new Scanner(System.in); static int hero_health = 100; static int hero_strength = 10; static int hero_defense = 8; static int hero_agility = 6; static int hero_exp = 0; static int s_health = 25; static int s_strength = 3; static int s_defense = 3; static int s_agility = 1; public static void main(String args[]){ System.out.println("What is your name?"); name = sc.next(); System.out.println("Welcome " + name); System.out.println("Your stats: "); System.out.println(hero_health + " health"); System.out.println(hero_strength + " strength"); System.out.println(hero_defense + " defense"); System.out.println(hero_agility + " agility"); startGame(); } public static void startGame() { Scanner sc = new Scanner(System.in); System.out.println("You awake in a tavern. You walk outside to hear commotion in the streets. " + "From what you can gather, the princess of the kingdom has been kidnapped and has been taken to a " + "nearby castle."); System.out.println("You wander around the village asking where the castle is, hoping to make a few quick bucks." + "After a good hour, you learn that the castle is just outside the village, but it is guarded by a dragon"); System.out.println("Even though you are weak, you have slain dragons before. All you need is to train back to your normal level and fight the dragon"); System.out.println("Do you want to go outside of the village to train? Or would you rather relax at the tavern?"); System.out.println("Type '1' to rest or '2' to go fight monsters"); int start = sc.nextInt(); switch(start) { case 1: System.out.println("You decide to return to the tavern to rest."); hero_health = 100; System.out.println("HEALTH RESTORED."); break; case 2: System.out.println("You wander out of the town looking for a fight"); battleSlime(); break; } } public static void fightTech() { boolean fight_tech = true; int basedamage = hero_strength - s_defense; final int LOWF = 1; int damage = (int)(Math.random() * (basedamage - s_defense + 1)) + LOWF; final int HIGHF = 20; int ROLLF = (int)(Math.random() * 20) + 1; int crithit = basedamage * 2; while (fight_tech) { if (ROLLF <= 13) { System.out.println("You hit!"); s_health = s_health - damage; System.out.println("You dealt " + damage + " damage to slime."); System.out.println("Slime has " + s_health + " health left"); if (s_health == 0 || s_health < 0) { fight_tech = false; Victory(); } slimeAttack(); } else if (ROLLF >= 14 && ROLLF <= 18) { System.out.println("Aww you missed!"); System.out.println("Slime has " + s_health + " health left"); slimeAttack(); } else if (ROLLF >=19) { System.out.println("Critical hit!!!!!"); s_health = s_health - crithit; System.out.println("Slime has " + s_health + " health left"); slimeAttack(); } System.out.println("Slime has " + s_health + " health left"); } } public static void defendTech() { final int LOWD = 1; final int HIGHD = 10; final int ROLLD = (int)(Math.random() * (HIGHD - LOWD + 1)) + LOWD; if (ROLLD <=3) { System.out.println("You avoided half the damage by blocking"); } else if (ROLLD >=4 && ROLLD < 10) { System.out.println("Close but not close enough. You only blocked 25% of damage."); } else if (ROLLD == 10) { System.out.println("You managed to roll out of the way. Took no damage"); } } public static void runTech() { final int LOWR = 1; final int HIGHR = 10; final int ROLLR = (int)(Math.random() * (HIGHR - LOWR +1)) + LOWR; if (ROLLR <= 10) { System.out.println("You successfully escaped"); } else { System.out.println("Couldn't escape"); } } public static void battleSlime() { boolean battle_slime = true; System.out.println("You've encountered a slime!!"); while (battle_slime) { System.out.println("Type 'F' to fight, 'D' to defend, or 'R' to run away); "); switch('F') { case 'F': fightTech(); break; case 'D': defendTech(); break; case 'R': break; } } } public static void slimeAttack() { int low = 1; int high = 10; int roll = (int)(Math.random() * 10); int damage = hero_defense - s_strength; if (roll <= 5) { hero_health = hero_health - damage; System.out.println("Slime dealt " + damage + " damage."); System.out.println("You have " + hero_health + " health left"); fightTech(); } else { System.out.println("Slime missed!"); System.out.println("You have " + hero_health + " health left"); fightTech(); } } public static void slimeDead() { } public static void Victory() { System.out.println("You've defeated the slime!"); } }[/source]
  6. dajiandbun

    java books

    i recently bought ivor hortons java tutorial book and its a little too hard for me still so i bought java for dummies 9 in one book and im getting the hang of it. i want to get into game programming so i had to start somewhere lol. what good tutorials or books are there that can direct me more towards programming games?
  7. Your lips Your lies Your lust Like the devils in your hands #anberlin
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!