Home > Web Front-end > JS Tutorial > body text

Use to implement the game of whack-a-mole in js

不言
Release: 2018-07-14 17:24:53
Original
2274 people have browsed it

This article mainly introduces the implementation of the whack-a-mole game in js. It has a certain reference value. Now I share it with you. Friends in need can refer to it

<!DOCTYPE html>
<html lang="">
<head>
    <mata charset = "utf-8" />
<title>打地鼠</title>
<style>
    #tb{
        background:url(beijing.jpg) no-repeat;
    }
    #fen{
        font-weight:bold;
        font-size:60px;
        font-family:迷你简哈哈;
    }
    #fen1{
        font-weight:bold;
        font-size:60px;
        font-family:迷你简哈哈;
    }
</style>
<script>
    function kaishi(){
        setTimeout("bh()",3000);
        sum=0;
    }
//    实现地鼠出现的动作
    function bh(){
        sj=Math.floor(Math.random()*9);
        img1=document.getElementsByTagName("img");
        img1[sj].src="dishu.gif";
//        给地鼠图片添加单击事件和打击动作
        img1[sj].setAttribute("onclick","daji()");
        setTimeout("xs()",1000);
    }
//    实现打击地鼠的动作,将地鼠图片更换为击中图片,并且去掉onclick事件,避免出现一只地鼠击中两次,加两次分数的情况
    function daji(){
        img1[sj].src="shang.gif";
        img1[sj].removeAttribute("onclick");
//        更换一次图片积分器加十分
        sum+=10;
//        改变积分器
        document.getElementById("fen").innerHTML=sum/10;
        document.getElementById("fen1").innerHTML=sum;
    }
//    一秒后没击中地鼠,地鼠消失动作
    function xs(){
        img1[sj].removeAttribute("onclick");
        img1[sj].src="keng.gif";
        setTimeout("bh()",1000);
    }
</script>
</head>

<body onload="kaishi()">
<center>
    <table id="tb">
        <foction>打地鼠</foction>
        <tr>
            <td><img src="keng.gif"></td>
            <td><img src="keng.gif"></td>
            <td><img src="keng.gif"></td>
        </tr>
        <tr>
            <td><img src="keng.gif"></td>
            <td><img src="keng.gif"></td>
            <td><img src="keng.gif"></td>
        </tr>
        <tr>
            <td><img src="keng.gif"></td>
            <td><img src="keng.gif"></td>
            <td><img src="keng.gif"></td>
        </tr>

    </table>
    <p id="fen">击中次数</p>
    <p id="fen1">分数</p>
</center>
</body>
</html>
Copy after login

The above is the entire article Content, I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Use js to implement the code of the snake game

The above is the detailed content of Use to implement the game of whack-a-mole in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!