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

游戏玩家(名称,生命值,等级),坦克,大兵类,玩家之间可以相互攻击,大兵拥有武器,用枪弹和反坦克炮弹,造成攻击不同,坦克攻击值固定,请设计玩家互相攻击的过程,实现坦克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);

		
		
	}

}
相关推荐
2501_92164949几秒前
从WebSocket到SQL查询:金融数据落库存储及查询接口全流程开发
java·sql·websocket·程序人生·spring cloud·金融·系统架构
专注API从业者3 分钟前
淘宝 API 调用链路追踪实战:基于 SkyWalking/Pinpoint 的全链路监控搭建
大数据·开发语言·数据库·skywalking
jinanwuhuaguo3 分钟前
OpenClaw v2026.4.1 深度剖析报告:任务系统、协作生态与安全范式的全面跃迁
java·大数据·开发语言·人工智能·深度学习
努力不熬夜 ^_^4 分钟前
我用 GLM-5.1 重构了我的 AI 项目
java·重构·react·glm·vibe coding·coding plan
小雷君8 分钟前
SpringBoot + SpringSecurity + JWT 完整整合实战(生产级无状态认证)
java·spring boot·spring
澄风9 分钟前
IDEA 代码模板配置教程(prs快捷生成private String)
java·ide·intellij-idea
弹简特9 分钟前
【JavaEE25-后端部分】从“统一回执单”到“统一投诉处理”:Spring Boot 轻松搞定统一返回格式和统一异常处理
java·spring boot·后端·统一返回格式·统一异常
小邓的技术笔记11 分钟前
Python 入门:从“其他语言”到 Pythonic 思维的完整迁移手册
开发语言·python
leo_messi9415 分钟前
2026版商城项目(二)-- 压力测试&缓存
java·缓存·压力测试·springcloud
ok_hahaha15 分钟前
java从头开始-黑马点评-附近商户
java