Skip to content

Commit

Permalink
fixed a bug where snake lived in .5 grid under some circumstances, al…
Browse files Browse the repository at this point in the history
…so fixed some lints
  • Loading branch information
deificx committed Jan 3, 2016
1 parent 432c180 commit 7fea248
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export const IDLE = 'IDLE';
export const STARTED = 'STARTED';
export const ENDED = 'ENDED';

export const COOLDOWN = 300;
export const SCORE_CAP = 200;
export const SCORE_INCREASE = 10;
23 changes: 13 additions & 10 deletions src/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
IDLE,
STARTED,
ENDED,
SCORE_CAP,
SCORE_INCREASE,
COOLDOWN,
} from './constants';

function Snake(ctx, width, height, gridWidth) {
Expand All @@ -24,8 +26,8 @@ function Snake(ctx, width, height, gridWidth) {
this.radius = Math.round(width / gridWidth) / 2;

this.apple = {
x:null,
y:null,
x: null,
y: null,
};
this.position = {
x:null,
Expand All @@ -44,15 +46,15 @@ Snake.prototype.newGame = function() {

this.snake = [];
this.snake.push({
x:this.grid.w/2,
y:this.grid.h/2,
x:Math.round(this.grid.w/2),
y:Math.round(this.grid.h/2),
});
this.snake.push({
x:this.snake[0].x+1,
x:this.snake[0].x + 1,
y:this.snake[0].y,
});
this.snake.push({
x:this.snake[0].x+2,
x:this.snake[0].x + 2,
y:this.snake[0].y,
});

Expand Down Expand Up @@ -153,8 +155,8 @@ 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;
if (this.score < SCORE_CAP) {
this.cooldown = COOLDOWN - this.score;
}
} else if (this.collision(move)) {
this.state = ENDED;
Expand All @@ -170,6 +172,7 @@ Snake.prototype.render = function() {
var d;
var offsetX;
var offsetY;
var flip = -1;

if (this.state === IDLE) {
return;
Expand Down Expand Up @@ -197,10 +200,10 @@ Snake.prototype.render = function() {
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;
offsetX = 1 / this.grid.w * this.width * this.wait / this.cooldown * flip;
break;
case DOWN:
offsetY = 1 / this.grid.h * this.height * this.wait / this.cooldown * -1;
offsetY = 1 / this.grid.h * this.height * this.wait / this.cooldown * flip;
break;
default:
offsetX = 1 / this.grid.w * this.width * this.wait / this.cooldown;
Expand Down

0 comments on commit 7fea248

Please sign in to comment.