Advertisement
Guest User

Snake code

a guest
Jul 16th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. //The direction the snake is traveling
  2. var current_direction = 'right';
  3.  
  4. //The snake array
  5. var snake = [[10,4], [10,3], [10,2], [10,1]];
  6.  
  7. //The score
  8. var score = 0;
  9.  
  10. //Is the game currently playing
  11. var active = true;
  12.  
  13. var t = null;
  14. var speed = 250;
  15.  
  16. function move_snake()
  17. {
  18.     for(var i = snake.length - 1; i >= 0; i--)
  19.     {  
  20.         //Handle the head
  21.         if(i == 0)
  22.         {
  23.             if(current_direction == 'up')
  24.             {
  25.                 snake[0][0]--;
  26.                 //current_direction = 'up';
  27.             }
  28.             else if(current_direction == 'down')
  29.             {
  30.                 snake[0][0]++;
  31.                 //current_direction = 'down';
  32.             }
  33.             else if(current_direction == 'left')
  34.             {
  35.                 snake[0][1]--;
  36.                 //current_direction = 'left';
  37.             }
  38.             else if(current_direction == 'right')
  39.             {
  40.                 snake[0][1]++;
  41.                 //current_direction = 'right';
  42.             }
  43.            
  44.             //Make sure we're within the boundaries
  45.             if(snake[0][1] < 0 ||
  46.                snake[0][1] > 24 ||
  47.                snake[0][0] < 0 ||
  48.                snake[0][0] > 14)
  49.             {
  50.                 active = false;
  51.                 alert('You died with a score of ' + score);
  52.                 return;
  53.             }
  54.  
  55.             //Check if we ate a piece of food
  56.             if($('#c' + snake[0][0] + '-' + snake[0][1]).find('img').attr('src').indexOf('food') != -1)
  57.             {
  58.                 generate_food(snake[0][0], snake[0][1]);
  59.                 snake.push([snake[snake.length - 1][0], snake[snake.length - 1][1]]);
  60.                 score += 10;
  61.                 $('#score').html('Score: ' + score);
  62.             }
  63.            
  64.             //Check if we hit ourselves
  65.             if($('#c' + snake[0][0] + '-' + snake[0][1]).find('img').attr('src').indexOf('snake') != -1)
  66.             {
  67.                 active = false;
  68.                 alert('You died with a score of ' + score);
  69.                 return;
  70.             }
  71.            
  72.             //Move the head
  73.             $('#c' + snake[i][0] + '-' + snake[i][1]).find('img').attr('src', 'images/snake.gif');
  74.         }
  75.         else
  76.         {
  77.             if(i == (snake.length - 1))
  78.             {
  79.                 $('#c' + snake[i][0] + '-' + snake[i][1]).find('img').attr('src', 'images/empty.gif');
  80.             }
  81.            
  82.             snake[i][0] = snake[i - 1][0];
  83.             snake[i][1] = snake[i - 1][1];
  84.         }
  85.     }
  86.    
  87.     if(active)
  88.     {
  89.         t = setTimeout(move_snake, speed);
  90.     }
  91. }
  92.  
  93. function generate_food(x, y)
  94. {
  95.     var food_x = Math.round(Math.random() * 14);
  96.     var food_y = Math.round(Math.random() * 24);
  97.    
  98.     //Ensure we don't generate a piece of food on the snake or the same place as the food that was just eaten
  99.     while($('#c' + food_x + '-' + food_y).find('img').attr('src').indexOf('snake') != -1 || (food_x == x && food_y == y))
  100.     {
  101.         food_x = Math.round(Math.random() * 14);
  102.         food_y = Math.round(Math.random() * 24);
  103.     }
  104.    
  105.     $('#c' + food_x + '-' + food_y).find('img').attr('src', 'images/food.gif');
  106. }
  107.  
  108. function setup()
  109. {
  110.     $(window).keydown(function(e)
  111.     {
  112.         if(e.keyCode == 38 && current_direction != 'down' && current_direction != 'up')
  113.         {
  114.             current_direction = 'up';
  115.             clearTimeout(t);
  116.             move_snake();
  117.         }
  118.         else if(e.keyCode == 40 && current_direction != 'up')
  119.         {
  120.             current_direction = 'down';
  121.             clearTimeout(t);
  122.             move_snake();
  123.         }
  124.         else if (e.keyCode == 37 && current_direction != 'right')
  125.         {
  126.             current_direction = 'left';
  127.             clearTimeout(t);
  128.             move_snake();
  129.         }
  130.         else if (e.keyCode == 39 && current_direction != 'left')
  131.         {
  132.             current_direction = 'right';
  133.             clearTimeout(t);
  134.             move_snake();
  135.         }
  136.     });
  137.    
  138.     t = setTimeout(move_snake, speed);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement