Skip to content

Instantly share code, notes, and snippets.

class Game {
constructor(grid, snake) {
...
this.score = 0;
}
async run() {
...
while (true) {
...
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' });
@eamonnmcevoy
eamonnmcevoy / index.js
Last active September 11, 2018 18:42
class Grid {
...
getFillStyle(state) {
switch (state) {
case 'on':
return 'green';
case 'off':
return 'lightgrey';
case 'food':
return 'purple';
generateDataSet() {
this.updatedCells = [];
...
}
setPoint(point, state) {
...
this.updatedCells.push(point);
}
class Snake {
constructor(position, direction) {
this.parts = [position];
this.direction = direction;
this.newDirection = null;
}
update() {
const updates = [];
<html>
<head>
</head>
<body>
<canvas id="game"></canvas>
<br>
<span id="gameover"></span>
<script src="./index.js"></script>
</body>
class Game {
constructor(grid, snake) {
this.grid = grid;
this.snake = snake;
}
async run() {
while (true) {
this.grid.render();
class Snake {
constructor(position) {
this.parts = [position];
}
update() {
let updates = [];
updates.push({
point: this.head,
state: 'on'
class Grid {
constructor(width, height, blockarea, margin, ctx) {
...
this.generateDataSet();
}
generateDataSet() {
this.dataset = [];
for (let x = 0; x < width; x++) {
this.dataset[x] = [height];
class Grid {
constructor(width, height, blockarea, margin, ctx) {
this.width = width;
this.height = height;
this.ctx = ctx;
this.blockarea = blockarea;
this.margin = margin;
this.blocksize = blockarea - margin;
}