java王者荣耀技能对战代码
时间: 2023-07-09 19:38:02 浏览: 36
以下是一个简单的 Java 王者荣耀技能对战代码示例:
```java
// 定义技能接口
interface Skill {
public void castSkill(Hero target);
}
// 定义英雄类
class Hero {
private String name;
private int hp;
private int mp;
private List<Skill> skills;
private Skill currentSkill;
public Hero(String name, int hp, int mp, List<Skill> skills) {
this.name = name;
this.hp = hp;
this.mp = mp;
this.skills = skills;
}
public void selectSkill(int index) {
this.currentSkill = this.skills.get(index);
}
public void useSkill(Hero target) {
if (this.currentSkill != null && this.mp >= 10) {
this.currentSkill.castSkill(target);
this.mp -= 10;
}
}
public void receiveDamage(int damage) {
this.hp -= damage;
if (this.hp < 0) {
this.hp = 0;
}
}
public boolean isDead() {
return this.hp == 0;
}
}
// 定义技能类
class NormalAttack implements Skill {
private String name;
private int damage;
public NormalAttack(String name, int damage) {
this.name = name;
this.damage = damage;
}
public void castSkill(Hero target) {
target.receiveDamage(this.damage);
System.out.println(this.name + "造成了" + this.damage + "点伤害!");
}
}
class Fireball implements Skill {
private String name;
private int damage;
public Fireball(String name, int damage) {
this.name = name;
this.damage = damage;
}
public void castSkill(Hero target) {
target.receiveDamage(this.damage);
System.out.println(this.name + "造成了" + this.damage + "点伤害,并使目标进入燃烧状态!");
}
}
// 游戏场景
public class Game {
public static void main(String[] args) {
// 初始化英雄和技能
Hero hero1 = new Hero("盖伦", 1000, 100, Arrays.asList(new NormalAttack("普通攻击", 100), new Fireball("火球术", 200)));
Hero hero2 = new Hero("寒冰", 800, 150, Arrays.asList(new NormalAttack("普通攻击", 100), new Fireball("火球术", 200)));
// 开始战斗
while (!hero1.isDead() && !hero2.isDead()) {
// 英雄1释放技能
hero1.selectSkill(1);
hero1.useSkill(hero2);
// 英雄2释放技能
hero2.selectSkill(0);
hero2.useSkill(hero1);
}
// 判断胜负
if (hero1.isDead()) {
System.out.println(hero2.getName() + "获胜!");
} else {
System.out.println(hero1.getName() + "获胜!");
}
}
}
```
上述代码实现了英雄和技能的定义,以及简单的对战逻辑。在游戏场景中,我们创建了两个英雄对象和多个技能对象,然后让两个英雄轮流释放技能,直到有一方英雄死亡为止,最后判断胜负并输出结果。