作一个小游戏,看你是不是菜鸟。
用java作一个非常简单的游戏:
要求:
1.角色可以向怪物实施攻击,一次攻击后,怪物损失部分HP,当HP损失完后,怪物死亡。
2.角色可装备部同武器,目前有木剑、铁剑、魔剑三种。
3.木剑每次攻击,怪物损失20HP;铁剑每次攻击怪物损失50HP,魔剑每次攻击,怪物损失100HP,并有50%的概率出现暴击。
注意:暴击指攻击效能增加一倍,既魔剑若出现暴击怪物损失200HP。
不能超过三个构造方法,代码70行以内。
[解决办法]
我来抛砖引玉了。
- Java code
package com.hm;import java.util.Random;public class Main { private void show() { Hero hero = new Hero(); hero.weaponType = 3; Monster monster = new Monster(); monster.blood = 1000; while (monster.blood > 0) attack(hero, monster); } private void attack(Hero hero, Monster monster) { String weaponName = ""; int power = 0; boolean crit = new Random().nextBoolean(); switch (hero.weaponType) { case 1: weaponName = "木剑"; power = crit ? 20 * 2 : 20; break; case 2: weaponName = "铁剑"; power = crit ? 50 * 2 : 50; break; case 3: weaponName = "魔剑"; boolean b = new Random().nextBoolean(); power = crit ? 100 * 2 : 100; break; default: break; } monster.blood -= power; if (monster.blood > 0) System.out.println((crit ? "暴击!" : "") + "玩家使用" + weaponName + "对怪物造成了" + power + "点伤害!"); else System.out.println("怪物被击毙"); } class Hero {public int weaponType;} class Monster {public int blood;}}
[解决办法]
思路如下,直接已经4个类了,70行写不完,我是菜鸟……
- Java code
Hero hero = HeroFactory.getHero();Monstor monstor = MonstorFactory.getMonstor();while(monstor.isAlive()){ AttackAttribute attr = hero.attack(); monstor.getattack(attr);}
[解决办法]
- Java code
package test;import java.util.Random;public class Main{ public void show(){ Hero h = new Hero(); Monster monster = new Monster(); monster.blood=1000; h.attack(0, monster); } class Hero { public String[] weapon={"木剑","铁剑","魔剑"}; public int [] power = {20,50,100}; public void attack(int weaponType,Monster monster){ boolean crit = new Random().nextBoolean(); monster.blood -=power[weaponType]; if (monster.blood > 0){ System.out.println((crit ? "暴击!" : "") + "玩家使用" + weapon[weaponType] + "对怪物造成了" + power[weaponType] + "点伤害!"); attack(new Random().nextInt(3),monster); }else System.out.println("怪物被击毙"); } } class Monster { public int blood; }}
[解决办法]
[解决办法]
- Java code
public class Test3 { public static void main(String[] args){ new Test3().run(); } public void run(){ Hero hero = new Hero(1); Monster monster = new Monster(1000); while(!monster.die()){ hero.attack(monster); } System.out.print("怪物被击毙!"); } }class Hero{ int weapon = 0; int heat = 0 ; double r = 0; String[] weapons = {"木剑","铁剑","魔剑"}; int[] heats = { 20 , 50 , 100 }; public Hero(int weapon){ this.weapon = weapon; } public void attack(Monster m){ r = Math.random(); heat = r<0.5 ? heats[weapon] : (heats[weapon]*2) ; m.blood = m.blood - heat; if(r>0.5){ System.out.print("暴击!"); } System.out.println("玩家使用"+weapons[weapon]+"对怪物造成了"+heat+"点伤害!"); }}class Monster{ int blood = 0; public Monster(int blood){ this.blood = blood; } public boolean die(){ if(blood<=0){ return true; } return false; }}
[解决办法]
如果按正常的写法,至少需要 武器工厂、英雄工厂、怪物工厂 3个工厂类,光构造工厂类都不下70行代码了。压缩代码的话只能放弃工厂类了。下面代码拟用枚举代替工厂,去掉注释和main函数,大概50多行。
- Java code
import java.util.Random;public class Game2 { public enum Weapons { Wood,Iron,Magic; int Attack;//攻击 int Crit;//暴击 String weaponName;//武器名称 public static Weapons get(String WeaponName) { if(WeaponName.endsWith("木剑")) { Wood.Attack = 20; Wood.weaponName = WeaponName; return Wood; }else if (WeaponName.endsWith("铁剑")) { Iron.Attack = 50; Iron.weaponName = WeaponName; return Iron; }else if (WeaponName.endsWith("魔剑")) { Magic.Attack = 100; Magic.Crit = 50;//暴击50% Magic.weaponName = WeaponName; return Magic; }else{ return null; } } } public class Monster{ int Life; /** * 怪物被攻击 * @param AP (int) 受到的攻击数值 */ public void getAttack(int AP){ this.Life-=AP; System.out.println("怪物受到了"+AP+"点伤害,剩余"+this.Life+"点生命;"); if(this.Life<=0){ System.out.println("怪物被打倒了!\r\n"); } } } public class Hero{ Weapons weapon; /** * 攻击怪物 * @param monster (Monster) 怪物 */ public void attack(Monster monster){ int isCrit = this.weapon.Crit>new Random().nextInt(100)?1:0;//是否发生暴击 System.out.print((isCrit==1?"暴击!":"")+"英雄使用"+this.weapon.weaponName+"进行攻击,"); monster.getAttack(this.weapon.Attack*(1+isCrit));//怪物被攻击 } } /** * 游戏开始 * @param selectWeapon (Weapons) 英雄装备的武器 */ public void Start(Weapons selectWeapon){ Hero hero = new Hero(); hero.weapon = selectWeapon;//装备武器 Monster monster = new Monster(); monster.Life = new Random().nextInt(1000)+1000;//生成怪物的生命 System.out.println("游戏开始,英雄使用武器为"+hero.weapon.weaponName+",怪物生命值:"+monster.Life); do{ hero.attack(monster);//攻击 }while(monster.Life>0); } public static void main(String args[]){ Game2 game = new Game2(); game.Start(Weapons.get("木剑")); game.Start(Weapons.get("铁剑")); game.Start(Weapons.get("魔剑")); }}
[解决办法]
我也发上我自己做的,只做了个大概思路,写得不太全。
- Java code
//这个是英雄类public class Hero { private Weapon weapon; private String name; public Hero(String name) { this.name = name; } public void equip(Weapon weapon) { this.weapon = weapon; } public int attack(Monster monster) { int basicAttack = weapon.getAttack(); int actualAttack = basicAttack * getAttackFactor(); monster.addDamage(actualAttack); return actualAttack; } public String getName() { return name; } //暴击的计算 private int getAttackFactor() { double rand = Math.random(); int res = 1; if (rand >= 0.0 && rand < 0.5) { res = 2; } return res; }}
[解决办法]
我也写了一个~~
- Java code
public class Game { public static void main(String[] args) { play(); } static void play(){ Monster monster = new Monster(1500); Player player = new Player(); int round = 1; System.out.println("----进入战斗----"); while(monster.getHp() > 0){ System.out.println("第 " + round++ + " 回合"); player.setWeapon((int)(Math.random() * 3 + 1));//用于换武器测试 monster.setHp(monster.getHp() - player.getAttack()); System.out.println(); } System.out.println("怪物被打倒,游戏结束!"); }}class Monster{ private int mHp; public Monster(int hp) { mHp = hp; } public int getHp() { return mHp; } public void setHp(int hp){ mHp = hp; System.out.print("怪物剩余血量 " + (mHp > 0 ? mHp : 0) + " "); }}class Player{ private Weapon mWeapon; public Player() { setWeapon(0); System.out.println(); } public Weapon getmWeapon() { return mWeapon; } public void setWeapon(int id) { mWeapon = Weapon.getWeapon(id); System.out.print("你装备了 " + mWeapon.getName() + " "); } public int getAttack(){ int normalAttack = mWeapon.getAttack(); int extraAttack = (int)(Math.random() * 100) < mWeapon.getProbability() ? 200 : 0; int attack = normalAttack + extraAttack; System.out.print("攻击力为 " + attack + (extraAttack > 0 ? " 暴击!!! " : " ")); return attack; }}class Weapon{ private String mName; private int mAttack; private double mProbability; private static Weapon quanTou = new Weapon("拳头",1,0.0); private static Weapon muJian = new Weapon("木剑",20,0.0); private static Weapon tieJian = new Weapon("铁剑",50,0.0); private static Weapon moJian = new Weapon("魔剑",100,50.00); public String getName() { return mName; } public int getAttack() { return mAttack; } public double getProbability() { return mProbability; } public static Weapon getWeapon(int id) { switch (id) { case 1: return muJian; case 2: return tieJian; case 3: return moJian; } return quanTou; } private Weapon(String name,int attack,double probability){ mName = name; mAttack = attack; mProbability = probability; }}