From 81e509fcce15a82d345526be1d53a3a638bbedab Mon Sep 17 00:00:00 2001 From: Nick <41508176+M1lkZ@users.noreply.github.com> Date: Tue, 3 May 2022 22:18:31 +0300 Subject: [PATCH 1/5] Add files via upload --- style.css | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 style.css diff --git a/style.css b/style.css new file mode 100644 index 0000000..17b29c2 --- /dev/null +++ b/style.css @@ -0,0 +1,8 @@ +#HardMode { + position: absolute; + color: #fff; + background-color: #333; + width: 110px; + height: 50px; + font-size: 18px; +} \ No newline at end of file From 4d50436fe1202926cdc8c8a5e330eaf298291344 Mon Sep 17 00:00:00 2001 From: Nick <41508176+M1lkZ@users.noreply.github.com> Date: Tue, 3 May 2022 22:18:57 +0300 Subject: [PATCH 2/5] Update game.js --- game.js | 99 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/game.js b/game.js index 4001687..837f7a5 100644 --- a/game.js +++ b/game.js @@ -1,12 +1,16 @@ const canvas = document.getElementById("canvas") const canvasContext = canvas.getContext('2d') +let HardModeOn = false +const HardButton = document.getElementById('HardMode'); + + window.onload = () => { gameLoop() } function gameLoop() { - setInterval(show, 1000/20) // here 15 is our fps value + setInterval(show, 1000 / 20) // here 15 is our fps value } function show() { @@ -17,50 +21,78 @@ function show() { function update() { canvasContext.clearRect(0, 0, canvas.width, canvas.height) snake.move() + SnakeHitCheck(); + if (HardModeOn) { + checkHitWallHard() + } else { + checkHitWall() + } eatApple() - checkHitWall() } function eatApple() { - if(snake.tail[snake.tail.length - 1].x == apple.x && - snake.tail[snake.tail.length - 1].y == apple.y){ - snake.tail[snake.tail.length] = {x:apple.x, y: apple.y} - apple = new Apple(); + if (snake.tail[snake.tail.length - 1].x == apple.x && + snake.tail[snake.tail.length - 1].y == apple.y) { + snake.tail[snake.tail.length] = { + x: apple.x, + y: apple.y } + apple = new Apple(); + } } -function checkHitWall() { - let headTail = snake.tail[snake.tail.length -1] +function SnakeHitCheck() { + let headTail = snake.tail[snake.tail.length - 1]; + for (let i = 0; i < snake.tail.length - 1; i++) { + if (headTail.x == snake.tail[i].x && headTail.y == snake.tail[i].y) { + snake = new Snake(20, 20, 20); + } + } +} - if (headTail.x == - snake.size) { +function checkHitWall() { + let headTail = snake.tail[snake.tail.length - 1] + if (headTail.x == -snake.size) { headTail.x = canvas.width - snake.size - } else if (headTail.x == canvas.widh) { + } else if (headTail.x == canvas.width) { headTail.x = 0 - } else if (headTail.y == - snake.size) { + } else if (headTail.y == -snake.size) { headTail.y = canvas.height - snake.size } else if (headTail.y == canvas.height) { - headTail.y = 0 + headTail.y = 0 + } +} + +function checkHitWallHard() { + let headTail = snake.tail[snake.tail.length - 1] + let WallHit = false + if (headTail.x == -snake.size || headTail.x == canvas.width || headTail.y == -snake.size || headTail.y == canvas.height) { + WallHit = true + } + if (WallHit) { + record = (snake.tail.length - 1); + snake = new Snake(20, 20, 20); } } function draw() { - createRect(0,0,canvas.width, canvas.height, "black") - createRect(0,0, canvas.width, canvas.height) + createRect(0, 0, canvas.width, canvas.height, "black") - for (let i = 0; i < snake.tail.length; i++){ + for (let i = 0; i < snake.tail.length; i++) { createRect(snake.tail[i].x + 2.5, snake.tail[i].y + 2.5, - snake.size - 5, snake.size- 5, "white") + snake.size - 5, snake.size - 5, "white") } canvasContext.font = "20px Arial" canvasContext.fillStyle = "#00FF42" - canvasContext.fillText("Score: " + (snake.tail.length -1),canvas.width - 120, 18) + canvasContext.fillText("Score: " + (snake.tail.length - 1), canvas.width - 120, 18) createRect(apple.x, apple.y, apple.size, apple.size, apple.color) } -function createRect(x,y,width, height,color) { +function createRect(x, y, width, height, color) { canvasContext.fillStyle = color canvasContext.fillRect(x, y, width, height) + } window.addEventListener("keydown", (event) => { @@ -81,12 +113,31 @@ window.addEventListener("keydown", (event) => { }, 1) }) +HardButton.addEventListener('click', function onClick(event) { + if (HardModeOn) { + canvas.width = "800" + canvas.height = "800" + HardModeOn = false + event.target.style.color = 'white'; + } else { + canvas.width = "500" + canvas.height = "500" + HardModeOn = true + event.target.style.color = 'red'; + } + snake = new Snake(20, 20, 20); + apple = new Apple(); +}); + class Snake { constructor(x, y, size) { this.x = x this.y = y this.size = size - this.tail = [{x:this.x, y:this.y}] + this.tail = [{ + x: this.x, + y: this.y + }] this.rotateX = 0 this.rotateY = 1 } @@ -121,15 +172,15 @@ class Snake { } } -class Apple{ - constructor(){ +class Apple { + constructor() { let isTouching - + while (true) { isTouching = false; this.x = Math.floor(Math.random() * canvas.width / snake.size) * snake.size this.y = Math.floor(Math.random() * canvas.height / snake.size) * snake.size - + for (let i = 0; i < snake.tail.length; i++) { if (this.x == snake.tail[i].x && this.y == snake.tail[i].y) { isTouching = true @@ -146,5 +197,5 @@ class Apple{ } } -const snake = new Snake(20,20,20); +let snake = new Snake(20, 20, 20); let apple = new Apple(); From 6f0300138294b78c3d0b401ecccc28a4f28fdabe Mon Sep 17 00:00:00 2001 From: Nick <41508176+M1lkZ@users.noreply.github.com> Date: Tue, 3 May 2022 22:19:21 +0300 Subject: [PATCH 3/5] Update snake.html --- snake.html | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/snake.html b/snake.html index 0285274..0551631 100644 --- a/snake.html +++ b/snake.html @@ -1,11 +1,14 @@ -
-