>  기사  >  웹 프론트엔드  >  jquery_jquery를 기반으로 한 주소 표시줄 슈팅 게임 코드

jquery_jquery를 기반으로 한 주소 표시줄 슈팅 게임 코드

WBOY
WBOY원래의
2016-05-16 18:09:461068검색

데모 주소:http://demo.jb51.net/js/2011/hunt/index.htm

게임 플레이를 살펴보세요
를 참조하세요. 바에 있는 문자 O를 사용하여 a를 쏘세요. 키보드의 왼쪽 및 오른쪽 화살표를 사용하여 문자 O를 이동하세요. O가 a 위로 이동하면 스페이스바를 눌러 쏘세요. 게임은 30초 동안 시간 초과되며, 다시 시작하려면 ESC 키를 누르세요.
참고: 이 링크를 열려면 시스템과 함께 제공되는 IE 브라우저를 사용하십시오.

jquery_jquery를 기반으로 한 주소 표시줄 슈팅 게임 코드

O를 사용하여 a를 촬영합니다. 키보드의 왼쪽 화살표오른쪽 화살표를 사용하여 문자 O를 이동합니다. O가 a 위로 이동하면 스페이스바를 누릅니다.
jquery_jquery를 기반으로 한 주소 표시줄 슈팅 게임 코드

코드 복사 코드는 다음과 같습니다.

(function() {
var Animal, Game;
var __bind = function(fn, me){ return function(){ return fn.apply(me, 인수); }; } ;
Game = (function() {
function Game() {
this.eventReceived = __bind(this.eventReceived, this);;
this.update = __bind(this.update, this );; this.level = 1;
this.playerLocation = this.levelSize / 2;
}
Game.prototype .start = function() {
var num;
this.points = 0;
this.startTime = new Date;
this.timeLimit = [] ;
for (num = 4; num >= 1; num--) {
this.addAnimal()
}
return this.interval(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 = new Animal(Math.floor(Math.random() * this.levelSize));
return this.animals.push(animal);
};
Game.prototype.removeAnimal = function(deadAnimal) {
var 동물;
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)
}
}
return _results
}).call(this);
};
Game.prototype.isAnimalAt = function(position) {
var 동물, 일치;
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);
일치 항목 반환[0];
};
Game.prototype.update = function() {
var 동물, 위치, 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 < 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 = "포인트 " this.points;
}
};
Game.prototype.eventReceived = function(event) {
var 동물;
스위치(event.which) {
case 37:
this.playerLocation -= 1;
if (this.playerLocation < 0) {
return this.playerLocation = this.levelSize - 1;
}
휴식;
사례 39:
this.playerLocation = 1;
return this.playerLocation %= this.levelSize;
사례 38:
사례 32:
animal = this.isAnimalAt(this.playerLocation);
if (동물) {
this.points = 1;
this.removeAnimal(동물);
console.log(this.animals.length);
if (this.animals.length === 0) {
return this.gameOver();
}
}
휴식;
사례 27:
return this.start();
}
};
귀환 게임;
})();
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);


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.