演示地址:http://demo.jb51.net/js/mouse/index.html
打包下载地址 http://www.jb51.net/jiaoben/32434.html
这个是我无聊的时候写的,先看看效果(UI做得比较丑):
说明:红色的点击得分100,蓝色的点击扣分100.
只是想用js来写个小游戏,顺便练练js的代码。
先看html部分:
html
说明:红色的点击得分100,蓝色的点击扣分100.
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 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 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 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还是可以做小游戏的。。。欢迎拍砖!

C 和JavaScript通过WebAssembly实现互操作性。1)C 代码编译成WebAssembly模块,引入到JavaScript环境中,增强计算能力。2)在游戏开发中,C 处理物理引擎和图形渲染,JavaScript负责游戏逻辑和用户界面。

JavaScript在网站、移动应用、桌面应用和服务器端编程中均有广泛应用。1)在网站开发中,JavaScript与HTML、CSS一起操作DOM,实现动态效果,并支持如jQuery、React等框架。2)通过ReactNative和Ionic,JavaScript用于开发跨平台移动应用。3)Electron框架使JavaScript能构建桌面应用。4)Node.js让JavaScript在服务器端运行,支持高并发请求。

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

Dreamweaver CS6
视觉化网页开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)