Java打怪小游戏

Java文字小游戏

怪物分为四个类型:
超级丧尸/变异丧尸/普通丧尸/吸血鬼.
不同类别的怪物具有不同的攻击力,防御力,躲避以及特殊属性等等.
人类杀死僵尸后会获得经验升级,升级会回复血量,提高攻击力.
下面是运行截图
Java打怪小游戏_第1张图片

Java打怪小游戏_第2张图片
下面是代码

game.java

public class game {
    public static void main(String[] args){
        new GameStart().start();
    }
}

GameStart.java

package one;

public class GameStart {
    Hunter hunter;
    Monster m1,m2,m3,m4,m5;

    public GameStart(){
        hunter = new Hunter("小何佩","大冰棒");
        m1 = new Monster(3);
        m2 = new Monster(3);
        m3 = new Monster(3);
        m4 = new Monster(2);
        m5 = new Monster(4);
    }
    public void start(){

        while(hunter.isLive && (m1.isLive || m2.isLive || m3.isLive || m4.isLive || m5.isLive)){
            System.out.println("------------------对手寻找中---------------------"); 
            /**让程序休息3秒钟**/
            try{
                Thread.sleep(3000);         
                }
            catch(Exception e)
            {}  
            int ran = GameUtil.randomaRange(1,6);     // 产生随机数,随机寻找僵尸进行战斗
            switch(ran){
            case 1: hunter.fight(m1);   break;
            case 2: hunter.fight(m2);   break;
            case 3: hunter.fight(m3);   break;
            case 4: hunter.fight(m4);   break;
            case 5: hunter.fight(m5);   break;
            default:System.out.println("拜托啊!你要找个正常一点的战斗");     break;   //rand到不存在的僵尸; 
            }
        }
        end();
    }
    public void end(){
        if(hunter.isLive == true){
            System.out.println("来自李俊标的祝贺,恭喜你!过关啦");
        }else{
            System.out.println("哈哈哈哈哈哈,被僵尸打死了吧");
        }
    }
}

GameUtil.java

package one;

public class GameUtil {
    public static int randomaRange(int start,int end){
        return (int)(Math.random()*(end-start)+start);
    }
    public static boolean hidden(int agile,int hideRate){
        int sucRate = agile*hideRate/100;
        int ran = GameUtil.randomaRange(1,101);
        if(ranreturn true;
        }
        return false;
    }
    static int lostBasicLife = 10;
    public static int calLostLife(int attack,int defend){
        int lostLife = attack-defend;
        int rel = 0;
        if(lostLife<=0){
            rel = lostBasicLife;
        }else{
            rel = (lostLife+lostBasicLife);
        }
        return rel;
    }
}

Hunter.java

package one;

public class Hunter {   //爱丽丝
    String name;
    int maxLife;
    int curLife;
    boolean isLive;
    String weapon;
    int attack;         //攻击力
    int defend;         //防御力
    int level;
    int exp;
    int agile;
    int hideRate;

    public Hunter(String name,String weapon){
        this.name = name;
        this.weapon = weapon;
        maxLife = 100;
        curLife = maxLife;
        isLive = true;
        attack = 25;
        defend = 8;
        level = 1;
        exp = 0;
        agile = 35;
        hideRate = 60;
    }
    public void fight(Monster monster){     //战斗
        if(monster.isLive){
        if(isLive){
        System.out.println("--------->"+name+"无情的拿起"+weapon+"杀向"+monster.type+"<----------------");
        monster.injured(this);
        }else{
            System.out.println("--------->"+"我们的主角"+name+"已经牺牲了"+"<----------------");
        }
        }else
        {
            System.out.println("拜托啊!这个丧尸已经被你打死啦!");
        }
    }

