⚠️ This article is the original code of love article, is not authorized to reprint

First, a rendering:

  • The development train of thought

To develop a game, first you need to know the rules.

The game is called “Smash Big Big Wolf”.

Rules:

  • The game lasts 60 seconds
  • The game role is grey Wolf, small grey
  • Beat gray Wolf with a quick hand
  • + 10 points for beating Gray Wolf and – 10 points for beating gray

Development technology

  • html
  • css
  • jq

Implementation approach

  • 1. Use HTML + CSS to lay out the game interface
  • 2. Import the JQ library
  • 3. Achieve the logic of wildly shooting Grey Wolf

The core logic

  • Encapsulate 60 s progress bar method
  • Encapsulate the method of processing grey Wolf animation
  • Game button click monitor

The HTML code

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Slap Big Big Wolf</title>
    <link rel="stylesheet" href="css/index.css">
    <script src="Js/jquery - 1.12.4. Js"></script>
    <script src="js/index.js"></script>
</head>
<body>
<div class="container">
    <h1 class="score">0</h1>
    <div class="progress"></div>
    <button class="start">Start the game</button>
    <div class="rules">The rules of the game</div>
    <div class="rule">
        <p>Rules of the game:</p>
        <p>1. Game time :60 seconds</p>
        <p>2. Hand speed, beating Grey Wolf +10 points</p>
        <p>3. Beating small Ash -10 points</p>
        <a href="#" class="close">[closed]</a>
    </div>
    <div class="mask">
        <h1>GAME OVER</h1>
        <button class="reStart">Start all over again</button>
    </div>
</div>
</body>
</html>
Copy the code

CSS code

