首页 > 代码库 > 第十八章----面向对象(宠物乱斗之子类篇)

第十八章----面向对象(宠物乱斗之子类篇)

1、关于时间格式的修改

package com.maya.chongwu;


    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class DateFormat {
        public static String convert(Date date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = sdf.format(date);
            return dateStr;
        }
    }

2、生肖狗

package com.maya.chongwu;

public class Gou extends Pet{
    public Gou(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Gou(){
        super();
    }
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        
        System.out.println(this.getName()+"%%%发动技能:天狗吞日");
        
        }
        count++;
        return damage+5*this.getIntelligence()+(int)(Math.random()*20);
        
    }
    
}

3、生肖猴

package com.maya.chongwu;

public class Hou extends Pet{
    public Hou(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Hou(){}
    public static int count=1;
    
    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:大闹天宫");
    
        }
        count++;
        return damage+8*this.getIntelligence()+20+(int)(Math.random()*20);
    }

}

4、生肖虎

package com.maya.chongwu;

public class Hu extends Pet{
    public Hu(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Hu(){}
    public static int count=1;

    public int skill(int damage){
    
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:虎啸山林");
        this.setStrength(this.getStrength()+20);
    
        }
        count++;
        return 0;
    }
    
}

5、生肖鸡

package com.maya.chongwu;

public class Ji extends Pet{
    public Ji(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Ji(){}
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:神佑复生");
        this.setHp(this.getHp()+this.getIntelligence()*15);
        }
        count++;
        return 0;
    }

}

6、生肖龙

package com.maya.chongwu;

public class Long extends Pet{
    public Long(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Long(){}
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:神龙降世");
        
        }
        count++;
        return damage*this.getIntelligence()+(int)(Math.random()*20);
    }

}

7、生肖马

package com.maya.chongwu;

public class Ma extends Pet{
    public Ma(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Ma(){}
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:草泥马");
        
    }
        count++;
        return damage+3*this.getIntelligence()+10+(int)(Math.random()*20);
    }

}

8、生肖牛

package com.maya.chongwu;

public class Niu extends Pet{
    public Niu(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Niu(){}
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:混世魔王");
        this.setCrit(this.getCrit()+0.1);
        }
        count++;
        return 0;
    }

}

9、生肖蛇

package com.maya.chongwu;

public class She extends Pet{
    public She(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public She(){}
    public static int count=1;

    public int skill(int damage){
    
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:死亡缠绕");
        
        }
        count++;
         return damage+40+(int)(Math.random()*20);
    }

}

10、生肖鼠

package com.maya.chongwu;

public class Shu extends Pet{
    public Shu(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);    
    }
    public Shu(){
        
    }
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:偷梁换柱");
    
    }
        count++;
        return damage+this.getIntelligence()*this.getIntelligence()+(int)(Math.random()*20);
    }

}

11、生肖兔

package com.maya.chongwu;

public class Tu extends Pet{
    public Tu(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Tu(){}
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:月宫玉兔");
        this.setHp(this.getHp()+60);
        System.out.println("恢复了60生命值");
        
        }
        count++;
        return 0;
    }

}

12、生肖羊

package com.maya.chongwu;

public class Yang extends Pet{
    public Yang(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Yang(){}
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:咩");
        
    }
        count++;
        return damage+5*this.getIntelligence()+30+(int)(Math.random()*20);
    }

}

13、生肖猪

package com.maya.chongwu;

public class Zhu extends Pet{
    public Zhu(String name, String sex) {
        super(name, sex);
        System.out.println("选择的宠物类型: "+Main.a+Main.b+Main.c);
    }
    public Zhu(){}
    public static int count=1;

    public int skill(int damage){
        
        if(count%2!=0){
        System.out.println(this.getName()+"%%%发动技能:天篷之威");
        
        }
        count++;
        return damage+(this.getIntelligence()-3)*20+(int)(Math.random()*20);
    }

}

14、写了这么多类,主要是为了我们的主角能放牛逼的技能。但是,如果没有强大的对手,怎么会有游戏的乐趣。因此,我又写了一个对手的类,一个无比强大的对手,甚至比我们的主角还要强大的角色,当然,我们也可以换个角度,把我们创建的角色当作反派角色,那样就很爽了,有木有!

package com.maya.chongwu;

