Snake game for the browser using vanilla JS and basic HTML and CSS. There was a heavy emphasis on OOP when creating this small app.
The game mechanics and rules are explained in detail in UPenn's 2008 assignment on the topic
- HTML
- CSS
- Javascript
I grew up on the Snake Game on my small Nokia phone. As such, it was nice to come back and make something I played so much as a kid. Also I created a Python version of this game that can be found here
- Sounds taken from Mixkit
- Background taken from Shutterstock
Use the up, down, right, left buttons or W, S, D, A keys to move around. Click here to play game
- Snake will be an object/class with the following states:
- Length of snake
- Snake speed variable?
- The direction the snake is going
- Position (a 2D array of all its cell positions or a array of objects that have an x, y key)
- On initialization it will randomly pick a position
- isDead state (boolean)
- Reset position function
- Simple getter function that gets the head
- A turn function to change where the snake moves
- Check if new head position is in positions array, and if so snake dead
- turn function will take a direction as parameter
- This will be passed by EventListeners for keyup
- Use event.keyCode to determine the direction
- Snake head moves in that direction
- The other segments move to previous's position
- A move function that just moves the snake on that trajectory
- move just moves the segments up-down right-left depending on direction
- Game field will be a grid of cells
- Food cells will be red
- Cells that the snake occupies will be green
- Empty cells will be grey
- It will be a grid of divs
- Traversing the grid would work like a 2D array
- Each div has class cell [Possible: make each row a separate div with class row?]
- When snake is at the edge of the screen, it will loop back to beginning
- This can be accomplished by using modulo on the grid row/column + 1
- Food will be placed randomly on the game board (assuming nothing is in that cell)
- It will check whether the snake is in that space
- Init functions will initialize everything to their starting positions
- Re-initialize the grid
- Re-initialize the snake
- Call random placement of food
- Score keeping function (how score is calculated TBD)
- Reset button with EventListener
- Start game button with EventListener that uses while loop to continually run game and move the snake
- while loop's condition is the snake's "dead" state
- remove snake class from div as snake moves
- Check if snake is "dead"
- If so, then call render end game
- Render function will handle rendering everything
- Render the different colors for the div cells
- If food div needs to be rendered (after previous one being eaten), then render it in it's random spot
- Render the snake in it's moved position
- If food eaten in previous state, then remove the color of the food (use remove from classList)
- Separate render functions for snake and food
- Render function for end of game
- Render points in score based on food eaten
- Directions will be a 2D array of up-down, right-left
Being able to control the speed of the snakeSnake speed increases as he eatsAudio related to losing and points added