Java小小RPG游戏第三版(基于第二版优化)

package com.game.huntervsmonster02;

public class Hunter
{
	String name;
	int life;
	boolean isLive;
	String weapon;
	int attack;
	int defend;

	public Hunter(int i)
	{
		switch (i)
		{
		case 1:
		{
			this.name = "李逍遥";
			this.attack = 100;
			this.defend = 40;
			this.isLive = true;
			this.life = 200;
			this.weapon = "无尘剑";
		}
			break;
		case 2:
		{
			this.name = "景天";
			this.attack = 500;
			this.defend = 200;
			this.isLive = true;
			this.life = 1000;
			this.weapon = "魔剑";
		}
			break;
		case 3:
		{
			this.name = "孙悟空";
			this.attack = 1000;
			this.defend = 400;
			this.isLive = true;
			this.life = 3000;
			this.weapon = "金箍棒";
		}
			break;
		}
	}

	/*
	 * public Hunter(int i, int life, boolean isLive, String weapon, int attack,
	 * int defend) { this(i); this.life = life; this.isLive = isLive;
	 * this.weapon = weapon; this.attack = attack; this.defend = defend; }
	 */
	void fight(Monster monster)
	{
		if (!isLive)
		{
			return;
		}
		monster.injured(this);
		show();
	}

	void injured(Monster monster)
	{
		if (monster.attack >= this.defend)
			this.life = this.life - (monster.attack - this.defend);
		if (this.life <= 0)
		{
			this.dead();
			return;
		}
		this.show();
		this.fight(monster);
	}

	void dead()
	{
		this.isLive = false;
	}

	void show()
	{
		if (this.isLive)
			System.out.println(this.name + "还有" + this.life + "点血");
		else
			System.out.println(this.name + "壮烈牺牲了!");
	}
}

package com.game.huntervsmonster02;

public class Monster
{
	String type;
	int life;
	boolean isLive;
	int attack;
	int defend;

	public Monster(int i)
	{
		switch (i)
		{
		case 1:
		{
			this.type = "小僵尸";
			this.attack = 80;
			this.defend = 20;
			this.isLive = true;
			this.life = 200;
		}
			break;
		case 2:
		{
			this.type = "大僵尸";
			this.attack = 130;
			this.defend = 50;
			this.isLive = true;
			this.life = 500;
		}
			break;
		case 3:
		{
			this.type = "僵尸老大";
			this.attack = 200;
			this.defend = 90;
			this.isLive = true;
			this.life = 1000;
		}
			break;
		case 4:
		{
			this.type = "小鬼";
			this.attack = 210;
			this.defend = 100;
			this.isLive = true;
			this.life = 1200;
		}
			break;
		case 5:
		{
			this.type = "老鬼";
			this.attack = 350;
			this.defend = 150;
			this.isLive = true;
			this.life = 2000;
		}
			break;
		case 6:
		{
			this.type = "火鬼王";
			this.attack = 500;
			this.defend = 200;
			this.isLive = true;
			this.life = 5000;
		}
			break;
		case 7:
		{
			this.type = "黑无常";
			this.attack = 600;
			this.defend = 250;
			this.isLive = true;
			this.life = 8000;
		}
			break;
		case 8:
		{
			this.type = "白无常";
			this.attack = 600;
			this.defend = 250;
			this.isLive = true;
			this.life = 8000;
		}
			break;
		case 9:
		{
			this.type = "阎王";
			this.attack = 900;
			this.defend = 300;
			this.isLive = true;
			this.life = 10000;
		}
			break;
		}
	}

	/*
	 * public Monster(int i, int life, boolean isLive, int attack, int defend) {
	 * this(i); this.life = life; this.isLive = isLive; this.attack = attack;
	 * this.defend = defend; }
	 */
	public void fight(Hunter hunter)
	{
		if (!isLive)
		{
			return;
		}
		hunter.injured(this);
		show();
	}

	public void injured(Hunter hunter)
	{
		if (hunter.attack >= this.defend)
			this.life = this.life - (hunter.attack - this.defend);
		if (this.life <= 0)
		{
			this.dead();
			return;
		}
		this.show();
		this.fight(hunter);
	}

	void dead()
	{
		this.isLive = false;
	}

	void show()
	{
		if (this.isLive)
			System.out.println(this.type + "还有" + this.life + "点血");
		else
			System.out.println(this.type + "被杀死了!");
	}
}

package com.game.huntervsmonster02;

public class TestGme
{
	int i;

	public static void main(String[] args)
	{
		Hunter hunter = new Hunter(3);
		Monster monster = new Monster(9);
		System.out.println(hunter.name + "VS" + monster.type);
		hunter.fight(monster);
		/*
		 * if(hunter.isLive || monster.isLive) break;
		 */
		// monster.fight(hunter);
		/*
		 * if(hunter.isLive || monster.isLive) break;
		 */
		System.out.println("战斗结束");
		monster.show();
		hunter.show();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值