黑马程序员技术交流社区

标题: 自制文字小游戏 [打印本页]

作者: 朱学诚    时间: 2019-3-11 23:56
标题: 自制文字小游戏
用这几天学的知识写的小游戏,之前所有方法是和主函数在一个类里用静态定义的,但是静态创建的类好像是直接在栈中定义的,因为敌人是一个死亡另一个出现的所以没什么问题,到装备类的时候静态定义的缺点就出现了,静态类new的时候好像并不是在堆里重新创建一个类而是把之前创建的类的地址赋给新定义的指针变量,这就导致每回调用类里的初始化方法时,并不是想象中的吧新的类初始化而是直接把老的类里的各种属性初始化了,装备需要同时存在多个所以只能把所有的都改成动态定义了。


import java.util.Random;
import java.util.Scanner;

class wourd {
    //创建敌人对象,之后都用初始化方法改变敌人属性
    private foe foe = new foe();
    private player player = new player();
    private money money = new money();
    private map map = new map();
    private event event = new event();
    private static stage[] playerW = new stage[4];
    private static Scanner sc = new Scanner(System.in);
    private static Random R = new Random();
    private static boolean lightningRod = false;

    class stage {
        String[] namearr = {"♈龙鳞甲", "♐物理学圣剑", "♐搅屎棍", "♉通天镜", "♐格斗家的拳套",
                "♈破旧的皮甲", "♎破皮鞋", "♎香港脚穿过的鞋子", "♉避雷针", "♈灼热斗篷", "♉逢考必过"};
        int[] typearr = {0, 1, 1, 3, 1, 0, 2, 1, 3, 0, 3};
        int type;
        int ID;
        String name;
        String bewrite = "普通";
        int virtueHP = 0;
        int virtueattack = 0;
        int virtuedefence = 0;
        int gup = 0;
        int bup = 0;

        void the0() {
            int p;
            int b;
            p = R.nextInt(9);
            b = R.nextInt(3) * 10;
            virtueHP = b + p;
            b = R.nextInt(5);
            virtuedefence = b + 3;
            p = R.nextInt(21);
            if (p < 5) {
                gup = 1;
                bewrite = "[稀有]";
            } else if (p > 17) {
                gup = 2;
                bewrite = "[史诗]";
            } else if (p == 20) {
                gup = 3;
                bewrite = "[传说]";
            }
        }

        void the1() {
            int p;
            int b;
            b = R.nextInt(5);
            virtueattack = b + 3;
            p = R.nextInt(21);
            if (p < 5) {
                bup = 1;
                bewrite = "稀有";
            } else if (p > 17) {
                bup = 2;
                bewrite = "史诗";
            } else if (p == 20) {
                bup = 3;
                bewrite = "传说";
            }
        }

        void the2() {
            int p;
            int b;
            b = R.nextInt(3);
            virtuedefence = b + 1;
            p = R.nextInt(21);
            if (p < 5) {
                bup = 1;
                bewrite = "[稀有]";
            } else if (p > 17) {
                bup = 2;
                bewrite = "[史诗]";
            } else if (p == 20) {
                bup = 3;
                bewrite = "[传说]";
            }
        }

        void the3() {
            virtueattack = 1;
            virtuedefence = 1;
            int p;
            p = R.nextInt(21);
            if (p < 5) {
                bup = 1;
                bewrite = "[稀有]";
            } else if (p > 17) {
                bup = 2;
                bewrite = "[史诗]";
            } else if (p == 20) {
                bup = 3;
                bewrite = "[传说]";
            }
        }

        void initial() {
            int i = R.nextInt(11);
            name = namearr[i];
            type = typearr[i];
            ID = i;
            switch (type) {
                case 0:
                    the0();
                    break;
                case 1:
                    the1();
                    break;
                case 2:
                    the2();
                    break;
                case 3:

                    the3();
                    break;
                default:
                    break;
            }
            if (ID == 7) {
                type = 2;
            }
        }

        void show() {
            System.out.println("***********************\n" + name + "\n" + bewrite + "\n" + "生命:+" + virtueHP + "\n"
                    + "攻击:+" + virtueattack + "\n" + "守备:+" + virtuedefence);
            if (gup != 0) {
                System.out.println("格挡:+" + gup);
            } else if (bup != 0) {
                System.out.println("暴击:+" + bup);
            } else {
                System.out.println();
            }
            System.out.println();
        }

    }