* {margin: 0;
    padding: 0;
}
.container{
    width: 320px;
    height: 480px;
    background: url(".. /images/game_bg.jpg") no-repeat 0 0;
    margin: 50px auto;
    position: relative;
}
.container>h1{
    color: white;
    margin-left: 60px;
}
.container>.progress{
    width: 180px;
    height: 16px;
    background: url(".. /images/progress.png") no-repeat 0 0;
    position: absolute;
    top: 66px;
    left: 63px;
}
.container>.start{
    width: 150px;
    line-height: 35px;
    text-align: center;
    color: white;
    background: linear-gradient(#E55C3D.#C50000);
    border-radius: 20px;
    border: none;
    font-size: 20px;
    position: absolute;
    top: 320px;
    left: 50%;
    margin-left: -75px;
}
.container>.rules{
    width: 100%;
    height: 20px;
    background: #ccc;
    position: absolute;
    left: 0;
    bottom: 0;
    text-align: center;
}
.container>.rule{
    width: 100%;
    height: 100%;
    background: rgba(0.0.0.0.5);
    position: absolute;
    left: 0;
    top: 0;
    padding-top: 100px;
    box-sizing: border-box;
    text-align: center;
    display: none;
}
.rule>p{
    line-height: 50px;
    color: white;
}
.rule>a{
    color: red;
}
.container>.mask{
    width: 100%;
    height: 100%;
    background: rgba(0.0.0.0.5);
    position: absolute;
    left: 0;
    top: 0;
    padding-top: 200px;
    box-sizing: border-box;
    text-align: center;
    display: none;
}
.mask>h1{
    color: #ff4500;
    text-shadow: 3px 3px 0 #fff;
    font-size: 40px;
}
.mask>button{
    width: 150px;
    line-height: 35px;
    text-align: center;
    color: white;
    background: linear-gradient(#74ACCF.#007DDC);
    border-radius: 20px;
    border: none;
    font-size: 20px;
    position: absolute;
    top: 320px;
    left: 50%;
    margin-left: -75px;
}
Copy the code

Jq code

$(function () {
    // 1. Listen for game rule clicks
    $(".rules").click(function () {$(".rule").stop().fadeIn(100);
    });

    // 2. Listen for the close button click
    $(".close").click(function () {$(".rule").stop().fadeOut(100);
    });

    // 3. Listen for the start button click
    $(".start").click(function () {$(this).stop().fadeOut(100);
        // Calls the method that handles the progress bar
        progressHandler();
        // Call the method that handles the grey Wolf animation
        startWolfAnimation();
    });

    // 4. Listen for the restart button click
    $(".reStart").click(function () {$(".mask").stop().fadeOut(100);
        // Calls the method that handles the progress bar
        progressHandler();
        // Call the method that handles the grey Wolf animation
        startWolfAnimation();
    });

    // Define a special method to handle the progress bar
    function progressHandler() {
        // Reset the width of the progress bar
        $(".progress").css({
            width: 180
        });
        // Start the timer to process the progress bar
        var timer = setInterval(function () {
            // Get the current width of the progress bar
            var progressWidth = $(".progress").width();
            // Reduce the current width
            progressWidth -= 1;
            // Reassign the width to the progress bar
            $(".progress").css({
                width: progressWidth
            });
            // Check whether the progress bar is complete
            if(progressWidth <= 0) {// Turn off the timer
                clearInterval(timer);
                // The restart page is displayed
                $(".mask").stop().fadeIn(100);
                // Stop the animation of Grey WolfstopWolfAnimation(); }},100);
    }

    var wolfTimer;
    // Define a special method to handle the Grey Wolf animation
    function startWolfAnimation() {
        // 1. Define two arrays to hold all the pictures of grey Wolf and small Grey Wolf
        var wolf_1=['./images/h0.png'.'./images/h1.png'.'./images/h2.png'.'./images/h3.png'.'./images/h4.png'.'./images/h5.png'.'./images/h6.png'.'./images/h7.png'.'./images/h8.png'.'./images/h9.png'];
        var wolf_2=['./images/x0.png'.'./images/x1.png'.'./images/x2.png'.'./images/x3.png'.'./images/x4.png'.'./images/x5.png'.'./images/x6.png'.'./images/x7.png'.'./images/x8.png'.'./images/x9.png'];
        // 2. Define an array to hold all possible locations
        var arrPos = [
            {left:"100px".top:"115px"},
            {left:"20px".top:"160px"},
            {left:"190px".top:"142px"},
            {left:"105px".top:"193px"},
            {left:"19px".top:"221px"},
            {left:"202px".top:"212px"},
            {left:"120px".top:"275px"},
            {left:"30px".top:"295px"},
            {left:"209px".top:"297px"}];// 3. Create an image
        var $wolfImage = $("<images src='' class='wolfImage'>");
        // Randomly get the position of the image
        var posIndex = Math.round(Math.random() * 8);
        // 4. Set the image display position
        $wolfImage.css({
           position: "absolute".left:arrPos[posIndex].left,
            top:arrPos[posIndex].top
        });
        // Randomly get the array type
        var wolfType = Math.round(Math.random()) == 0 ? wolf_1 : wolf_2;
        // 5. Set the content of the image
        window.wolfIndex = 0;
        window.wolfIndexEnd = 5;
        wolfTimer = setInterval(function () {
            if(wolfIndex > wolfIndexEnd){
                $wolfImage.remove();
                clearInterval(wolfTimer);
                startWolfAnimation();
            }
            $wolfImage.attr("src", wolfType[wolfIndex]);
            wolfIndex++;
        }, 300);

        // 6. Add the image to the interface
        $(".container").append($wolfImage);

        // 7. Call methods that handle game rules
        gameRules($wolfImage);
    }

    function gameRules($wolfImage) {
        $wolfImage.one("click".function () {
            // Modify the index
            window.wolfIndex = 5;
            window.wolfIndexEnd = 9;

            // Get the address of the currently clicked image
            var $src = $(this).attr("src");
            // Check if it is grey Wolf
            var flag = $src.indexOf("h") > =0;
            // Add or subtract points according to the type of image clicked
            if(flag){
                / / + 10
                $(".score").text(parseInt($(".score").text()) + 10);
            }else{
                / / - 10
                $(".score").text(parseInt($(".score").text()) - 10); }}); }function stopWolfAnimation() {$(".wolfImage").remove();
        clearInterval(wolfTimer); }});Copy the code

The final result

The basic production process is not difficult, the core is to understand the business logic.

Video on extraction code: NQVT

Please remember to like it before you collect it! Good articles deserve to be seen by more people.