JS宠物游戏源码

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>

<body>
<button type="button" id="btn">启动宠物游戏</button>
<script>

    var mybtn = document.querySelector("#btn");
    mybtn.onclick = function () {
        petGame.welcome();
    };

    // 猫,狗,鱼 ,三种宠物的属性:都有 名字,性别,主人,状态,类型
    // 三种宠物的共有的方法:介绍,喂食
    // 父类:定义三种宠物的共性
    function Pet(name, gender, owner) {
        this.name = name;
        this.gender = gender;
        this.owner = owner;
        this.healthy = 80;
        this.lastEatTime = new Date() - 10 * 1000;
    }
    Pet.prototype.introduceSelf = function () {
        console.log(`
        大家好,我是一只叫${this.name}的 ${this.type},
                    我是个${this.gender === "1" ? "男生" : '女生'},
                    我的主人是${this.owner},
                    我的状态是${this.healthy}
        `);
    };
    Pet.prototype.eat = function () {
        var now = new Date();
        if (now - this.lastEatTime < 1000 * 10) {
            console.log("我吃的太频繁啦,目前不需要吃东西哦!");
        } else {
            this.healthy += 20;
            console.log(`嘻嘻,${this.name}吃饭了,当前状态${this.healthy},今天也要冲呀`);
            this.lastEatTime = new Date();
        }
    }

    // 定义子类:狗
    function Dog() {
        Pet.apply(this, arguments);
        this.type = "狗狗";
    }
    Dog.prototype = new Pet();
    Dog.prototype.constructor = Dog;
    Dog.prototype.play = function () {
        this.healthy += 10;
        console.log(`${this.name}最喜欢玩飞盘了,当前健康值${this.healthy}`);
    };
    // var dog1 = new Dog("旺财","男生","大明")

    // 定义子类:猫
    function Cat() {
        Pet.apply(this, arguments);
        this.type = "猫咪";
    }
    Cat.prototype = new Pet();
    Cat.prototype.constructor = Cat;
    Cat.prototype.play = function () {
        this.healthy += 10;
        console.log(`${this.name}最喜欢玩毛线球了,当前健康值${this.healthy}`);
    };

    // 定义子类:鱼
    function Fish() {
        Pet.apply(this, arguments);
        this.type = "鱼";
    }
    Fish.prototype = new Pet();
    Fish.prototype.constructor = Fish;
    Fish.prototype.play = function () {
        this.healthy += 10;
        console.log(`${this.name}最喜欢吐泡泡了,当前健康值${this.healthy}`);
    };

    var petGame = {
        loginedName: "",
        // 本地模拟的一个数据库,用户列表
        userList: [
            { nickname: "aaa", password: "123123" },
            { nickname: "张三", password: "abc8888" }
        ],
        // 宠物列表
        petList: [],
        welcome: function () { // 欢迎界面
            var result = prompt("欢迎来到宠物游戏,请选择:1,登录  2,注册  3,退出游戏");
            switch (result) {
                case '1':
                    this.login();
                    break;
                case '2':
                    this.register();
                    break;
                case '3':
                    console.log("欢迎下次再来")
                    break;
                default:
                    console.log("输入有误,请重新输入!");
                    // this.welcome();
                    arguments.callee.call(this);
                    break;
            }
        },
        register: function () {

            // 验证注册昵称
            do {
                var isError = true;
                var registerName = prompt("注册--请输入昵称:");

                // 判断昵称是否存在
                var result = this.userList.some(function (item) {
                    return item.nickname === registerName;
                });
                if (registerName === null) {
                    this.welcome();
                    return;
                } else if (registerName.trim() === "") {
                    console.log("昵称不能为空");
                } else {
                    if (result) {
                        console.log("昵称已存在");
                    } else {
                        console.log("昵称OK");
                        isError = false;
                    }
                }
            } while (isError);

            // 验证注册密码
            do {
                var isPwdError = true;
                var registerPwd = prompt("注册--请输入密码:");
                if (registerPwd === null || registerPwd.trim() === "" || registerPwd.trim().length < 6 || registerPwd.trim().length > 12) {
                    console.log("密码输入有误,请输入长度为6-12之间的密码");
                } else {
                    console.log("注册密码OK");
                    isPwdError = false;
                }
            } while (isPwdError);

            // 确认密码
            do {
                var isError3 = true;
                var confirmPwd = prompt("注册--请确认密码:");
                if (confirmPwd === registerPwd) {
                    console.log("注册密码OK");
                    isError3 = false;
                } else {
                    console.log("两次密码不一致!");
                }
            } while (isError3);

            this.userList.push({ nickname: registerName, password: registerPwd });
            console.log("恭喜,注册成功!");
            this.welcome();
        },

        login() {

            // 验证登录昵称
            do {
                var isError = true;
                var loginName = prompt("登录--请输入昵称:");
                if (loginName === "") {
                    console.log("昵称不能为空");
                } else if (loginName === null) {
                    this.welcome();
                    return;
                } else {
                    var result = this.userList.some(function (user) {
                        return user.nickname === loginName;
                    });
                    if (result) {
                        console.log("登录昵称OK");
                        isError = false;
                    } else {
                        console.log("昵称不存在!");
                    }
                }
            } while (isError);

            // 验证登录密码
            do {
                var isError2 = true;
                var loginPwd = prompt("登录--请输入密码:");
                if (loginPwd === null || loginPwd.trim() === "" || loginPwd.trim().length < 6 || loginPwd.trim().length > 12) {
                    console.log("登录密码输入有误,请输入长度为6-12之间的密码");
                } else {
                    isError2 = false;
                }
            } while (isError2);

            // 验证登录昵称和密码是否都匹配
            var isAlive = this.userList.some(function (item) {
                return item.nickname === loginName && item.password === loginPwd;
            });

            if (isAlive) {
                console.log(`欢迎您,${loginName},登录成功`);
                this.loginedName = loginName;
                this.petMain(); // 宠物游戏界面
            } else {
                console.log("昵称或密码错误!请重新登录!");
                arguments.callee.call(this);
            }
        },
        petMain() {
            var result = prompt("欢迎来到宠物游戏,请操作:1、购买宠物 2、查看我的宠物 3、注销");
            switch (result) {
                case '1':
                    this.buyPet();
                    break;
                case '2':
                    this.showPet();
                    break;
                case null:
                case '3':
                    this.welcome();
                    break;
                default:
                    console.log("输入有误");
                    arguments.callee.call(this);
                    break;
            }
        },
        buyPet() {

            // var that = this;
            var result = this.petList.find(item => item.owner === this.loginedName);

            if (result) {
                console.log("你已经有宠物了,请好好对待" + (result.gender === '1' ? "他" : "她"))
                this.petMain();
                return;
            }

            // 选择宠物类型
            do {
                var isTypeError = true;
                var petType = prompt("请选择宠物类型:1:狗狗 2:猫咪 3:鱼");
                if (['1', '2', '3'].includes(petType)) {
                    console.log("宠物类型OK");
                    isTypeError = false;
                } else {
                    console.log("宠物类型输入有误");
                }
            } while (isTypeError);

            // 输入宠物昵称
            do {
                var isNameError = true;
                var petName = prompt("请输入宠物昵称:");
                if (petName === null || petName.trim() === "") {
                    console.log("昵称有误,请重新输入!");
                } else {
                    console.log("宠物昵称OK");
                    isNameError = false;
                }
            } while (isNameError);

            // 选择宠物性别
            do {
                var isGenderError = true;
                var petGender = prompt("请输入宠物的性别:1.GG  2.MM");
                if (['1', '2'].includes(petGender)) {
                    console.log("宠物性别OK");
                    isGenderError = false;
                } else {
                    console.log("宠物性别输入有误");
                }
            } while (isGenderError);

            // 根据用户所选宠物类型,创建宠物对象
            var mypet = null;
            switch (petType) {
                case "1":
                    mypet = new Dog(petName, petGender, this.loginedName);
                    break;
                case "2":
                    mypet = new Cat(petName, petGender, this.loginedName);
                    break;
                case "3":
                    mypet = new Fish(petName, petGender, this.loginedName);
                    break;
            }
            console.log("领养成功!");
            this.petList.push(mypet);
            this.petMain();

        },

        // 查看宠物
        showPet() {
            var result = this.petList.find(item => item.owner === this.loginedName);

            if (!result) {
                console.log("你还没有领养宠物,快去领养一只吧!")
                this.petMain();
                return;
            }

            // console.log(result);
            result.introduceSelf(); // 介绍自己

            var doSomething = prompt("请操作:1,喂食  2,玩耍  3,回到主菜单");
            switch (doSomething) {
                case "1":
                    result.eat();
                    break;
                case "2":
                    result.play();
                    break;
                case "3":
                    this.petMain();
                    return false;
                    break;
                default:
                    console.log("输入有误");
                    break;
            }

            arguments.callee.call(this);
        }
    }
</script>

</body>

</html>

猜你喜欢

转载自blog.51cto.com/14648170/2497862