Skip to content

Instantly share code, notes, and snippets.

@blairdow
Created October 14, 2016 16:08
Show Gist options
  • Select an option

  • Save blairdow/fc90d88c25daebfd8952da39770fb958 to your computer and use it in GitHub Desktop.

Select an option

Save blairdow/fc90d88c25daebfd8952da39770fb958 to your computer and use it in GitHub Desktop.
A gist of my most challenging code from BlairSnake
//runs when snake hits food to add length
function eatFood() {
//if food coordinates equal snake head coordinates, eat food and add length
if(foodX === snakeHeadX && foodY === snakeHeadY) {
score++
foodSound()
//increases speed every 5 points
speedIncrease()
var length = snake.length
//if moving horizontally add length to x-axis
if(leftPressed || rightPressed) {
//add food space to snake
snake.unshift([foodX, foodY])
//add two units to end of snake
snake.push([snake[length-1][0], snake[length-1][1]], [snake[length-1][0]+5, snake[length-1][1]])
}
//if moving vertically add length to y-axis
if(upPressed || downPressed)
{
//add food space to snake
snake.unshift([foodX, foodY])
//add two units to end of snake
snake.push([snake[length-1][0], snake[length-1][1]], [snake[length-1][0], snake[length-1][1]+5])}
//reset food location
randomizeFood()
//reset food type (burger, pizza, or cherries)
pickFood()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment