Created
October 14, 2016 16:08
-
-
Save blairdow/fc90d88c25daebfd8952da39770fb958 to your computer and use it in GitHub Desktop.
A gist of my most challenging code from BlairSnake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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