java回合制游戏实现阵容布阵以及释放技能代码示例
时间: 2024-01-22 07:03:29 浏览: 71
java实现一个简单的网络爬虫代码示例
好的,下面是一个简单的 Java 回合制游戏实现阵容布阵以及释放技能的示例代码:
首先,我们需要定义一个角色类 Role,包含角色的基本属性和方法:
```java
public class Role {
private String name; // 角色名称
private int hp; // 生命值
private int mp; // 魔法值
private int atk; // 攻击力
private int def; // 防御力
private Skill skill; // 技能
// 构造方法
public Role(String name, int hp, int mp, int atk, int def, Skill skill) {
this.name = name;
this.hp = hp;
this.mp = mp;
this.atk = atk;
this.def = def;
this.skill = skill;
}
// 攻击方法
public void attack(Role target) {
int damage = this.atk - target.getDef();
if (damage > 0) {
target.setHp(target.getHp() - damage);
System.out.println(this.name + "对" + target.getName() + "造成了" + damage + "点伤害!");
} else {
System.out.println(this.name + "攻击" + target.getName() + "没有造成伤害!");
}
}
// 使用技能方法
public void useSkill(Role target) {
if (this.mp < this.skill.getMpCost()) {
System.out.println(this.name + "魔法值不足,无法使用技能!");
return;
}
this.mp -= this.skill.getMpCost();
this.skill.apply(target);
System.out.println(this.name + "使用了" + this.skill.getName() + "技能,对" + target.getName() + "造成了" + this.skill.getDamage() + "点伤害!");
}
// getter 和 setter 方法
// ...
}
```
接下来,我们定义一个技能类 Skill,包含技能的基本属性和方法:
```java
public class Skill {
private String name; // 技能名称
private int damage; // 技能伤害
private int mpCost; // 技能魔法值消耗
// 构造方法
public Skill(String name, int damage, int mpCost) {
this.name = name;
this.damage = damage;
this.mpCost = mpCost;
}
// 应用技能方法
public void apply(Role target) {
int damage = this.damage - target.getDef();
if (damage > 0) {
target.setHp(target.getHp() - damage);
}
}
// getter 和 setter 方法
// ...
}
```
然后,我们定义一个战斗类 Battle,用于控制游戏的战斗过程:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Battle {
private List<Role> playerTeam; // 玩家队伍
private List<Role> enemyTeam; // 敌人队伍
// 构造方法
public Battle(List<Role> playerTeam, List<Role> enemyTeam) {
this.playerTeam = playerTeam;
this.enemyTeam = enemyTeam;
}
// 开始战斗方法
public void start() {
System.out.println("战斗开始!");
while (true) {
// 玩家回合
System.out.println("玩家回合:");
for (Role role : playerTeam) {
if (role.getHp() <= 0) {
continue;
}
System.out.println(role.getName() + "的回合:");
System.out.println("1.攻击 2.使用技能");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
// 选择攻击目标
System.out.println("请选择攻击目标:");
for (int i = 0; i < enemyTeam.size(); i++) {
Role target = enemyTeam.get(i);
if (target.getHp() <= 0) {
continue;
}
System.out.println((i + 1) + "." + target.getName());
}
int targetIndex = scanner.nextInt() - 1;
if (targetIndex >= 0 && targetIndex < enemyTeam.size()) {
Role target = enemyTeam.get(targetIndex);
role.attack(target);
}
break;
case 2:
// 选择使用技能目标
System.out.println("请选择使用技能目标:");
for (int i = 0; i < enemyTeam.size(); i++) {
Role target = enemyTeam.get(i);
if (target.getHp() <= 0) {
continue;
}
System.out.println((i + 1) + "." + target.getName());
}
int skillTargetIndex = scanner.nextInt() - 1;
if (skillTargetIndex >= 0 && skillTargetIndex < enemyTeam.size()) {
Role target = enemyTeam.get(skillTargetIndex);
role.useSkill(target);
}
break;
default:
break;
}
}
// 敌人回合
System.out.println("敌人回合:");
for (Role role : enemyTeam) {
if (role.getHp() <= 0) {
continue;
}
Role target = playerTeam.get((int) (Math.random() * playerTeam.size()));
int choice = (int) (Math.random() * 2) + 1;
switch (choice) {
case 1:
role.attack(target);
break;
case 2:
role.useSkill(target);
break;
default:
break;
}
}
// 检查是否有一方全部死亡
boolean allPlayerDead = true;
for (Role role : playerTeam) {
if (role.getHp() > 0) {
allPlayerDead = false;
break;
}
}
if (allPlayerDead) {
System.out.println("玩家全部死亡,游戏结束!");
break;
}
boolean allEnemyDead = true;
for (Role role : enemyTeam) {
if (role.getHp() > 0) {
allEnemyDead = false;
break;
}
}
if (allEnemyDead) {
System.out.println("敌人全部死亡,游戏结束!");
break;
}
}
}
// getter 和 setter 方法
// ...
}
```
最后,我们在主函数中创建角色、技能、队伍,并开始战斗:
```java
public class Main {
public static void main(String[] args) {
// 创建角色
Role player1 = new Role("玩家1", 100, 50, 20, 10, new Skill("普通攻击", 20, 0));
Role player2 = new Role("玩家2", 120, 30, 10, 20, new Skill("火球术", 50, 20));
Role enemy1 = new Role("敌人1", 80, 10, 30, 5, new Skill("普通攻击", 20, 0));
Role enemy2 = new Role("敌人2", 60, 20, 5, 30, new Skill("冰冻术", 40, 15));
// 创建队伍
List<Role> playerTeam = new ArrayList<>();
playerTeam.add(player1);
playerTeam.add(player2);
List<Role> enemyTeam = new ArrayList<>();
enemyTeam.add(enemy1);
enemyTeam.add(enemy2);
// 开始战斗
Battle battle = new Battle(playerTeam, enemyTeam);
battle.start();
}
}
```
以上就是一个简单的 Java 回合制游戏实现阵容布阵以及释放技能的示例代码。
阅读全文