JavaScript 打地鼠游戏代码说明


Posted in Javascript onOctober 12, 2010

演示地址:http://demo.3water.com/js/mouse/index.html
打包下载地址 https://3water.com/jiaoben/32434.html
这个是我无聊的时候写的,先看看效果(UI做得比较丑):
JavaScript 打地鼠游戏代码说明 
说明:红色的点击得分100,蓝色的点击扣分100.

只是想用js来写个小游戏,顺便练练js的代码。
先看html部分:
html

<style> 
#panel{height:300px;width:300px;background:#ccc;margin:50px 0 0 200px;} 
#panel ul{list-style:none;display:block;float:left;margin:0;padding:0;} 
#panel li{display:block;float:left;width:100px;height:100px; 
overflow:hidden;position:relative;text-align:center;} 
#panel li span{display:block;position:relative;left:0;top:60px; 
width:100px;height:40px;background:url(img/hole.gif) 0 -60px;z-index:100;} 
</style> 
</head> 
<body> 
<span>说明:红色的点击得分100,蓝色的点击扣分100.</span> 
<div id="panel"> 
<ul> 
<li><span></span></li> 
<li><span></span></li> 
<li><span></span></li> 
<li><span></span></li> 
<li><span></span></li> 
<li><span></span></li> 
<li><span></span></li> 
<li><span></span></li> 
<li><span></span></li> 
</ul> 
</div> 
<div>分数:<span id="score">0</span></div> 
<div>倒计时:<span id="time">60</span></div> 
<input type="button" value="开始" onclick="GameStart()" />

js部分:地鼠类
var Mouse = function(type){ 
//地鼠的具体dom元素,添加到页面上的 
this.mouse = null; 
//地鼠的编号 
this.num = -1; 
//地洞的编号(地鼠藏身在哪个洞) 
this.hole = -1; 
//初始化,type为地鼠类型,好与坏 
this.init(type); 
} 
Mouse.prototype = { 
//地鼠类型,好,坏,好的被杀,坏的被杀 
mousetype: { 
"good": "img/good.gif", 
"bad": "img/bad.gif", 
"goodkill":"img/goodkill.gif", 
"badkill":"img/badkill.gif" 
}, 
//初始化地鼠 
init : function(type){ 
type = type || 'good'; 
var _this = this; 
//创建地鼠的dom元素 
this.mouse = document.createElement("div"); 
//扩展属性--地鼠类型 
this.mouse.mousetype = type; 
//扩展类型--属否活着 
this.mouse.islive = true; 
this.mouse.style.cssText = 'width:75px;height:100px;background:url('+this.mousetype[type]+');left:0;top:20px;\ 
position:relative;margin:auto;cursor:pointer;'; 
//绑定地鼠被点击事件 
this.mouse.onclick = function(e){_this.beat(e);}; 
}, 
//地鼠被点中 
beat : function(e){ 
if(this.mouse.islive){ 
this.mouse.islive = false; 
this.onbeat(); 
this.mouse.style.background = "url("+this.mousetype[this.mouse.mousetype+"kill"]+")"; 
} 
}, 
//地鼠的动画 
animation : function(speed){ 
speed = speed == 'fast'?20:speed == 'normal'?30:50; 
var obj = this.mouse,ost = obj.style,oTop = parseInt(ost.top,10),cut=5,_this = this; 
//让地鼠从地洞冒出来 
var show = function(top){ 
top = top-cut; 
if(top >= -40){ 
ost.top = top + 'px'; 
setTimeout(function(){show(top);},speed); 
} 
else 
{ 
setTimeout(function(){hide(-40);},speed*10); 
} 
} 
//隐藏地鼠 
var hide = function(top){ 
top = top+cut; 
if(top <= oTop){ 
ost.top = top + 'px'; 
setTimeout(function(){hide(top);},speed); 
} 
else { 
_this.reset(); 
} 
} 
show(oTop); 
}, 
//重置地鼠,当地鼠滚回洞里的时候 
reset : function(){ 
this.mouse.islive =true; 
this.mouse.style.background = "url("+this.mousetype[this.mouse.mousetype]+")"; 
this.onend(); 
}, 
//扩展方法:地鼠被点中 
onbeat : function(){}, 
//扩展方法:地鼠动画结束后 
onend : function(){} 
}

