#文字RPG Beta 2.0

2017-11-29  本文已影响0人  EricsMoon

Class Main

//Beta 2.0
//1 重写了各个类
//2 运用构造函数
//3 实现角色的攻击力和防御力
//show方法增加攻击力防御力提示
//4 增加了Monster的反击

package com.company;

public class Main {

    public static void main(String[] args) {

        Man p1=new Man("勇者","菜刀");
        Monster m1=new Monster(4);
        p1.hit(m1);
        m1.kill(p1);

        }
    }

Class Man

package com.company;

public class Man{
    String name;
    String weapon;
    int HP;
    boolean isalive;
    //攻击力防御力
    int attack;
    int defend;

    //构造函数确定name,weapon
    public Man(String name,String weapon){
        this.name=name;
        this.weapon=weapon;
        HP = 100;
        isalive =true;
        attack=25;
        defend=15;
    }

    public void hit(Monster monster){
        if(!monster.isalive) {
            return;
        }
        if(!isalive) {
            return;
        }
        System.out.println(name+":挥舞着"+weapon+"无情的制裁了"+monster.type);
        monster.injured(this);
    }

    public void injured(Monster monster){
        //实现基础伤害防止被攻击回血
        int lostHP;
        int lostBase=5;
        lostHP=monster.attack-this.defend;
        if(lostHP<=0){
            HP-=lostBase;
        }else{
            HP-=lostHP+lostBase;
        }
        System.out.println(name+":好疼!"+monster.type+"攻击了我!");
        if(HP<=0){
            dead();
            return;
        }
        if(!isalive) {
            return;
        }
        show();
    }

    public void dead(){
        isalive=false;
        System.out.println("系统提示:"+name+"已经牺牲了");
    }

    public void show(){
        System.out.println("系统提示:伟大的"+name+"还剩下"+HP+"点生命值"+",攻击力为"+attack+",防御力为"+defend+"。");
    }

}

Class monster

package com.company;

class Monster{
    String type;
    int HP;
    boolean isalive;
    //攻击力防御力
    int attack;
    int defend;


    //通过构造函数实现怪兽的分级设置
    public Monster(int mt){
        isalive=true;
        if(mt==1){
            type="地狱小犬";
            HP=30;
            attack=15;
            defend=10;
        }else if(mt==2){
            type="地狱法师";
            HP=20;
            attack=25;
            defend=5;
        }else  if(mt==3){
            type="地狱火";
            HP=60;
            attack=5;
            defend=25;
        }else if(mt==4){
            type="BOSS";
            HP=50;
            attack=20;
            defend=20;
        }


    }

    public void kill(Man man){
        if(!man.isalive) {
            return;
        }
        if(!isalive) {
            return;
        }
        System.out.println(type+"撕咬着"+man.name);
        man.injured(this);
    }

    public void injured(Man man){
        //实现基础伤害防止被攻击回血
        int lostHP;
        int lostBase=1;
        lostHP=man.attack-this.defend;
        if(lostHP<=0){
            HP-=lostBase;
        }else{
            HP-=lostHP+lostBase;
        }
        System.out.println(type+":Aaaa~");
        if(HP<=0){
            dead();
            return;
        }
        if(!isalive) {
            return;
        }
        //实现反击
        show();
        System.out.println("系统提示:"+type+"发起了反击!");
        kill(man);
    }

    public void dead(){
        isalive=false;
        System.out.println("系统提示:"+type+"已经被干掉了");
    }

    public void show(){
        System.out.println("系统提示:"+type+"还剩下"+HP+"点生命值"+",攻击力为"+attack+",防御力为"+defend+"。");
    }

}


上一篇下一篇

猜你喜欢

热点阅读