I've made a text based turn based fighting application game. You fight an enemy until your enemy or your life points run out. It works perfectly as a plain application, but I'm running into problems converting it to swing, so I can make it look nice with a GUI. I'm having the text display in a JTextArea. Problem is when it loops it never gives me the chance to attack, it only allows the enemy to attack until my life points reach 0. What can I do to have it stop when its my turn, so I can type in my attack?
Here is the part of the code that is creating this problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| while(!gameover) { if(first_player) { players[r] = 0; int E_attack1 = 5; int E_attack2 = 10; int E_attack3 = 15; int[] attacks = { E_attack1, E_attack2, E_attack3 }; int A = attacks.length; int p = (int) (Math.random() * A); if(attacks[p] == E_attack1) { Player_LP -=E_attack1; } else if(attacks[p] == E_attack2) { Player_LP -=E_attack2;
} else if(attacks[p] == E_attack3) { Player_LP -=E_attack3; } text.append("Eggman does "+ attacks[p] + " damage." + "\n"); } else { int attack1 = 5; int attack2 = 10; int attack3 = 15; text.append( "Choose attack1 , attack2, or attack3: " + "\n"); inputField.getText(); text.getText(); if(text.equals("attack1")) { Eggman_LP -=attack1; text.append( "\n" + "You attack with attack1 causing " + attack1 + " damage to Eggman."+"\n"); } else
if (text.equals("attack2")) { Eggman_LP -=attack2; text.append( "\n" + "You attack with attack2 causing " + attack2 + " damage to Eggman."+"\n"); } else if(text.equals("attack3")) { Eggman_LP -=attack3; text.append( "\n" + "You attack with attack3 causing " + attack3 + " damage to Eggman."+"\n"); } } first_player = !first_player; if (Player_LP <=0) { gameover = true; text.append("Eggman Wins"); } else if (Eggman_LP <=0) { gameover = true; text.append("You Win"); } } |
And the game appears like this when I run it:
What is your player name: DKN
You chose DKN as your player name.
Your enemy will be Eggman.
You and your opponent will have 100 life points.
The player to lose all of their life points loses the game.
You go first
Choose attack1 , attack2, or attack3:
Eggman does 10 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 10 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 15 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 15 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 15 damage.
Eggman Wins