面向对象练习坦克大兵游戏

游戏玩家(名称,生命值,等级),坦克,大兵类,玩家之间可以相互攻击,大兵拥有武器,用枪弹和反坦克炮弹,造成攻击不同,坦克攻击值固定,请设计玩家互相攻击的过程,实现坦克A大兵,大兵打坦克,大兵用枪打坦克,大兵用反坦克导弹打坦克。

bash 复制代码
大兵被坦克攻击了,从200到0
坦克被反坦克炮弹攻击了,从1000到500
坦克被手枪攻击了,从500到400
java 复制代码
package com;

public class Tank extends Player{

	public Tank() {
			this.setName("坦克");
			this.setBlood(1000);
			this.setAtackvalue(200);
	}
	
}
java 复制代码
package com;

public class Soldier extends Player {

	private Weapon wp;
	
	public Soldier() {
		this.setName("大兵");
		this.setAtackvalue(20);
		this.setBlood(200);
		
	}
	
	public void setWeapon(Weapon wp ) {
		
		this.wp=wp;
	}
	
	public void fire(Player target) {
	
		if(wp==null) {
			
			int blood0,blood;
			blood0=target.getBlood();
			blood=blood0-this.getAtackvalue();
			target.setBlood(blood);
			System.out.println(target.getName()+"被"+this.getName()+"攻击了,从"+blood0+"到"+target.getBlood());
			
			
		}
		else {			
			wp.fire(target);
		}
	}
	
	
}
java 复制代码
package com;

public class Player {

	private String name; // 名称
	private int blood ; // 生命值
	private int level; // 等级默认为1,所有输出值为等级x数值
	private int atackvalue;  //攻击值
	
	public int getAtackvalue() {
		return atackvalue;
	}
	public void setAtackvalue(int atackvalue) {
		this.atackvalue = atackvalue;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getBlood() {
		return blood;
	}
	public void setBlood(int blood) {
		this.blood = blood;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	
	public void fire(Player target) {
		int blood,blood0;
		blood0=target.getBlood();
		blood =blood0 - this.getAtackvalue();
		target.setBlood(blood);
        System.out.println(target.getName()+"被"+this.getName()+"攻击了,从"+blood0+"到"+target.getBlood());
	}
	
	
	

}
java 复制代码
package com;

public  class Weapon {
	private String name ; //武器名称
	private  int atackvalue;  //攻击值
		
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAtackvalue() {
		return atackvalue;
	}

	public void setAtackvalue(int atackvalue) {
		this.atackvalue = atackvalue;
	}

	public void fire(Player target) {
		int blood0,blood;
		blood0=target.getBlood();
		blood =blood0  - this.getAtackvalue();
		target.setBlood(blood);

		System.out.println(target.getName()+"被"+this.getName()+"攻击了,从"+blood0+"到"+target.getBlood());
	}



	
}
java 复制代码
package com;

public class Bullet extends Weapon {

	public Bullet() {

		this.setAtackvalue(100);
		this.setName("手枪");
	}




}
java 复制代码
package com;

public class Shell extends Weapon {

	public Shell() {

		this.setAtackvalue(500);
		this.setName("反坦克炮弹");
	}


	
	
	

}
java 复制代码
package com;

public class Test {

	public static void main(String[] args) {
		 	
      Tank tk=new Tank();
      Soldier s=new Soldier();
      tk.fire(s);
      
      Soldier s2=new Soldier();
      Shell shell=new Shell();
      s2.setWeapon(shell);
      s2.fire(tk);
      Bullet bl=new Bullet();
      s2.setWeapon(bl);
      s2.fire(tk);

		
		
	}

}
相关推荐
都叫我大帅哥27 分钟前
从Python到Java:为什么企业级Agent最终会选择Java?
java·ai编程
wanderist.40 分钟前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
海天鹰2 小时前
PHP上传文件
android·开发语言·php
腻害兔2 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:IM 即时通讯模块,一个被低估的「全功能聊天系统」
java·前端·vue.js·产品经理·ai编程
心念枕惊2 小时前
.NET CORE 授权进阶-角色、策略与动态权限实现
java·前端·.netcore
Yeauty3 小时前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频
geovindu4 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
景同学4 小时前
把 AI 用到线上运维:可行、有效,前提是喂足信息——一次 Full GC 排障实录
java·人工智能·后端
库克克4 小时前
【C++】set 与multiset
开发语言·c++
Wang's Blog4 小时前
Go-Zero项目开发34: 微服务超时控制与重试机制实践
开发语言·微服务·golang·go-zero