直入正题,用JS实现一个简单的打地鼠游戏

因为功能比较简单就直接裸奔JS了,先看看效果图,或者 在线玩玩 吧

如果点击颜色比较深的那个(俗称坏老鼠),将扣分50;如果点击颜色比较浅的那个(俗称好老鼠),将得分100

实现

老鼠好像有点难画,又不想用图片,就直接用CSS画个简单的图代表老鼠和坑吧

html结构

挺简单,用9个 li 标签代表坑,用9个 div 标签代表老鼠

  <div class="container">
<h4>无聊打打地鼠</h4> <div class="game-top">
<p><input type="button" value="开始游戏" id="game-start"><p>
<p>得分:<span id="game-score">0</span></p>
<p>剩余时间:<span id="game-time">60</span> s</p>
</div>
<div class="game-content">
<ul>
<li><div></div></li>
<li><div></div></li>
<li><div></div></li>
<li><div></div></li>
<li><div></div></li>
<li><div></div></li>
<li><div></div></li>
<li><div></div></li>
<li><div></div></li>
</ul>
</div>
</div>

CSS的实现

有点小技巧

对于坑,加个box-shadow: ... inset 美化一下样式

        .game-content li {
float: left;
margin-top: 50px;
margin-left: 30px;
width: 100px;
height: 50px;
border-radius: 50%;
background-color: #67d0b4;
box-shadow: 0 0 50px #706565 inset;
}

对于老鼠,用 border-radius:50%/40% 绘制,第二个参数还是有点使用价值的

        .game-content div {
position: relative;
margin-top: -15px;
margin-left: 25px;
width: 50px;
height: 70px;
border-radius: 50%/40%;
background-color: #dfb25d;
opacity:;
}

而要让老鼠动起来,这里的处理方式就是用动画了,老鼠运动的时候,先往上再往下即可,控制好相对位置看起来和谐一点就行

        @keyframes mouse-move {
50% {
margin-top: -40px;
opacity:;
}
100% {
margin-top: -15px;
opacity:;
}
}
.game-content div.active {
animation: mouse-move 2s ease-in-out infinite;
}

注意 animation: ... infinite 的使用,让动画能一直进行下去,我们使用JS控制好时间差判断应该显示那个老鼠,应该显示多少只老鼠即可

不然的画,会发现动画完成了再也无法让它继续进行了

点击的是好老鼠还是坏老鼠,应该给出提示如:

  

可以直接用CSS的伪元素::after置入对错,在点击的时候,根据不同的性质设置不同的值及颜色

.game-content div.good {
background-color: #dfb25d;
}
.game-content div.good[clicked="1"]::after {
content: "✓";
line-height: 70px;
font-size: 40px;
color: #0ad845;
} .game-content div.bad {
background-color: #a48f5c;
}
.game-content div.bad[clicked="1"]::after {
content: "✕";
line-height: 70px;
font-size: 40px;
color: #db1536;
}
 .container {
width: 500px;
height: 300px;
margin: 50px auto;
border: 1px solid #ddd;
text-align: center;
} .game-top {
padding-top: 10px;
width: 100%;
height: 90px;
}
.game-top p {
margin: 5px;
} .game-content {
overflow: auto;
width: 100%;
border-top: 1px solid #ddd;
background-color: #ddf;
} .game-content ul {
list-style: none;
}
.game-content li {
float: left;
margin-top: 50px;
margin-left: 30px;
width: 100px;
height: 50px;
border-radius: 50%;
background-color: #67d0b4;
box-shadow: 0 0 50px #706565 inset;
}
.game-content li:last-child {
margin-bottom: 50px;
} .game-content div {
position: relative;
margin-top: -15px;
margin-left: 25px;
width: 50px;
height: 70px;
border-radius: 50%/40%;
background-color: #dfb25d;
opacity:;
} .game-content div.good {
background-color: #dfb25d;
}
.game-content div.good[clicked="1"]::after {
content: "✓";
line-height: 70px;
font-size: 40px;
color: #0ad845;
} .game-content div.bad {
background-color: #a48f5c;
}
.game-content div.bad[clicked="1"]::after {
content: "✕";
line-height: 70px;
font-size: 40px;
color: #db1536;
} @media screen and (max-width: 500px) {
.container {
width: 290px;
}
.game-content ul {
padding:;
}
.game-content li {
margin-left: 5px;
width: 90px;
}
.game-content div {
margin-left: 20px;
}
} @-webkit-keyframes mouse-move {
50% {
margin-top: -40px;
opacity:;
}
100% {
margin-top: -15px;
opacity:;
}
}
@keyframes mouse-move {
50% {
margin-top: -40px;
opacity:;
}
100% {
margin-top: -15px;
opacity:;
}
} .game-content div.active {
-webkit-animation: mouse-move 2s ease-in-out infinite;
animation: mouse-move 2s ease-in-out infinite;
}

完整CSS

JS的处理

逻辑是点击开始游戏,倒计时开始,同时好坏老鼠不断运动,控制好坑中好坏老鼠及其数量的随机性,点击好老鼠加分,点击坏老鼠减分,时间到结束游戏。

先看看老鼠的运动

          // 运动操作
moveUpAndDown: function() {
var that = this; // 定时器随机定义good|bad老鼠个数,以及需要显示的个数
that.moveTime = setInterval(function() { for (var i = 0, j = that.mouses.length; i < j; ++i) {
that.mouses[i].setAttribute('clicked', '0');
that.mouses[i].className = 'good active';
that.mouses[i].style.display = 'none';
} // bad 的个数
var badNum = that.getRandom(0, 8);
for (var i = 0; i < badNum; i++) {
that.mouses[that.getRandom(0, 8)].className = 'bad active';
} // 要显示的个数
var showNum = that.getRandom(0, 8);
for (var i = 0; i < showNum; i++) {
that.mouses[that.getRandom(0, 8)].style.display = 'block';
}
}, 2000);
},

使用定时器,定时器的循环与CSS中的动画设置一致,保证循环连贯性

设置class为good 即可定义出一只好老鼠,同理bad 为坏老鼠

在开始游戏,进行调用时,设置class为active 即可让老鼠运动起来

对于打老鼠的操作,要注意到只有运动的老鼠才能点击,每只老鼠只能点击一次

              // 打地鼠操作
that.mousesWrap[0].addEventListener('click', function(e) {
e = e || window.event;
var elem = e.target || e.srcElement;
// 如果当前项被隐藏则不操作,多次点击只取第一次分数
if (elem.style.display !== 'block' || elem.getAttribute('clicked') === '1') {
return;
}
// 扣分
if (elem.className.indexOf('bad') !== -1) {
that.score -= that.badScore;
}
// 加分
else {
that.score += that.goodScore;
} elem.setAttribute('clicked', '1');
that.text(that.gameScore[0], that.score);
}, false);

倒计时结束之后,清除两个计时器,同时将所有老鼠项display都设为none 即可(否则动画会一直循环展示出来)

            // 倒计时,当前剩余游戏时间
countDown: function() {
var that = this; var t = setInterval(function() {
that.text(that.gameTime[0], --that.totalTime); if (that.totalTime === 0) {
clearInterval(t);
clearInterval(that.moveTime); for (var i = 0, j = that.mouses.length; i < j; ++i) {
that.mouses[i].style.display = 'none';
} alert('游戏结束,得分为:' + that.score);
}
}, 1000);
},
     function MouseGame() {
this.mousesWrap = this.$('.game-content');
this.mouses = this.$('.game-content div');
this.gameStart = this.$('#game-start');
this.gameTime = this.$('#game-time');
this.gameScore = this.$('#game-score');
this.goodScore = 100;
this.badScore = 50; this.bindEvent();
} MouseGame.prototype = {
constructor: MouseGame, /**
* 获取元素
* @param {String} elem 元素的字符串标识
* @example
* $('div') | $('p.active')
* @return {NodeList} 获取的元素集
*/
$: function(elem) {
return document.querySelectorAll(elem);
}, /**
* 获取给定范围的随机数
* @param {Number} from 起始
* @param {Number} to 结束
* @return {Number} 随机数
*/
getRandom: function(from, to) {
return Math.floor(Math.random() * (to - from + 1)) + from;
}, /**
* 设置元素内容
* @param {HTMLElement} elem 要设置的元素
* @param {String} val 设置的内容
* @return {String} 设置好的内容|元素本身的内容
*/
text: function(elem, val) {
if (elem.textContent) {
return val !== undefined ? elem.textContent = val : elem.textContent;
} else if (elem.innerText) {
return val !== undefined ? elem.innerText = val : elem.innerText;
}
}, // 运动操作
moveUpAndDown: function() {
var that = this; // 定时器随机定义good|bad老鼠个数,以及需要显示的个数
that.moveTime = setInterval(function() { for (var i = 0, j = that.mouses.length; i < j; ++i) {
that.mouses[i].setAttribute('clicked', '0');
that.mouses[i].className = 'good active';
that.mouses[i].style.display = 'none';
} // bad 的个数
var badNum = that.getRandom(0, 8);
for (var i = 0; i < badNum; i++) {
that.mouses[that.getRandom(0, 8)].className = 'bad active';
} // 要显示的个数
var showNum = that.getRandom(0, 8);
for (var i = 0; i < showNum; i++) {
that.mouses[that.getRandom(0, 8)].style.display = 'block';
}
}, 2000);
}, // 打地鼠操作
bindEvent: function() {
var that = this; // 监听游戏开始/重新开始
that.gameStart[0].addEventListener('click', function() {
that.startGame();
}, false); // 打地鼠操作
that.mousesWrap[0].addEventListener('click', function(e) {
e = e || window.event;
var elem = e.target || e.srcElement;
// 如果当前项被隐藏则不操作,多次点击只取第一次分数
if (elem.style.display !== 'block' || elem.getAttribute('clicked') === '1') {
return;
}
// 扣分
if (elem.className.indexOf('bad') !== -1) {
that.score -= that.badScore;
}
// 加分
else {
that.score += that.goodScore;
} elem.setAttribute('clicked', '1');
that.text(that.gameScore[0], that.score);
}, false);
}, // 倒计时,当前剩余游戏时间
countDown: function() {
var that = this; var t = setInterval(function() {
that.text(that.gameTime[0], --that.totalTime); if (that.totalTime === 0) {
clearInterval(t);
clearInterval(that.moveTime); for (var i = 0, j = that.mouses.length; i < j; ++i) {
that.mouses[i].style.display = 'none';
} alert('游戏结束,得分为:' + that.score);
}
}, 1000);
}, // 开始游戏
startGame: function() {
this.score = 0;
this.totalTime = 60;
this.text(this.gameTime[0], this.totalTime);
this.text(this.gameScore[0], this.score); this.countDown();
this.moveUpAndDown();
}
}; new MouseGame();

