本次向大家展示的是一个回合制小游戏,首先创建一个角色类,用来存放角色,角色类包含角色名、角色血量、技能、被动技能、死亡次数一系列属性,和指定攻击对象的方法。接着就是创建技能类,用来给角色传入主动技能、被动技能。再创建游戏规则类,给游戏制定规则。最后进行测试。
下面就是代码展示
有兴趣的可以,自行传入,对应的角色名、血量、技能名等。。。可以自行体验。
角色类
public class Role {
/**
* 角色名
*/
private String name;
/**
* 角色剩余血量
*/
private long blood;
/**
* 技能
*/
private Skill[] skills;
/**
* 被动技能
*/
private Skill[] passiveSkillS;
/**
* 角色最大血量
*/
private long maxBlood;
/**
* 死亡次数
*/
private int deadCount ;
public Role(String name, long blood, Skill[] skills, Skill[] passiveSkillS) {
this.name = name;
this.blood = blood;
this.skills = skills;
this.passiveSkillS = passiveSkillS;
this.maxBlood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getBlood() {
return blood;
}
/**
* 随机获取一个主动技能
*
* @return
*/
public Skill getSkill() {
int index = (int) (Math.random() * skills.length);
return skills[index];
}
/**
* 随机获取一个被动技能
*
* @return
*/
public Skill getPassiveSkill() {
int index = (int) (Math.random() * passiveSkillS.length);
return passiveSkillS[index];
}
/**
* 攻击 指定的角色
*
* @param role
*/
public void attack(Role role) {
// 获取一个 主动技能,准备攻击
Skill skill = getSkill();
// 获取该技能的伤害值
long hurt = skill.getHurt();
System.out.println(this.name + "使用" + skill.getName() + "发起攻击");
// 对方 随机产生一个 被动 技能
Skill passiveSkill = role.getPassiveSkill();
if ("玄龟宝术".equals(passiveSkill.getName())) {
System.err.println(role.name + "使用" + passiveSkill.getName() + "进行防御,免疫所有伤害");
return;
}
// 如果不是 铜墙铁壁, 获取被动技能 抵抗的伤害值
long _hurt = passiveSkill.getHurt();
// 计算本次攻击带来的伤害值
long $hurt = hurt - _hurt;
if ($hurt < 0) $hurt = 0;
if ($hurt > 0) role.blood -= $hurt;
System.err.println(role.name + "使用" + passiveSkill.getName() + "进行被动防御,本次共造成" + $hurt + "伤害,还剩下" + role.blood + "血");
if (role.blood <= 0) {
// 判断是否会触发 起死回生技能
if (deadCount == 0 && Math.random() < 0.5) {
$hurt = role.maxBlood / 10;
role.blood = $hurt;
System.err.println(role.name + "触发了 柳神法 ,血量恢复到" + $hurt);
deadCount++ ;
}
}
}
}
技能类
import java.util.Objects;
/**
* 技能类
*/
public class Skill {
/**
* 技能名
*/
private String name;
/**
* 技能伤害
*/
private long hurt;
/**
* 技能描述
*/
private String desc;
public Skill(String name, long hurt) {
this.name = name;
this.hurt = hurt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getHurt() {
return hurt;
}
public void setHurt(long hurt) {
this.hurt = hurt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Skill{" +
"name='" + name + '\'' +
", hurt=" + hurt +
", desc='" + desc + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Skill skill = (Skill) o;
return hurt == skill.hurt && Objects.equals(name, skill.name) && Objects.equals(desc, skill.desc) ;
}
@Override
public int hashCode() {
return Objects.hash(name, hurt, desc);
}
}
游戏规则类
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Game {
/**
* 我方
*/
private Role my;
/**
* 敌方
*/
private Role you;
/**
* 回合数
*/
private int count ;
public Game(Role my, Role you) {
this.my = my ;
this.you = you ;
}
public void start() throws Exception{
// 猜硬币绝对谁先发动攻击
System.out.println("由" + my.getName() + "进行猜硬币、正面请输入1、反面请输入2");
Scanner scanner = new Scanner(System.in) ;
int num = scanner.nextInt();
Role r = my ; // 攻击对象
Role s = you ; // 被攻击对象
// 由 you 率先发动攻击
if (num !=1 && num !=2) {
System.out.println("由于没有按照规则输入内容,由" + you.getName() + "率先发动攻击");
r = you ;
s = my ;
}
double x = Math.random() ;
// 随机生成 0 - 1, 小于 0.5代表正面
if (x < 0.5 && num == 1 ) {
System.out.println("硬币抛出、结果为正面、由" + my.getName() + "率先发动攻击");
}else if(x >=0.5 && num == 2) {
System.out.println("硬币抛出、结果为反面、由" + my.getName() + "率先发动攻击");
}else{
System.out.println("硬币抛出、结果为"+ (x <0.5 ? "正": "反") +"面、由" + you.getName() + "率先发动攻击");
r = you ;
s = my ;
}
System.out.println(); // 内容换行
while(r.getBlood() > 0 && s.getBlood() > 0) {
System.out.println("=====================第" + ++count +"回合=======================");
r.attack(s);
TimeUnit.SECONDS.sleep(1);
// 交换2个对象
Role temp = r ;
r = s ;
s = temp ;
}
// r 死掉了
System.out.println("♥♥♥♥♥" + s.getName() + "赢了本次战斗");
}
}
测试类
public class GameTest {
public static void main(String[] args) throws Exception {
// String name, long blood, Skill[] skills, Skill[] passiveSkillS
Skill[] skills = new Skill[]{
new Skill("以身为种", 3000),
new Skill("他化自在大法", 1000),
new Skill("雷道四绝天", 5000),
new Skill("草灭剑诀", 3000),
new Skill("荒无剑诀", 7000),
new Skill("上苍之手", 1000),
new Skill("轮回", 2000),
};
Skill[] passiveSkillS = new Skill[]{
new Skill("原始真解", 1000),
new Skill("鲲鹏宝术", 888),
new Skill("真龙宝术", 888),
new Skill("玄龟宝术", 200),
new Skill("麒麟宝术", 666)
};
// 创建一个角色
Role my = new Role("石昊", 50000, skills, passiveSkillS);
Role you = new Role("石毅", 50000, skills, passiveSkillS);
// 创建游戏对象
Game game = new Game(my, you);
game.start();
}
}
结果展示