• 【HTML】【休闲益智】还有9块月饼并未获得!请及时出战!(解锁月饼小游戏


    前言

    《找月饼》:是一款 pc 端单机 html 灵活智力和鼠标技巧小游戏,休闲娱乐游戏;同时,需要耐心和探索,琢磨变化多端的关卡;锻炼摸鱼的时刻到了,出发吧,找到需要拯救的月饼!

    九种获取月饼的方法~(回答关卡 + 触发机制次数关卡)
    在这里插入图片描述

    如:回答题
    在这里插入图片描述

    一、游戏介绍与规则

    游戏介绍

    回答关卡:根据题目,输入正确答案,即可获得月饼。
    触发机制次数关卡:多次触发机制,达到一定次数,获得月饼。

    管你看没看懂,玩就行了!(破音;海皇式)

    技术介绍

    css + jq

    游戏名称

    《解锁月饼》

    游戏规则

    ① 不限时间.

    ② 分为两种方式获得:回答关卡;触发机制次数关卡。

    ③ 仔细观察对应灰框上数字的变化 (小技巧课堂:多摆弄摆弄鼠标,说不定就出现变化了呢!)

    二、大体设计与代码讲解

    大体设计

    首先,这个不好说吧,最好通过玩的过程;去了解代码吧~ 直接放逻辑就不好玩了吧~ 先玩起来,加深印象~ 代码上也有比较完整代码,就不多说设计了哈~(也因为比较简单)

    代码讲解

    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="shortcut icon"
            href="https://p3-passport.byteimg.com/img/user-avatar/db3b09f9ca107d8843cee3fe8f4f0cd4~100x100.awebp">
        <title>找月饼title>
    head>
    
    <body>
    
        <div class="v-title">
            <h1>解锁月饼h1>
            <h3>当前已解锁月饼:<span id="moon-cake" style="color: rgb(226, 184, 184);">0span>(共 9 个)h3>
            <div class="v-detail">
                <span style="color: black;">解锁月饼,找出9种获取月饼的方法!赶紧出发吧!span><br>
                <span>(ps:一个月饼对应一个灰框)span>
            div>
        div>
        <div class="v-body">
            <div class="v-game">
                <div class="v-game-div">
                    <div class="v-img">
                        <span id="s-sum-1">q1span>
                        <img src="./moon-cake.gif" alt="大月饼" onclick="question1(this)">
                    div>
                    <div class="v-img">
                        <span id="s-sum-2">3span>
                        <img src="./moon-cake.gif" alt="大月饼" onmousedown="downMoonCake(this)">
                    div>
                    <div class="v-img">
                        <span id="s-sum-3">5span>
                        <img src="./moon-cake.gif" alt="大月饼" onmouseup="upMoonCake(this)">
                    div>
                div>
                <div class="v-game-div">
                    <div class="v-img">
                        <span id="s-sum-4">q2span>
                        <img src="./moon-cake.gif" alt="大月饼" onclick="question2(this)">
                    div>
                    <div class="v-img">
                        <span id="s-sum-5">4span>
                        <img src="./moon-cake.gif" alt="大月饼" onclick="onclikeMoonCake(this)">
                    div>
                    <div class="v-img">
                        <span id="s-sum-6">8span>
                        <img src="./moon-cake.gif" alt="大月饼" onmouseleave="leaveMoonCake(this)">
                    div>
                div>
                <div class="v-game-div">
                    <div class="v-img">
                        <span id="s-sum-7">6span>
                        <img src="./moon-cake.gif" alt="大月饼" oncontextmenu="menuMoonCake(this)">
                    div>
                    <div class="v-img">
                        <span id="s-sum-8">520span>
                        <img src="./moon-cake.gif" alt="大月饼" onmousemove="moveMoonCake(this)">
                    div>
                    <div class="v-img">
                        <span id="s-sum-9">9span>
                        <img src="./moon-cake.gif" alt="大月饼" onmouseenter="enterMoonCake(this)">
                    div>
                div>
            div>
            
            <span class="btn" onclick="location.reload()">再来一把span>
        div>
    body>
    <script>
    
    
        var countList = ["+", "-", "*", "/", "%"];
        var q1 = false; // 标记是否已回答问题获取月饼了
        var q2 = false;
        // 当前需要回答一个问题解锁
        function question1(e) {
            if (q1) { // 已经回答过
                return;
            }
            var countStr = countList[Math.floor(Math.random() * (countList.length))];
            var ques = String(Math.floor(Math.random() * (100))) + countStr + String(Math.floor(Math.random() * (100)));
            var ans = prompt("请输入:" + ques);
            // console.log(ans)
            if (ans == null) {
                return;
            }
            if (ques != ans) {
                alert("错误!请重新输入");
                question1(e);
            } else {
                var span = document.getElementById("s-sum-1");
                removeAndShow(e, span);
                q1 = true;
            }
        }
    
        // 当前需要回答一个问题解锁
        function question2(e) {
            if (q2) { // 已经回答过
                return;
            }
            var countStr = countList[Math.floor(Math.random() * (countList.length))];
            var num1 = Number(Math.floor(Math.random() * (100)));
            var num2 = Number(Math.floor(Math.random() * (100)));
            let res = Number(num1) + Number(num2);
            if (countStr == "+") {
                // console.log("+")
            } else if (countStr == "-") {
                // console.log("-")
                res = num1 - num2;
            } else if (countStr == "*") {
                // console.log("*")
                res = num1 * num2;
            } else if (countStr == "/") {
                // console.log("/")
                res = num1 / num2;
            } else if (countStr == "%") {
                // console.log("%")
                res = num1 % num2;
            }
            var ques = String(num1) + countStr + String(num2);
            var ans = prompt("请计算:" + ques);
            if (ans == null) {
                return;
            }
            // console.log("答案:", res, "回答:", ans)
            if (res != ans) {
                alert("错误!请重新输入");
                question2(e);
            } else {
                var span = document.getElementById("s-sum-4");
                removeAndShow(e, span);
                q2 = true;
            }
        }
    
        // 点击事件
        var c1 = 0; // 记录需要点击两次(不论点击取消、确定)
        function onclikeMoonCake(e) {
            var span = document.getElementById("s-sum-5");
            var spanSum = Number(span.innerText);
            if (spanSum == 0) {
                return;
            }
            if (c1 < 2) {
                var msg = "点击我没用~";
                if (c1 == 1) {
                    msg = "不要点击啦!真没用!";
                }
                confirm(msg);
                c1++;
                return;
            }
            if (spanSum > 1) {
                var msg = "再点我生气了!😠";
                confirm(msg);
                spanSum--;
                span.innerText = spanSum;
                return;
            }
            spanSum--;
            alert("行吧,月饼给你了!")
            removeAndShow(e, span);
        }
    
        // 鼠标滑入事件
        function enterMoonCake(e) {
            var span = document.getElementById("s-sum-9");
            var spanSum = Number(span.innerText) - 1;
            if (spanSum > 0) {
                span.innerText = spanSum;
            } else if (spanSum == 0) {
                removeAndShow(e, span);
            }
        }
    
        var m2 = 0; // 故意使坏,第3次滑出才开始计算
        // 鼠标滑出事件
        function leaveMoonCake(e) {
            var span = document.getElementById("s-sum-6");
            var spanSum = Number(span.innerText);
            if (spanSum == 8 && m2 < 2) {
                m2++;
                return;
            }
            spanSum--;
            if (spanSum > 0) {
                span.innerText = spanSum;
            } else if (spanSum == 0) {
                removeAndShow(e, span);
            }
        }
    
        // 鼠标点下事件(鼠标点击的瞬间)
        function upMoonCake(e) {
            var span = document.getElementById("s-sum-3");
            var spanSum = Number(span.innerText);
            // e.style.opacity = 1;
            if (spanSum == 0) {
                return;
            }
            if (spanSum > 1) {
                var msg = "你要不试试取消?";
                if (spanSum == 3) {
                    msg = "点我真没用~";
                }
                var c = confirm(msg);
                if (c) {
                    spanSum--;
                    span.innerText = spanSum;
                }
                return;
            }
            spanSum--;
            alert("可恶,还是被你识破了!")
            removeAndShow(e, span);
        }
    
        // 鼠标松开事件(鼠标点击后,松开的瞬间)
        function downMoonCake(e) {
            var span = document.getElementById("s-sum-2");
            var spanSum = Number(span.innerText);
            if (spanSum == 0) {
                return;
            }
            // console.log(spanSum)
            if (spanSum > 1) {
                var msg = "点击我没用~";
                if (spanSum % 2 == 1) {
                    msg = "你点击取消试试?";
                }
                var c = confirm(msg);
                if (!c) {
                    spanSum--;
                    span.innerText = spanSum;
                }
                return;
            }
            spanSum--;
            alert("获得一块月饼")
            removeAndShow(e, span);
        }
    
        // 长按事件
        // function touchMoonCake(e) {
        //     e.style.opacity = 1;
        // }
    
        // 鼠标在当前区域内移动的事件
        function moveMoonCake(e) {
            var span = document.getElementById("s-sum-8");
            var spanSum = Number(span.innerText);
            spanSum--;
            if (spanSum > 0) {
                span.innerText = spanSum;
            } else if (spanSum == 0) {
                removeAndShow(e, span);
            }
        }
    
        var m1 = false;
        // 鼠标右击
        function menuMoonCake(e) {
            console.log("鼠标右击");
            var span = document.getElementById("s-sum-7");
            var spanSum = Number(span.innerText);
            if (spanSum == 6 && !m1) {
                // 故意使坏右击第一次无变化
                m1 = true;
                return;
            }
            spanSum--;
            if (spanSum > 0) {
                span.innerText = spanSum;
            } else if (spanSum == 0) {
                removeAndShow(e, span);
            }
        }
    
        // 把span里的字去掉,同时显示图片(通过透明度)
        function removeAndShow(e, span) {
            span.innerHTML = ""; // 置空
            e.style.opacity = 1; // 显示图片
            countMoonCake();
        }
    
        // 记录已月饼++
        function countMoonCake() {
            var moonCake = document.getElementById("moon-cake");
            var moonCakeSum = Number(moonCake.innerText);
            moonCakeSum++;
            moonCake.innerText = moonCakeSum;
            if (moonCakeSum == 9) {
                var msg = "恭喜你解锁了所有月饼!获得了胜利!";
                console.log(msg);
                setTimeout(function () {
                    alert(msg);
                }, 300);
            }
        }
    script>
    
    <style>
        .btn {
            cursor: pointer;
            background: #428bca;
            border: #357ebd solid 1px;
            border-radius: 3px;
            color: #fff;
            display: inline-block;
            font-size: 14px;
            padding: 8px 15px;
            text-decoration: none;
            text-align: center;
            min-width: 60px;
            position: relative;
            transition: color 0.1s ease;
        }
    
        .btn:hover {
            background: #357ebd;
        }
    
        .v-game {
            margin: 0 auto;
            /* left: 30%; */
            width: 500px;
        }
    
        .v-game-div {
            display: flex;
        }
    
        .v-img {
            background-color: #ccc;
            margin: 5px;
            height: 145px;
            width: 30%;
        }
    
        .v-img img {
            -webkit-user-drag: none;
            opacity: 0;
            border: 1px solid #ccc;
            /* margin: 5px; */
            width: 100%;
            height: 100%;
            float: left;
            box-sizing: border-box;
            padding: 10px;
            cursor: pointer;
        }
    
        .v-body {
            margin-top: 10px;
            display: inline-block;
        }
    
        body {
            align-items: center;
            text-align: center;
            justify-content: center;
            /* min-height: 100vh; */
            /* background: linear-gradient(-135deg, #c850c0, #4158d0); */
        }
    
        .v-detail {
            font-size: small;
            color: #aaa;
        }
    style>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376

    三、仓库地址与体验地址

    这里代码片段展示(注:手机端有部分关卡无法获得月饼 ~ )点击右上角可直达码上掘金!查看更完整的代码!

    代码片段 目前,码上掘金无法 alter()方法失效的,功能不全~

      大家可以直接来笔者的网站来体验
      在线体验(pc端):体验传送门
      仓库地址:暂无(想要的可以直接去扒笔者的网站传送门

    文章小尾巴

    文章写作、模板、文章小尾巴可参考:《写作“小心思”》

      感谢你看到最后,最后再说两点~
      ①如果你持有不同的看法,欢迎你在文章下方进行留言、评论。
      ②如果对你有帮助,或者你认可的话,欢迎给个小点赞,支持一下~
      我是南方者,一个热爱计算机更热爱祖国的南方人。

      (文章内容仅供学习参考,如有侵权,非常抱歉,请立即联系作者删除。)

  • 相关阅读:
    【知识分享】Java获取当前周的开始时间结束时间
    CSS选择符和可继承属性
    Vue-计算属性和属性监听器
    Transformer详解学习
    后端返回图片流前端展示图片
    通信原理学习笔记6-1:数字解调——基础解调链路、匹配滤波器和AWGN信道最佳接收机
    现代前端工程化实践:Git、Husky、Commitlint与PNPM的协同作战
    SAP UI5 应用中的 sap.ui.require 使用场景
    如何使用XSSFWorkbook读取文本薄?
    Centos (含Rocky-Linux) VSFTPD 简单设置
  • 原文地址:https://blog.csdn.net/qq_43263647/article/details/126822860