package baoKeMeng; import java.util.Random; import java.util.Scanner; public class Role { //这里是角色类 //攻击手段为调用者攻击参数 //角色有的属性为血量,姓名 Scanner sc = new Scanner(System.in); private String id; private int Hp; public Role(String id, int hp) { this.id = id; Hp = hp; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getHp() { return Hp; } public void setHp(int hp) { Hp = hp; } //定义一个攻击手段 //调用者攻击参数 //r1攻击r2 //r1.attack(r2) public void attack(Role role) { //随机伤害 Random r = new Random(); int damege = r.nextInt(20) + 1; //定义剩余血量 int remainHp = role.Hp - damege; //但是血量不能为负,判断剩余血量如果为负则赋值为0 remainHp = remainHp < 0 ? 0 : remainHp; role.setHp(remainHp); // System.out.println(role.getId()+"剩余的血量为"+remainHp); } //人r1技能 public void skill1(Role role) { //随机伤害 Random r = new Random(); int damege = r.nextInt(30) + 1; //定义剩余血量 int remainHp = role.Hp - damege; //但是血量不能为负,判断剩余血量如果为负则赋值为0 remainHp = remainHp < 0 ? 0 : remainHp; role.setHp(remainHp); // System.out.println(role.getId()+"剩余的血量为"+remainHp); } public void skill2(Role role) { //随机伤害 Random r = new Random(); int damege = r.nextInt(50) + 1; //定义剩余血量 int remainHp = role.Hp - damege; //但是血量不能为负,判断剩余血量如果为负则赋值为0 remainHp = remainHp < 0 ? 0 : remainHp; role.setHp(remainHp); // System.out.println(role.getId()+"剩余的血量为"+remainHp); } //r2的技能 Random r = new Random(); public void skill21(Role role) { //随机伤害 int damege = r.nextInt(30) + 1; //定义剩余血量 int remainHp = role.Hp - damege; //但是血量不能为负,判断剩余血量如果为负则赋值为0 remainHp = remainHp < 0 ? 0 : remainHp; role.setHp(remainHp); System.out.println(this.getId() + "对你使用了水流喷射!"); } public void skill22(Role role) { //随机伤害 Random r = new Random(); int damege = r.nextInt(50) + 1; //定义剩余血量 int remainHp = role.Hp - damege; //但是血量不能为负,判断剩余血量如果为负则赋值为0 remainHp = remainHp < 0 ? 0 : remainHp; role.setHp(remainHp); System.out.println(this.getId() + "对你使用了激流勇进!!"); } public void RanSkill(Role role) { Random r = new Random(); int index = r.nextInt(2) + 1; if (index == 1) { skill21(role); } else if (index == 2) { skill22(role); } else { attack(role); } } //回血机制 //需要数组 //需要hp //需要返回hp public int restore() { int choice = -1; int pitch=5; int blood=5; System.out.println("你要做什么?"); System.out.println("1.桃子"); System.out.println("2.血瓶"); // System.out.println("当前有桃子:"+pitch+"血瓶:"+blood); switch (choice = sc.nextInt()) { case 1: { int remainHp = this.getHp() + 20; System.out.println("hp恢复了!"); remainHp = remainHp > 100 ? 100 : remainHp; this.setHp(remainHp); pitch=pitch-1; break; } case 2: { int remainHp = this.getHp() + 40; System.out.println("hp恢复了!"); remainHp = remainHp > 100 ? 100 : remainHp; this.setHp(remainHp); blood=blood-1; break; } } return this.getHp(); } }
package baoKeMeng; import java.util.Random; import java.util.Scanner; public class Battle { public static void main(String[] args) { //首先定义一个数组,为了遍历信息 Role[] arr = new Role[2]; //添加两个角色,赋值姓名血量 Role r1 = new Role("皮卡丘", 100); Role r2 = new Role("杰尼龟", 100); arr[0] = r1; arr[1] = r2; //技能数组 Role[] skillArr = new Role[3]; //首先是主页面 System.out.println("你要干什么?"); System.out.println("1.战斗"); System.out.println("2.背包"); System.out.println("3.信息"); System.out.println("4.逃跑"); int choice = -1; Scanner sc = new Scanner(System.in); while (r1.getHp() != 0 || r2.getHp() != 0) { switch (choice = sc.nextInt()) { case 1: { clear(); System.out.println("请选择你要释放的技能:"); System.out.println("1.普通攻击"); System.out.println("2.十万伏特"); System.out.println("3.电闪雷鸣"); System.out.println("4.返回"); switch (choice = sc.nextInt()) { case 1: { clear(); System.out.println(r1.getId() + "对" + r2.getId() + "使用了普通攻击!!"); r1.attack(r2); r2.RanSkill(r1); hpJudg(arr); break; } case 2: { clear(); System.out.println(r1.getId() + "对" + r2.getId() + "使用了十万伏特!!"); r1.skill1(r2); r2.RanSkill(r1); hpJudg(arr); break; } case 3: { clear(); System.out.println(r1.getId() + "对" + r2.getId() + "使用了电闪雷鸣!!"); r1.skill1(r2); r2.RanSkill(r1); hpJudg(arr); break; } case 4: { break; } } break; } case 2: { clear(); int pitch=5; int blood=5; r1.restore(); //背包里面可以有回血量的食物 break; } case 3: { //可以查看角色信息 clear(); info(arr); break; } case 4: { clear(); System.out.println(r1.getId() + "灰溜溜的逃走啦!"); break; } } } } //定义一个方法,判断血量是否为0,判断输赢 //需要数组,hp public static void hpJudg(Role[] arr) { for (int i = 0; i < arr.length; i++) { Role r = arr[i]; if (r.getHp() == 0) { System.out.println(r.getId() + "的hp为0!"); } } } //遍历数组查看信息 public static void info(Role[] arr) { for (int i = 0; i < arr.length; i++) { Role r = arr[i]; System.out.println(r.getId() + ",剩余hp为" + r.getHp()); } } //定义一个清屏指令 public static void clear() { System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); } }