JavaScript implements the pot-beating game of Gray Wolf

JavaScript implements the pot-beating game of Gray Wolf

1. Project Documents

insert image description here

2. Use HTML and CSS for page layout

HTML Part

<div class="container">
        <h1 class="score">0</h1>
        <div class="progress"></div>
        <div id="start">
            <h2>The pot is on Big Bad Wolf</h2>
            <button class="start">Start the game</button></div>
        <div class="rules">Game Rules</div>
        <div class="rule">
            <p>Game rules:</p>
            <p>1. Game time: 60s</p>
            <p>2. Compete with hand speed, beat Gray Wolf +10 points</p>
            <p>3. Beat up Xiao Huihui - 10 points</p>
            <a href="#" rel="external nofollow" class="close">[Close]</a>
        </div>
        <div class="mask">
            <h1>GAME OVER</h1>
            <button class="reStart">Restart</button>
            <button class="finish">End the game</button>
        </div>
        <div id="finish">
            <h2>The pot is on Big Bad Wolf</h2>
            <h3>Score: <span class="scoreEnd"></span> </h3>
        </div>
    </div>

CSS part

* {
    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 {
    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>h2 {
    margin-top: 180px;
    color: white;
    text-align: center;
}

.container>#start>.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: 100px;
    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: 30%;
}

.mask>.reStart {
    margin-left: -50px;
}

.mask>.finish {
    margin-left: 80px;
    float: right;
}

#finish {
    color: white;
    text-align: center;
    display: none;
    margin-top: 100px;
}

#finish h2 {
    padding: 25px;
}

3. Use JavaScript to achieve the effect

var begin = document.querySelector('#start');
var h = begin.querySelector('h2');
var start = document.querySelector('.start'); //Start game button var mask = document.querySelector('.mask'); //Includes restart var rules = document.querySelector('.rules'); //Game rules var rule = document.querySelector('.rule'); //Game rules details var reStart = document.querySelector('.reStart'); //Restart game button var close = document.querySelector('.close'); //Close var progress = document.querySelector('.progress'); //Progress bar var container = document.querySelector('.container'); //Container var score = document.querySelector('.score'); //Game score var finishBtn = document.querySelector('.finish'); //End game button var finish = document.querySelector('#finish'); //End game button var scoreEnd = document.querySelector('.scoreEnd'); //Final score//Click to start the game start.onclick = function() {
    // console.log(123);
    // Hide button finish.style.display = 'none';
    var fadIndex = this.parentNode;
    fadIndex.style.display = 'none';
    // Set the length of the progress bar var progressWidth = 180;
    progressHandler(progressWidth);
    var timer;
    startAnimation(); // animation starts };
// Rules // console.log(rules);
rules.onclick = function() {
    console.log('Click on the game rules');
    rule.style.display = 'block';
};
// Close close.onclick = function() {
    console.log('close');
    rule.style.display = 'none';
};
// Restart the game reStart.onclick = function() {
    score.innerHTML = 0;
    mask.style.display = 'none';
    // console.log(score.innerHTML);
    var progressWidth = 180;
    progress.style.width = '180px';
    progressHandler(progressWidth);
    startAnimation();
};
// End game button finishBtn.onclick = function() {
        mask.style.display = 'none';
        finish.style.display = 'block';
        scoreEnd.innerHTML += score.innerHTML;
        begin.style.display = 'block';
        h.style.display = 'none';
        progress.style.width = 180 + 'px';
    }
    //Progress bar function progressHandler(index) {
    // Set the timer var setProgress = setInterval(function() {
        index--;
        progress.style.width = index + "px";
        if (index <= 0) {
            clearInterval(setProgress); //Clear timer mask.style.display = 'block';
            stopAnimation(); //Stop animation}
    }, 100);
}
//Start animation function startAnimation() {
    //Define two arrays to store images var imgArr = ['./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 imgArr2 = ['./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'
    ];
    // Define an array to save all possible positions var arrPos = [{
        left: "98px",
        top: "115px"
    }, {
        left: "17px",
        top: "160px"
    }, {
        left: "15px",
        top: "220px"
    }, {
        left: "30px",
        top: "293px"
    }, {
        left: "122px",
        top: "273px"
    }, {
        left: "207px",
        top: "295px"
    }, {
        left: "200px",
        top: "211px"
    }, {
        left: "187px",
        top: "141px"
    }, {
        left: "100px",
        top: "185px"
    }];
    // Create an image var imgs = document.createElement('img');
    imgs.setAttribute('class', 'wolfImages');
    //The random position of the image var posIndex = Math.round(Math.random() * 8);
    //Set the image display position imgs.style.position = 'absolute';
    imgs.style.left = arrPos[posIndex].left;
    imgs.style.top = arrPos[posIndex].top;
    // console.log(img.style.left);
    // Randomly get array type var imgType = Math.round(Math.random()) == 0 ? imgArr : imgArr2;
    // Set the content of the picture to be limited to the 0th to the 5th window.index = 0;
    window.indexEnd = 5;
    timer = setInterval(() => {
        if (index > indexEnd) {
            imgs.remove();
            clearInterval(timer);
            startAnimation();
        }
        imgs.setAttribute('src', imgType[index]);
        index++;
    }, 400);
    //Add picturescontainer.appendChild(imgs);
    //scorescoreEverySum(imgs);

}
// Score statistics function scoreEverySum(e) {
    e.onclick = function() {
        // Set the content of the picture to be limited to the 5th to the 9th window.index = 5;
        window.indexEnd = 9;
        // Get the path of the currently clicked image var src = this.getAttribute('src');
        // Judge based on the image address // Increase or decrease the score based on the type of image clicked if (src.indexOf("h") >= 0) {
            score.innerHTML = parseInt(score.innerHTML) + 10;
        } else {
            score.innerHTML = parseInt(score.innerHTML) - 10;
        }
        e.onclick = null
    }
}
//Stop animation function stopAnimation() {
    var img = document.querySelector('.wolfImages');
    console.log(img);
    img.remove();
    clearInterval(timer);
}

4. Rendering

Start screen

insert image description here

End interface

insert image description here

This is the end of the article about implementing the pot-beating Gray Wolf game with JavaScript. For more relevant js pot-beating Gray Wolf content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript to implement a brick-breaking game (with complete source code)
  • js to realize the aircraft war game
  • js to realize the jump game
  • js to realize the flip card game
  • js to realize the snake game (add wall)
  • JavaScript to implement the snake game

<<:  Detailed explanation of storage engine in MySQL

>>:  How to set up URL link in Nginx server

Recommend

Example code for text origami effect using CSS3

Preface This article mainly shares with you an ex...

How to add and delete unique indexes for fields in MySQL

1. Add PRIMARY KEY (primary key index) mysql>A...

MySQL data type details

Table of contents 1. Numeric Type 1.1 Classificat...

React Fragment Introduction and Detailed Usage

Table of contents Preface Motivation for Fragment...

Implementation of mysql decimal data type conversion

Recently, I encountered a database with the follo...

Steps for packaging and configuring SVG components in Vue projects

I just joined a new company recently. After getti...

WeChat applet implements a simple dice game

This article shares the specific code of the WeCh...

Two solutions for Vue package upload server refresh 404 problem

1: nginx server solution, modify the .conf config...

18 Web Usability Principles You Need to Know

You can have the best visual design skills in the...

How to use Node.js to determine whether a png image has transparent pixels

background PNG images take up more storage space ...