吃鸡游戏:模拟玩家将弹夹装入子弹,再将弹夹装入枪,玩家向敌人开枪,敌人掉血。导包的快捷键(ctrl+shift+o)
面向对象中,某一个类中也会有其他类的对象存在,这些类在交换数据时是以对象进行交换 (以对象进行传参)
import java.util.Arrays;
//吃鸡游戏:模拟玩家将弹夹装入子弹,再将弹夹装入枪,玩家向敌人开枪,敌人掉血。
/*类有:玩家(敌人),弹夹,枪,子弹
- 枪是人的属性,弹夹是枪的属性,子弹是弹夹的属性
Player 玩家
hp 血量
name 人有名字
Gun 人有枪
putBulletIntoClip(Clip clip,Bullet bullet) //把子弹装入弹夹
putClipIntoGun(Clip clip)//把弹夹装在枪上
takeGun(Gun gun)//人拿枪
fire(Player enemy)//人扣扳机,向敌人开火
reduce(int execution) 掉血动作
Gun 枪
Clip clip
takeClip(Clip clip) //枪换弹夹
shoot(Player enemy) //枪射出子弹
Clip 弹夹
Bullet[] bullets //一组子弹
pushBullet(Bullet bullet)//装一个子弹
popBullet()射出子弹
Bullet 子弹
execution 伤害值
injury(Player enemy) 伤害目标人,目标人会掉血*/
public class Demo8_6 {
public static void main(String[] args) {
Player p1=new Player("小明",100);
Player p2=new Player("小花",50);
Player p3=new Player("小H",20);
p1.info();
p2.info();
p3.info();
Clip clip=new Clip(40);
clip.info();
for(int i=1;i<=10;i++){
Bullet bullet=new Bullet();
//p1.putBulletIntoClip(clip, new Bullet());
p1.putBulletIntoClip(clip, bullet);
}
clip.info();
Gun gun=new Gun();
gun.info();
p1.takeGun(gun);
p1.info();
p1.putClipIntoGun(clip);
gun.info();
p1.fire(p2);
p1.fire(p2);
p1.fire(p2);
p1.fire(p2);
p1.fire(p2);
p2.info();
}
}
class Player{
int hp;
Gun gun;
String name;
public Player(String name,int hp){
this.name=name;
this.hp=hp;
gun=null;
}
public void reduce(int execution) {
hp=hp-execution;
}
public void putBulletIntoClip(Clip clip,Bullet bullet){
//人把子弹装入弹夹,人没有弹夹和子弹的属性,需要传入弹夹和子弹,人只充当连接作用
clip.pushBullet(bullet);
}
public void putClipIntoGun(Clip clip){
//把弹夹装在枪上,因为枪是人的属性,不用外界传入
if(gun!=null){
gun.takeClip(clip);
}else{
System.out.println(">>>"+name+"没枪,无法装弹夹");
}
}
public void takeGun(Gun gun){//人拿枪
if(gun==null){
this.gun=gun;
System.out.println(">>>"+name+"持枪");
}else{
this.gun=gun;
System.out.println(">>>"+name+"换枪");
}
}
public void fire(Player enemy){
//人扣扳机,向敌人开火
gun.shoot(enemy);
System.out.println(this.name+"向"+enemy.name+"射击");
}
public void info(){
//打印当前状态,人血量多少,有没有枪
System.out.println("姓名:"+name);
System.out.println("血量:"+hp);
System.out.println("持枪状态:"+(gun==null?"无枪":"有枪"));
}
}
class Gun{
Clip clip;
Gun(){
//clip=null;
this(null);
}
Gun(Clip clip){//自带弹夹
this.clip=clip;
}
public void takeClip(Clip clip){//枪装弹夹
if(clip==null){//如果枪没有弹夹,则将进入的弹夹装枪
this.clip=clip;
System.out.println("枪成功装上弹夹");
}else{//枪上已有弹夹,若再来弹夹,替换
this.clip=clip;
System.out.println("枪成功被换掉弹夹");
}
}
public void shoot(Player enemy){
//先弹出一个子弹
Bullet bullet=clip.popBullet();
if(bullet==null){
System.out.println(">>>子弹已空,放了一个空枪");
}else{
bullet.injury(enemy);//子弹伤人,人掉血
}
}
public void info(){
//打印当前状态,枪有无弹夹
System.out.println("弹夹="+(clip==null?"无弹夹":"有弹夹"));
}
}
class Clip{
Bullet[] bullets;
int size;//弹夹的最大容量
Clip(){
// size=30;
// bullets=new Bullet[0];
this(30);
}
public Clip(int size){
this.size=size;
bullets=new Bullet[0];
}
//弹夹装入一个子弹
public void pushBullet(Bullet bullet){
if(bullets.length<size){//若弹夹容量
bullets=Arrays.copyOf(bullets, bullets.length+1);
bullets[bullets.length-1]=bullet;
}else{
System.out.println(">>>弹夹已满,无法装入子弹");
}
}
//弹出一个子弹,最后返回一个子弹
public Bullet popBullet(){
if(bullets.length==0){
System.out.println(">>>弹夹为空,无法弹出子弹,请装弹");
return null;
}else{
//把数组中的最后一个子弹弹出,即数组缩容
Bullet b=bullets[bullets.length-1];
bullets=Arrays.copyOf(bullets, bullets.length-1);
return b;
}
}
public void info(){
//打印当前状态,子弹容量
System.out.println("子弹容量"+bullets.length+"/"+size);
}
}
class Bullet{
int execution;
Bullet(){
//execution=10;
this(10);
}
public Bullet(int execution){
this.execution=execution;
}
public void injury(Player enemy){
enemy.reduce(execution);
}
}