I created a game that allows you to battle an enemy until one is defeated. It's my first attempt at writing a game like this in Java and I have been using it as a means to practice OOP and other topics as I learn them.
The current move choices each character has are a basic attack, throw ice crystal(freeze target for 2 turns), drink potion(restore 15 health), and throw fireball(guaranteed 20 damage).
I am having a hard time figuring out what kinds of additions I can add from here or how I can improve/ build upon my code in a way that I understand still being fairly new to the language.
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class GameFile extends JFrame
{
public static void main(String[] args)
{
int playAgain;
Random rng = new Random();
do
{
GameCharacter player = generatePlayer();
GameCharacter enemy = generateEnemy();
int playerFrozen = 0;
int enemyFrozen = 0;
do
{
//player's turn
if (player.isFrozenStatus() == false)
{
int choice = menuSelect(player);
if (choice == 0)
basicAttack(player, enemy);
else if (choice == 1)
drinkPotion(player);
else if (choice == 2)
useFireBall(player, enemy);
else if (choice == 3)
useIceCrystal(player, enemy);
else
System.exit(0);
}
else
{
playerFrozen += 1;
if (playerFrozen % 2 == 0)
player.setFrozenStatus(false);
}
//enemy's turn
if (enemy.getHealthPoints() > 0 && !enemy.isFrozenStatus())
{
int enemyChoice = rng.nextInt(4);
if ((enemy.getHealthPoints() <= 10) && (enemy.getPotions() > 0))
drinkPotion(enemy);
else if (enemyChoice == 0 && enemy.getFireBalls() > 0)
useFireBall(enemy, player);
else if (enemyChoice == 1 && enemy.getIceCrystals() > 0 && !player.isFrozenStatus())
useIceCrystal(enemy, player);
else
basicAttack(enemy, player);
}
else if (enemy.getHealthPoints() > 0)
{
enemyFrozen += 1;
if (enemyFrozen % 2 == 0)
enemy.setFrozenStatus(false);
}
} while (player.getHealthPoints() > 0 && enemy.getHealthPoints() > 0);
playAgain = displayWinner(player, enemy);
} while (playAgain == 0);
}
// OTHER METHODS
public static GameCharacter generateEnemy()
{
GameCharacter enemy = new GameCharacter(10, "the Evil Wizard", CharacterType.ENEMY);
return enemy;
}
public static GameCharacter generatePlayer()
{
String name = JOptionPane.showInputDialog("Welcome! What is your name?");
if (name == null)
System.exit(0);
GameCharacter player = new GameCharacter(10, name, CharacterType.PLAYER);
simpleMessage("Hi " + player.getName() + "! Get ready to battle your opponent!");
return player;
}
public static void basicAttack(GameCharacter attacker, GameCharacter target)
{
attacker.attack(target);
simpleMessage((attacker.getType() == CharacterType.ENEMY
? attacker.getName() + " attacked you! You have " + target.getHealthPoints() + " health left."
: "You attacked " + target.getName() + "! They have " + target.getHealthPoints() + " health left."));
}
public static void useIceCrystal(GameCharacter attacker, GameCharacter target)
{
if (attacker.throwIceCrystals(target))
{
target.setFrozenStatus(true);
simpleMessage((attacker.getType() == CharacterType.ENEMY
? attacker.getName() + " threw an Ice Crystal!\nYou are frozen for the next 2 rounds!"
: "You threw an Ice Crystal!\n" + target.getName() + " is frozen for the next 2 rounds!"));
}
else if (attacker.getType() == CharacterType.PLAYER)
simpleMessage("You are out of Ice Crystals....");
}
public static void useFireBall(GameCharacter attacker, GameCharacter target)
{
if (attacker.throwFireBall(target))
simpleMessage((attacker.getType() == CharacterType.ENEMY
? attacker.getName() + " threw a FireBall at you! You have " + target.getHealthPoints() + " health left."
: "You threw a FireBall at " + target.getName() + "! They have " + target.getHealthPoints() + " health left."));
else if (attacker.getType() == CharacterType.PLAYER)
simpleMessage("You are out of FireBalls...");
}
public static void drinkPotion(GameCharacter drinker)
{
if (drinker.usePotion())
simpleMessage((drinker.getType() == CharacterType.ENEMY
? drinker.getName() + " used a potion! They have " + drinker.getHealthPoints() + " health."
: "You used a potion! You have " + drinker.getHealthPoints() + " health."));
else if (drinker.getType() == CharacterType.PLAYER)
simpleMessage("You are out of potions!");
}
public static int displayWinner(GameCharacter player, GameCharacter enemy)
{
if (player.getHealthPoints() <= 0)
JOptionPane.showMessageDialog(null, "Sorry, you lose!\n" + enemy.getName() + " has defeated you...");
else
JOptionPane.showMessageDialog(null, "Great job " + player.getName() + ", you win!" + "\nYou have defeated " + enemy.getName() + "!");
return JOptionPane.showConfirmDialog(null, "Would you like to play again?", " ", JOptionPane.YES_NO_OPTION);
}
public static int menuSelect(GameCharacter player)
{
String[] moveChoices = { "Attack", "Use Potion(" + player.getPotions() + ")", "Throw FireBall(" + player.getFireBalls() + ")",
"Throw Ice Crystal(" + player.getIceCrystals() + ")" };
int choice = JOptionPane.showOptionDialog(null, "What would you like to do?", " ", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, moveChoices, moveChoices[0]);
return choice;
}
public static void simpleMessage(String boxMessage)
{
if (JOptionPane.showConfirmDialog(null, boxMessage, " ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) != 0)
System.exit(0);
}
}