    //商店类
    class money {
        int money;
        String stone[] = {"+5HP  ", "+2攻击  ", "+1防御  ", "+10经验  ", "赌博  ", "道具  "};
        int price[] = {10, 10, 10, 10, 20, 30};
        int L = stone.length;
        Random R = new Random();
        int p[] = new int[3];

        void initial() {
            for (int i = 0; i < 3; i++) {
                p[i] = R.nextInt(5);
            }

        }


        void show() {
            initial();
            System.out.println("欢迎来到本小店,您需要买点什么?");
            System.out.println("你的金币:" + money);
            System.out.println("1:" + stone[p[0]] + "2:" + stone[p[1]] + "3:" + stone[p[2]]);
            int c = event.keyinM() - 1;
            int e = p[c];
            switch (e) {
                case 0:
                    if (money >= price[e]) {
                        money -= price[e];
                        player.hpChange(-5);
                        event.thedo(event.keyin());
                    } else {
                        System.out.println("你的钱不够滚吧");
                        event.thedo(event.keyin());
                    }
                    break;
                case 1:
                    if (money >= price[e]) {
                        money -= price[e];
                        player.attack(2);
                        event.thedo(event.keyin());
                    } else {
                        System.out.println("你的钱不够滚吧");
                        event.thedo(event.keyin());
                    }
                    break;
                case 2:
                    if (money >= price[e]) {
                        money -= price[e];
                        player.defence(1);
                        event.thedo(event.keyin());
                    } else {
                        System.out.println("你的钱不够滚吧");
                        event.thedo(event.keyin());
                    }
                    break;
                case 3:
                    if (money >= price[e]) {
                        money -= price[e];
                        player.experience(10);
                        event.thedo(event.keyin());
                    } else {
                        System.out.println("你的钱不够滚吧");
                        event.thedo(event.keyin());
                    }
                    break;
                case 4:
                    System.out.println("请输入你要赌多少钱,赌赢押金翻倍,赌输血本无归:");
                    if (sc.hasNextInt()) {
                        c = sc.nextInt();
                    } else {
                        sc.next();
                        System.out.println("白痴,你再乱输个试试?");
                    }
                    if (money >= c) {
                        money -= c;
                        int j = R.nextInt(10);
                        for (int i1 = 0; i1 < playerW.length; i1++) {
                            if (playerW[i1] != null) {
                                if (playerW[i1].ID == 3) {
                                    j = 0;
                                }
                            }
                        }
                        if (j == 0) {
                            money += c * 2;
                            System.out.println("恭喜你赌赢了!");
                        } else {
                            System.out.println("啊,您赌输了,要再接再厉哦!");
                        }
                        event.thedo(event.keyin());
                    } else {
                        System.out.println("你的钱不够滚吧");
                        event.thedo(event.keyin());
                    }
                    break;
                case 5:
                    break;
            }

        }
    }

    //玩家类
    class player {
        String name;
        int attack;
        int experience;
        int defence;
        int maxHP;
        int HP;
        int experienceUP = 2;
        int rank;

        void getstge() {
            stage z = new stage();
            z.initial();
            boolean ok = true;
            int B = 0;
            for (int i1 = 0; i1 < playerW.length; i1++) {
                stage s = playerW[i1];
                if (s != null) {
                    if (z.type == s.type) {
                        ok = false;
                        B = i1;
                        break;
                    }
                } else {
                    B = i1;
                    break;
                }
            }
            if (ok) {
                playerW[B] = z;
                System.out.println("你获得了:");
                z.show();
            } else {
                System.out.println("你已经有同类型的装备了:");
                System.out.println("装备中:");
                playerW[B].show();
                System.out.println("***********************");
                System.out.println("新获得:");
                z.show();
                System.out.println("是否更换装备?\n 1:是 2:否");
                int c;
                if (sc.hasNextInt()) {
                    c = sc.nextInt();
                } else {
                    sc.next();
                    System.out.println("白痴,你再乱输个试试?");
                    c = 2;
                }
                if (c == 1) {
                    playerW[B] = z;
                }
            }
        }

