基于jquery的地址欄射擊游戲代碼
更新時(shí)間:2011年03月10日 12:07:37 作者:
地址欄射擊游戲!對(duì),你沒(méi)看錯(cuò),就是在地址欄上玩的游戲,有圖有真相!之前在網(wǎng)上看到這個(gè)小游戲,可惜在墻外,中午研究了一下,搬到國(guó)內(nèi)來(lái)了,看看吧!
演示地址:http://demo.jb51.net/js/2011/hunt/index.htm
玩法向下看
請(qǐng)看地址欄上的字母 O! 你使用O來(lái)向 a射擊。 使用鍵盤上的 左箭頭 和 右箭頭 移動(dòng)字母O. 當(dāng)O移動(dòng)到 a 上時(shí),按 空格鍵射擊! 游戲會(huì)定時(shí)30秒時(shí)間,按ESC鍵重新開始。
注:請(qǐng)使用系統(tǒng)自帶的IE瀏覽器來(lái)打開本鏈接。

核心代碼:
(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 + "個(gè)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);
玩法向下看
請(qǐng)看地址欄上的字母 O! 你使用O來(lái)向 a射擊。 使用鍵盤上的 左箭頭 和 右箭頭 移動(dòng)字母O. 當(dāng)O移動(dòng)到 a 上時(shí),按 空格鍵射擊! 游戲會(huì)定時(shí)30秒時(shí)間,按ESC鍵重新開始。
注:請(qǐng)使用系統(tǒng)自帶的IE瀏覽器來(lái)打開本鏈接。
你使用O來(lái)向 a射擊。 使用鍵盤上的 左箭頭 和 右箭頭 移動(dòng)字母O. 當(dāng)O移動(dòng)到 a 上時(shí),按 空格鍵射擊!

核心代碼:
復(fù)制代碼 代碼如下:
(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 + "個(gè)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);
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)拼圖小游戲(實(shí)例講解)
- jquery實(shí)現(xiàn)的美女拼圖游戲?qū)嵗?/a>
- jQuery制作可自定義大小的拼圖游戲
- jQuery制作拼圖小游戲
- 使用vue.js編寫藍(lán)色拼圖小游戲
- 基于Vue.js實(shí)現(xiàn)數(shù)字拼圖游戲
- JQuery開發(fā)的數(shù)獨(dú)游戲代碼
- 分享20款好玩的jQuery游戲
- jQuery編寫網(wǎng)頁(yè)版2048小游戲
- jQuery實(shí)現(xiàn)的五子棋游戲?qū)嵗?/a>
- jQuery實(shí)現(xiàn)簡(jiǎn)易的天天愛消除小游戲
- jQuery+vue.js實(shí)現(xiàn)的九宮格拼圖游戲完整實(shí)例【附源碼下載】
相關(guān)文章
jquery追加元素的所有方法全面深入實(shí)例講解(append、prepend、after、before、wrap等等)
幾乎所有網(wǎng)站的開發(fā)都離不開jQuery,jQuery可以很方便的實(shí)現(xiàn)網(wǎng)頁(yè)中的一些效果,也很輕松的對(duì)網(wǎng)頁(yè)中的一些DIV元素進(jìn)行添加,修改或刪除的操作。由于不同位置追加元素的代碼不一樣,本文實(shí)例講解如何對(duì)網(wǎng)頁(yè)中某個(gè)指定的DIV進(jìn)行元素的追加操作。2023-03-03基于jQuery替換table中的內(nèi)容并顯示進(jìn)度條的代碼
這個(gè)例子使我更加明白呈現(xiàn)數(shù)據(jù)是前端工作滴一部分,如何使table中的數(shù)值變?yōu)榍逦貤l狀圖呢?聽我細(xì)細(xì)道來(lái)2011-08-08簡(jiǎn)單實(shí)現(xiàn)jQuery彈窗效果
這篇文章主要教大家簡(jiǎn)單實(shí)現(xiàn)jQuery彈窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10uploadify多文件上傳參數(shù)設(shè)置技巧
uploadify插件配置實(shí)用比較簡(jiǎn)單,很多開發(fā)者都喜歡使用。但是它有個(gè)缺點(diǎn)就是剛加載的時(shí)候稍微慢了一秒左右,本文通過(guò)一段代碼實(shí)例給大家介紹uploadify多文件上傳參數(shù)設(shè)置技巧,朋友們一起學(xué)習(xí)吧2015-11-11NiftyCube——輕松實(shí)現(xiàn)圓角邊框
這篇文章主要介紹了NiftyCube——輕松實(shí)現(xiàn)圓角邊框2007-02-02