简单回合制小游戏

一个简单的回合制游戏,设定两个阵营5名角色。灰太狼与喜羊羊、懒洋洋、美羊羊以及暖羊羊。
(初始设定:灰太狼血量为100,四只羊的血量分别是50,60,45,40)
小羊只有普通攻击造成3点伤害。灰太狼普通攻击和科技技能一次只能攻击一只小羊。
灰太狼普通攻击造成13点伤害,魔法技能攻击造成8点伤害(群体攻击),科技技能造成20点伤害。
灰太狼首先攻击一波小羊,小羊轮流反击灰太狼。

public class Animal {//创建动物类
    protected String name;
    protected int blood;

    public Animal(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }

    public String getName() {
        return name;
    }

    public int getBlood() {
        return blood;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }
}
//创建羊群
public class Sheep extends Animal {

    public Sheep(String name, int blood) {
        super(name, blood);
    }

    public void generalAttack(Wolf wolf) {//羊群对灰太狼的依次攻击
        if (wolf.blood > 3) {
            wolf.blood = wolf.blood - 3;
            System.out.println(this.name + "进行了一次普通攻击  " + wolf.name + "的剩余血量为  " + wolf.blood);
        } else {
            wolf.setBlood(0);
            System.out.println(this.name + "击败了灰太狼");
            System.out.println("游戏结束!");
            return;
        }
    }
}

//创建灰太狼
public class Wolf extends Animal {
    public Wolf(String name, int blood) {
        super(name, blood);
    }

    public int generalAttack(Sheep sheep) {//灰太狼对小样的普通攻击
        if (sheep.blood > 13) {
            sheep.blood = sheep.blood - 13;
            System.out.println(name + "进行了一次普通攻击  " + sheep.name + "的剩余血量为  " + sheep.blood);
            return sheep.blood;
        } else {
            sheep.setBlood(0);
            System.out.println(sheep.name + "已经被灰太狼击败");
        }
        return sheep.blood = 0;
    }

    public int techWeaponAttack(Sheep sheep) {//灰太狼对小样的科技武器攻击
        if (sheep.blood > 20) {
            sheep.blood = sheep.blood - 20;
            System.out.println(name + "进行了一次科技技能攻击  " + sheep.name + "的剩余血量为  " + sheep.blood);
            return sheep.blood;
        } else {
            sheep.setBlood(0);
            System.out.println(sheep.name + "已经被灰太狼击败");
        }
        return sheep.blood = 0;
    }

    public void magicAttack(Sheep[] sheeps) {//灰太狼对羊群的魔法攻击
        for (int i = 0; i < sheeps.length; i++) {
            if (sheeps[i].blood > 8) {
                sheeps[i].blood = sheeps[i].blood - 8;
                System.out.println(name + "进行了一次魔法技能攻击  " + sheeps[i].name + "的剩余血量为  " + sheeps[i].blood);
            } else {
                sheeps[i].setBlood(0);
                System.out.println(sheeps[i].name + "已经被灰太狼击败");
            }
        }
    }

    public int roundsAttack() {//**** 每回合的灰太狼采取的随机攻击方式
        int num = (int)(Math.random()*3);
        return num;
    }
}

public class WarTest01 {//游戏测试
    static Sheep[] sheepArr;
    static Wolf wolf;

    public static void main(String[] args) {
        init();
        boolean flag = false;

        int round  = 1 ;
        while (!(wolf.getBlood() == 0|| flag)) {//每回合灰太狼采取的攻击方式
            System.out.println("-------第"+(round++)+"回合-------");
            switch (wolf.roundsAttack()) {
            case 0:
                wolfGeneralAttack();
                break;
            case 1:
                wolfTechWeaponAttack();
                break;
            case 2:
                wolf.magicAttack(sheepArr);
                break;
            }
            sheepAttack();
            flag=true;
            for (int i = 0; i < sheepArr.length; i++) {
                if (sheepArr[i].getBlood() != 0) {
                    flag = false;
                    break;
                }
            } 
        }
        if (wolf.blood>0) {
            System.out.println("灰太狼获得胜利!!!");
        }else {
            System.out.println("羊群获得胜利!!!");
        }

    }

    private static void init() {
        wolf = new Wolf("灰太狼", 100);
        Sheep sheep1 = new Sheep("喜羊羊", 50);
        Sheep sheep2 = new Sheep("懒羊羊", 60);
        Sheep sheep3 = new Sheep("暖羊羊", 45);
        Sheep sheep4 = new Sheep("美羊羊", 40);
        sheepArr = new Sheep[4];
        sheepArr[0] = sheep1;
        sheepArr[1] = sheep2;
        sheepArr[2] = sheep3;
        sheepArr[3] = sheep4;
    }

    public static void wolfGeneralAttack() {//灰太狼的普通攻击
        int i = (int) (Math.random() * 4);
        while(true) {
            if (sheepArr[i].getBlood() >0) {
                break;
            }else {
                i = (int) (Math.random() * 4);
            }
        }
        wolf.generalAttack(sheepArr[i]);
    }

    public static void wolfTechWeaponAttack() {//灰太狼的科技武器攻击
        int i = (int) (Math.random() * 4);
        while(true) {
            if (sheepArr[i].getBlood() >0) {
                break;
            }else {
                i = (int) (Math.random() * 4);
            }
        }
        wolf.techWeaponAttack(sheepArr[i]);
    }

    public static void sheepAttack() {//遍历检测羊群是否都阵亡
        for (int i = 0; i < sheepArr.length; i++) {
            if (sheepArr[i].blood != 0) {
                sheepArr[i].generalAttack(wolf);
            }
        }
    }
}
阅读更多

没有更多推荐了,返回首页