Advertisement
Guest User

Untitled

a guest
Aug 13th, 2013
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function(){
  2.     //Canvas stuff
  3.     var canvas = $("#canvas")[0];
  4.     var ctx = canvas.getContext("2d");
  5.     var w = $("#canvas").width();
  6.     var h = $("#canvas").height();
  7.  
  8.     //Lets save the cell width in a variable for easy control
  9.     var cw = 10;
  10.     var d;
  11.     var nd;
  12.     var food;
  13.     var score;
  14.    
  15.     //Lets create the snake now
  16.     var snake_array; //an array of cells to make up the snake
  17.    
  18.     function init()
  19.     {
  20.         d = "right"; //default direction
  21.         nd = [];
  22.         create_snake();
  23.         create_food(); //Now we can see the food particle
  24.         //finally lets display the score
  25.         score = 0;
  26.        
  27.         //Lets move the snake now using a timer which will trigger the paint function
  28.         //every 60ms
  29.         if(typeof game_loop != "undefined") clearInterval(game_loop);
  30.         game_loop = setInterval(paint, 60);
  31.     }
  32.     init();
  33.    
  34.     function create_snake()
  35.     {
  36.         var length = 5; //Length of the snake
  37.         snake_array = []; //Empty array to start with
  38.         for(var i = length-1; i>=0; i--)
  39.         {
  40.             //This will create a horizontal snake starting from the top left
  41.             snake_array.push({x: i, y:0});
  42.         }
  43.     }
  44.    
  45.     //Lets create the food now
  46.     function create_food()
  47.     {
  48.         food = {
  49.             x: Math.round(Math.random()*(w-cw)/cw),
  50.             y: Math.round(Math.random()*(h-cw)/cw),
  51.         };
  52.         //This will create a cell with x/y between 0-44
  53.         //Because there are 45(450/10) positions accross the rows and columns
  54.     }
  55.    
  56.     //Lets paint the snake now
  57.     function paint()
  58.     {
  59.         if (nd.length) {
  60.             d = nd.shift();
  61.         }
  62.         //To avoid the snake trail we need to paint the BG on every frame
  63.         //Lets paint the canvas now
  64.         ctx.fillStyle = "white";
  65.         ctx.fillRect(0, 0, w, h);
  66.         ctx.strokeStyle = "black";
  67.         ctx.strokeRect(0, 0, w, h);
  68.        
  69.         //The movement code for the snake to come here.
  70.         //The logic is simple
  71.         //Pop out the tail cell and place it infront of the head cell
  72.         var nx = snake_array[0].x;
  73.         var ny = snake_array[0].y;
  74.         //These were the position of the head cell.
  75.         //We will increment it to get the new head position
  76.         //Lets add proper direction based movement now
  77.         if(d == "right") nx++;
  78.         else if(d == "left") nx--;
  79.         else if(d == "up") ny--;
  80.         else if(d == "down") ny++;
  81.        
  82.         //Lets add the game over clauses now
  83.         //This will restart the game if the snake hits the wall
  84.         //Lets add the code for body collision
  85.         //Now if the head of the snake bumps into its body, the game will restart
  86.         if(nx == -1 || nx == w/cw || ny == -1 || ny == h/cw || check_collision(nx, ny, snake_array))
  87.         {
  88.             //restart game
  89.             init();
  90.             //Lets organize the code a bit now.
  91.             return;
  92.         }
  93.        
  94.         //Lets write the code to make the snake eat the food
  95.         //The logic is simple
  96.         //If the new head position matches with that of the food,
  97.         //Create a new head instead of moving the tail
  98.         if(nx == food.x && ny == food.y)
  99.         {
  100.             var tail = {x: nx, y: ny};
  101.             score++;
  102.             //Create new food
  103.             create_food();
  104.         }
  105.         else
  106.         {
  107.             var tail = snake_array.pop(); //pops out the last cell
  108.             tail.x = nx; tail.y = ny;
  109.         }
  110.         //The snake can now eat the food.
  111.        
  112.         snake_array.unshift(tail); //puts back the tail as the first cell
  113.        
  114.         for(var i = 0; i < snake_array.length; i++)
  115.         {
  116.             var c = snake_array[i];
  117.             //Lets paint 10px wide cells
  118.             paint_cell(c.x, c.y);
  119.         }
  120.        
  121.         //Lets paint the food
  122.         paint_cell(food.x, food.y);
  123.         //Lets paint the score
  124.         var score_text = "Score: " + score;
  125.         ctx.fillText(score_text, 5, h-5);
  126.     }
  127.    
  128.     //Lets first create a generic function to paint cells
  129.     function paint_cell(x, y)
  130.     {
  131.         ctx.fillStyle = "blue";
  132.         ctx.fillRect(x*cw, y*cw, cw, cw);
  133.         ctx.strokeStyle = "white";
  134.         ctx.strokeRect(x*cw, y*cw, cw, cw);
  135.     }
  136.    
  137.     function check_collision(x, y, array)
  138.     {
  139.         //This function will check if the provided x/y coordinates exist
  140.         //in an array of cells or not
  141.         for(var i = 0; i < array.length; i++)
  142.         {
  143.             if(array[i].x == x && array[i].y == y)
  144.              return true;
  145.         }
  146.         return false;
  147.     }
  148.    
  149.     //Lets add the keyboard controls now
  150.     $(document).keydown(function(e){
  151.         var key = e.which;
  152.         var td;
  153.         if (nd.length) {
  154.             var td = nd[nd.length - 1];
  155.         } else {
  156.             td = d;
  157.         }
  158.         //We will add another clause to prevent reverse gear
  159.         if(key == "37" && td != "right") nd.push("left");
  160.         else if(key == "38" && td != "down") nd.push("up");
  161.         else if(key == "39" && td != "left") nd.push("right");
  162.         else if(key == "40" && td != "up") nd.push("down");
  163.         //The snake is now keyboard controllable
  164.     })
  165.    
  166.    
  167.    
  168.    
  169.    
  170.    
  171.    
  172. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement