日期:2014-05-20  浏览次数:20708 次

作一个小游戏,看你是不是菜鸟。
用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;
    }
}

------解决方案--------------------
探讨

14楼暴击没实现要求,暴击应该是倆倍伤害。

------解决方案--------------------
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;
    }
}