您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/6/29 16:46:27发布17次查看
演示地址:http://demo.jb51.net/js/2011/hunt/index.htm
玩法向下看
请看地址栏上的字母 o! 你使用o来向 a射击。 使用键盘上的 左箭头 和 右箭头 移动字母o. 当o移动到 a 上时,按 空格键射击! 游戏会定时30秒时间,按esc键重新开始。
注:请使用系统自带的ie浏览器来打开本链接。
你使用o来向 a射击。 使用键盘上的 左箭头 和 右箭头 移动字母o. 当o移动到 a 上时,按 空格键射击!
核心代码:
复制代码 代码如下:
(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 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 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 animal = _ref[_i];
animal.update(this.levelsize);
}
while (url.length 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 return this.gameover();
} else {
if (timeleft 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 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 return this.position += levelsize;
}
};
return animal;
})();
$(function() {
var game;
game = new game();
return $(document).keydown(game.eventreceived);
});
}).call(this);
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product