喫雞遊戲:模擬玩家將彈夾裝入子彈,再將彈夾裝入槍,玩家向敵人開槍,敵人掉血。導包的快捷鍵(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);
}
}