这是我第一次写代码类的博客,也是我上学的第一个项目,希望各位朋友们能给小的点个赞什么的,有什么问题的欢迎指正 。。。代码随意使用,只求能在用的时候给个赞。。。
打怪兽小游戏
GS类
public class GS {
int curLife;
int attack; //攻击力
int defend; //防御
int maxLife;
String type;
boolean isLive=true;
int agile; //敏捷
int hideRate;
public GS(int Gs){
switch(Gs){
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(WJ wj){ //掉血
//增加躲避的判断
if(process.hidden(this.agile,this.hideRate)){
System.out.println(""+type+":你砍我啊!qwq");
show();
kill(wj);
return;
}
System.out.println(""+type+"又被砍了一刀"+"");
int lostLife = process.calLostLife(wj.attack, this.defend);
curLife-=lostLife;
if(curLife<0){
curLife=0;
died(wj);
return;
}
show();
kill(wj);
}
public void died(WJ wj){
this.isLive = false;
System.out.println(""+type+"被砍的快凉了"+""+isLive);
wj.expAdd(this); //this
}
public void kill(WJ wj){
if(isLive){
System.out.println(""+type+"冲上去咬了"+wj.name+"一大口"+"");
wj.injured(this);
}else{
System.out.println(""+type+"已经被砍的四分五裂了"+"");
}
}
public void show(){
System.out.println(""+type+" "+"生命值"+curLife+" "+"生命状态"+isLive+"");
}
}
WJ(玩家)类
public class WJ {
String name;
int maxLife;
int curLife;
boolean isLive;
String weapon;
int attack; //攻击力
int defend; //防御力
int level;
int exp;
int agile;
int hideRate;
public WJ(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(GS gs){ //战斗
if(gs.isLive){
if(isLive){
System.out.println(""+name+"无情的拿起"+weapon+"杀向"+gs.type+"");
gs.injured(this);
}else{
System.out.println(""+"我们的主角"+name+"已经牺牲了"+"");
}
}else
{
System.out.println("拜托啊!这个怪兽已经被你打死啦!");
}
}
public void injured(GS gs){ //掉血
//增加躲避的判断
if(gs.type == "怪兽"){
if(process.hidden(this.agile,this.hideRate)){
System.out.println(""+name+":小样,打不到我");
show();
fight(gs);
return;
}
System.out.println(""+name+":疼死了,打死你个龟孙"+"");
int lostLife = process.calLostLife(gs.attack, this.defend);
curLife-=lostLife;
if(curLife<0){
curLife=0;
died();
return;
}
gs.curLife+=this.curLife/10;
show();
fight(gs);
}else{
if(process.hidden(this.agile,this.hideRate)){
System.out.println(""+name+":小样,打不到我");
show();
fight(gs);
return;
}
System.out.println(""+name+":疼死了,打死你个龟孙"+"");
int lostLife = process.calLostLife(gs.attack, this.defend);
curLife-=lostLife;
if(curLife<0){
curLife=0;
died();
return;
}
show();
fight(gs);
}
}
public void expAdd(GS gs){
this.exp+=gs.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+"" +"");
}
}
process类
public class process {
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 = process.randomaRange(1,101);
if(ran<sucRate){
return 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;
}
}
Start类
public class Start {
WJ wj;
GS gs1,gs2,gs3,gs4,gs5;
public Start(){
wj = new WJ("凹凸曼","杀猪刀");
gs1 = new GS(4);
gs2 = new GS(3);
gs3 = new GS(3);
gs4 = new GS(2);
gs5 = new GS(4);
}
public void start(){
while(wj.isLive && (gs1.isLive || gs2.isLive || gs3.isLive || gs4.isLive || gs5.isLive)){
System.out.println("对手寻找中");
/**让程序休息3秒钟**/
try{
Thread.sleep(3000);
}
catch(Exception e)
{}
int ran = process.randomaRange(1,6); // 产生随机数,随机寻找僵尸进行战斗
switch(ran){
case 1: wj.fight(gs1); break;
case 2: wj.fight(gs2); break;
case 3: wj.fight(gs3); break;
case 4: wj.fight(gs4); break;
case 5: wj.fight(gs5); break;
default:System.out.println("拜托啊!你要找个正常一点的战斗"); break;
}
}
end();
}
public void end(){
if(wj.isLive == true){
System.out.println("恭喜你!过关啦");
}else{
System.out.println("哈哈哈哈哈哈,被怪兽打死了吧");
}
}
}
game类(测试类)
public class game {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Start().start();
}
}
卑微小张再次求赞。。。。