Class Main
//Beta 2.0
//1 重寫(xiě)了各個(gè)類(lèi)
//2 運(yùn)用構(gòu)造函數(shù)
//3 實(shí)現(xiàn)角色的攻擊力和防御力
//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;
//構(gòu)造函數(shù)確定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+"無(wú)情的制裁了"+monster.type);
monster.injured(this);
}
public void injured(Monster monster){
//實(shí)現(xiàn)基礎(chǔ)傷害防止被攻擊回血
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("系統(tǒng)提示:"+name+"已經(jīng)犧牲了");
}
public void show(){
System.out.println("系統(tǒng)提示:偉大的"+name+"還剩下"+HP+"點(diǎn)生命值"+",攻擊力為"+attack+"融欧,防御力為"+defend+"敏弃。");
}
}
Class monster
package com.company;
class Monster{
String type;
int HP;
boolean isalive;
//攻擊力防御力
int attack;
int defend;
//通過(guò)構(gòu)造函數(shù)實(shí)現(xiàn)怪獸的分級(jí)設(shè)置
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){
//實(shí)現(xiàn)基礎(chǔ)傷害防止被攻擊回血
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;
}
//實(shí)現(xiàn)反擊
show();
System.out.println("系統(tǒng)提示:"+type+"發(fā)起了反擊!");
kill(man);
}
public void dead(){
isalive=false;
System.out.println("系統(tǒng)提示:"+type+"已經(jīng)被干掉了");
}
public void show(){
System.out.println("系統(tǒng)提示:"+type+"還剩下"+HP+"點(diǎn)生命值"+"噪馏,攻擊力為"+attack+"麦到,防御力為"+defend+"绿饵。");
}
}