-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
252 lines (229 loc) · 6.81 KB
/
index.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const gameContainer = document.getElementById("gameContainer")
const gameBoard = document.getElementById("gameBoard")
const context = gameBoard.getContext("2d")
const scoreText = document.getElementById("scoreText")
const resetBtn = document.getElementById("resetBtn")
const gameWidth = gameBoard.width
const gameHeight = gameBoard.height
const boardColor = "linen"
const snakeColor = "lightgreen"
const foodColor = "tomato"
const unitSize = 50
const image = new Image(100, 50)
image.src = "Snake-head.png"
let running = false
let win = false
let xVel = unitSize
let yVel = 0
let foodX
let foodY
let score = 0
let snake = [
{ x: unitSize * 3, y: 100 },
{ x: unitSize * 2, y: 100 },
{ x: unitSize, y: 100 }
]
let timeOut
window.addEventListener("keydown", changeDir)
resetBtn.addEventListener("click", restart)
displayLoadingScreen()
function displayLoadingScreen() {
context.fillStyle = foodColor
context.fillRect(50, 100, unitSize * 3, unitSize)
context.fillStyle = snakeColor
setTimeout(() => {
context.drawImage(image, 50, 100)
score += 1
scoreText.textContent = "Loading... " + score * 4 + "%"
}, 700);
setTimeout(() => {
context.fillRect(50, 100, 100, 50)
context.drawImage(image, 100, 100)
score += 1
scoreText.textContent = "Loading... " + score * 4 + "%"
}, 1500);
setTimeout(() => {
context.fillRect(50, 100, 150, 50)
context.drawImage(image, 150, 100)
score += 1
scoreText.textContent = "Loading... " + score * 4 + "%"
start()
}, 2500);
}
function start() {
clearBoard()
running = true
scoreText.textContent = "Loading... " + score * 4 + "%"
drawSnake()
createFood()
drawFood()
nextTick()
}
function nextTick() {
if (running) {
timeOut = setTimeout(() => {
clearBoard()
drawFood()
moveSnake()
drawSnake()
checkWin()
checkGameOver()
nextTick()
}, 400)
}
else if (!win) {
displayGameOver()
}
}
function clearBoard() {
context.fillStyle = boardColor
context.fillRect(0, 0, gameWidth, gameHeight)
}
function createFood() {
function generatePos(min, max) {
const rand = Math.round((Math.random() * (max - min) + min) / unitSize) * unitSize
return rand
}
let empty = true
do {
foodX = generatePos(0, gameWidth - unitSize)
foodY = generatePos(0, gameHeight - unitSize)
empty = true
snake.forEach(part => {
if (part.x == foodX && part.y == foodY) {
empty = false
}
})
} while (!empty)
}
function drawFood() {
context.fillStyle = foodColor
context.fillRect(foodX, foodY, unitSize, unitSize)
}
function moveSnake() {
const head = { x: snake[0].x + xVel, y: snake[0].y + yVel }
snake.unshift(head)
if (head.x == foodX && head.y == foodY) {
score += 1
scoreText.textContent = "Loading... " + score * 4 + "%"
createFood()
}
else {
snake.pop()
}
}
function drawSnake() {
context.fillStyle = snakeColor
const up = (yVel == -unitSize)
const down = (yVel == unitSize)
const left = (xVel == -unitSize)
for (let i = 0; i < snake.length; i += 1) {
part = snake[i]
if (i == 0) {
context.save()
context.translate(part.x + unitSize / 2, part.y + unitSize / 2)
switch (true) {
case (up):
context.rotate(-Math.PI / 2)
break
case (down):
context.rotate(Math.PI / 2)
break
case (left):
context.rotate(Math.PI)
break
}
context.translate(-25, -25)
context.drawImage(image, 0, 0)
context.restore()
continue
}
context.fillRect(part.x, part.y, unitSize, unitSize)
}
}
function changeDir(event) {
const keypressed = event.keyCode
const al = 37
const au = 38
const ar = 39
const ad = 40
const a = 65
const w = 87
const d = 68
const s = 83
const up = (yVel == -unitSize)
const down = (yVel == unitSize)
const left = (xVel == -unitSize)
const right = (xVel == unitSize)
switch (true) {
case ((keypressed == al || keypressed == a) && !right):
xVel = -unitSize
yVel = 0
break
case ((keypressed == au || keypressed == w) && !down):
xVel = 0
yVel = -unitSize
break
case ((keypressed == ar || keypressed == d) && !left):
xVel = unitSize
yVel = 0
break
case ((keypressed == ad || keypressed == s) && !up):
xVel = 0
yVel = unitSize
break
}
}
function checkGameOver() {
switch (true) {
case (snake[0].x < 0):
running = false
break
case (snake[0].x >= gameWidth):
running = false
break
case (snake[0].y < 0):
running = false
break
case (snake[0].y >= gameHeight):
running = false
break
}
for (let i = 1; i < snake.length; i += 1) {
if (snake[i].x == snake[0].x && snake[i].y == snake[0].y) {
running = false
}
}
}
function displayGameOver() {
context.fillStyle = "crimson"
context.font = "2em New Amsterdam"
context.textAlign = "center"
context.fillText("404 PAGE NOT FOUND", gameWidth / 2, gameHeight / 2)
}
function checkWin() {
if (score >= 25){
running = false
win = true
context.fillStyle = "limegreen"
context.font = "2em New Amsterdam"
context.textAlign = "center"
context.fillText("LOADING COMPLETE", gameWidth / 2, gameHeight / 2)
setTimeout(() => {
gameContainer.outerHTML = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/dQw4w9WgXcQ?si=uyJQYaR9u39ZOMgw&controls=0\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay=1; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen></iframe>"
}, 1000);
}
}
function restart() {
score = 0
xVel = unitSize
yVel = 0
snake = [
{ x: unitSize * 3, y: 100 },
{ x: unitSize * 2, y: 100 },
{ x: unitSize, y: 100 }
]
clearTimeout(timeOut)
clearBoard()
displayLoadingScreen()
}