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:
while(!gameover) //loops until gameover
{
if(first_player) //what happens if the player or eggman is chosen first
{
players[r] = 0; //eggman's turn
//attack damages
int E_attack1 = 5;
int E_attack2 = 10;
int E_attack3 = 15;
//attacks in an array
int[] attacks = { E_attack1, E_attack2, E_attack3 };
int A = attacks.length;
int p = (int) (Math.random() * A); //makes the attacks random
//the effects of what the damages will do to the Player's Life Points
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"); //the print out of the attack and damage
}
//players turn
else
{
//players attack damages
int attack1 = 5;
int attack2 = 10;
int attack3 = 15;
text.append( "Choose attack1 , attack2, or attack3: " + "\n"); //gives player a choice of attacks
inputField.getText(); //gets the text
text.getText(); //gets the text from the text area
//what happens when one of the attacks is chose
if(text.equals("attack1"))
{
//the effect of the attack on Eggman's life points
Eggman_LP -=attack1;
//the print out of the attack and the damage
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");
}
}
//make it turn base
first_player = !first_player;
//how to determine gameover
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: