JavaScript 实现锅拍灰太狼小游戏

1、项目文件

2、使用HTML及css进行页面布局

HTML部分

<div class="container">
        <h1 class="score">0</h1>
        <div class="progress"></div>
        <div id="start">
            <h2>锅打灰太狼</h2>
            <button class="start">开始游戏</button></div>
        <div class="rules">游戏规则</div>
        <div class="rule">
            <p>游戏规则:</p>
            <p>1.游戏时间:60s</p>
            <p>2.拼手速,殴打灰太狼+10分</p>
            <p>3.殴打小灰灰-10分</p>
            <a href="#" rel="external nofollow"  class="close">[关闭]</a>
        </div>
        <div class="mask">
            <h1>GAME OVER</h1>
            <button class="reStart">重新开始</button>
            <button class="finish">结束游戏</button>
        </div>
        <div id="finish">
            <h2>锅打灰太狼</h2>
            <h3>得分:<span class="scoreEnd"></span> </h3>
        </div>
    </div>

css部分

* {
    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、使用JavaScript来实现效果

var begin = document.querySelector('#start');
var h = begin.querySelector('h2');
var start = document.querySelector('.start'); //开始游戏按钮
var mask = document.querySelector('.mask'); //包含重新开始
var rules = document.querySelector('.rules'); //游戏规则
var rule = document.querySelector('.rule'); //游戏规则详细
var reStart = document.querySelector('.reStart'); //重新开始游戏按钮
var close = document.querySelector('.close'); //关闭
var progress = document.querySelector('.progress'); //进度条
var container = document.querySelector('.container'); //容器
var score = document.querySelector('.score'); //游戏分数
var finishBtn = document.querySelector('.finish'); // 结束游戏按钮
var finish = document.querySelector('#finish'); //结束游戏按钮
var scoreEnd = document.querySelector('.scoreEnd'); //最后得分
//点击开始游戏
start.onclick = function() {
    // console.log(123);
    // 隐藏按钮
    finish.style.display = 'none';
    var fadIndex = this.parentNode;
    fadIndex.style.display = 'none';
    // 设置进度条长度
    var progressWidth = 180;
    progressHandler(progressWidth);
    var timer;
    startAnimation(); //动画开始
};
// 规则
// console.log(rules);
rules.onclick = function() {
    console.log('点击游戏规则');
    rule.style.display = 'block';
};
// 关闭
close.onclick = function() {
    console.log('关闭');
    rule.style.display = 'none';
};
// 重新开始游戏
reStart.onclick = function() {
    score.innerHTML = 0;
    mask.style.display = 'none';
    // console.log(score.innerHTML);
    var progressWidth = 180;
    progress.style.width = '180px';
    progressHandler(progressWidth);
    startAnimation();
};
// 结束游戏按钮
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';
    }
    //进度条
function progressHandler(index) {
    // 设置计时器
    var setProgress = setInterval(function() {
        index--;
        progress.style.width = index + "px";
        if (index <= 0) {
            clearInterval(setProgress); //清除计时器
            mask.style.display = 'block';
            stopAnimation(); //停止动画
        }
    }, 100);
}
//开始动画
function startAnimation() {
    //定义两个数组存放图片
    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'
    ];
    // 定义一个数组保存所有可能出现的位置
    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"
    }];
    // 创建一个图片
    var imgs = document.createElement('img');
    imgs.setAttribute('class', 'wolfImages');
    //图片随机出现的位置
    var posIndex = Math.round(Math.random() * 8);
    //设置图片显示位置
    imgs.style.position = 'absolute';
    imgs.style.left = arrPos[posIndex].left;
    imgs.style.top = arrPos[posIndex].top;
    // console.log(img.style.left);
    // 随机获取数组类型
    var imgType = Math.round(Math.random()) == 0 ? imgArr : imgArr2;
    // 设置图片的内容 限定为第0张到第5张
    window.index = 0;
    window.indexEnd = 5;
    timer = setInterval(() => {
        if (index > indexEnd) {
            imgs.remove();
            clearInterval(timer);
            startAnimation();
        }
        imgs.setAttribute('src', imgType[index]);
        index++;
    }, 400);
    //添加图片
    container.appendChild(imgs);
    //分数
    scoreEverySum(imgs);

}
// 分数统计
function scoreEverySum(e) {
    e.onclick = function() {
        // 设置图片的内容 限定为第5张到第9张
        window.index = 5;
        window.indexEnd = 9;
        // 拿到当前点击图片的路径
        var src = this.getAttribute('src');
        // 根据图片地址判断
        // 根据点击的图片类型增减分数
        if (src.indexOf("h") >= 0) {
            score.innerHTML = parseInt(score.innerHTML) + 10;
        } else {
            score.innerHTML = parseInt(score.innerHTML) - 10;
        }
        e.onclick = null
    }
}
//停止动画
function stopAnimation() {
    var img = document.querySelector('.wolfImages');
    console.log(img);
    img.remove();
    clearInterval(timer);
}

