**********************************怪物类*************************************
package javazy_0519;
public class Monster { //怪物类
String type;
int life;
int atk;
boolean isLive=false;
public void kill(Hunter ss2) { //还击
System.out.println("怪物还击");
ss2.injured();
}
public void injured() { //受伤
System.out.println("你击伤怪物,怪物-"+atk+" mLife:"+life);
}
public void dead() { //判断是否死亡
if(life<=0)
isLive=true;
else
isLive=false;
}
public void show() { //显示状态
if(isLive==true)
System.out.println("怪物死亡");
else
System.out.println("怪物存活 剩余生命值:"+life);
}
public Monster(int life) {
this.life=life;
}
}
******************************猎人类**********************************
package javazy_0519;
public class Hunter {//猎人类
String name,weapon;
int life;
int atk;
boolean isLive=false;
public void fight(Monster ss1) { //攻击
System.out.println("攻击");
ss1.injured();
}
public void injured() { //受伤
System.out.println("你受伤-"+atk+" hLife:"+life);
}
public void dead() { //判断是否存活
if(life<=0)
isLive=true;
else
isLive=false;
}
public void show() { //显示状态
if(isLive==true)
System.out.println("你死了");
else
System.out.println("你存活 剩余生命值:"+life);
}
public Hunter(int life) {
this.life=life;
}
}
****************************游戏测试类********************************
package javazy_0519;
public class TextGame {
public static void main(String[] args) throws InterruptedException {
Hunter a=new Hunter(60);
a.name="superme";
a.weapon="拳头";
a.atk=12;
a.isLive=false;
Monster b=new Monster(37);
b.type="普通僵尸";
b.atk=7;
b.isLive=false;
while(1!=0) {
if(b.life<=0) {
break;
}
a.fight(b);
b.life-=11;
b.dead();
if(b.life<=0) {
break;
}
Thread.sleep(1000);
b.kill(a);
a.life-=7;
a.dead();
if(a.life<=0) {
break;
}
Thread.sleep(1000);
}
a.show();
b.show();
}
}