java实现模拟RPG格斗
三个英雄角色参与pk
每个英雄具有以下几个属性:生命值(为0时英雄倒下)、攻击力(每次攻击时扣除对方的生命值点数)、攻击间隔(每次攻击过后都要等待间隔时间才能进行下次攻击,首次攻击之前也要先等待间隔时间)
另外,每个英雄都拥有两个技能:攻击技能和防御技能,攻击技能在攻击对方时有一定概率发动,防御技能在被对方攻击时有一定概率发动,具体参数如下
bm:
生命650 攻击力40 攻击间隔1.5s
攻击技能(跳劈):每次攻击时有30%几率造成双倍伤害
防御技能(反弹):每次被攻击时有30%几率把我方受到的伤害反弹给对方,例如我方被攻击,对方攻击力30,扣除我方30点生命值,如果技能发动,则对方也要扣除30点生命值,伤害只能反弹一次(两个bm相互pk的时候不出现连续反弹)
dh:生命600 攻击力30 攻击间隔1s
攻击技能(吸血):每次攻击时有30%几率把造成的伤害转变为自己的生命值(对被攻击者造成伤害,并且将攻击伤害转变成自己的生命值),但不能超过上限,例如我方攻击,扣除对方30的生命值,同时给自己增加30点生命值
防御技能(闪避):每次被攻击时有30%几率闪避不受伤害
mk:
生命700 攻击力50 攻击间隔2.5s
攻击技能(重击):每次攻击时有30%几率造成对方眩晕3s的效果(对方受到伤害后附加眩晕),对方英雄眩晕期间不能发起攻击,只能挨打,被攻击时也不能发起防御技能,且眩晕结束后对方英雄要重新等待攻击间隔,眩晕时间不能叠加,如果对方已经处于眩晕,我方又发动攻击技能,那么对方眩晕时间重新开始计算
防御技能(天神):每次被攻击时有60%的几率防御一半伤害,例如我方被攻击,对方攻击力为40,如果技能发动,则只扣除我方20点生命值
1.程序启动后,监听控制台输入
2.输入任意两个英雄名称(逗号分隔)发起pk,格式:bm,dh
3.系统输出pk详细过程,直到有一方胜出,格式如下:
bm攻击dh,bm发动攻击技能,dh未发动防御技能,bm:350->350,dh:280->200
....
bm胜出
package com.lxi; import java.io.bufferedreader; import java.io.inputstreamreader; import java.util.random; //三个人物的基类 abstract class person { int val; //生命值 double coldtime; //冷却时间 int waittime; //晕眩时间 int fight; //攻击力 int chancehit; //发起主动技能的概率 int chancedefense; //发起防御技能的概率 abstract void hit(person p); //攻击技能 abstract int defense(person p); //防御技能,返回被伤害点数 } class dh extends person { public dh() { val = 600; coldtime = 1.0; fight = 30; chancehit = 3; //表示30%的概率 chancedefense = 3; waittime = 0; } random rand = new random(); boolean hitflag = false; //主动技能发动的标识 boolean defenseflag = false; //防御技能发动的标识 public void hit(person p) { if (rand.nextint(10) < chancehit) { //发动主动技能 int hurt = p.defense(this); p.val = p.val - hurt; if (p.val <= 0) { system.out.println(this.getclass().getsimplename() + "胜出!"); system.exit(0); } val = val + hurt; if (val > 600) val = 600; hitflag = true; //标记主动技能已经发动 } else { //进行普通攻击 int hurt = p.defense(this); p.val = p.val - hurt; if (p.val <= 0) { system.out.println(this.getclass().getsimplename() + "胜出!"); system.exit(0); } } system.out.println(this.getclass().getsimplename() + "攻击" + p.getclass().getsimplename() + "," + this.getclass().getsimplename() + (this.hitflag ? "发动攻击技能 " : "未发动攻击技能 ") + p.getclass().getsimplename() + (this.defenseflag ? "发动防御技能 " : "未发动防御技能 ") + this.getclass().getsimplename() + ":" + this.val + "," + p.getclass().getsimplename() + ":" + p.val); hitflag = false; // defenseflag = false; //重置标记,下次重用 } public int defense(person p) { if (rand.nextint(10) < chancedefense) { defenseflag = true; //标记防御技能已经发动 return 0; } else { return p.fight; } } }
class bm extends person { public bm() { val = 650; coldtime = 1.5; fight = 40; chancehit = 3; chancedefense = 3; waittime = 0; } int count = 0; //防御技能发动的次数 int temp = 40; //攻击力,值同fight boolean hitflag = false; boolean defenseflag = false; random rand = new random(); public void hit(person p) { if (rand.nextint(10) < chancehit) { fight = fight * 2; //发动双倍攻击 hitflag = true; } int hurt = p.defense(this); p.val = p.val - hurt; fight = temp; //还原为单倍攻击 if (p.val <= 0) { system.out.println(this.getclass().getsimplename() + "胜出!"); system.exit(0); } system.out.println(this.getclass().getsimplename() + "攻击" + p.getclass().getsimplename() + "," + this.getclass().getsimplename() + (this.hitflag ? "发动攻击技能 " : "未发动攻击技能 ") + p.getclass().getsimplename() + (this.defenseflag ? "发动防御技能 " : "未发动防御技能 ") + this.getclass().getsimplename() + ":" + this.val + "," + p.getclass().getsimplename() + ":" + p.val); hitflag = false; defenseflag = false; } public int defense(person p) { if (rand.nextint(10) < chancedefense) { if (count != 0) { p.val = p.val - p.fight; count++; defenseflag = true; if (p.val <= 0) { system.out.println(this.getclass().getsimplename() + "胜出!"); system.exit(0); } } } return p.fight; } } class mk extends person { public mk() { val = 700; coldtime = 2.5; fight = 50; chancedefense = 6; chancehit = 3; waittime = 0; } boolean hitflag = false; boolean defenseflag = false; random rand = new random(); public void hit(person p) { if (rand.nextint(10) < chancehit) { p.waittime = 3; //使对方晕眩3s hitflag = true; } int hurt = p.defense(this); p.val = p.val - hurt; if (p.val <= 0) { system.out.println(this.getclass().getsimplename() + "胜出!"); system.exit(0); } system.out.println(this.getclass().getsimplename() + "攻击" + p.getclass().getsimplename() + "," + this.getclass().getsimplename() + (this.hitflag ? "发动攻击技能 " : "未发动攻击技能 ") + p.getclass().getsimplename() + (this.defenseflag ? "发动防御技能 " : "未发动防御技能 ") + this.getclass().getsimplename() + ":" + this.val + "," + p.getclass().getsimplename() + ":" + p.val); hitflag = false; defenseflag = false; } public int defense(person p) { if (rand.nextint(10) < chancedefense) { defenseflag = true; return p.fight / 2; //防御技能发动,伤害减半 } return p.fight; } }
public class rpg { @suppresswarnings("unchecked") public static void main(string[] args) throws exception { system.out.println("在这里输入两个人物进行pk,以英文逗号分隔: [bm,dh,mk]"); bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); class<person> c1; class<person> c2; try { string temp = br.readline(); string[] str = temp.split(","); if (str.length != 2) { throw new exception("输入格式有误,按默认pk"); } c1 = (class<person>) class.forname("com.lxi." + str[0].touppercase()); c2 = (class<person>) class.forname("com.lxi." + str[1].touppercase()); } catch (exception e) { // todo auto-generated catch block c1 = (class<person>) class.forname("com.lxi.bm"); c2 = (class<person>) class.forname("com.lxi.dh"); } try { person p1 = c1.newinstance(); person p2 = c2.newinstance(); long time = system.currenttimemillis(); long nexttime1 = (long) (time + p1.coldtime*1000); // long nexttime2 = (long) (time + p2.coldtime*1000); //发动攻击的时间 system.out.println("---游戏开始---"); while (true) { long currentime = system.currenttimemillis(); if (nexttime1 < currentime) { //时间到则发动攻击 p1.hit(p2); nexttime1 += p1.coldtime*1000 + p1.waittime*1000; //下次攻击时间=冷却时间+被晕眩时间 p1.waittime = 0; //回合结束,重置被晕眩时间为0 } if (nexttime2 < currentime) { p2.hit(p1); nexttime2 += p2.coldtime*1000 + p2.waittime*1000; p2.waittime = 0; } } } catch (classcastexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (instantiationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalaccessexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } }
以上所述就是本文的全部内容了,希望大家能够喜欢。