怎么用Java的代码制作简单的王者荣耀
怎么用Java的代码制作简单的王者荣耀用Java代码实现简单的王者荣耀游戏:
张飞的生命值:100 攻击力:10 防御力:12 命中率:0.85 )(后羿的生命值:80 攻击力:15 防御力:12 命中率:0.7)(妲己的生命值:50 攻击力:20 防御力:5 命中率:0.75)(元芳的生命值:70 攻击力:10 防御力:8 命中率:0.7)(露露的生命值:70 攻击力:12 防御力:10 命中率:0.8)(大鱼的生命值:100 攻击力:5 防御力:20命中率:0.75) //英雄名字,攻击力,防御力,命中率,用数组来添加,并一 一对应,并添加为英雄类.
并用game类制作游戏过程
import java.util.Random;
class Hero {
private String name;
private int health;
private int attack;
private int defense;
private double hitRate;
public Hero(String name, int health, int attack, int defense, double hitRate) {
this.name = name;
this.health = health;
this.attack = attack;
this.defense = defense;
this.hitRate = hitRate;
}
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public int getAttack() {
return attack;
}
public int getDefense() {
return defense;
}
public double getHitRate() {
return hitRate;
}
public void attack(Hero target) {
Random random = new Random();
if (random.nextDouble() < hitRate) {
int damage = (int) (attack * (1 - target.getDefense() / 100.0));
target.takeDamage(damage);
System.out.println(name + "攻击了" + target.getName() + ",造成了" + damage + "点伤害!");
} else {
System.out.println(name + "攻击了" + target.getName() + ",但未命中!");
}
}
public void takeDamage(int damage) {
health -= damage;
if (health < 0) {
health = 0;
}
}
public boolean isAlive() {
return health > 0;
}
}
public class Game {
public static void main(String[] args) {
Hero zhangFei = new Hero("张飞", 100, 10, 12, 0.85);
Hero houYi = new Hero("后羿", 80, 15, 12, 0.7);
Hero daJi = new Hero("妲己", 50, 20, 5, 0.75);
Hero yuanFang = new Hero("元芳", 70, 10, 8, 0.7);
Hero luLu = new Hero("露露", 70, 12, 10, 0.8);
Hero daYu = new Hero("大鱼", 100, 5, 20, 0.75);
Hero[] heroes = { zhangFei, houYi, daJi, yuanFang, luLu, daYu };
while (true) {
int attackerIndex = new Random().nextInt(heroes.length);
int targetIndex = new Random().nextInt(heroes.length);
Hero attacker = heroes;
Hero target = heroes;
if (attacker.isAlive() && target.isAlive() && attacker != target) {
attacker.attack(target);
if (!target.isAlive()) {
System.out.println(target.getName() + "被击败了!");
}
}
boolean allDead = true;
for (Hero hero : heroes) {
if (hero.isAlive()) {
allDead = false;
break;
}
}
if (allDead) {
System.out.println("所有英雄都被击败了!");
break;
}
}
}
}在这个示例中,我们首先创建了一个 Hero 类,包含了英雄的各项属性和方法。然后,在 Game 类的 main 方法中,我们创建了各个英雄对象,并将它们存储在一个数组中。然后,通过循环不断进行攻击,直到所有英雄都被击败。 每次攻击时,随机选择一个攻击者和一个目标英雄。攻击者使用自己的攻击力和命中率对目标英雄造成伤害,如果未命中则不造成伤害。如果目标英雄的生命值降为0,则被击败。 最后,检查是否所有英雄都被击败,如果是,则游戏结束。
页:
[1]