猎人打怪兽小游戏

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="\t\t\t\t";
  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("\t\t\t"+hunter.getName()+"VS"+"\t\t"+monster.getType());
 	 System.out.println(sider);
 	 System.out.println("\t攻击力:\t"+hunter.getAttack()+"\t\t\t"+monster.getAttack());
 	 System.out.println("\t防御力:\t"+hunter.getDefence()+"\t\t\t"+monster.getDefence());
      System.out.println("\t闪避率:\t"+GameUtil.getHideRate(hunter.getAgile(), hunter.getMaxAgile())
     		 +"\t\t\t"+GameUtil.getHideRate(monster.getAgile(), monster.getMaxAgile()));	 
      System.out.println("\t当前血量:\t"+hunter.getHp()+"\t\t\t"+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();
   }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值