Jquery-based address bar shooting game code

Source: Internet
Author: User

Demo address: http://demo.jb51.net/js/2011/hunt/index.htm

Look down
See the letter "O" in the address bar! You use O to fire. Use the left and right arrows on the keyboard to move the letter O. When O moves to a, press the Space key to shoot! The game will be scheduled for 30 seconds, and Press ESC to start again.
Note: Use the IE browser that comes with the system to open this link.

You use O AShooting. Use Left arrowAnd Right arrowMove letter O. When O moves AWhen, press Space keyShooting!


Core code:Copy codeThe Code is 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 shot" + this. points + "! (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 ("");
} 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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.