Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/graphic.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SnakeGraphic.prototype.reset = function() {
this.blockSize.y * this.field.y + this.offset
);
this.ctx.strokeStyle = 'yellow';
this.ctx.lineWidth = 2;
this.ctx.lineWidth = 500;
this.ctx.strokeRect(
this.offset / 2 - 1,
this.offset / 2 - 1,
Expand All @@ -47,11 +47,19 @@ SnakeGraphic.prototype.setApple = function(pos) {
this.apple = pos;
};

/**
* Set the bomb position
* @param {{x: Number, y: Number}} pos, position of where to set the apple
*/
SnakeGraphic.prototype.setBomb = function(pos) {
this.bomb = pos;
};

SnakeGraphic.prototype.setScore = function(score) {
this.ctx.font = '30px Arial';
this.ctx.fillStyle = 'black';
this.ctx.fillRect(0, 0, 200, 30);
this.ctx.fillStyle = 'yellow';
this.ctx.fillStyle = 'white';
this.ctx.fillText(score, 10, 30);
};

Expand All @@ -67,10 +75,12 @@ SnakeGraphic.prototype.draw = function(snake) {
//draw the new snake
this.oldSnake = snake.slice();
for (var i = 0; i < snake.length; i++) {
this.fillCell(snake[i],'white');
this.fillCell(snake[i],'green');
}
//Draw the apple
this.fillCell(this.apple, 'red');
//Draw the bomb
this.fillCell(this.bomb, 'yellow');
};

/**
Expand Down
11 changes: 8 additions & 3 deletions src/snakegame.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Game.prototype.generateRandomApple = function() {
y: Math.floor(Math.random() * this.field.y)
};
this.graphic.setApple(this.apple);
this.bomb = {
x: Math.floor(Math.random() * this.field.x),
y: Math.floor(Math.random() * this.field.y)
};
this.graphic.setBomb(this.bomb);
};

Game.prototype.keypress = function(ev, self) {
Expand All @@ -61,7 +66,7 @@ Game.prototype.keypress = function(ev, self) {

Game.prototype.update = function(self) {
self.snake.move();
if (self.snake.isDead()) {
if (self.snake.isDead() || self.snake.isOn(self.bomb)) {
self.score = 0;
self.graphic.reset();
self.snake.respawn();
Expand All @@ -72,5 +77,5 @@ Game.prototype.update = function(self) {
self.score += 100;
self.graphic.setScore(self.score);
}
self.graphic.draw(self.snake.get());
};
self.graphic.draw(self.snake.get());
};