        int stgeA() {
            int i = 0;
            for (int i1 = 0; i1 < playerW.length; i1++) {
                stage s = playerW[i1];
                if (s != null) {
                    i += playerW[i1].virtueattack;
                }
            }
            return i;
        }

        int stgeD() {
            int i = 0;
            for (int i1 = 0; i1 < playerW.length; i1++) {
                stage s = playerW[i1];
                if (s != null) {
                    i += playerW[i1].virtuedefence;
                }
            }
            return i;
        }

        int stgeH() {
            int i = 0;
            for (int i1 = 0; i1 < playerW.length; i1++) {
                stage s = playerW[i1];
                if (s != null) {
                    i += playerW[i1].virtueHP;
                }
            }
            return i;
        }

        int stgeG() {
            int i = 0;
            for (int i1 = 0; i1 < playerW.length; i1++) {
                stage s = playerW[i1];
                if (s != null) {
                    i += playerW[i1].gup;
                }
            }
            return i;
        }

        int stgeB() {
            int i = 0;
            for (int i1 = 0; i1 < playerW.length; i1++) {
                stage s = playerW[i1];
                if (s != null) {
                    i += playerW[i1].bup;
                }
            }
            return i;
        }

        //玩家血量变动及死亡判定
        boolean hpChange(int i) {
            HP -= i;
            if (i >= 0) {
                if (HP <= 0) {

                    System.out.println(name + "死亡");
                    return true;
                } else {
                    System.out.println(name + "受到了" + (i) + "点伤害" + "当前血量为" + HP);
                    return false;
                }
            } else {
                if (HP > maxHP + stgeH()) {
                    HP = maxHP + stgeH();
                }
                System.out.println(name + "回复了" + (-i) + "点血量" + "当前血量为" + HP);
                return false;
            }
        }

        void maxHpChange(int i) {
            HP += i;
            maxHP += i;
            System.out.println(name + "最大血量提升了" + i + "当前血量为" + HP + "当前血量上限为" + maxHP);
        }

        void attack(int i) {
            attack += i;
            System.out.println(name + "攻击力提升了" + i + "当前攻击力为" + attack);
        }

        void defence(int i) {
            defence += i;
            System.out.println(name + "防御力提升了" + i + "当前防御为" + defence);
        }

        //获得经验
        void experience(int i) {
            experience += i;
            if (experience < experienceUP) {
                System.out.println(name + "经验提升了" + i + "还需要" + (experienceUP - experience) + "点经验升级");
            } else {
                rank += 1;
                experienceUP *= 10;
                attack += rank;
                defence += rank;
                System.out.println(name + "升级了" + "当前等级为" + rank);
                maxHpChange(rank);
            }
        }
    }

    //地图事件
    class map {

        //事件名称
        String map[] = {
                "无事发生", "遭遇敌人", "装逼遭雷劈", "遭遇学姐", "遭遇失足少女", "遭遇敌人", "发现商店"
        };

        //事件实现
        boolean mapeve(int i) {
            int c;
            //输出事件名称
            System.out.println(map[i]);
            //判断事件类型
            switch (i) {
                case 0:
                    //直接进入下次输入
                    event.thedo(event.keyin());
                    break;
                case 1:
                    //返回真,创建敌人,进入战斗
                    return true;
                case 2:
                    lightningRod = false;
                    for (int i1 = 0; i1 < playerW.length; i1++) {
                        if (playerW[i1] != null) {
                            if (playerW[i1].ID == 8) {
                                lightningRod = true;
                            }
                        }
                    }
                    if (lightningRod) {
                        System.out.println("避雷针生效了!");
                        player.hpChange(-1);
                    } else {
                        if (player.hpChange(3)) {
                            return false;
                        }
                    }
                    event.thedo(event.keyin());
                    break;
                case 3:
                    System.out.println("蛋白质是牛肉的6倍");
                    player.hpChange(-2);
                    event.thedo(event.keyin());
                    break;
                //失足少女
                case 4:
                    System.out.println("对方想上你,是否从了对方ps:干了将-3HP+2攻击;\n0:否 | 1:是");
                    if (sc.hasNextInt()) {
                        c = sc.nextInt();
                    } else {
                        sc.next();
                        player.hpChange(2);
                        System.out.println("白痴,你再乱输个试试?");
                        event.thedo(event.keyin());
                        break;
                    }
                    if (c == 1) {
                        if (player.hpChange(2)) {
                            return false;
                        }
                        player.attack += 2;
                        event.thedo(event.keyin());
                    } else {
                        System.out.println("你居然放弃了啪啪啪的机会这是人干的事吗");
                        event.thedo(event.keyin());
                        break;
                    }
                    break;
                case 5:
                    return true;
                case 6:
                    money.show();
                    break;
                default:
                    break;
            }
            return false;
        }


    }

