From 7f23f4a40c4e7569d5296bb99b9945520d40ee9f Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:29:07 -0600 Subject: [PATCH] snake: add stashed changes --- src/snake.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/snake.ts b/src/snake.ts index 794528c..39055e0 100644 --- a/src/snake.ts +++ b/src/snake.ts @@ -68,12 +68,8 @@ class SnakeCore { this.width = 25; this.height = 15; this.board = []; - for (let i = 0; i < this.height; i++) { + for (let i = 0; i < this.height; i++) this.board.push(new Array(this.width)); - for (let j = 0; j < this.width; j++) { - this.board[i][j] = 0; - } - } this.body = []; this.gameover = false; this.foodAte = true; @@ -86,19 +82,22 @@ class SnakeCore { } reset() { - snake.gameover = false; - snake.foodAte = true; + this.gameover = false; + this.foodAte = true; for (let i = 0; i < this.height; i++) { for (let j = 0; j < this.width; j++) { this.board[i][j] = 0; } } - snake.context.fillStyle = "black"; - for (let i = 0; i < snake.height; i++) { - for (let j = 0; j < snake.width; j++) { - snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid); - } - } + while (this.body.length > 0) + this.body.pop(); + this.startRandom(); + this.foodRegen(); + this.draw(); + } + + startRandom() { + this.body.push(new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height))); } foodRegen() {