    public void injured(Monster monster){   //掉血
        //增加躲避的判断
        if(monster.type == "吸血鬼"){
            if(GameUtil.hidden(this.agile,this.hideRate)){
                System.out.println("--------"+name+":小样,打不到我");
                show();
                fight(monster);
                return;
            }
            System.out.println("--------->"+name+":疼死了,打死你个龟孙"+"<---------");
            int lostLife = GameUtil.calLostLife(monster.attack, this.defend);
            curLife-=lostLife;
            if(curLife<0){
                curLife=0;
                died();
                return;
            }
            monster.curLife+=this.curLife/10;
            show();
            fight(monster);
        }else{
        if(GameUtil.hidden(this.agile,this.hideRate)){
            System.out.println("--------"+name+":小样,打不到我");
            show();
            fight(monster);
            return;
        }
        System.out.println("--------->"+name+":疼死了,打死你个龟孙"+"<---------");
        int lostLife = GameUtil.calLostLife(monster.attack, this.defend);
        curLife-=lostLife;
        if(curLife<0){
            curLife=0;
            died();
            return;
        }
        show();
        fight(monster);
        }
    }
    public void expAdd(Monster monster){
        this.exp+=monster.maxLife;
        int needExp = 0;
        for(int i=1;i<=level;i++){
            needExp+=i*50;
        }
        if(exp>=needExp){
            upgrade();
        }

    }
    public void upgrade(){
        attack+=4;
        defend+=3;
        maxLife+=20;
        curLife+=maxLife;
        level++;
        System.out.println("--------------------------分割线-------------------------");
        System.out.println("系统提示:升级啦,目前等级"+level+"血量"+curLife+"攻击力"+attack+"防御力"+defend);
    }
    public void died(){
        System.out.println("**********"+name+"被丧尸咬死了"+"**********");
        isLive = false;
        show();
    }
    public void show(){
        System.out.println("---------->"+name+"  "+"生命值:"+curLife+"  "+
    "生命状态"+isLive+"   "+"等级"+level+"   "
    +"<-------------");
    }

}

Monster.java

package one;

public class Monster{
        int curLife;
        int maxLife;
        String type;
        boolean isLive = true;
        int attack;         //攻击力
        int defend;         //防御力
        int agile;          //敏捷
        int hideRate;       //躲避率
        public Monster(int mt){
            switch(mt){
            case 1: type = "超级丧尸";maxLife = 80; curLife = 80;   attack = 25;defend = 15;    agile = 30;hideRate = 80;   break;
            case 2: type = "变异丧尸";maxLife = 80; curLife = 60;   attack = 28;defend = 10;    agile = 40;hideRate = 70;   break;
            case 3: type = "普通丧尸";maxLife = 80; curLife = 40;   attack = 10;defend = 5;     agile = 30;hideRate = 60;   break;
            case 4: type = "吸血鬼"; maxLife = 80; curLife = 60; attack = 20; defend = 8; agile = 30;hideRate = 60;
            }
        }

        public void injured(Hunter hunter){   //掉血
            //增加躲避的判断
            if(GameUtil.hidden(this.agile,this.hideRate)){
                System.out.println("*********"+type+":lueluelue,砍不到我");
                show();
                kill(hunter);
                return;
            }
            System.out.println("**********"+type+"又被砍了一刀"+"***********");
            int lostLife = GameUtil.calLostLife(hunter.attack, this.defend);
            curLife-=lostLife;
            if(curLife<0){
                curLife=0;
                died(hunter);
                return;
            }
            show();
            kill(hunter);
        }
        public void died(Hunter hunter){
            this.isLive = false;
            System.out.println("**********"+type+"被砍的四分五裂了"+"**********"+isLive);
            hunter.expAdd(this);    //this
        }
        public void kill(Hunter hunter){
            if(isLive){
            System.out.println("**********>"+type+"冲上去咬了"+hunter.name+"一大口"+"***********");
            hunter.injured(this);
            }else{
                System.out.println("**********"+type+"已经被砍的四分五裂了"+"**********");
            }
        }
        public void show(){
            System.out.println("**********"+type+"  "+"生命值"+curLife+"  "+"生命状态"+isLive+"***************");
        }
    }

你可能感兴趣的:(Java学习,java,游戏)