Skip to content

Instantly share code, notes, and snippets.

@eamonnmcevoy
Last active September 9, 2018 20:28
Show Gist options
  • Save eamonnmcevoy/86ac06230eea7dde5341241c9f47158b to your computer and use it in GitHub Desktop.
Save eamonnmcevoy/86ac06230eea7dde5341241c9f47158b to your computer and use it in GitHub Desktop.
class Snake {
...
update() {
...
this._previousTailPosition = Object.assign({}, this.parts[this.parts.length - 1]);
this.parts[this.parts.length - 1] = next;
const lastPart = this.parts.splice(-1, 1)[0];
this.parts.unshift(lastPart);
updates.push({ point: this.head, state: 'on' });
updates.push({ point: this._previousTailPosition, state: 'off' });
return updates;
}
get previousTailPosition() {
return this._previousTailPosition;
}
grow() {
this.parts.push(Object.assign({},this.previousTailPosition));
return { point:this.previousTailPosition, state:'on' };
}
}
class Game {
...
async run() {
...
while (true) {
...
if(this.food) {
if(!this.food.isRendered) {
grid.setPoint(this.food, 'food');
this.food.isRendered = false;
}
if(this.snake.collision(this.snake.head, this.food)) {
const growUpdate = this.snake.grow();
this.grid.setPoint(growUpdate.point, growUpdate.state);
this.food = null;
}
}
...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment