JAVA项目 ----宠物小精灵12.1

pokemon 下面的 Pokemon

----------------------------------------------------------------------------------------------------------------------------

public abstract class Pokemon extends Item {
    /**
     * 攻击力
     */
    protected int attack;
    /**
     * 防御力
     */
    protected int defense;
    /**
     * 生命值
     */
    protected int health;

    /**
     * 星级
     */
    private int star = 1;

    //小精灵要自带装备
    private Equipment[] equipments = new Equipment[8];

    public void merge(Pokemon other){

        this.attack +=(other.attack >> 1);   //  >>除以2    <<乘以2
        this.defense += (other.defense >> 1);
        this.health += (other.health >> 1);
        star += 1;
        System.out.println("融合成功");
        System.out.println(getItemInformation());
    }

    @Override
    public String getItemInformation() {
        return name + ":攻击=" + attack + " 防御=" + defense + " 生命值=" + health;
    }

    public Equipment changeEquipment(Equipment newEquipment){
        //  确定装备的位置
        int index;
        if(newEquipment instanceof Helmet){//头盔
            index = 0;
        } else if(newEquipment instanceof Armor){//铠甲
            index = 1;
        } else if(newEquipment instanceof Leggings){//护腿
            index = 2;
        } else if(newEquipment instanceof Shoe){//鞋子
            index = 3;
        } else if(newEquipment instanceof Weapon){//武器
            index = 4;
        } else if(newEquipment instanceof Necklace){//项链
            index = 5;
        } else if(newEquipment instanceof Ring){//戒指
            index = 6;
        } else {//手镯
            index = 7;
        }
        //旧装备 ---》 即即将要穿的装备
        Equipment old = equipments[index];
          if(old == null) {
              equipments[index] = newEquipment;
          }else {   //和原来的装行比较。如果比原来的装备好的话就替换 否则就放到背包中来
              
          }
          return old;
    }


    //宠物小精灵和关卡没关系
     public Pokemon(String name) {
        super(name);
    }
}

定义Pokemon这一个abstract类 , 因为后面的小精灵类得继承这个Pokemon抽象类(其实说到底Pokemon也是继承的Item类,同样有构造方法)这里注意,融合的具体方法(融合之后属性提升百分之五十),还有小精灵自带装备。

>>除以2    <<乘以2  左移x2 右移/2

融合之后打印融合之后的信息---> 

System.out.println(getItemInformation());

特别注意changeEquipment方法是怎么用的

1.这个方法的数据类型是自己定义的Equipment类型 又叫引用数据类型类似于C语言的指针

2.instanceof关键字的用法使用于自定义数据类型

3.确定装备的位置,然后再确定具体换的方法

4.JAVA中数组的定义 数据类型 [ ] 数组名 = new 数据类型[数组的长度];

Equipment[] equipments = new Equipment[8];

数组的用法(old数组怎么出来的)

Equipment old = equipments[index];

 

------------------------------------------------------------------------------------------------------------

package com.cyx.pokemon.item.pokemon;

/**
 * 妙蛙种子
 */
public class Bulbasaur extends Pokemon{

    public Bulbasaur() {
        super("妙蛙种子");
        this.attack = 60;
        this.defense = 40;
        this.health = 600;
    }
}

package com.cyx.pokemon.item.pokemon;

/**
 * 小火龙
 */
public class Charmander extends Pokemon{

    public Charmander() {
        super("小火龙");
        this.attack = 100;
        this.defense = 80;
        this.health = 1000;
    }
}

package com.cyx.pokemon.item.pokemon;

/**
 * 比卡丘
 */
public class Bikachu extends Pokemon{

    public Bikachu() {
        super("比卡丘");
        this.attack = 150;
        this.defense = 100;
        this.health = 2000;
    }
}

package com.cyx.pokemon.item.pokemon;

/**
 * 雷精灵
 */
public class Jolteon extends Pokemon{

    public Jolteon() {
        super("雷精灵");
        this.attack = 80;
        this.defense = 60;
        this.health = 800;
    }
}

精灵属性

------------------------------------------------------------------------------------------------------------------

补充一下新老装备的比较办法 在Equipment抽象类中

 在Pokemon抽象类中,既然是多个装备,是数组表示的就应该想到是

--------------------------------------------------------------------------------------------------------------------------

Equipment抽象类中提供get方法,方便返回装备的属性 后面总的攻击是返回宠物的攻击+武器的攻击

  public int getAttack() {
        return attack;
    }

    public int getDefense() {
        return defense;
    }

    public int getHealth() {
        return health;
    }

 --------------------------------------------------------------------------------------------------------------------------

