Skip to content

Instantly share code, notes, and snippets.

@VEINHORN
Created March 30, 2015 19:18
Show Gist options
  • Save VEINHORN/ed76a59f49fe31d8ef86 to your computer and use it in GitHub Desktop.
Save VEINHORN/ed76a59f49fe31d8ef86 to your computer and use it in GitHub Desktop.
// Generated by CoffeeScript 1.8.0
(function() {
var SnakeGame,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
window.onload = function() {
var canvas, snakeGame;
canvas = document.getElementById("snake_game");
snakeGame = new SnakeGame(canvas, 40, "#061059");
snakeGame.start();
snakeGame.directionProperty = 1;
return document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode === 37) {
snakeGame.directionProperty = 1;
return snakeGame.draw();
} else if (e.keyCode === 39) {
snakeGame.directionProperty = 2;
return snakeGame.draw();
} else if (e.keyCode === 38) {
snakeGame.directionProperty = 3;
return snakeGame.draw();
} else if (e.keyCode === 40) {
snakeGame.directionProperty = 4;
return snakeGame.draw();
}
};
};
SnakeGame = (function() {
SnakeGame.prototype.left = 1;
SnakeGame.prototype.right = 2;
SnakeGame.prototype.up = 3;
SnakeGame.prototype.down = 4;
SnakeGame.prototype.separatorColor = "#E8DCDC";
SnakeGame.prototype.separatorWidth = 1;
SnakeGame.prototype.cellSize = 15;
SnakeGame.prototype.cellPadding = 3;
SnakeGame.prototype.snakeCellColor = "#F2003D";
SnakeGame.prototype.foodCellColor = "#1ED64B";
SnakeGame.prototype.direction = 2;
SnakeGame.prototype.snake = [];
SnakeGame.prototype.snakeFood = [];
Object.defineProperties(SnakeGame.prototype, {
directionProperty: {
get: function() {
return this.direction;
},
set: function(direction) {
return this.direction = direction;
}
}
});
function SnakeGame(canvas, cellsNum, background) {
this.canvas = canvas;
this.cellsNum = cellsNum;
this.background = background;
this.initSnake = __bind(this.initSnake, this);
this.getHeight = __bind(this.getHeight, this);
this.getWidth = __bind(this.getWidth, this);
this.drawLine = __bind(this.drawLine, this);
this.drawGrid = __bind(this.drawGrid, this);
this.init = __bind(this.init, this);
this.canvas.width = this.getWidth();
this.canvas.height = this.getHeight();
this.initSnake();
}
SnakeGame.prototype.start = function() {
var ctx;
ctx = this.canvas.getContext("2d");
setInterval(this.init(ctx), 500);
return setInterval(this.drawGrid(ctx), 500);
};
SnakeGame.prototype.init = function(ctx) {
ctx.fillStyle = this.background;
return ctx.fillRect(0, 0, this.getWidth(), this.getHeight());
};
SnakeGame.prototype.drawGrid = function(ctx) {
var i, _i, _j, _ref, _ref1, _results;
for (i = _i = 0, _ref = this.cellsNum; _i <= _ref; i = _i += 1) {
this.drawLine(ctx, i * this.cellSize, 0, i * this.cellSize, this.getHeight());
}
_results = [];
for (i = _j = 0, _ref1 = this.cellsNum; _j <= _ref1; i = _j += 1) {
_results.push(this.drawLine(ctx, 0, i * this.cellSize, this.getWidth(), i * this.cellSize));
}
return _results;
};
SnakeGame.prototype.drawLine = function(ctx, x, y, toX, toY) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(toX, toY);
ctx.strokeStyle = this.separatorColor;
ctx.lineWidth = this.separatorWidth;
return ctx.stroke();
};
SnakeGame.prototype.getWidth = function() {
return this.cellSize * this.cellsNum;
};
SnakeGame.prototype.getHeight = function() {
return this.cellSize * this.cellsNum;
};
SnakeGame.prototype.initSnake = function() {
this.snake.push([15, 16]);
this.snake.push([15, 15]);
this.snake.push([15, 14]);
this.snake.push([15, 13]);
this.snake.push([15, 12]);
this.snake.push([15, 11]);
return this.snake.push([15, 10]);
};
return SnakeGame;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment