Java小小RPG游戏第二版 (基于第一版优化)

来源:互联网 发布:淘宝药店提交需求后 编辑:程序博客网 时间:2024/05/19 02:21
英雄打怪物,源码如下
package com.game.huntervsmonster01;public class Hunter{String name;int life;boolean isLive;String weapon;int attack;int defend;public Hunter(int i){switch (i){case 1:this.name = "孙悟空";break;case 2:this.name = "景天";break;case 3:this.name = "李逍遥";break;}}public Hunter(int i, int life, boolean isLive, String weapon, int attack,int defend){this(i);this.life = life;this.isLive = isLive;this.weapon = weapon;this.attack = attack;this.defend = defend;}void fight(Monster monster){if (!isLive){return;}monster.injured(this.attack);show();}void injured(int mattack){if (mattack >= this.defend)this.life = this.life - (mattack - this.defend);if (this.life <= 0){dead();return;}}void dead(){this.isLive = false;}void show(){if (this.isLive)System.out.println(this.name + "还有" + this.life + "点血");elseSystem.out.println(this.name + "壮烈牺牲了!");}}

package com.game.huntervsmonster01;public class Monster{String type;int life;boolean isLive;int attack;int defend;public Monster(int i){switch (i){case 1:this.type = "小僵尸";break;case 2:this.type = "大僵尸";break;case 3:this.type = "僵尸老大";break;case 4:this.type = "小鬼";break;case 5:this.type = "老鬼";break;case 6:this.type = "火鬼王";break;case 7:this.type = "黑无常";break;case 8:this.type = "白无常";break;case 9:this.type = "阎王";break;}}public Monster(int i, int life, boolean isLive, int attack, int defend){this(i);this.life = life;this.isLive = isLive;this.attack = attack;this.defend = defend;}public void fight(Hunter hunter){if (!isLive){return;}hunter.injured(this.attack);show();}public void injured(int hattack){if (hattack >= this.defend)this.life = this.life - (hattack - this.defend);if (this.life <= 0){dead();return;}}void dead(){this.isLive = false;}void show(){if (this.isLive)System.out.println(this.type + "还有" + this.life + "点血");elseSystem.out.println(this.type + "被杀死了!");}}

package com.game.huntervsmonster01;public class TestGme{int i;public static void main(String[] args){Hunter hunter = new Hunter(1, 100, true, "金箍棒", 30, 12);Monster monster = new Monster(1, 200, true, 13, 1);System.out.println(hunter.name + "VS" + monster.type);while (hunter.isLive && monster.isLive){hunter.fight(monster);/* * if(hunter.isLive || monster.isLive) break; */monster.fight(hunter);/* * if(hunter.isLive || monster.isLive) break; */}System.out.println("战斗结束");monster.show();hunter.show();}}


原创粉丝点击