Yet another Snake game made with plain JavaScript.
This game is made using only plan JavaScript (ES5 standard).
The game itself is wrapped using the Module Pattern and it can be accesed using the global variable snakeJs.
The game itself can be accesed using the variable snakeJs in the global scope.
The game exposes a few methods:
init()When this method is called, the game will initialize. Game cannot start until it is initialized.start()When this method is called, the game will start.changeDirectionUp()will change the snake direction upwards. Same as triggeringsnake-js-change-direction-upevent.changeDirectionDown()will change the snake direction downwards. Same as triggeringsnake-js-change-direction-downevent.changeDirectionLeft()will change the snake direction to the left. Same as triggeringsnake-js-change-direction-leftevent.changeDirectionRight()will change the snake direction to the righ. Same as triggeringsnake-js-change-direction-rightevent.
The game will trigger a few events:
snake-js-initwhen the game is initialized.snake-js-startwhen the game starts.snake-js-endwhen the game finishes. The event will have the final score in theevent.detail.scoreproperty.snake-js-score-upwhen the snake eats an apple, thus scoring a point. The event will have the new score in theevent.detail.score: numberproperty.snake-js-level-upwhen the speed of the game increases.snake-js-food-generatedwhen new food is generated somewhere in the grid. The event will have the new food positionevent.detail.position: {x: number, y: number}property and the type in theevent.detail.type: stringproperty.snake-js-snake-updatedwhen the snake changes position. The event will have the new snake position array in theevent.detail.snake: [{position: {x: number, y: number}}, ...]property.
You can use these events, for example, for updating or resetting a scoreboard, or to show messages to the user.
The game will also respond to some events:
snake-js-change-direction-upwill change the snake direction upwards. Same as callingchangeDirectionUp()event.snake-js-change-direction-downwill change the snake direction downwards. Same as callingchangeDirectionDown()event.snake-js-change-direction-leftwill change the snake direction to the left. Same as callingchangeDirectionLeft()event.snake-js-change-direction-rightwill change the snake direction to the right. Same as callingchangeDirectionRight()event.
You can use these events to set up some controls, i.e. attaching events to screen buttons or hardware keyboard.
Clone this repository, open the demo folder and open index.html with your web browser for playing the game.
You can also play the game visiting alvarovazquez.es/snake-js.
The game can run without any styles, but its easier for the player to have some visual feedback :). The demo is provided with some basic styles trying to emulate the good old cellular phone screens. The only styles you need to play this game is to have a well formed grid, and different colors for the snake, the apples and the background.