-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.min.js
10 lines (10 loc) · 4.79 KB
/
snake.min.js
1
2
3
4
5
6
7
8
9
10
/*!
* SnakeJS UWC v0.1
* https://github.com/pavel-lyapin/snake-js
*
* Requires jQuery (http://jquery.com), Underscore.js (http://underscorejs.org)
*
* Released under the MIT license
* Date: Sat Nov 24 2012
*/
(function(c,d,b){var a=a||{};a.getBlock=function(e,f){return d("div.game-block.col"+parseInt(e,10)+".row"+parseInt(f,10))};a.getBlockByObject=function(e){return d("div.game-block.col"+e.x+".row"+e.y)};a.clear=function(e){var f=d("div.game-block");f.removeClass("snake-here").removeClass("snake-head").removeClass("down").removeClass("up").removeClass("right").removeClass("left");if(e){f.removeClass("food-here")}};a.bindKeyboard=function(){var e=this;d(document).keydown(function(h){var g=h.keyCode||h.which,f={37:"LEFT",38:"UP",39:"RIGHT",40:"DOWN"};if(!(f[g+2]===e.direction||f[g-2]===e.direction)){e.direction=f[g]}})};a.generateField=function(h){var f=0,e=0;var g="";for(;f<this.settings.cols*this.settings.rows;f++){if(f>0&&f%this.settings.cols===0){e++;g+='<div class="game-block col'+f%this.settings.cols+" row"+e+'" style="clear: both" />'}else{g+='<div class="game-block col'+f%this.settings.cols+" row"+e+'" />'}}h.html(g)};a.generateFood=function(g){var h=Math.floor((Math.random()*g)+1),f;for(f=0;f<h;f++){var j=Math.floor((Math.random()*this.settings.rows));var e=Math.floor((Math.random()*this.settings.cols));if(!this._inSnake({x:e,y:j})){this.getBlock(e,j).addClass("food-here")}else{f--}}};a.initSnake=function(){var e=Math.round(this.settings.cols/2),f;this.snake=[];for(f=-this.settings.snakeLength;f<0;f++){this.snake.push({x:e,y:f})}};a.paintSnake=function(){for(var e in this.snake){if(this.snake.hasOwnProperty(e)){this.getBlock(this.snake[e].x,this.snake[e].y).addClass("snake-here")}}this.getBlock(this.snake[e].x,this.snake[e].y).addClass(this.direction.toLowerCase()).addClass("snake-head")};a.timerTick=function(){this.time++;var e=Math.floor(this.time/60);var f=this.time%60;d(".game-timer").text((e<10?"0":"")+e+":"+(f<10?"0":"")+f)};a.levelUp=function(){var e=d(".game-level");e.text(parseInt(e.text(),10)+1);if(!d(".food-here").length){this.generateFood(this.settings.maxFood)}this.accelerate()};a.accelerate=function(){clearInterval(this.intervalPlay);if(this.currentSpeed>this.settings.maxSpeed){this.currentSpeed-=this.settings.acceleration}var e=this;this.intervalPlay=setInterval(function(){e.move()},e.currentSpeed)};a._inSnake=function(e){return b.find(this.snake,function(f){return(f.x===e.x&&f.y===e.y)})};a.move=function(){this.clear();var h=a.snake[a.snake.length-1];var f={x:h.x,y:h.y};switch(this.direction){case"DOWN":f.y++;break;case"UP":f.y--;break;case"LEFT":f.x--;break;case"RIGHT":f.x++;break}var g=this._inSnake(f);if(f.x<0||f.x>=this.settings.cols||f.y<0||f.y>=this.settings.rows||g){this.lose();return}var e=this.getBlockByObject(f);if(e.hasClass("food-here")){e.removeClass("food-here");this.levelUp()}else{this.snake.shift()}this.snake.push(f);this.paintSnake()};a.lose=function(){clearInterval(this.intervalPlay);clearInterval(this.intervalTimer);d(".game-alert").fadeIn().find(".close").click(function(){d(this).parent().fadeOut()});this.addResult();this.reset()};a.addResult=function(){var e=d(".game-best-results tbody");e.append("<tr><td>"+d("#game-name").val()+"</td><td>"+d(".game-level").text()+"</td><td>"+d(".game-timer").text()+"</td></tr>");localStorage.results=e.html()};a.loadResults=function(){var e=d(".game-best-results tbody");if(localStorage.results){e.html(localStorage.results)}else{e.html("")}};a.reset=function(){this.clear("full");clearInterval(this.intervalPlay);clearInterval(this.intervalTimer);this.time=0;this.paused=false;this.currentSpeed=this.settings.speed;this.direction="DOWN";d(".game-timer").text("00:00");d(".game-level").text("0")};a.pause=function(){var e=this;if(this.paused===false){clearInterval(this.intervalPlay);clearInterval(this.intervalTimer);this.paused=true}else{e.intervalTimer=setInterval(function(){e.timerTick()},1000);e.intervalPlay=setInterval(function(){e.move()},e.currentSpeed);this.paused=false}};a.init=function(e){this.settings=d.extend({snakeLength:6,speed:500,acceleration:20,maxSpeed:200,cols:20,rows:13,maxFood:5},e);this.generateField(d(".game-field"));this.bindKeyboard();this.loadResults();var f=this;d(".game-start").click(function(){var g=d("#game-name");if(!g.val()){g.parent().addClass("error");return}g.parent().removeClass("error");f.reset();f.initSnake();f.generateFood(f.settings.maxFood);f.intervalTimer=setInterval(function(){f.timerTick()},1000);f.intervalPlay=setInterval(function(){f.move()},f.settings.speed)});d(".game-pause").click(function(){f.pause();if(d(this).hasClass("btn-info")){d(this).removeClass("btn-info").addClass("btn-warning").text("Resume")}else{d(this).removeClass("btn-warning").addClass("btn-info").text("Pause")}});d(".game-history-clear").click(function(){localStorage.clear();f.loadResults()})};c.SnakeJS=a})(window,jQuery,_);