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

这是一个简单的游戏程序示例,展示了英雄与怪物之间的战斗过程。通过定义Hero和Monster两个类,设置了生命值、攻击力等属性,并实现了攻击和受伤的方法。在主程序中创建了一个名为孙悟空的英雄和一个小僵尸的怪物进行对决,直到一方生命值归零。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英雄打怪物,源码如下
package com.game.huntervsmonster01;

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 = "孙悟空";
			break;
		case 2:
			this.name = "景天";
			break;
		case 3:
			this.name = "李逍遥";
			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.attack);
		show();
	}

	void injured(int mattack)
	{
		if (mattack >= this.defend)
			this.life = this.life - (mattack - this.defend);
		if (this.life <= 0)
		{
			dead();
			return;
		}
	}

	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.huntervsmonster01;

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

	public Monster(int i)
	{
		switch (i)
		{
		case 1:
			this.type = "小僵尸";
			break;
		case 2:
			this.type = "大僵尸";
			break;
		case 3:
			this.type = "僵尸老大";
			break;
		case 4:
			this.type = "小鬼";
			break;
		case 5:
			this.type = "老鬼";
			break;
		case 6:
			this.type = "火鬼王";
			break;
		case 7:
			this.type = "黑无常";
			break;
		case 8:
			this.type = "白无常";
			break;
		case 9:
			this.type = "阎王";
			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.attack);
		show();
	}

	public void injured(int hattack)
	{
		if (hattack >= this.defend)
			this.life = this.life - (hattack - this.defend);
		if (this.life <= 0)
		{
			dead();
			return;
		}
	}

	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.huntervsmonster01;

public class TestGme
{
	int i;

	public static void main(String[] args)
	{
		Hunter hunter = new Hunter(1, 100, true, "金箍棒", 30, 12);
		Monster monster = new Monster(1, 200, true, 13, 1);
		System.out.println(hunter.name + "VS" + monster.type);
		while (hunter.isLive && monster.isLive)
		{
			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、付费专栏及课程。

余额充值