怪物代码
package com.rpg.monster;public class Monster implements Attack {/* 成员变量 Field*///怪物名字private String name;//怪物等级private int level;//怪物血量private int hp;//怪物蓝量private int mp;//怪物攻击private int attack;//怪物防御private int def;//怪物防御private int speed;//怪物死亡后提供经验值private int offerExp;/*** 无参构造方法Constructor*/public Monster() {}//有参构造方法public Monster(String name, int level, int hp, int mp, int attack, int def, int speed, int offerExp) {this.name = name;this.level = level;this.hp = hp;this.mp = mp;this.attack = attack;this.def = def;this.speed = speed;this.offerExp = offerExp;}/*** 攻击一次英雄显示,计算公式为怪物攻击减去英雄实际防御,若小于则造成0点伤害* * @param hero 英雄*/public void attackHero(Hero hero) {System.out.println(getName() + "攻击了" + hero.getName()+ "一次!");if (getAttack() < hero.getActualDef()) {System.out.println("对" + hero.getName() + "造成了0点伤害");} else {System.out.println("对" + hero.getName() + "造成了" + (getAttack() - hero.getActualDef()) + "点伤害");}}/*** 受伤一次显示 怪物血量减少英雄实际攻击减去怪物防御,如果血量为0,则打印死亡* * @param hero*/public void injuried(Hero hero) {setHp(getHp() - (hero.getActualAttack() - getDef()));System.out.println(getName() + "hp: " + getHp());if (getHp() <= 0) {System.out.println(getName() + "dead" + " +" + getOfferExp() + "exp");return;}}//getter与setter方法public String getName() {return name;}public void setName(String name) {this.name = name;}public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}public int getHp() {return hp;}public void setHp(int hp) {this.hp = hp;}public int getMp() {return mp;}public void setMp(int mp) {this.mp = mp;}public int getAttack() {return attack;}public void setAttack(int attack) {this.attack = attack;}public int getDef() {return def;}public void setDef(int def) {this.def = def;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int getOfferExp() {return offerExp;}public void setOfferExp(int offerExp) {this.offerExp = offerExp;}}
史莱姆小怪代码
package com.rpg.monster;public class Slime extends Monster implements Attack {public Slime() {setName("史莱姆");setLevel(1);setHp(10);setMp(0);setAttack(1);setDef(0);setSpeed(0);setOfferExp(5);System.out.println("怪物名:" + getName() + "LV" + getLevel());}}