接着是游戏控制类,控制游戏的逻辑:
//游戏控制类 
var Game = { 
//游戏时间,一分钟 
time : 61, 
//地鼠地图,总共有十只,其中两只是坏的 
mouseMap : { 
1:'good', 
2:'bad', 
3:'good', 
4:'good', 
5:'bad', 
6:'good', 
7:'bad', 
8:'good', 
9:'good', 
10:'good' 
}, 
//所有的地鼠dom元素 
allMouse : [], 
//目前分数 
nowScore : 0, 
//目前有哪几个地洞给占用 
hasHole : {}, 
//目前有哪几只地鼠是活动的 
hasMouse : {}, 
//页面的地洞集合 
lis : null, 
//初始化地鼠与地洞 
init : function(){ 
//获取页面的地洞集合 
this.lis = document.getElementById('panel').getElementsByTagName('li'); 
_this = this; 
//初始化10只地鼠 
for(var i=1;i <=10;i++){ 
var mouse = new Mouse(this.mouseMap[i]); 
//扩展地鼠被点中事件 
mouse.onbeat = function(){ 
//修改分数 
Game.changeScore(100 * (this.mouse.mousetype=='good'?1:-1)); 
} 
//扩展地鼠动画结束事件 
mouse.onend = function(){ 
//移除地洞中的地鼠 
var li = _this.lis[this.hole]; 
li.removeChild(li.mouse.mouse); 
li.mouse = null; 
//清除对应的地洞与地鼠 
_this.hasHole[this.hole] = null; 
_this.hasMouse[this.num] = null; 
} 
this.allMouse.push(mouse); 
} 
}, 
//修改游戏分数 
changeScore : function(score){ 
this.nowScore += score; 
document.getElementById('score').innerHTML = this.nowScore; 
}, 
//游戏开始 
start : function(){ 
if(this.time <= 0)return; 
var _this = this; 
//随机地洞编号 
var random = parseInt(Math.random()*9,10); 
while(this.hasHole[random]){ 
random = parseInt(Math.random()*9,10); 
} 
//随机地鼠编号 
var randomMouse = parseInt(Math.random()*10,10); 
while(this.hasMouse[randomMouse]){ 
randomMouse = parseInt(Math.random()*10,10); 
} 
//添加地鼠到地洞中 
this.allMouse[randomMouse].hole = random; 
this.allMouse[randomMouse].num = randomMouse; 
this.lis[random].appendChild(this.allMouse[randomMouse].mouse); 
this.lis[random].mouse = this.allMouse[randomMouse]; 
this.lis[random].mouse.animation('normal'); 
//记录地鼠与地洞编号 
this.hasHole[random] = 'true'; 
this.hasMouse[randomMouse] = 'true'; 
setTimeout(function(){_this.start();},250); 
}, 
//倒计时 
startTime : function(){ 
this.time -= 1; 
var _this = this; 
document.getElementById('time').innerHTML = this.time; 
if(this.time > 0){ 
setTimeout(function(){_this.startTime()},1000); 
} 
}, 
//重置游戏设置 
reset : function(){ 
this.time = 61; 
this.allMouse = []; 
this.nowScore = 0; 
this.hasHole = {}; 
this.hasMouse = {}; 
this.lis = null; 
this.changeScore(0); 
} 
} 
//游戏开始函数 
function GameStart(){ 
if(Game.time > 0 && Game.time != 61){ 
alert("游戏尚未结束,不能重新开始哦!"); 
return; 
} 
Game.reset(); 
Game.init(); 
Game.start(); 
Game.startTime(); 
}

