Skip to content

Commit

Permalink
increase speed with score up to 190, start of smooth animation code
Browse files Browse the repository at this point in the history
  • Loading branch information
Even Alander committed Jan 2, 2016
1 parent 783ad14 commit 0b04096
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function Snake(ctx, width, height, gridWidth) {

this.snake = [];
this.wait = 300;
this.cooldown = 300;
}

Snake.prototype.newGame = function() {
Expand Down Expand Up @@ -124,7 +125,7 @@ Snake.prototype.update = function(dt) {
return;
}

this.wait = 300;
this.wait = this.cooldown;

switch (this.direction) {
case UP:
Expand Down Expand Up @@ -152,6 +153,9 @@ Snake.prototype.update = function(dt) {
this.snake.unshift(move);
this.position = move;
this.score = this.score + SCORE_INCREASE;
if (this.score < 200) {
this.cooldown = 300 - this.score;
}
} else if (this.collision(move)) {
this.state = ENDED;
} else {
Expand All @@ -163,6 +167,9 @@ Snake.prototype.update = function(dt) {

Snake.prototype.render = function() {
var i;
var d;
var offsetX;
var offsetY;

if (this.state === IDLE) {
return;
Expand All @@ -171,10 +178,38 @@ Snake.prototype.render = function() {
this.c.fillStyle = '#333';

for (i = 0; i < this.snake.length; i++) {
offsetX = 0;
offsetY = 0;
if (this.state === IDLE) {
if (i === 0) {
d = this.direction;
} else if (this.snake[i-1].y < this.snake[i].y) {
d = UP;
} else if (this.snake[i-1].x > this.snake[i].x) {
d = RIGHT;
} else if (this.snake[i-1].y > this.snake[i].y) {
d = DOWN;
} else {
d = LEFT;
}
switch (d) {
case UP:
offsetY = 1 / this.grid.h * this.height * this.wait / this.cooldown;
break;
case RIGHT:
offsetX = 1 / this.grid.w * this.width * this.wait / this.cooldown * -1;
break;
case DOWN:
offsetY = 1 / this.grid.h * this.height * this.wait / this.cooldown * -1;
break;
default:
offsetX = 1 / this.grid.w * this.width * this.wait / this.cooldown;
}
}
this.c.beginPath();
this.c.arc(
this.snake[i].x / this.grid.w * this.width,
this.snake[i].y / this.grid.h * this.height,
this.snake[i].x / this.grid.w * this.width + offsetX,
this.snake[i].y / this.grid.h * this.height + offsetY,
this.radius,
0,
Math.PI * 2);
Expand Down

0 comments on commit 0b04096

Please sign in to comment.