美文网首页
锅打灰太狼

锅打灰太狼

作者: 一枚奋斗青年 | 来源:发表于2016-07-30 10:58 被阅读21次

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
#game {
width: 320px;
height: 480px;
position: relative;
background: url(img/game_bg.jpg) no-repeat;
margin: 100px auto 0;
}

        #score {
            height: 45px;
            line-height: 48px;
            font-size: 18px;
            color: white;
            text-indent: 62px;
        }

        #progress {
            background: url(img/progress.png) no-repeat;
            width: 180px;
            height: 16px;
            position: absolute;
            left: 63px;
            top: 66px;
        }

        #menu {
            width: 320px;
            height: 480px;
            position: absolute;
            left: 0;
            top: 0;
            background: rgba(0, 0, 0, 0.3);
        }

        #start {
            width: 160px;
            height: 40px;
            font-size: 25px;
            text-align: center;
            line-height: 40px;
            color: white;
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            text-shadow: 0 0 8px yellow;
        }

        #wolves>img {
            position: absolute;
        }

    </style>
</head>
<body>
    <div id="game">
        <p id="score">0</p>
        <p id="progress"></p>
        <div id="wolves">

        </div>
        <div id="menu">
            <p id="start">开始游戏</p>
        </div>
    </div>
</body>

</html>
<script type="text/javascript">

// 获取startBtn
var startBtn = document.getElementById('start');
// 获取menuDiv
var menuDiv = document.getElementById('menu');
// 获取scoreP
var scoreP = document.getElementById('score');
// 获取progressP
var progressP = document.getElementById('progress');
// 获取wolvesDiv
var wolvesDiv = document.getElementById('wolves');

// 记录当前进度条的宽度
var pW = 180;
// 进度条定时器
var timer1;

// 创建狼的定时器
var wolfTimer;

// 点击开始游戏
startBtn.onclick = function() {
    menuDiv.style.display = 'none';
    // scoreP.innerHTML = "0";
    progressP.style.width = "180px";
    // 改变时间
    timer1 = setInterval(changeTime, 100);
    // 创建狼
    wolfTimer = setInterval(createWolf, 2000);
}

// 开始计时
function changeTime() {
    pW--;
    progressP.style.width = pW + 'px';
    if (pW == 0) {
        console.log("游戏结束!!!");
        menuDiv.style.display = "block";
        startBtn.innerHTML = "重新开始";
        pW = 180;
        clearInterval(timer1);
        clearInterval(wolfTimer);
    }
}

// 存放图片的位置
var positions = [
    {l: "98px", t: "115px"},
    {l: "17px", t: "160px"},
    {l: "15px", t: "220px"},
    {l: "30px", t: '293px'},
    {l: '122px', t: '273px'},
    {l: '207px', t: "295px"},
    {l: "200px", t: "211px"},
    {l: "187px", t: "141px"},
    {l: "100px", t: "185px"}
];

// 随机数函数
function randomNum(x, y) {
    return Math.floor(Math.random() * (y - x + 1) + x);
}


// 创建狼
function createWolf() {
    var img = document.createElement('img');

    // 记录打击状态
    img.hit = true;

    // 如果随机出来的数字小于80出来灰太狼
    var num = randomNum(1, 100);
    img.type = num <= 80 ? 'h' : 'x';
    // 随机一个数
    var n = randomNum(0, 8);
    img.style.left = positions[n].l;
    img.style.top = positions[n].t;
    img.src = "img/" + img.type + "0.png";
    wolvesDiv.appendChild(img);

    // 向上动画
    var up = 0;
    var down = 5;
    var downTimer;
    var upTimer = setInterval(function() {
        up++;
        img.src = "img/" + img.type + up + ".png";
        if (up == 5) {

           // 下降动画, 上升动画执行完以后才会执行下降动画
            downTimer = setInterval(function() {
               down--;
               img.src = "img/" + img.type + down + ".png";
                if (down == 0) {
                   // 删除图片
                   wolvesDiv.removeChild(img);

                   // 关闭定时器
                   clearInterval(downTimer);
                }

            }, 150);
            clearInterval(upTimer);
        }
    }, 150);


    // 图片点击事件
    img.onclick = function() {

        // 点击第一次才有效果
        if (img.hit) {
            img.hit = false;
        } else {
            return;
        }
        // 清除向上, 向下动画定时器
        clearInterval(upTimer);
        clearInterval(downTimer);

        var score = scoreP.innerHTML - 0;
        if (img.type == "h") {
            scoreP.innerHTML = score + 10 + '';
        } else {
            scoreP.innerHTML = score - 10 + '';
        }

        // 打击动画
        var hit = 5;

        var hitTimer = setInterval(function() {
            hit++;
            img.src = "img/" + img.type + hit + '.png';
            if (hit == 9) {
                // 移除img
                wolvesDiv.removeChild(img);

                // 清除定时器
                clearInterval(hitTimer);
            }
        }, 150);

    }


}

</script>

相关文章

  • 锅打灰太狼

    * {padding: 0;margin: 0;}#game {widt...

  • 转行当程序员,用206行代码写出锅打灰太狼

    锅打灰太狼效果图如下: 上面图片锅打灰太狼效果就是我206行代码的杰作,不是程序员之前很热爱于这些小游戏的,自从学...

  • 故事都是写给大人看的

    “你相信光吗?” 这样的一句调侃成了现实,仔细一想也是,喜羊羊与灰太狼,虐待动物,吃羊,红太狼用平底锅打灰太狼。被...

  • 青春的悸动一一之青春有料

    青春的悸动 文/秋叶 青春有料 40 “嗨皮!嗨皮!”“小魔仙变身”“红太狼永远都带着平底锅打灰太狼吗?”这些都巳...

  • 红太狼的平底锅终于有了新的用途,这样的创意,简直是天才

    相信看过《喜羊羊与灰太狼》的朋友们都会对红太狼的平底锅记忆犹新。剧中的平底锅是用来教训灰太狼的神器,可如今聪明的设...

  • js原生之锅打灰太狼

    0. 前言 现在网上的小游戏太多了,4399啊,7k7k啊什么的,就有这锅打灰太狼的游戏,觉得挺有意思,就做一个看...

  • 疼?

    有人问灰太狼 红太狼打你 你不疼吗 灰太狼说 疼 疼那你为什么不离开她 灰太狼说 那样就会更疼 就像你明明给我那么...

  • 我和我的灰太狼

    动画片:喜洋洋与灰太狼,里的灰太狼大家都不陌生吧,他每天除了捉羊剩下的就是被红太狼打。但我今天要说的灰太狼不是那个...

  • 谁还没个放荡不羁的初恋

    1 灰太狼今天又被赶出了家,头上还顶着老婆专用的平底锅。 红太狼的怒骂从身后追来:“灰太狼,今天抓不到羊你就不要回...

  • H5 js锅打灰太狼

    Document /* 标签样式初始化 */body{margin:0;p...

网友评论

      本文标题:锅打灰太狼

      本文链接:https://www.haomeiwen.com/subject/vkftsttx.html