-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanscript.js
209 lines (162 loc) · 5.33 KB
/
cleanscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const width = 15;
const height = 15;
const gameboard = document.querySelector("#gameboard");
const resetBtn = document.querySelector("#resetBtn");
const gulpSound = new Audio("gulp.mp3");
const loseSound = new Audio("lose1.wav");
const backgroundSound = new Audio("backgroundmusic.mp3");
const hueChange = Math.floor(Math.random() * 360);
let gameState = "ready"; // "ready" to start, "running" or currently playing, "over" is game end
let timeoutInterval = 300;
let scoreSpan = document.querySelector("#score-span-one");
let score = 0;
let playCount = 0;
let direction = "right";
let previousDirection = "";
let snake = [
{ x: 3, y: 4 }, // tail of snake
{ x: 4, y: 4 }, // body of snake
{ x: 5, y: 4 }, // head of snake
]
let food;
// On page load:
relocateFood();
redrawGameBoard();
function start() {
gameState = "running";
backgroundSound.play();
setTimeout(slither, timeoutInterval);
}
function slither() {
// updates the contents of the snake array
updateGame()
// the last object in the snake array is going to be the head of the snake
redrawGameBoard()
// schedule slither if game is still runnning
if (gameState === "running") {
setTimeout(slither, timeoutInterval);
}
}
function updateGame() {
let currentHeadOfSnake = snake[snake.length - 1];
let newHeadOfSnake = {
x: currentHeadOfSnake.x,
y: currentHeadOfSnake.y
};
// snake changing directions when player presses key
previousDirection = direction;
if (direction === "up") {
newHeadOfSnake.y--;
}
if (direction === "down") {
newHeadOfSnake.y++;
}
if (direction === "left") {
newHeadOfSnake.x--;
}
if (direction === "right") {
newHeadOfSnake.x++;
}
if (newHeadOfSnake.x >= width || newHeadOfSnake.x < 0 ||
newHeadOfSnake.y >= height || newHeadOfSnake.y < 0 ||
snake.some(cell => cell.x === newHeadOfSnake.x && cell.y === newHeadOfSnake.y)
) {
// then the game ends
gameState = "over"
// setting playcount from 0 to 1, which is basically setting up a next round for player 2
if(gameState === "over") {
loseSound.play();
if(playCount === 0) {
// console.log(gameState);
playCount++;
startPlayerTwo();
}
}
} else {
snake.push(newHeadOfSnake);
// if next head of snake matches food positon, increase score by 1, play sound then move food
if (newHeadOfSnake.x === food.x && newHeadOfSnake.y === food.y) {
score++
gulpSound.play();
relocateFood();
timeoutInterval = timeoutInterval * 0.9
} else {
//.shift removes the first element of the existing snake array
snake.shift();
}
}
}
function redrawGameBoard() {
gameboard.innerHTML = "<div></div>".repeat(width * height);
// Draw the snake
snake.forEach(function (cell, index) {
let x = cell.x;
let y = cell.y;
// adds inner div to snake class we can further manipulate
let innerDiv = document.createElement("div");
innerDiv.className = "snake";
// changes snake color
let hue = hueChange + (index * 20)
innerDiv.style.backgroundColor = `hsl(${hue}, 100%, 71%)`;
// this gradually makes the snake smaller at the end
let margin = Math.min((snake.length - index) * 2, 32);
innerDiv.style.margin = `${margin}%`
// adds new color with smaller margin everytime snake grows
gameboard.children[(width * y) + x].appendChild(innerDiv)
})
// Drawing the food on the gameboard
let foodImg = document.createElement("img");
foodImg.className = "food";
foodImg.src = "apple.png";
// Adding food to the gameboad
gameboard.children[ (width * food.y) + food.x].appendChild(foodImg);
// Updates the score on html when point is added
scoreSpan.textContent = score;
}
function relocateFood() {
food = {
x: Math.floor(Math.random() * width),
y: Math.floor(Math.random() * height)
};
if (snake.some(cell => cell.x === food.x && cell.y === food.y)) {
relocateFood();
}
}
onkeydown = (function (keyboardEvent) {
let code = keyboardEvent.code;
console.log(code)
// This triggers game to start if any key is pressed
if (gameState === "ready") {
start();
}
// preventing complete opposite of the previous direction
if (code === "ArrowUp" && previousDirection !== "down") {
direction = "up";
}
if (code === "ArrowDown" && previousDirection !== "up") {
direction = "down";
}
if (code === "ArrowLeft" && previousDirection !== "right") {
direction = "left";
}
if (code === "ArrowRight" && previousDirection !== "left") {
direction = "right";
}
})
function startPlayerTwo() {
score = 0;
scoreSpan = document.querySelector("#score-span-two");
snake = [
{ x: 3, y: 4 },
{ x: 4, y: 4 },
{ x: 5, y: 4 }, // head of snake
]
direction = "right";
previousDirection = "";
timeoutInterval = 300
food = {
x: Math.floor(Math.random() * width),
y: Math.floor(Math.random() * height)
};
gameState = "ready";
}