2020-09-13第一篇(面向对象),用java写了一个小项目,1v1对战文字游戏,可以自定义名字

//斧王类
public class AXE extends Hero {
	Random r=new Random();
	// 构造器重载
	public AXE() {
		
	}

	public AXE(String name, String ini, double hp) {
		super(name, ini, hp);

	}
//方法重写
	@Override
	public int attack() {
		int s=r.nextInt(10)+1;
		return 5+s;

	}

	
}
//骷颅射手类
public class Bone extends Hero {
	Random r=new Random();
	public Bone() {

	}

	public Bone(String name, String ini, double hp) {
		super(name, ini, hp);

	}
	// 方法重写
	@Override
	public int attack() {
		int s=r.nextInt(10)+11;
		return 8+s;
	}


}
/**
 * 
 * @ClassName:  Gametest   
 * @Description:TODO(小君的专属作业)   
 * @author:
 * @date:   2020年9月12日 下午9:41:16      
 * @Copyright: 2020 
 * 注意:
 */
public class Gametest {


	public static void main(String[] args) throws InterruptedException{
		// 创建对象
		Hero axe = new AXE("武汉第一斧王", "我的大斧已经饥渴难耐(造成1-1.9倍随机伤害)", 100);
		Hero bone = new Bone("广州第一射手", "射穿你的屁股(造成1-1.9倍随机伤害)", 80);
		new Menu().run(axe, bone);
		new Game().change(axe, bone);
		
	}
}
//英雄类
public class Hero {
	String name;// 名字
	String ini;// 技能
	double hp;// 血量
	// 构造器
	public Hero() {
	}

	public Hero(String name, String ini, double hp) {
		super();
		this.name = name;
		this.ini = ini;
		this.hp = hp;
	}

	// 方法
	public int attack() {

		return 10;
	}

}
//攻击调用
public class Game {

	public void change(Hero hero1, Hero hero2) throws InterruptedException {
		
		System.out.println("********在线表演斧王大战骷颅射手********");
		System.out.println("登场选手:  " + hero1.name + " \n攻击:" + hero1.attack() + " \n血量:" + hero1.hp + " \n技能:" + hero1.ini
				+ "\n\t" + hero2.name + " \n攻击:" + hero2.attack() + " \n血量:" + hero2.hp + " \n技能:" + hero2.ini);
		Random r1 = new Random();
		while (true) {

			Thread.sleep(200);
			int i = r1.nextInt(2);
			if (i == 1) {
				System.out.println("******\n轮到"+hero1.name+"攻击");
				double x = r1.nextDouble() + 1;
				x = Math.floor(x * 10) / 10;
				double b = hero1.attack() * x;
				double a = (double) (hero2.hp - b);
				if (a < 0) {
					a = 0;
				}
				System.err.println(hero1.name + "发动技能" + hero1.ini +"\n触发幸运一爆" + x + "倍暴击\n对" + hero2.name + "造成" + b
						+ "点伤害" + hero2.name + "剩余血量" + (String.format("%.2f", a)));
				hero2.hp = a;
				if (hero2.hp <= 0) {
					System.out.println(hero1.name+"胜利!游戏结束......");
					break;
				}

			} else if (i == 0) {
				System.out.println("******\n轮到"+hero2.name+"攻击");
				double x = r1.nextDouble() + 1;
				x = Math.floor(x * 10) / 10;
				double b = hero2.attack() * x;
				double a = (double) (hero1.hp - b);
				if (a < 0) {
					a = 0;
				}
				System.err.println(hero2.name + "发动技能" + hero2.ini + "\n触发幸运一爆" + x + "倍暴击\n对" + hero1.name + "造成" + b
						+ "点伤害" + hero1.name + "剩余血量" + (String.format("%.2f", a)));
				hero1.hp = a;
				if (hero1.hp <= 0) {
					System.out.println(hero2.name+"胜利!游戏结束......");
					break;
				}
			}
		}
	}
//注册调用
public class Menu {
	Scanner s=new Scanner(System.in);
	Random  r=new Random();
	public void run(Hero hero1, Hero hero2){
		System.out.println("欢迎来到xx异世界");
		System.out.println("请选择一个职业吧\n1.战士(血量高攻击一般)\t2.射手(血量一般攻击高)");
		int i=s.nextInt();
		if(i==1){
			System.out.println("请输入你的名字:");
			hero1.name=s.next();
			System.out.println("请自定义一个技能名字:");
			hero1.ini=s.next();
			int hp = r.nextInt(50)+151;
			System.out.println(hp);
			hero1.hp=hp;
			int atk=hero1.attack();
			
			System.out.println("生成英雄如下\n名字:"+hero1.name+"\t攻击:"+atk+"\t血量:"+hero1.hp+"\t技能:"+hero1.ini);
		}else if(i==2){
			System.out.println("请输入你的名字:");
			hero2.name=s.next();
			System.out.println("请自定义一个技能名字:");
			hero2.ini=s.next();
			int hp = r.nextInt(100)+51;
			System.out.println(hp);
			hero2.hp=hp;
			
			System.out.println("生成英雄如下\n名字:"+hero2.name+"\t攻击:"+hero2.attack()+"\t血量:"+hero2.hp+"\t技能:"+hero2.ini);
		}else{
			System.out.println("请输入1或2");
			
		}
		
		
		
	}

}

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值