这样就完成了。。。功能还是很简陋。。。只是想说明,js还是可以做小游戏的。。。欢迎拍砖!
Javascript 相关文章推荐
jQuery示例收集
Nov 05 Javascript
javascript文本模板用法实例
Jul 31 Javascript
javascript中字体浮动效果的简单实例演示
Nov 18 Javascript
javascript判断回文数详解及实现代码
Feb 03 Javascript
html+javascript+bootstrap实现层级多选框全层全选和多选功能
Mar 09 Javascript
canvas实现爱心和彩虹雨效果
Mar 09 Javascript
jQuery修改DOM结构_动力节点Java学院整理
Jul 05 jQuery
详解webpack多页面配置记录
Jan 22 Javascript
浅谈vue-cli 3.0.x 初体验
Apr 11 Javascript
Vue-router的使用和出现空白页,路由对象属性详解
Sep 03 Javascript
解决vux 中popup 组件Mask 遮罩在最上层的问题
Nov 03 Javascript
vue 实现基础组件的自动化全局注册
Dec 25 Vue.js
理解Javascript_03_javascript全局观
Oct 11 #Javascript
理解Javascript_02_理解undefined和null
Oct 11 #Javascript
理解Javascript_01_理解内存分配原理分析
Oct 11 #Javascript
javascript getElementsByClassName实现代码
Oct 11 #Javascript
javascript Array.prototype.slice使用说明
Oct 11 #Javascript
javascript 伪数组实现方法
Oct 11 #Javascript
javascript forEach通用循环遍历方法
Oct 11 #Javascript
You might like
PHP自定义大小验证码的方法详解
2013/06/07 PHP
PHP static局部静态变量和全局静态变量总结
2014/03/02 PHP
在PHP程序中使用Rust扩展的方法
2015/07/03 PHP
php实现的一段简单概率相关代码
2016/05/30 PHP
php while循环控制的简单实例
2016/05/30 PHP
搭建自己的PHP MVC框架详解
2017/08/16 PHP
php 可变函数使用小结
2018/06/12 PHP
PHP PDOStatement::fetchAll讲解
2019/01/31 PHP
js jquery验证银行卡号信息正则学习
2013/01/21 Javascript
JS实现简单的键盘打字的效果
2015/04/24 Javascript
js实现文本框宽度自适应文本宽度的方法
2015/08/13 Javascript
jquery 标签 隔若干行加空白或者加虚线的方法
2016/12/07 Javascript
jQuery实现判断控件是否显示的方法
2017/01/11 Javascript
Node.js调试技术总结分享
2017/03/12 Javascript
vue实现百度搜索下拉提示功能实例
2017/06/14 Javascript
Angular2.js实现表单验证详解
2017/06/23 Javascript
JS实现为动态添加的元素增加事件功能示例【基于事件委托】
2018/03/21 Javascript
JS正则表达式常见函数与用法小结
2020/04/13 Javascript
js实现搜索提示框效果
2020/09/05 Javascript
Python 读写文件和file对象的方法(推荐)
2016/09/12 Python
快速实现基于Python的微信聊天机器人示例代码
2017/03/03 Python
python虚拟环境virtualenv的安装与使用
2017/09/21 Python
python机器学习实战之最近邻kNN分类器
2017/12/20 Python
python的debug实用工具 pdb详解
2019/07/12 Python
Django生成PDF文档显示网页上以及PDF中文显示乱码的解决方法
2019/12/17 Python
Python3开发实例之非关系型图数据库Neo4j安装方法及Python3连接操作Neo4j方法实例
2020/03/18 Python
python 将视频 通过视频帧转换成时间实例
2020/04/23 Python
Django crontab定时任务模块操作方法解析
2020/09/10 Python
HTML5视频播放插件 video.js介绍
2018/09/29 HTML / CSS
美国领先的户外服装与装备用品店:Moosejaw
2016/08/25 全球购物
Carolina工作鞋官网:Carolina Footwear
2019/03/14 全球购物
Engel & Bengel官网:婴儿推车、儿童房家具和婴儿设备
2019/12/28 全球购物
促销活动方案模板
2014/02/24 职场文书
计算机专业毕业生求职信
2014/04/30 职场文书
2015中学学校工作总结
2015/07/20 职场文书
社会心理学学习心得体会
2016/01/22 职场文书