以下是一个简单的Java面向对象文字版格斗游戏示例:
import java.util.Random; public class FighterGame { public static void main(String[] args) { Fighter player1 = new Fighter("Player 1"); Fighter player2 = new Fighter("Player 2"); while (player1.isAlive() && player2.isAlive()) { player1.attack(player2); if (player2.isAlive()) { player2.attack(player1); } } if (player1.isAlive()) { System.out.println(player1.getName() + " wins!"); } else { System.out.println(player2.getName() + " wins!"); } } } class Fighter { private String name; private int health; private int attackPower; private int defensePower; private Random random = new Random(); public Fighter(String name) { this.name = name; health = 100; attackPower = 10; defensePower = 5; } public String getName() { return name; } public boolean isAlive() { return health > 0; } public void attack(Fighter opponent) { int damage = random.nextInt(attackPower); opponent.defend(damage); System.out.println(name + " attacks " + opponent.getName() + " for " + damage + " damage!"); } public void defend(int damage) { int actualDamage = damage - defensePower; if (actualDamage > 0) { health -= actualDamage; System.out.println(name + " takes " + actualDamage + " damage!"); } else { System.out.println(name + " deflects the attack!"); } } }
在这个示例中,有一个Fighter类,代表游戏中的一个角色,每个角色都有名字、血量、攻击力和防御力。每个角色可以攻击对手,并对其造成一定的随机伤害,攻击力越高,造成的伤害就越高。每个角色也可以防御,防御力越高,就越能减少对手攻击的伤害。
Fighter
在FighterGame类中,我们创建了两个Fighter对象代表游戏中的两个角色,并在一个while循环中让他们互相攻击,直到有一个角色的血量归零。当循环结束时,我们输出胜利者的名字。
FighterGame
while
接下来,我们可以进一步完善这个文字版格斗游戏示例,添加更多的功能和更丰富的交互性。
首先,我们可以让玩家输入角色的名字,而不是在代码中直接指定。我们可以使用Scanner类来读取玩家输入的数据。
Scanner
import java.util.Scanner; public class FighterGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter name for player 1: "); String name1 = scanner.nextLine(); Fighter player1 = new Fighter(name1); System.out.print("Enter name for player 2: "); String name2 = scanner.nextLine(); Fighter player2 = new Fighter(name2); // ... } }
现在,玩家可以通过控制台输入角色的名字,而不需要直接修改代码。
接下来,我们可以让玩家选择每回合要进行的操作,包括攻击、防御和逃跑。我们可以添加一个playRound()方法来处理每回合的逻辑。
playRound()
import java.util.Scanner; public class FighterGame { public static void main(String[] args) { // ... while (player1.isAlive() && player2.isAlive()) { playRound(player1, player2); if (player2.isAlive()) { playRound(player2, player1); } } // ... } public static void playRound(Fighter attacker, Fighter defender) { Scanner scanner = new Scanner(System.in); System.out.println(attacker.getName() + "'s turn:"); System.out.println("1. Attack"); System.out.println("2. Defend"); System.out.println("3. Run"); int choice = scanner.nextInt(); switch (choice) { case 1: attacker.attack(defender); break; case 2: attacker.defend(); break; case 3: System.out.println(attacker.getName() + " runs away!"); attacker.runAway(); break; default: System.out.println("Invalid choice!"); playRound(attacker, defender); break; } } } class Fighter { // ... public void defend() { defensePower *= 2; System.out.println(name + " defends!"); } public void runAway() { health = 0; } }
现在,每回合玩家都可以选择攻击、防御或逃跑。如果玩家选择防御,那么他的防御力将会翻倍。如果玩家选择逃跑,那么他的血量将会立即归零,游戏将在下一回合结束时结束。
最后,我们可以让游戏更加动态,使得每个角色的属性(血量、攻击力、防御力)都能够随着游戏的进行而改变。我们可以添加一个Level类来代表角色的等级,每个等级都有自己的属性加成。我们还可以添加一个Experience类来代表角色的经验,每次击败一个对手都能够获得一
Level
Experience
定数量的经验,并有可能升级。
import java.util.Scanner; public class FighterGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter name for player 1: "); String name1 = scanner.nextLine(); Fighter player1 = new Fighter(name1); System.out.print("Enter name for player 2: "); String name2 = scanner.nextLine(); Fighter player2 = new Fighter(name2); while (player1.isAlive() && player2.isAlive()) { playRound(player1, player2); if (player2.isAlive()) { playRound(player2, player1); } } if (player1.isAlive()) { System.out.println(player1.getName() + " wins!"); } else { System.out.println(player2.getName() + " wins!"); } } public static void playRound(Fighter attacker, Fighter defender) { Scanner scanner = new Scanner(System.in); System.out.println(attacker.getName() + "'s turn:"); System.out.println("1. Attack"); System.out.println("2. Defend"); System.out.println("3. Run"); int choice = scanner.nextInt(); switch (choice) { case 1: attacker.attack(defender); break; case 2: attacker.defend(); break; case 3: System.out.println(attacker.getName() + " runs away!"); attacker.runAway(); break; default: System.out.println("Invalid choice!"); playRound(attacker, defender); break; } if (defender.isAlive()) { System.out.println(defender.getName() + "'s health: " + defender.getHealth()); } if (!attacker.isAlive()) { System.out.println(attacker.getName() + " is defeated!"); } } } class Fighter { private String name; private int health; private int attackPower; private int defensePower; private Level level; private Experience experience; public Fighter(String name) { this.name = name; this.health = 100; this.attackPower = 10; this.defensePower = 5; this.level = new Level(); this.experience = new Experience(); } public String getName() { return name; } public int getHealth() { return health; } public int getAttackPower() { return attackPower + level.getAttackBonus(); } public int getDefensePower() { return defensePower + level.getDefenseBonus(); } public boolean isAlive() { return health > 0; } public void attack(Fighter opponent) { int damage = getAttackPower() - opponent.getDefensePower(); if (damage <= 0) { damage = 1; } opponent.takeDamage(damage); System.out.println(name + " attacks " + opponent.getName() + " for " + damage + " damage!"); experience.addExperience(10); levelUp(); } public void defend() { defensePower *= 2; System.out.println(name + " defends!"); } public void runAway() { health = 0; } public void takeDamage(int damage) { health -= damage; } public void levelUp() { if (experience.getExperience() >= level.getExperienceRequired()) { level.levelUp(); experience.resetExperience(); System.out.println(name + " has leveled up to level " + level.getLevel() + "!"); }
}
private class Level { private int level; private int attackBonus; private int defenseBonus; private int experienceRequired; public Level() { this.level = 1; this.attackBonus = 0; this.defenseBonus = 0; this.experienceRequired = 100; } public int getLevel() { return level; } public int getAttackBonus() { return attackBonus; } public int getDefenseBonus() { return defenseBonus; } public int getExperienceRequired() { return experienceRequired; } public void levelUp() { level++; attackBonus += 2; defenseBonus += 1; experienceRequired += level * 100; } } private class Experience { private int experience; public Experience() { this.experience = 0; } public int getExperience() { return experience; } public void addExperience(int experience) { this.experience += experience; } public void resetExperience() { this.experience = 0; } }
这个示例展示了如何使用Java编写一个基本的文字格斗游戏,使用面向对象编程的概念,如封装,继承和多态。游戏由两个Fighter对象组成,每个对象都有一个名称,攻击力,防御力和健康值。每个战士还具有经验和等级,他们可以通过战斗获得经验并升级。游戏通过在命令行中交互地循环进行回合,直到一个战士的健康值降到零或他选择逃跑。
在Fighter类中,我们使用了构造函数和getter和setter方法来访问私有成员变量。我们还实现了一个attack方法,该方法接受另一个Fighter对象作为参数,并根据两个战士的攻击力和防御力计算出对手的损失健康值。我们还为每个战士实现了一个Level类和一个Experience类,用于跟踪每个战士的等级和经验。
attack
在Level类中,我们定义了一些属性,例如攻击和防御的加成,以及升级所需的经验。我们还实现了一个levelUp方法,该方法在战士升级时增加他们的等级和属性加成。
levelUp
在Experience类中,我们定义了一个experience属性,用于跟踪每个战士的经验值。我们还实现了addExperience和resetExperience方法,用于增加和重置战士的经验值。
experience
addExperience
resetExperience
在我们的主方法中,我们创建了两个Fighter对象,并在命令行中循环,直到一个战士的健康值降到零或他选择逃跑。在每个回合中,我们询问玩家要进行哪种操作,并根据选择调用不同的方法。例如,如果玩家选择攻击,我们将调用攻击方法并打印出损失的健康值。如果玩家选择逃跑,我们将退出循环并结束游戏。
这只是一个基本的示例,您可以根据需要进行扩展和改进。例如,您可以添加更多的战斗选项,如防御或使用技能。您还可以改进游戏逻辑,例如通过实现随机数生成器来增加随机性,或者通过增加伤害计算来添加更多深度。
原文链接:codingdict.net