重新书写Pokemon抽象类get方法,增强for循环普通for循环

   public int getAttack() {
        int totalAttack = attack;

//        for(int i=0;i<equipments.length;i++){
//            Equipment equipment = equipments[i];
//        }

        //增强for循环
        for(Equipment equipment : equipments) {
            totalAttack += equipment.getAttack();
        }
        return totalAttack;
    }

    public int getDefense() {
        int totalDefense = defense;
        for(Equipment equipment: equipments){
            if(equipment != null)
                totalDefense += equipment.getDefense();
        }
        return totalDefense;
    }

    public int getHealth() {
        int totalHealth = health;
        for(Equipment equipment: equipments){
            if(equipment != null)
                totalHealth += equipment.getHealth();
        }
        return totalHealth;
    }   public int getAttack() {
        int totalAttack = attack;

//        for(int i=0;i<equipments.length;i++){
//            Equipment equipment = equipments[i];
//        }

        //增强for循环
        for(Equipment equipment : equipments) {
            totalAttack += equipment.getAttack();
        }
        return totalAttack;
    }

    public int getDefense() {
        int totalDefense = defense;
        for(Equipment equipment: equipments){
            if(equipment != null)
                totalDefense += equipment.getDefense();
        }
        return totalDefense;
    }

    public int getHealth() {
        int totalHealth = health;
        for(Equipment equipment: equipments){
            if(equipment != null)
                totalHealth += equipment.getHealth();
        }
        return totalHealth;
    }   public int getAttack() {
        int totalAttack = attack;

//        for(int i=0;i<equipments.length;i++){
//            Equipment equipment = equipments[i];
//        }

        //增强for循环
        for(Equipment equipment : equipments) {
            totalAttack += equipment.getAttack();
        }
        return totalAttack;
    }

    public int getDefense() {
        int totalDefense = defense;
        for(Equipment equipment: equipments){
            if(equipment != null)
                totalDefense += equipment.getDefense();
        }
        return totalDefense;
    }

    public int getHealth() {
        int totalHealth = health;
        for(Equipment equipment: equipments){
            if(equipment != null)
                totalHealth += equipment.getHealth();
        }
        return totalHealth;
    }

这里的for循环也是使用了 创建数组的方法

 ---------------------------------------------------------------------------------------------------------------------------

  public Item open(){     //引用数据类型 这个open方法操作是基于Item类
      //  Random r = new Random();
        //[0,10);
        //int number = r.nextInt(10);
        int number = Tools.getRandomNumber(10); //引入包import com.cyx.pokemon.util.Tools; 才能Tools 类

        if(number == 0){        //比例 初级:中级:高级:究级 = 80:15:4:1
            int rate = Tools.getRandomNumber(100);
            if(rate == 0){
                return new Bikachu();   //Bikachu() 的 构造方法
            }else if(rate <= 4){//高级宠物小精灵 => 小火龙
                return new Charmander();
            } else if(rate <= 20){//中级宠物小精灵 => 雷精灵
                return new Jolteon();
            } else {//初级宠物小精灵
                return new Bulbasaur();
            }
            //宠物小精灵
        } else if (number <= 3){
            //获得装备  注意 随着关卡的升级,装备也会跟着升级 所以呢在new装备的时候要传入levelNumber
            // 比例  武器:项链:戒指:手镯:头盔:铠甲:护腿:鞋子 = 3:5:8:8:19:19:19:19
            int rate = Tools.getRandomNumber(100);
            if(rate < 3){//武器
                return new Weapon(levelNumber);
            } else if(rate < 8){//项链
                return new Necklace(levelNumber);
            } else if(rate < 16){//戒指
                return new Ring(levelNumber);
            } else if(rate < 24){//手镯
                return new Bracelet(levelNumber);
            } else if(rate < 43){//头盔
                return new Helmet(levelNumber);
            } else if(rate < 62){//铠甲
                return new Armor(levelNumber);
            } else if(rate < 81){//护腿
                return new Leggings(levelNumber);
            } else {//鞋子
                return new Shoe(levelNumber);
            }
        } else {
            //获得药品
            return new HP(levelNumber, 10);
        }
    }

1.首先是比例问题,这个提前要设计好,每个宝箱里面东西的比例

2.例:返回的 Bikachu( );Bikachu类里面的构造方法,说到底返回的还是一个方法,但是要注意的是返回值方法是根据你的”方法“的数据类型决定的

我需要返回什么东西再决定我的数据类型是什么

比如我的Equipment方法的返回值最终返回的也是”old“

3.返回的同时也要注意返回的时候返回值的构造方法里面的参数列表

if(rate == 0){
    return new Bikachu();   //Bikachu() 的 构造方法
if(rate < 3){//武器
    return new Weapon(levelNumber);
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值