Skip to content

Instantly share code, notes, and snippets.

@eamonnmcevoy
Created September 9, 2018 18:44
Show Gist options
  • Save eamonnmcevoy/c622f8b1b549044a67d762251b0048a3 to your computer and use it in GitHub Desktop.
Save eamonnmcevoy/c622f8b1b549044a67d762251b0048a3 to your computer and use it in GitHub Desktop.
class Game {
constructor(grid, snake) {
this.grid = grid;
this.snake = snake;
}
async run() {
while (true) {
this.grid.render();
const updates = this.snake.update();
updates.forEach(x => {
this.grid.setPoint(x.point, x.state);
});
await this.sleep(delay);
}
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
...
const delay = 50;
const grid = new Grid(width, height, blockarea, margin, ctx);
const snake = new Snake({x:10, y:10});
const game = new Game(grid, snake, delay);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment