package game;//猎人类
public class Hunter {private String name;//名字private String weapon;//武器private int maxHp;//最大血量private int hp;//血量private boolean isLive;//是否存活private int attack;//攻击力private int defence;//防御力private int leavel;//等级private int exp;//经验private int agile;//敏捷private int maxAgile;//最大敏捷private int ownMoney;//金币public Hunter() {}public Hunter(String name, int maxHp, int attack, int defence, int agile,int ownMoney,String weapon) {this.name = name;this.maxHp = maxHp;this.hp=this.maxHp;this.attack = attack;this.defence = defence;this.agile = agile;this.maxAgile=70;this.ownMoney = ownMoney;this.weapon=weapon;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getWeeper() {return weapon;}public void setWeeper(String weeper) {this.weapon = weeper;}public int getMaxHp() {return maxHp;}public void setMaxHp(int maxHp) {this.maxHp = maxHp;}public int getHp() {return hp;}public void setHp(int hp) {this.hp = hp;}public boolean getIsLive() {return isLive;}public void setLive(boolean isLive) {this.isLive = isLive;}public int getAttack() {return attack;}public void setAttack(int attack) {this.attack = attack;}public int getDefence() {return defence;}public void setDefence(int defence) {this.defence = defence;}public int getLeavel() {return leavel;}public void setLeavel(int leavel) {this.leavel = leavel;}public int getExp() {return exp;}public void setExp(int exp) {this.exp = exp;}public int getAgile() {return agile;}public void setAgile(int agile) {this.agile = agile;}public int getMaxAgile() {return maxAgile;}public void setMaxAgile(int maxAgile) {this.maxAgile = maxAgile;}public int getOwnMoney() {return ownMoney;}public void setOwnMoney(int ownMoney) {this.ownMoney = ownMoney;}//猎人开始打猎public void fight(Monster m){int damage=m.injured(this);//伤害System.out.println("t>>>>>>>>"+this.name+"攻击----->"+m.getType()+"一次,造成伤害"+damage+","+m.getType()+",当前血量"+m.getHp());}//猎人受伤public int injured(Monster m){//猎人闪避if(GameUtil.hidden(this.agile, this.maxAgile)){System.out.println("t"+this.name+"灵巧的躲避了"+m.getType()+"的攻击!");System.out.println("*******************************************");return 0;}//获取丢失的生命值int lostHp=GameUtil.getLostLife(m.getAttack(), this.defence);this.hp-=lostHp;return lostHp;}
}
package game;//怪兽类
public class Monster {private String type;//类型private int maxHp;//最大血量private int hp;//血量private boolean isLive;//是否存活private int attack;//攻击力private int defence;//防御力private int agile;//敏捷private int maxAgile;//最大敏捷private int money;//金币public Monster(){}//初始化,并选择怪兽类型public Monster(int mt){isLive=true;//存活maxAgile=70;//最大敏捷if(mt==1){this.maxHp=40;this.hp=this.maxHp;this.type="螃蟹怪";attack=14;defence=8;agile=20;this.money=20;}else if(mt==2){this.maxHp=60;this.hp=this.maxHp;this.type="石头人";attack=16;defence=10;agile=25;this.money=30;}else if(mt==3){this.maxHp=50;this.hp=this.maxHp;this.type="三郎";attack=17;defence=12;agile=40;this.money=35;}else if(mt==4){this.maxHp=80;this.hp=this.maxHp;this.type="红buff";attack=36;defence=15;agile=30;this.money=50;}} public String getType() {return type;}public void setType(String type) {this.type = type;}public int getMaxHp() {return maxHp;}public void setMaxHp(int maxHp) {this.maxHp = maxHp;}public int getHp() {return hp;}public void setHp(int hp) {this.hp = hp;}public boolean getIsLive() {return isLive;}public void setLive(boolean isLive) {this.isLive = isLive;}public int getAttack() {return attack;}public void setAttack(int attack) {this.attack = attack;}public int getDefence() {return defence;}public void setDefence(int defence) {this.defence = defence;}public int getAgile() {return agile;}public void setAgile(int agile) {this.agile = agile;}public int getMaxAgile() {return maxAgile;}public void setMaxAgile(int maxAgile) {this.maxAgile = maxAgile;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}//怪兽反击public void fight(Hunter h){int damage=h.injured(this);//伤害System.out.println("t*********"+this.type+"攻击----->"+h.getName()+"一次,造成伤害"+damage+","+h.getName()+",当前血量"+h.getHp());}//怪兽受伤public int injured(Hunter h){if(GameUtil.hidden(this.agile, this.maxAgile)){System.out.println("t"+this.type+"灵巧的躲避了"+h.getName()+"的攻击!");System.out.println("-------------------------------------------");return 0;}//获取丢失的生命值int lostHp=GameUtil.getLostLife(h.getAttack(), this.defence);this.hp-=lostHp;return lostHp;}
}
package game;import java.util.Scanner;//游戏规则
public class Game {private Hunter hunter;private String space="tttt";private String sider="t**********************************************";Scanner input=new Scanner(System.in);//开始游戏public void start(){this.initial();//初始化对象this.menu();//菜单选择}//初始化public void initial(){System.out.println("游戏正在初始化...");try {Thread.sleep(1000);//休眠} catch (InterruptedException e) {e.printStackTrace();}System.out.println("正在初始化猎人...");hunter=new Hunter("路飞",100,20,12,30,50,"长枪");try {Thread.sleep(1000);//休眠} catch (InterruptedException e) {e.printStackTrace();}System.out.println("正在初始化商店...");try {Thread.sleep(1000);//休眠} catch (InterruptedException e) {e.printStackTrace();}System.out.println("初始化完成...");}//菜单选项public void menu(){System.out.println(sider);System.out.println(space+"1.查看状态 ");System.out.println(space+"2.逛商店");System.out.println(space+"3.装备管理");System.out.println(space+"4.打怪赚钱");System.out.println(space+"5.休息一晚(20金币)");System.out.println(space+"0.退出程序");System.out.println(sider);System.out.print("请选择:");int op=input.nextInt();switch(op){case 1://1.查看状态lookType();break;case 2://2.逛商店goShoping();break;case 3://3.装备管理zbManager();break;case 4://4.打怪赚钱beginFight();break;case 5://5.休息一晚(20金币)relaxTonight();break;case 0://0.退出程序break;}}//1.查看状态public void lookType(){}//2.逛商店public void goShoping(){}//3.装备管理public void zbManager(){}//4.打怪赚钱public void beginFight(){Monster monster=new Monster(GameUtil.random(1, 4));//随机出怪兽角色System.out.println(space+"怪兽出现!");showProperties(monster);//对比猎人与怪兽的能力值String turn="hunter";while(true){//猎人打怪兽if(turn.equals("hunter")){try {Thread.sleep(1000);//休眠} catch (InterruptedException e) {e.printStackTrace();}hunter.fight(monster);turn="monster";}else if(turn.equals("monster")){//怪兽打猎人try {Thread.sleep(1000);//休眠} catch (InterruptedException e) {e.printStackTrace();}monster.fight(hunter);turn="hunter";}if(hunter.getHp()<=0){//猎人死亡System.out.println("猎人死亡,请再来一次吧!");try {Thread.sleep(1000);//休眠} catch (InterruptedException e) {e.printStackTrace();}hunter.setHp(1);//复活System.out.println("恭喜你,复活了!");break;}else if(monster.getHp()<=0){//怪兽死亡System.out.print("怪兽死亡...猎人胜利...你获得了"+monster.getMoney()+"金钱:");hunter.setOwnMoney(hunter.getOwnMoney()+monster.getMoney());//设置猎人的金钱break;}}System.out.println("请按任意键继续");String n=input.next();menu();//调用菜单}//展示猎人和怪物之间的差别public void showProperties(Monster monster){//展示双方属性对比System.out.println("ttt"+hunter.getName()+"VS"+"tt"+monster.getType());System.out.println(sider);System.out.println("t攻击力:t"+hunter.getAttack()+"ttt"+monster.getAttack());System.out.println("t防御力:t"+hunter.getDefence()+"ttt"+monster.getDefence());System.out.println("t闪避率:t"+GameUtil.getHideRate(hunter.getAgile(), hunter.getMaxAgile())+"ttt"+GameUtil.getHideRate(monster.getAgile(), monster.getMaxAgile())); System.out.println("t当前血量:t"+hunter.getHp()+"ttt"+monster.getHp());System.out.println(sider);}//5.休息一晚(20金币)public void relaxTonight(){}
}
package game;//猎人打怪兽工具类
public class GameUtil {//生成随机数public static int random(int min,int max){int choice=max-min+1;int r=(int)(Math.random()*choice+min);return r;}//是否躲闪public static boolean hidden(int agile,int maxAgile){int rate=getHideRate(agile, maxAgile);int r=random(1,100);if(rate>=r){return true;}return false;}//丢失的生命值public static int getLostLife(int attack,int defence){int lostLife=attack-defence;//丢失的生命值=攻击力-防御力if(lostLife<=0){lostLife=1;}return lostLife;}//闪避率public static int getHideRate(int agile,int maxAgile){return agile*maxAgile/100;}
}
package game;//测试类
public class Test {public static void main(String[] args){Game g=new Game();g.start();}
}