Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The direction the snake is traveling
- var current_direction = 'right';
- //The snake array
- var snake = [[10,4], [10,3], [10,2], [10,1]];
- //The score
- var score = 0;
- //Is the game currently playing
- var active = true;
- var t = null;
- var speed = 250;
- function move_snake()
- {
- for(var i = snake.length - 1; i >= 0; i--)
- {
- //Handle the head
- if(i == 0)
- {
- if(current_direction == 'up')
- {
- snake[0][0]--;
- //current_direction = 'up';
- }
- else if(current_direction == 'down')
- {
- snake[0][0]++;
- //current_direction = 'down';
- }
- else if(current_direction == 'left')
- {
- snake[0][1]--;
- //current_direction = 'left';
- }
- else if(current_direction == 'right')
- {
- snake[0][1]++;
- //current_direction = 'right';
- }
- //Make sure we're within the boundaries
- if(snake[0][1] < 0 ||
- snake[0][1] > 24 ||
- snake[0][0] < 0 ||
- snake[0][0] > 14)
- {
- active = false;
- alert('You died with a score of ' + score);
- return;
- }
- //Check if we ate a piece of food
- if($('#c' + snake[0][0] + '-' + snake[0][1]).find('img').attr('src').indexOf('food') != -1)
- {
- generate_food(snake[0][0], snake[0][1]);
- snake.push([snake[snake.length - 1][0], snake[snake.length - 1][1]]);
- score += 10;
- $('#score').html('Score: ' + score);
- }
- //Check if we hit ourselves
- if($('#c' + snake[0][0] + '-' + snake[0][1]).find('img').attr('src').indexOf('snake') != -1)
- {
- active = false;
- alert('You died with a score of ' + score);
- return;
- }
- //Move the head
- $('#c' + snake[i][0] + '-' + snake[i][1]).find('img').attr('src', 'images/snake.gif');
- }
- else
- {
- if(i == (snake.length - 1))
- {
- $('#c' + snake[i][0] + '-' + snake[i][1]).find('img').attr('src', 'images/empty.gif');
- }
- snake[i][0] = snake[i - 1][0];
- snake[i][1] = snake[i - 1][1];
- }
- }
- if(active)
- {
- t = setTimeout(move_snake, speed);
- }
- }
- function generate_food(x, y)
- {
- var food_x = Math.round(Math.random() * 14);
- var food_y = Math.round(Math.random() * 24);
- //Ensure we don't generate a piece of food on the snake or the same place as the food that was just eaten
- while($('#c' + food_x + '-' + food_y).find('img').attr('src').indexOf('snake') != -1 || (food_x == x && food_y == y))
- {
- food_x = Math.round(Math.random() * 14);
- food_y = Math.round(Math.random() * 24);
- }
- $('#c' + food_x + '-' + food_y).find('img').attr('src', 'images/food.gif');
- }
- function setup()
- {
- $(window).keydown(function(e)
- {
- if(e.keyCode == 38 && current_direction != 'down' && current_direction != 'up')
- {
- current_direction = 'up';
- clearTimeout(t);
- move_snake();
- }
- else if(e.keyCode == 40 && current_direction != 'up')
- {
- current_direction = 'down';
- clearTimeout(t);
- move_snake();
- }
- else if (e.keyCode == 37 && current_direction != 'right')
- {
- current_direction = 'left';
- clearTimeout(t);
- move_snake();
- }
- else if (e.keyCode == 39 && current_direction != 'left')
- {
- current_direction = 'right';
- clearTimeout(t);
- move_snake();
- }
- });
- t = setTimeout(move_snake, speed);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement