基于jquery的地址欄射擊游戲代碼
玩法向下看
請(qǐng)看地址欄上的字母 O! 你使用O來(lái)向 a射擊。 使用鍵盤(pán)上的 左箭頭 和 右箭頭 移動(dòng)字母O. 當(dāng)O移動(dòng)到 a 上時(shí),按 空格鍵射擊! 游戲會(huì)定時(shí)30秒時(shí)間,按ESC鍵重新開(kāi)始。
注:請(qǐng)使用系統(tǒng)自帶的IE瀏覽器來(lái)打開(kā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鍵重新開(kāi)始)";
};
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編寫(xiě)藍(lán)色拼圖小游戲
- 基于Vue.js實(shí)現(xiàn)數(shù)字拼圖游戲
- JQuery開(kāi)發(fā)的數(shù)獨(dú)游戲代碼
- 分享20款好玩的jQuery游戲
- jQuery編寫(xiě)網(wǎng)頁(yè)版2048小游戲
- jQuery實(shí)現(xiàn)的五子棋游戲?qū)嵗?/a>
- jQuery實(shí)現(xiàn)簡(jiǎn)易的天天愛(ài)消除小游戲
- jQuery+vue.js實(shí)現(xiàn)的九宮格拼圖游戲完整實(shí)例【附源碼下載】
相關(guān)文章
基于jQuery實(shí)現(xiàn)選項(xiàng)卡效果
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)選項(xiàng)卡效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
jQuery選擇器源碼解讀(四):tokenize方法的Expr.preFilter
這篇文章主要介紹了jQuery選擇器源碼解讀(四):tokenize方法的Expr.preFilter,本文用詳細(xì)的注釋解讀了tokenize方法的Expr.preFilter的實(shí)現(xiàn)源碼,需要的朋友可以參考下2015-03-03
jquery購(gòu)物車(chē)結(jié)算功能實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了jquery購(gòu)物車(chē)結(jié)算功能的實(shí)現(xiàn)方法,購(gòu)買(mǎi)多個(gè)商品進(jìn)行統(tǒng)一結(jié)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
jQuery檢測(cè)返回值的數(shù)據(jù)類(lèi)型
本文給大家介紹的是一種檢測(cè)返回值的數(shù)據(jù)類(lèi)型的方法,有需要的小伙伴可以參考下。2015-07-07
一個(gè)基于jQuery的樹(shù)型插件(OrangeTree)使用介紹
這個(gè)樣式大家就將就一下吧!我將這個(gè)無(wú)限級(jí)樹(shù)型插件命名為桔子樹(shù),需要的朋友可以參考下2012-05-05
jQuery EasyUI編輯DataGrid用combobox實(shí)現(xiàn)多級(jí)聯(lián)動(dòng)
本文給大家分享jQuery EasyUI編輯DataGrid用combobox實(shí)現(xiàn)多級(jí)聯(lián)動(dòng)效果的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-08-08
一個(gè)小例子解釋如何來(lái)阻止Jquery事件冒泡
事件會(huì)向這個(gè)對(duì)象的父級(jí)對(duì)象傳播,從里到外,直至它被處理(父級(jí)對(duì)象所有同類(lèi)事件都將被激活),或者它到達(dá)了對(duì)象層次的最頂層2014-07-07
基于jQuery實(shí)現(xiàn)仿淘寶套餐選擇插件
本文給大家介紹的是一款基于jQuery實(shí)現(xiàn)放淘寶套餐選擇的插件,主要是基于本地json數(shù)據(jù)的選擇列創(chuàng)建,有需要的小伙伴參考下。2015-03-03
jQuery檢測(cè)鼠標(biāo)左鍵和右鍵點(diǎn)擊的方法
這篇文章主要介紹了jQuery檢測(cè)鼠標(biāo)左鍵和右鍵點(diǎn)擊的方法,涉及jQuery操作鼠標(biāo)事件的技巧,且針對(duì)IE瀏覽器具備良好的兼容性,需要的朋友可以參考下2015-03-03
jquery 刪除節(jié)點(diǎn) 添加節(jié)點(diǎn) 找兄弟節(jié)點(diǎn)的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇jquery 刪除節(jié)點(diǎn) 添加節(jié)點(diǎn) 找兄弟節(jié)點(diǎn)的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12

