Advertisement
Guest User

Untitled

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