Snake.js is a module written in vanilla javascript that lets you create your custom snake game.
It's very easy to use: create a Board, register snake(s) to it and play. You read that right! Multiplayer snake game are welcome with no extra coding.
You can use it to create a complex web game, or simply putting into your portfolio an enjoying easter egg.
Loading snake.js you have global access to Board and Snake objects.
<div class="board">
<div class="snake"></div>
</div>
...
<!-- vanilla w/ love -->
<script src="snake.js"></script>
<script>
var board = new Board(),
snake = new Snake();
// Setup commands as you want
document.onkeydown = function(e) {
e = e || window.event;
// arrow keys
if (e.keyCode === 38) snake.goUp();
if (e.keyCode === 39) snake.goRight();
if (e.keyCode === 40) snake.goDown();
if (e.keyCode === 37) snake.goLeft();
};
board
.register(snake)
.play();
</script>
bower install snake.js
var board = new Board({
'element': document.querySelector('.board'),
'columns': 32,
'rows': 16,
'cell-width': 24,
'cell-height': 24,
'timing': 300,
'food-count': 1,
'food-replace': true
});
var snake = new Snake({
'element': document.querySelector('.snake'),
'x': 0,
'y': 0,
'size': 4
});
/* Board */
.board {
position: relative;
border: 5px solid currentColor; /* optional */
}
/* Snake */
.snake { color: lawngreen; }
.snake .block {
width: 24px;
height: 24px;
position: absolute;
background: currentColor;
}
.snake.gameover .block {
background-color: red;
}
/* Food */
.food {
/* The resultant size m ust be the same of snake blocks */
width: 12px;
height: 12px;
margin: 6px;
position: absolute;
background: rebeccapurple;
}
Automatically invoked into constructor, can be used to restart the board status.
board.init();
Must be used to register snake(s) to the board.
// single snake
board.register(snake);
// multiple snakes at once
board.register([snakeOne, snakeTwo]);
// YES, you can add snakes whenever you want
Starts the game.
board.play();
Pause/Stop the game.
board.pause();
// you can use also the alias STOP
board.stop();
Very comfortable when you have to pause/play the game.
board.togglePause();
Put a food block on the board.
var x = 5,
y = 3;
board.placeFood(x, y);
// food can be placed also randomly
board.placeFood();
Remove a food block from the board.
var x = 5,
y = 3;
board.removeFood(x, y);
Automatically invoked into constructor, can be used to restart the snake status.
snake.init();
Let the snake make a movement (following its last direction).
snake.move();
Extend the snake tail.
snake.extend();
Change the status of the snake to gameover (usually when dead).
snake.gameover();
Stops the snake.
snake.stop();
Set the snake direction to UP.
snake.goUp();
Set the snake direction to RIGHT.
snake.goRight();
Set the snake direction to DOWN.
snake.goDown();
Set the snake direction to LEFT.
snake.goLeft();
Check the example to make it works easy.
Thanks for checking this out. For any questions just tweet me on Twitter.
Please let me know if you're using snake.js! I'm curious to see how you made it yours.
Mentions are very appreciated. dario.wtf