Native js to write a simple mole game

Native js to write a simple mole game

Weekend off

When I just woke up, a child's shoe asked me if it is difficult to write a game of moles originally?
Answer: "It's not difficult to sort out the logical relationship of the game of whack-a-mole, it won't take more than half an hour."
Then I didn't believe it. . . .
So just. . . . . . .
Wrote it to him on the spot. . . . . .

ps: I think that when writing things, the focus is on logic.

Common functions of Hamster are:

  • 1. The background image and the mouse are displayed and hidden randomly;
  • 2. Accumulate points or information prompt when clicked;
  • 3. The game will automatically end within the specified time;
  • 4. Stop, exit, and replay the game.
    *** That's basically it? ***

It's easy now, write styles, and write logic codes according to functional requirements;

Come

Structure and style

<style>
    * {
     
     
        padding: 0;
    }

    html,
    body {
     
     
        width: 90%;
        height: 80%;
        background-color: grey;
    }

    h1 {
     
     
        text-align: center;
    }

    #box {
     
     
        width: 90%;
        height: 93%;
    }

    .contbox {
     
     
        width: 100%;
        height: 100%;
        margin: auto;
        margin-top: 6%;
        border: 1px solid greenyellow;
        display: flex;
        flex-wrap: wrap;
        position: relative;
    }

    .minbox {
     
     
        width: 32%;
        height: 32%;
        border: 1px solid hotpink;
        margin-left: 1%;
    }

    .minbox img {
     
     
        width: 100%;
        height: 100%;
    }

    .textbox {
     
     
        position: absolute;
        top: 55px;
        font-size: 26px;
        color: #f4f4f4;
    }
</style>

<body>
    <h1>打地鼠游戏V01版</h1>
    <div id="box">
        <div class="contbox"></div>
        <div class="textbox">
            <span class="stops">停止游戏</span>||
            <span>信息提示处:</span>
            <span class="textboxs"></span>
        </div>
    </div>
</body>

The effect is as follows:
Insert picture description here
js behavior

  var contBox = document.querySelector('.contbox');
    var textBoxs = document.querySelector('.textboxs');
    var stops = document.querySelector('.stops');

    let mouseArr = [];
    let jsNum1 = 0, jsNum2 = 0;//计数器
    mouseArr.length = 9;//定义盒子个数

    for (let i = 0; i < mouseArr.length; i++) {
    
    
        contBox.innerHTML += `<div class="minbox"><img src='./img/greenbelt.jpg'alt></div>`
    }

    // 获取img标签 
    var minBox = document.querySelectorAll('img');

    // 封装老鼠随机出现
    function showMouse() {
    
    
        var num = Math.floor((Math.random() * minBox.length));
        minBox[num].src = './img/mouse.gif';
        setTimeout(`minBox[${
      
      num}].src = './img/greenbelt.jpg'`, 900);
    }
    // 启动游戏
    var mouseStart = setInterval(showMouse, 1500);

    // 敲打老鼠
    for (let i = 0; i < mouseArr.length; i++) {
    
    
        minBox[i].onclick = function () {
    
    
            let str = 'mouse';
            if (minBox[i].src.indexOf(str) < 0) {
    
    
                jsNum1++;
                textBoxs.innerText = '菜鸡,你第' + jsNum1 + '次没打到他了';
            } else {
    
    
                jsNum2++;
                textBoxs.innerText = '秀儿,你第' + jsNum2 + '次打到他了';
            }
        }
    }
    
    // 停止游戏
    stops.onclick = function () {
    
    
        clearInterval(mouseStart);
        alert('游戏已经停止');
    }

A simple game of whack-a-mole is realized like this

Guess you like

Origin blog.csdn.net/m0_46442996/article/details/107593413