基于jquery的地址栏射击游戏代码


Posted in Javascript onMarch 10, 2011

演示地址:http://demo.3water.com/js/2011/hunt/index.htm

玩法向下看
请看地址栏上的字母 O! 你使用O来向 a射击。 使用键盘上的 左箭头 和 右箭头 移动字母O. 当O移动到 a 上时,按 空格键射击! 游戏会定时30秒时间,按ESC键重新开始。
注:请使用系统自带的IE浏览器来打开本链接。

基于jquery的地址栏射击游戏代码

你使用O来向 a射击。 使用键盘上的 左箭头 和 右箭头 移动字母O. 当O移动到 a 上时,按 空格键射击! 
基于jquery的地址栏射击游戏代码
核心代码:
(function() { 
var Animal, Game; 
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; 
Game = (function() { 
function Game() { 
this.eventReceived = __bind(this.eventReceived, this);; 
this.update = __bind(this.update, this);; this.level = 1; 
this.levelSize = 60; 
this.playerLocation = this.levelSize / 2; 
this.start(); 
} 
Game.prototype.start = function() { 
var num; 
this.points = 0; 
this.startTime = new Date; 
this.timeLimit = 30; 
this.animals = []; 
for (num = 4; num >= 1; num--) { 
this.addAnimal(); 
} 
return this.interval = setInterval(this.update, 1000 / 30); 
}; 
Game.prototype.gameOver = function() { 
clearInterval(this.interval); 
return location.hash = "在" + (this.elapsedTime()) + "秒中你共射中了" + this.points + "个a! (按ESC键重新开始)"; 
}; 
Game.prototype.elapsedTime = function() { 
return Math.floor(((new Date).getTime() - this.startTime.getTime()) / 1000); 
}; 
Game.prototype.addAnimal = function() { 
var animal; 
animal = new Animal(Math.floor(Math.random() * this.levelSize)); 
return this.animals.push(animal); 
}; 
Game.prototype.removeAnimal = function(deadAnimal) { 
var animal; 
return this.animals = (function() { 
var _i, _len, _ref, _results; 
_ref = this.animals; 
_results = []; 
for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
animal = _ref[_i]; 
if (animal !== deadAnimal) { 
_results.push(animal); 
} 
} 
return _results; 
}).call(this); 
}; 
Game.prototype.isAnimalAt = function(position) { 
var animal, matches; 
matches = (function() { 
var _i, _len, _ref, _results; 
_ref = this.animals; 
_results = []; 
for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
animal = _ref[_i]; 
if (Math.floor(animal.position) === position) { 
_results.push(animal); 
} 
} 
return _results; 
}).call(this); 
return matches[0]; 
}; 
Game.prototype.update = function() { 
var animal, position, timeLeft, url, _i, _len, _ref; 
url = []; 
_ref = this.animals; 
for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
animal = _ref[_i]; 
animal.update(this.levelSize); 
} 
while (url.length < this.levelSize) { 
position = url.length; 
if (position === this.playerLocation) { 
if (this.isAnimalAt(this.playerLocation)) { 
url.push("@"); 
} else { 
url.push("O"); 
} 
} else if (this.isAnimalAt(position)) { 
url.push("a"); 
} else { 
url.push("-"); 
} 
} 
timeLeft = this.timeLimit - this.elapsedTime(); 
if (timeLeft <= 0) { 
return this.gameOver(); 
} else { 
if (timeLeft < 10) { 
timeLeft = "0" + timeLeft; 
} 
location.hash = (" " + timeLeft + "|") + url.join("") + ("|" + timeLeft); 
return document.title = "Points " + this.points; 
} 
}; 
Game.prototype.eventReceived = function(event) { 
var animal; 
switch (event.which) { 
case 37: 
this.playerLocation -= 1; 
if (this.playerLocation < 0) { 
return this.playerLocation = this.levelSize - 1; 
} 
break; 
case 39: 
this.playerLocation += 1; 
return this.playerLocation %= this.levelSize; 
case 38: 
case 32: 
animal = this.isAnimalAt(this.playerLocation); 
if (animal) { 
this.points += 1; 
this.removeAnimal(animal); 
console.log(this.animals.length); 
if (this.animals.length === 0) { 
return this.gameOver(); 
} 
} 
break; 
case 27: 
return this.start(); 
} 
}; 
return Game; 
})(); 
Animal = (function() { 
function Animal(position) { 
this.position = position; 
this.velocityChange = Math.random() * 0.5; 
this.velocityIndex = Math.random() * Math.PI; 
this.dampener = 0.4; 
} 
Animal.prototype.update = function(levelSize) { 
this.velocityIndex += Math.random() * this.velocityChange; 
this.position += Math.sin(this.velocityIndex) * this.dampener; 
this.position %= levelSize; 
if (this.position < 0) { 
return this.position += levelSize; 
} 
}; 
return Animal; 
})(); 
$(function() { 
var game; 
game = new Game(); 
return $(document).keydown(game.eventReceived); 
}); 
}).call(this);
Javascript 相关文章推荐
动态加载iframe
Jun 16 Javascript
JavaScript iframe的相互操作浅析
Oct 14 Javascript
location对象的属性和方法应用(解析URL)
Apr 12 Javascript
jquery操作下拉列表、文本框、复选框、单选框集合(收藏)
Jan 08 Javascript
js中数组结合字符串实现查找(屏蔽广告判断url等)
Mar 30 Javascript
Jquery插件仿百度搜索关键字自动匹配功能
May 11 Javascript
jQuery无缝轮播图代码
Dec 22 Javascript
详解angularJS自定义指令间的相互交互
Jul 05 Javascript
react同构实践之实现自己的同构模板
Mar 13 Javascript
浅谈对于“不用setInterval,用setTimeout”的理解
Aug 28 Javascript
js中switch语句的学习笔记
Mar 25 Javascript
javascript实现时钟动画
Dec 03 Javascript
基于jquery的无缝循环新闻列表插件
Mar 07 #Javascript
JavaScript对象之间的转换 jQuery对象和原声DOM
Mar 07 #Javascript
jQuery总体架构的理解分析
Mar 07 #Javascript
关于捕获用户何时点击window.onbeforeunload的取消事件
Mar 06 #Javascript
js中将具有数字属性名的对象转换为数组
Mar 06 #Javascript
js 优化次数过多的循环 考虑到性能问题
Mar 05 #Javascript
淘宝搜索框效果实现分析
Mar 05 #Javascript
You might like
几个有用的php字符串过滤,转换函数代码
2012/05/01 PHP
[原创]解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载)
2016/06/22 PHP
获取JavaScript用户自定义类的类名称的代码
2007/03/08 Javascript
JS 获取span标签中的值的代码 支持ie与firefox
2009/08/24 Javascript
JQuery 将元素显示在屏幕的中央的代码
2010/02/27 Javascript
初窥JQuery(一)jquery选择符 必备知识点
2010/11/25 Javascript
javascript标签在页面中的位置探讨
2013/04/11 Javascript
js+csss实现的一个带复选框的下拉框
2014/09/29 Javascript
node.js中的console.assert方法使用说明
2014/12/10 Javascript
JQuery中的事件及动画用法实例
2015/01/26 Javascript
javascript禁止访客复制网页内容的实现代码
2015/08/05 Javascript
jQuery each函数源码分析
2016/05/25 Javascript
angularjs封装$http为factory的方法
2017/05/18 Javascript
微信小程序左滑删除功能开发案例详解
2018/11/12 Javascript
JS实现二维数组元素的排列组合运算简单示例
2019/01/28 Javascript
详解vue中移动端自适应方案
2019/05/05 Javascript
JS数组扁平化、去重、排序操作实例详解
2020/02/24 Javascript
[46:10]2014 DOTA2国际邀请赛中国区预选赛 CnB VS HGT
2014/05/21 DOTA
Windows下安装python MySQLdb遇到的问题及解决方法
2017/03/16 Python
Python3 适合初学者学习的银行账户登录系统实例
2017/08/08 Python
R vs. Python 数据分析中谁与争锋?
2017/10/18 Python
基于Python List的赋值方法
2018/06/23 Python
django 外键model的互相读取方法
2018/12/15 Python
Flask框架踩坑之ajax跨域请求实现
2019/02/22 Python
Python实现个人微信号自动监控告警的示例
2019/07/03 Python
Django如何使用jwt获取用户信息
2020/04/21 Python
Python如何实现后端自定义认证并实现多条件登陆
2020/06/22 Python
一级方程式赛车官方网上商店:F1 Store(支持中文)
2018/01/12 全球购物
商场拾金不昧表扬信
2014/01/13 职场文书
移交协议书
2014/08/19 职场文书
2014年园林绿化工作总结
2014/12/11 职场文书
六一领导慰问欢迎词
2015/01/26 职场文书
借款民事起诉状范文
2015/05/19 职场文书
2016七夕情人节广告语
2016/01/28 职场文书
2016年先进班集体事迹材料
2016/02/26 职场文书
详解Python牛顿插值法
2021/05/11 Python