4、效果图

开始界面

结束界面

到此这篇关于JavaScript 实现锅拍灰太狼小游戏的文章就介绍到这了,更多相关js锅打灰太狼内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • js实现跳一跳小游戏

    本文实例为大家分享了js实现跳一跳小游戏的具体代码,供大家参考,具体内容如下 效果 流程分析 1.鼠标按下开始蓄力 2.鼠标松开,根据鼠标按下的时间让小球运动相应的距离 3.判断小球落点是否在平台内 4.如果在平台范围内,产生下一个平台,分数加10.如果不在游戏结束,判断分数是否大于历史最高分,更新历史最高分. 动画效果 5.鼠标按下小球所在平台要有蓄力效果,鼠标松开后慢慢恢复, 6.小球在空中的运动曲线要平滑 7.小球和平台要有3D效果 注意事项 8.运动涉及到计算器和延时器,要注意清除定时器

  • javascript实现贪吃蛇小游戏

    本文实例为大家分享了js实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> </body> <script> // 贪吃蛇: // 键盘的方向键,控制蛇的方向,碰撞食物,实现增加长度的

  • js实现贪吃蛇小游戏(加墙)

    本文实例为大家分享了js实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 1.贪吃蛇的规则 作为一款经典游戏,很多人玩他其实就是一种怀念.但是他不应该那么单一,应该有更多新的元素出现.然后我是个新手,希望我以后继续学习的途中能够再次回头重写一遍贪吃蛇.他的规则是;a.超出边界会死  b.碰到自身会死  c.吃食物会变长. 值得注意的是:1.在函数里所调用的函数的顺序很重要.2.<script src="mygame2.js"></script>必须在<

  • javascript实现打砖块小游戏(附完整源码)

    小时候玩一天的打砖块小游戏,附完整源码 在?给个赞? 实现如图 需求分析 1.小球在触碰到大盒子上.左.右边框,以及滑块后沿另一方向反弹,在碰到底边框后游戏结束: 2.小球在触碰到方块之后,方块消失: 3.消除所有方块获得游戏胜利: 4.可通过鼠标与键盘两种方式移动滑块: 5.游戏难度可调整,实时显示得分. 代码分析 1.html结构:左右两个提示框盒子分别用一个div,在其中添加需要的内容:中间主体部分用一个div,里面包含一个滑块(slider),一个小球(ball),以及一个装有所有方块的

  • js实现翻牌小游戏

    本文实例为大家分享了js实现翻牌小游戏的具体代码,供大家参考,具体内容如下 效果图 需求分析 1.生成两组顺序随机的1-8数据 2.卡片需要有翻转效果 3.两次翻转数据不相等,回复原状 4.两次翻转数据相等,卡片相等,不能再被点击 5.当所有卡片不能被点击游戏结束 6.限制最大点击次数50次 HTML结构 <div class="wrap"> <div> <p class="top"></p> <p class=

  • js实现飞机大战小游戏

    本文实例为大家分享了js实现飞机大战游戏的具体代码,供大家参考,具体内容如下 1.html代码 <html> <head> <title></title> <meta http-equiv="content" content="text/html" charset="utf-8"/> <link rel="stylesheet" type="text/

  • JavaScript 实现锅拍灰太狼小游戏

    1.项目文件 2.使用HTML及css进行页面布局 HTML部分 <div class="container"> <h1 class="score">0</h1> <div class="progress"></div> <div id="start"> <h2>锅打灰太狼</h2> <button class="s

  • javascript实现的猜数小游戏完整实例代码

    本文实例讲述了javascript实现的猜数小游戏.分享给大家供大家参考,具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <

  • 纯javascript模仿微信打飞机小游戏

    七夕情人节也不要忘了打游戏喔喔-,下面小编为大家准备的情人节礼物之纯javascript模仿微信打飞机小游戏分享给天下的情人们. 首先给大家展示效果图: 查看演示      源码下载 纯JavaScript模仿微信打飞机游戏,做网页小游戏的借鉴下,界面设计是竖长形仿手机屏幕风格,游戏效果流畅.具有分数统计,里面的JS封装类中包括有创建飞机类.飞机移动行为控制,创建子弹类,产生min到max之间的随机数,判断本方飞机是否移出边界,如果移出边界,则取消mousemove事件,反之加上mousemov

  • javascript+css3开发打气球小游戏完整代码

    效果知识点: css3画气球, 自定义属性运用,随机阵列, DOM元素操作,高级回调函数与参数复传,动态布局,鼠标事件,定时器运用,CSS3新增样式等. css代码如下: <style> {margin:0;padding:0;} body{background:#434343;overflow:hidden} .balloon{ position:absolute; left:0; top:0; margin:auto; width:160px; height:160px; 圆角: 左上 右

  • JavaScript写个贪吃蛇小游戏(超详细)

    贪吃蛇大家都玩过,但你会制作嘛?听起来好像很难的样子,其实非常的简单,话不多说直接上代码 我们先把dom结构写出来 <div id="content"> <div id="snake"> <div class="box head"></div> <div class="box"></div> </div> </div> 其中,con

  • python实现逢七拍腿小游戏的思路详解

    逢七拍腿游戏 几个小朋友在一起玩逢七拍腿的游戏,从1开始数数,当数到7的倍数或者尾号是7时,拍一下腿.现在从1数到99,假设每个人都没有错,计算一下共要拍腿几次? 第一种实现思路:通过在for循环语句中使用continue语句来实现计算拍腿次数.首先假设可拍腿次数为最高次数99,每触发满足的条件的时候就直接跳转到下一次循环当中,最后的total减1则不执行,不满足条件时total则减1.因此实际上total减去的是不满足条件的数字,代码如下: total = 99 #记录拍腿次数的变量 for

  • JavaScript实现气球打字的小游戏

    目录 一.实现效果 1.定义球的类 二.源码仓库和效果 一.实现效果 1.定义球的类 气球类中我们需要对26个字符进行处理 this.arr = "abcdefghijklmnopqrstuvwxyz".split(""); 生成一个随机字母 this.index = parseInt(Math.random() * this.arr.length); // 定义随机字符 this.str = this.arr[this.index]; 生成一个div标签并对图片进

  • 基于javascript实现贪吃蛇经典小游戏

    本文实例为大家分享了JS实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta htt

  • javascript实现简单飞机大战小游戏

    本文实例为大家分享了javascript实现飞机大战小游戏的具体代码,供大家参考,具体内容如下 效果图 html文件 <!DOCTYPE html><html lang='zh'><head>    <meta charset='UTF-8'>    <title>mm</title>    <link rel="stylesheet" href="./css/index.css">

  • 由JavaScript技术实现的web小游戏(不含网游)

    1.Mario(游戏地址:http://jsmario.com.ar/ ) 传说中的马里奥网页版,一比一实现了红白机时代超级马里奥中所有功能 与关卡,精细程度不逊原版游戏.依 赖库:无 2 . Bunny Hunt (游戏地址: http://www.themaninblue.com/experiment/BunnyHunt/) 猎兔,一款简单耐玩的网页狩猎游戏,没有使用第三方支持库,直接利用 css 与 JavasSript 实 现. 依 赖库:无 3 . Bomberman ( 游戏地址: 

随机推荐