    //敌人类
    class foe {
        //敌人名称数组
        String name[] = {"应杰", "柴匡", "野生的皮卡丘", "比利王", "贝爷", "五五开", "大贤者"};
        //敌人攻击力
        int attack;
        //敌人防御力
        int defence;
        //敌人最大血量
        int maxHP;
        //玩家最大血量
        int HP;
        //敌人名称
        String foeN;
        int BOSSI = 0;

        //敌人创建

        /**
         * @param i
         */
        void initial(int i) {
            //敌人随机属性值
            int a = R.nextInt(5) - 2;
            //初始化名字
            foeN = "☠" + name[R.nextInt(7)];
            attack = i * 2 + a;
            defence = i + a;
            maxHP = i * 10 + a;
            HP = maxHP;
            switch (BOSSI) {
                case 5:
                    foeN = "♜BOOS1";
                    attack = player.attack;
                    defence = 2;
                    maxHP = 30;
                    HP = maxHP;
                    break;
                case 10:
                    foeN = "♜BOOS2";
                    attack = player.attack;
                    defence = 10;
                    maxHP = 50;
                    HP = maxHP;
                    break;
                case 15:
                    foeN = "♜BOOS3";
                    attack = player.defence + 8;
                    defence = player.attack;
                    maxHP = 60;
                    HP = maxHP;
                    break;
                case 20:
                    foeN = "♜BOOS4";
                    attack = player.attack;
                    defence = player.defence;
                    maxHP = 60;
                    HP = maxHP;
                    break;
                case 30:
                    foeN = "♛最终BOSS,暗黑" + player.name;
                    attack = player.attack + 10;
                    defence = player.defence + 10;
                    maxHP = 100;
                    HP = maxHP;
                    break;
                default:
                    break;
            }
        }

        //敌人攻击
        boolean foeA() {
            int A = attack - player.defence;
            int o = R.nextInt(10 - player.stgeD());
            if (o == 0) {
                A = 0;
                System.out.println(player.name + "格挡了" + foeN + "的攻击");
            }
            if (o == 1) {
                A *= 2;
                System.out.println(foeN + "打出了暴击");
            }
            if (A < 0) {
                A = 0;
            }
            System.out.println(foeN + "攻击了" + player.name + "造成了" + A + "点伤害");
            if (player.hpChange(A)) {
                return true;
            }
            return false;

        }

        //敌人血量变动及死亡判定
        boolean hpChange(int i) {
            HP -= i;
            if (HP <= 0) {
                System.out.println(foeN + "死亡");
                BOSSI += 1;
                return true;
            } else {
                System.out.println(foeN + "受到了" + (i) + "点伤害" + "当前血量为" + HP);
                return false;
            }
        }
    }

    class event {
        //玩家面板展示
        void show() {
            System.out.println("姓名: " + player.name);
            System.out.println("等级: " + player.rank);
            System.out.println("HP❤: " + player.HP + "\\" + player.maxHP + "+" + player.stgeH());
            System.out.println("攻击: " + player.attack + "+" + player.stgeA());
            System.out.println("守备: " + player.defence + "+" + player.stgeD());
            System.out.println("经验: " + player.experience + "\\" + player.experienceUP);
            System.out.println("金钱: " + money.money);
            System.out.println("装备栏☬: ");
            for (int i = 0; i < playerW.length; i++) {
                if (playerW[i] != null) playerW[i].show();
            }
            System.out.println("那么萨比想做什么呢?");
            int i = keyin();
            event.thedo(i);
        }

