This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game { | |
constructor(grid, snake) { | |
... | |
this.score = 0; | |
} | |
async run() { | |
... | |
while (true) { | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Grid { | |
... | |
getFillStyle(state) { | |
switch (state) { | |
case 'on': | |
return 'green'; | |
case 'off': | |
return 'lightgrey'; | |
case 'food': | |
return 'purple'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generateDataSet() { | |
this.updatedCells = []; | |
... | |
} | |
setPoint(point, state) { | |
... | |
this.updatedCells.push(point); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Snake { | |
constructor(position, direction) { | |
this.parts = [position]; | |
this.direction = direction; | |
this.newDirection = null; | |
} | |
update() { | |
const updates = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body> | |
<canvas id="game"></canvas> | |
<br> | |
<span id="gameover"></span> | |
<script src="./index.js"></script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game { | |
constructor(grid, snake) { | |
this.grid = grid; | |
this.snake = snake; | |
} | |
async run() { | |
while (true) { | |
this.grid.render(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Snake { | |
constructor(position) { | |
this.parts = [position]; | |
} | |
update() { | |
let updates = []; | |
updates.push({ | |
point: this.head, | |
state: 'on' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Grid { | |
constructor(width, height, blockarea, margin, ctx) { | |
... | |
this.generateDataSet(); | |
} | |
generateDataSet() { | |
this.dataset = []; | |
for (let x = 0; x < width; x++) { | |
this.dataset[x] = [height]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
NewerOlder