Demo Address: http://demo.jb51.net/js/2011/hunt/index.htm
Play down look
Please look at the letter O on the address bar! You use O to shoot a. Use the left and right arrows on the keyboard to move the letter O. When O moves to a, press the SPACEBAR to shoot! The game will be timed for 30 seconds and press ESC to start again.
Note: Please use the system's own IE browser to open this link.
You use O to shoot
a . Use the
left and
right arrows on the keyboard to move the letter O. When O moves to
a , press SPACEBAR to
shoot!
Core code:
Copy Code code as follows:
(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 = "in" + (This.elapsedtime ()) + "seconds" you have hit "+ This.points +" A! (Press ESC to start again) ";
};
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);