        //玩家输入,随机事件
        int keyin() {
            System.out.println();
            Scanner sc = new Scanner(System.in);
            int i;
            if (sc.hasNextInt()) {
                i = sc.nextInt();
            } else {
                sc.next();
                System.out.println("白痴,你再乱输个试试?");
                i = 0;
            }
            return i;
        }

        int keyinM() {
            int i;
            String j;
            j = sc.next();
            i = j.codePointAt(0) - 48;
            while (i != 1 && i != 2 && i != 3) {
                System.out.println("请正确输入:");
                j = sc.next();
                i = j.codePointAt(0) - 48;
            }
            return i;
        }

        void thedo(int i) {
            if (i == 5) {
                show();
            } else {
                //随机情景
                i = R.nextInt(7);
                //敌人创建
                if (map.mapeve(i)) {
                    foe.initial(player.rank);
                    System.out.println(foe.foeN + "跳出来了,进入战斗");
                    fight();

                }
            }
        }

        //玩家战斗行为判断
        void fight() {
            int a = event.doin();
            switch (a) {
                //战斗
                case 0:
                    int o = R.nextInt(8);
                    int PINA = player.attack + player.stgeA() - foe.defence;
                    if (o == 0) {
                        PINA = 0;
                        System.out.println(foe.foeN + "格挡了你的攻击");
                    }
                    int c = R.nextInt(10 - player.stgeB());
                    if (c == 0) {
                        PINA *= 2;
                        System.out.println("你打出了暴击");
                    }
                    if (PINA <= 0) {
                        PINA = 0;
                    }
                    System.out.println(player.name + "攻击了" + foe.foeN + "造成了" + PINA + "点伤害");
                    if (foe.hpChange(PINA)) {
                        int E = (R.nextInt(player.rank) + 1) * 3;
                        money.money += 10 + E;
                        player.getstge();
                        player.experience(E);
                        if (foe.BOSSI == 30) {
                            System.out.println("恭喜你通关了");
                            return;
                        }
                        event.thedo(event.keyin());
                    } else {
                        if (foe.foeA()) {
                            return;
                        }
                        fight();
                    }
                    break;
                //逃跑
                case 1:
                    int B = R.nextInt(2);
                    for (int i1 = 0; i1 < playerW.length; i1++) {
                        if (playerW[i1] != null) {
                            if (playerW[i1].ID == 10) {
                                B = 1;
                            }
                        }
                    }
                    if (B == 1) {
                        System.out.println("你成功跑掉了");
                        event.thedo(event.keyin());
                    } else {
                        System.out.println("逃跑失败");
                        if (foe.foeA()) {
                            return;
                        }
                        fight();
                    }
                    break;
                //求饶
                case 2:
                    System.out.println(player.name + "向" + foe.foeN + "献上了一朵嫩菊,使其兽性大发,大干一场" + player.name + "乘机跑掉了");
                    event.thedo(event.keyin());
                    break;
            }
        }

        int doin() {
            System.out.println("1:战斗,2:逃跑,3:跪地求饶");
            Scanner sc = new Scanner(System.in);
            int i;
            String j;
            j = sc.next();
            i = j.codePointAt(0) - 48;
            while (i != 1 && i != 2 && i != 3) {
                System.out.println("请正确输入:");
                j = sc.next();
                i = j.codePointAt(0) - 48;
            }

            return i - 1;
        }
    }

    public void play() {
        player one = new player();
        int i;
        System.out.println("欢迎来到妈的智障世界,请输入您的姓名:");
        player.name = "[" + sc.next() + "]";
        System.out.println(player.name + "你好,您用行动证明了您闲的蛋疼");
        System.out.println("好吧,开始你的萨比冒险吧");
        System.out.println("输入任意数字行动,输入5查看属性...");
        player.maxHP = R.nextInt(10) + 30;
        player.HP = player.maxHP;
        player.attack = R.nextInt(5) + 3;
        player.defence = R.nextInt(3) + 1;
        player.experience = 0;
        player.rank = 1;
        event.show();
    }
}

public class MyGame {
    public static void main(String[] args) {
        wourd W = new wourd();
        W.play();
    }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2