ManiAz09 commited on
Commit
0f9b3ac
·
verified ·
1 Parent(s): 889f84f

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +93 -0
script.js ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const playBoard = document.querySelector(".play-board");
2
+ const scoreElement = document.querySelector(".score");
3
+ const highScoreElement = document.querySelector(".high-score");
4
+ const controls = document.querySelectorAll(".controls i");
5
+
6
+ let gameOver = false;
7
+ let foodX, foodY;
8
+ let snakeX = 5, snakeY = 5;
9
+ let velocityX = 0, velocityY = 0;
10
+ let snakeBody = [];
11
+ let setIntervalId;
12
+ let score = 0;
13
+
14
+ // Getting high score from the local storage
15
+ let highScore = localStorage.getItem("high-score") || 0;
16
+ highScoreElement.innerText = `High Score: ${highScore}`;
17
+
18
+ const updateFoodPosition = () => {
19
+ // Passing a random 1 - 30 value as food position
20
+ foodX = Math.floor(Math.random() * 30) + 1;
21
+ foodY = Math.floor(Math.random() * 30) + 1;
22
+ }
23
+
24
+ const handleGameOver = () => {
25
+ // Clearing the timer and reloading the page on game over
26
+ clearInterval(setIntervalId);
27
+ alert("Game Over! Press OK to replay...");
28
+ location.reload();
29
+ }
30
+
31
+ const changeDirection = e => {
32
+ // Changing velocity value based on key press
33
+ if(e.key === "ArrowUp" && velocityY != 1) {
34
+ velocityX = 0;
35
+ velocityY = -1;
36
+ } else if(e.key === "ArrowDown" && velocityY != -1) {
37
+ velocityX = 0;
38
+ velocityY = 1;
39
+ } else if(e.key === "ArrowLeft" && velocityX != 1) {
40
+ velocityX = -1;
41
+ velocityY = 0;
42
+ } else if(e.key === "ArrowRight" && velocityX != -1) {
43
+ velocityX = 1;
44
+ velocityY = 0;
45
+ }
46
+ }
47
+
48
+ // Calling changeDirection on each key click and passing key dataset value as an object
49
+ controls.forEach(button => button.addEventListener("click", () => changeDirection({ key: button.dataset.key })));
50
+
51
+ const initGame = () => {
52
+ if(gameOver) return handleGameOver();
53
+ let html = `<div class="food" style="grid-area: ${foodY} / ${foodX}"></div>`;
54
+
55
+ // Checking if the snake hit the food
56
+ if(snakeX === foodX && snakeY === foodY) {
57
+ updateFoodPosition();
58
+ snakeBody.push([foodY, foodX]); // Pushing food position to snake body array
59
+ score++; // increment score by 1
60
+ highScore = score >= highScore ? score : highScore;
61
+ localStorage.setItem("high-score", highScore);
62
+ scoreElement.innerText = `Score: ${score}`;
63
+ highScoreElement.innerText = `High Score: ${highScore}`;
64
+ }
65
+ // Updating the snake's head position based on the current velocity
66
+ snakeX += velocityX;
67
+ snakeY += velocityY;
68
+
69
+ // Shifting forward the values of the elements in the snake body by one
70
+ for (let i = snakeBody.length - 1; i > 0; i--) {
71
+ snakeBody[i] = snakeBody[i - 1];
72
+ }
73
+ snakeBody[0] = [snakeX, snakeY]; // Setting first element of snake body to current snake position
74
+
75
+ // Checking if the snake's head is out of wall, if so setting gameOver to true
76
+ if(snakeX <= 0 || snakeX > 30 || snakeY <= 0 || snakeY > 30) {
77
+ return gameOver = true;
78
+ }
79
+
80
+ for (let i = 0; i < snakeBody.length; i++) {
81
+ // Adding a div for each part of the snake's body
82
+ html += `<div class="head" style="grid-area: ${snakeBody[i][1]} / ${snakeBody[i][0]}"></div>`;
83
+ // Checking if the snake head hit the body, if so set gameOver to true
84
+ if (i !== 0 && snakeBody[0][1] === snakeBody[i][1] && snakeBody[0][0] === snakeBody[i][0]) {
85
+ gameOver = true;
86
+ }
87
+ }
88
+ playBoard.innerHTML = html;
89
+ }
90
+
91
+ updateFoodPosition();
92
+ setIntervalId = setInterval(initGame, 100);
93
+ document.addEventListener("keyup", changeDirection);