完整JS

代码有注释应该不难看懂了

那么..快来fork吧..

无聊的人用JS实现了一个简单的打地鼠游戏的更多相关文章

  1. Unity 2D游戏开发高速入门第1章创建一个简单的2D游戏

    Unity 2D游戏开发高速入门第1章创建一个简单的2D游戏 即使是如今,非常多初学游戏开发的同学.在谈到Unity的时候.依旧会觉得Unity仅仅能用于制作3D游戏的. 实际上.Unity在2013 ...

  2. 通过创建一个简单的骰子游戏来探究 Python

    在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏.这次,我将展示如何使用 Python 模块 Pygame 来创建一个图形化游戏.它将需要几篇文章才 ...

  3. 用canvas和原生js写的一个笨鸟先飞的小游戏(暂时只有一个关卡)

    其中一个画布背景是一张图片,还有小鸟,两个管子的图片.暂时不知道怎么附上去就不添加了.这里只有源代码,css和js都是在html写着的,感觉比他们的容易吧,hah <!DOCTYPE html& ...

  4. 利用前端三大件(html+css+js)开发一个简单的“todolist”项目

    一.介绍 todolist,即待办事项.在windows android ios上参考微软家出的那个To-Do应用,大概就是那样的.我这个更简单,功能只有“待办” “已完成”两项,并且是在浏览器打开的 ...

  5. react.js+easyui 做一个简单的商品表

    效果图:     import React from 'react'; import { Form, FormField, Layout,DataList,LayoutPanel,Panel, Lab ...

  6. js写的一个简单的手风琴菜单

    1 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&q ...

  7. 原生js写的一个简单slider

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 用JS写的一个简单的时钟

    没什么技术含量,单纯的想传上去.手痒了 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  9. Cocos2dx系列笔记7:一个简单的跑酷游戏《萝莉快跑》的消化(附下载)

    懒骨头(http://blog.csdn.com/iamlazybone) 或许有天 我们羡慕和崇拜的人 因为我们的努力 也会来了解我们 说不定 还会成为好友 骨头喜欢这样与哲哲共勉 多少个夜晚 一张 ...

随机推荐

  1. PHP 真正多线程的使用

    以前使用curl的多线程并不是真正的多线程,只是一种模拟的多线程,现在使用pthreads来实现真正意义上的多线程. 下载: windows下: http://windows.php.net/down ...

  2. Default Parameter Values in Python

    Python’s handling of default parameter values is one of a few things that tends to trip up most new ...

  3. Mono 3.2 测试NPinyin 中文转换拼音代码

    C#中文转换为拼音NPinyin代码  在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问题. using System; using System. ...

  4. (新年快乐)ABP理论学习之本地化(2016第一篇)

    返回总目录 本篇目录 应用语言 本地化资源 获取本地化文本 扩展本地化资源 最佳实践 应用语言 一个应用至少有一种UI语言,许多应用不止有一种语言.ABP为应用提供了一个灵活的本地化系统. 第一件事情 ...

  5. Nova PhoneGap框架 第七章 设备事件处理

    我们的框架包含了几种设备事件的处理,目的是为了让我们的程序员更容易的完成代码.这些事件包括:回退键(Android)和横竖屏切换事件. 7.1 Android回退键 首先来说说回退键的事件处理.当用户 ...

  6. [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦

    [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...

  7. EasyPR--一个开源的中文车牌识别系统

    我正在做一个开源的中文车牌识别系统,Git地址为:https://github.com/liuruoze/EasyPR. 我给它取的名字为EasyPR,也就是Easy to do Plate Reco ...

  8. 如何创建一个RESTful WCF Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html 转载请注明出处 (一)web.config文件 要创建REST WCF Service,endpoin ...

  9. Linux更改用户密码

    登录虚拟机后,使用passwd密令更改用户密码,新密码需要输入两次才能更改成功.不多说,直接上代码 [root@localhost Desktop]# passwd //使用passwd密令 Chan ...

  10. 学用MVC4做网站六:后台管理(续)

    关于后台的说明: 后台将会用easyui + ajax模式. 这里涉及两个问题,一个是使用easyui如何在前台验证模型的问题,另一个是ajax提交后返回数据. 一.Easyui验证 前台验证采用ea ...