public class JingjichangYanyuan {
    
    private int Hp;
    public static String[] npc={"董世豪","王林飞","封鹏","白念钊","杨其帅","郭世森","巩慧鑫","刘超","吕正茂","焦震","尹延磊","班巍","朱士峻","郇丽君"};

    private String name;

    private double skil;//技能发动几率

    private int healthy; //健康
    private int hungry; //饥饿
    private int happy; //心情
    private int intelligence;//智力
    private int strength;//力量
    private int agility;//敏捷
    private double crit;//暴击率
    private double evade;//闪避率
    //构造函数
    
    //随机生成属性
            public void attribute1(){
                double x,y;
                x=Math.random()/2;
                y=Math.random()/2;
                this.setName(npc[(int) Math.floor(Math.random()*14)]);
                this.setAgility((int) (Math.random()*10));
                this.setHp((int) (Math.random()*250));
                this.setCrit(x);
                this.setEvade((double) (Math.random()/3));
                this.setIntelligence((int) (Math.random()*10));
                this.setStrength((int) (Math.random()*20));
                this.setHealthy(100);
                this.setHappy(90);
                this.setHungry(0);
                this.setSkil(y);
            }
            // 打印信息
            public void printInfo1() {
                
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
                System.out.println("强大的对手:"+this.getName());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
                System.out.println("生命值: " + Hp);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
                System.out.println("力量值: " + strength);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
                System.out.println("暴击率: " + crit);
                
            
            }
            //演员的技能                
            public int skill13(int damage){
                return (int) (damage+20+Math.random()*10*this.getIntelligence()+Math.random()*20);
            }
        
            public int skill1(int damage){                                            
                return damage+5*this.getIntelligence()+(int)(Math.random()*30);
                
            }
            public int skill2(int damage){
                return damage+8*this.getIntelligence()+20+(int)(Math.random()*30);
            }
            public int skill3(int damage){
                this.setStrength(this.getStrength()+30);
                return 0;
            }
            public int skill4(int damage){
                this.setHp(this.getHp()+this.getIntelligence()*20);
                return 0;
            }
            public int skill5(int damage){        
                return damage*this.getIntelligence()+(int)(Math.random()*30);
            }
            public int skill6(int damage){
                return damage+3*this.getIntelligence()+10+(int)(Math.random()*30);
            }
            public int skill7(int damage){
                this.setCrit(this.getCrit()+0.2);            
                return 0;
            }
            public int skill8(int damage){
                 return damage+40+(int)(Math.random()*30);
            }
            public int skill9(int damage){
                return damage+this.getIntelligence()*this.getIntelligence()+(int)(Math.random()*30);
            }
            public int skill10(int damage){
                this.setHp(this.getHp()+70);        
                return 0;
            }
            public int skill11(int damage){
                return damage+5*this.getIntelligence()+30+(int)(Math.random()*30);
            }
            public int skill12(int damage){
                return damage+(this.getIntelligence()-3)*20+(int)(Math.random()*30);
            }
    //改变属性
    public int getHp() {
        return Hp;
    }
    public void setHp(int hp) {
        Hp = hp;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getHealthy() {
        return healthy;
    }
    public void setHealthy(int healthy) {
        this.healthy = healthy;
    }
    public int getHungry() {
        return hungry;
    }
    public void setHungry(int hungry) {
        this.hungry = hungry;
    }
    public int getHappy() {
        return happy;
    }
    public void setHappy(int happy) {
        this.happy = happy;
    }
    public int getIntelligence() {
        return intelligence;
    }
    public void setIntelligence(int intelligence) {
        this.intelligence = intelligence;
    }
    public int getStrength() {
        return strength;
    }
    public void setStrength(int strength) {
        this.strength = strength;
    }
    public int getAgility() {
        return agility;
    }
    public void setAgility(int agility) {
        this.agility = agility;
    }
    public double getCrit() {
        return crit;
    }
    public void setCrit(double crit) {
        this.crit = crit;
    }
    public double getEvade() {
        return evade;
    }
    public void setEvade(double evade) {
        this.evade = evade;
    }
    public double getSkil() {
        return skil;
    }
    public void setSkil(double skil) {
        this.skil = skil;
    }
}

 

第十八章----面向对象(宠物乱斗之子类篇)