-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakeGame.js
More file actions
269 lines (217 loc) · 6.5 KB
/
Copy pathsnakeGame.js
File metadata and controls
269 lines (217 loc) · 6.5 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const score = document.querySelector(".score--value");
const finalScore = document.querySelector(".final-score > span");
const menu = document.querySelector(".menu-screen");
const buttonPlay = document.querySelector(".btn-play");
const audio = new Audio("../projetos/assets/audio.mp3");
const audioGamerOver = new Audio("../projetos/assets/game-over.mp3");
const audioClique = new Audio("../projetos/assets/clique.mp3");
const size = 30;
// const initialPosition = { x: canvas.width / 2 - size / 2, y: canvas.height / 2 - size / 2 };
const roundedCanvasWidth = Math.floor(canvas.width / size) * size;
const roundedCanvasHeight = Math.floor(canvas.height / size) * size;
const initialPosition = {
x: (roundedCanvasWidth / 2 - size / 2) % size === 0 ? roundedCanvasWidth / 2 - size / 2 : (roundedCanvasWidth / 2) - (roundedCanvasWidth % size) / 2,
y: (roundedCanvasHeight / 2 - size / 2) % size === 0 ? roundedCanvasHeight / 2 - size / 2 : (roundedCanvasHeight / 2) - (roundedCanvasHeight % size) / 2
};
let snake = [initialPosition]
let audioPlayed = false; // Variável de controle
let wallCollision = false; // Defina a variável wallCollision como global
let selfCollision = false; // Defina a variável selfCollision como global
/* document.body.style.overflow = "hidden";
function atualizarEscala() {
const alturaJanela = window.innerHeight;
let scale = 1.0;
if (alturaJanela > 1920) {
scale = 1.5;
} else if (alturaJanela > 1600) {
scale = 1.4;
} else if (alturaJanela > 1366) {
scale = 1.3;
} else if (alturaJanela > 1000) {
scale = 1.1;
} else if (alturaJanela > 900) {
scale = 1.2;
} else if (alturaJanela > 800) {
scale = 1.0;
} else if (alturaJanela > 700) {
scale = 0.9;
} else if (alturaJanela > 600) {
scale = 0.8;
} else if (alturaJanela > 550) {
scale = 0.7;
} else if (alturaJanela < 500) {
scale = 0.5;
}
document.body.style.transform = `scale(${scale})`;
}
window.addEventListener("resize", atualizarEscala);
atualizarEscala(); */
const incrementScore = () => {
score.innerText = + score.innerText + 10
}
const randomNumber = (min, max) => {
return Math.round(Math.random() * (max - min) + min)
}
const randomPositionWidth = () => {
const number = randomNumber(0, canvas.width - size)
return Math.round(number / 30) * 30
}
const randomPositionHeight = () => {
const number = randomNumber(0, canvas.height - size)
return Math.round(number / 30) * 30
}
const randomColor = () => {
const red = randomNumber(0, 255)
const green = randomNumber(0, 255)
const blue = randomNumber(0, 255)
return `rgb(${red}, ${green}, ${blue})`
}
const food = {
x: randomPositionWidth(),
y: randomPositionHeight(),
color: randomColor()
}
let direction, loopId
const drawFood = () => {
const { x, y, color } = food
ctx.shadowColor = color
ctx.shadowBlur = 6
ctx.fillStyle = color
ctx.fillRect(x, y, size, size)
ctx.shadowBlur = 0
}
const drawSnake = () => {
ctx.fillStyle = "#ddd"
snake.forEach((position, index) => {
if (index == snake.length - 1) {
ctx.fillStyle = "white"
}
ctx.fillRect(position.x, position.y, size, size)
})
}
const moveSnake = () => {
if (!direction) return
const head = snake[snake.length - 1]
if (direction == "right") {
snake.push({ x: head.x + size, y: head.y })
}
if (direction == "left") {
snake.push({ x: head.x - size, y: head.y })
}
if (direction == "down") {
snake.push({ x: head.x, y: head.y + size })
}
if (direction == "up") {
snake.push({ x: head.x, y: head.y - size })
}
snake.shift()
}
const drawGrid = () => {
ctx.lineWidth = 1
ctx.strokeStyle = "#f1f1f10a"
for (let i = 30; i < canvas.width; i += 30) {
ctx.beginPath()
ctx.lineTo(i, 0)
ctx.lineTo(i, canvas.width)
ctx.stroke()
ctx.beginPath()
ctx.lineTo(0, i)
ctx.lineTo(canvas.height, i)
ctx.stroke()
}
}
const checkEat = () => {
const head = snake[snake.length - 1]
if (head.x == food.x && head.y == food.y) {
incrementScore()
snake.push(head)
audio.play()
let x = randomPositionWidth()
let y = randomPositionHeight()
while (snake.find((position) => position.x == x && position.y == y)) {
x = randomPositionWidth()
y = randomPositionHeight()
}
food.x = x
food.y = y
food.color = randomColor()
}
}
const checkCollision = () => {
const head = snake[snake.length - 1]
const canvasLimitWidth = canvas.width - size
const canvasLimitHeight = canvas.height - size
const nextIndex = snake.length - 2
wallCollision =
head.x < 0 || head.x > canvasLimitWidth || head.y < 0 || head.y > canvasLimitHeight
selfCollision = snake.find((position, index) => {
return index < nextIndex && position.x == head.x && position.y == head.y
})
if (wallCollision || selfCollision) {
gameOver()
if (!audioPlayed) { // Verifique se o áudio ainda não foi reproduzido
audioGamerOver.play();
audioPlayed = true; // Defina a variável para true para indicar que o áudio foi reproduzido
}
}
}
const gameOver = () => {
direction = undefined
menu.style.display = "flex"
finalScore.innerText = score.innerText
canvas.style.filter = "blur(4px)"
}
const gameLoop = () => {
clearInterval(loopId)
ctx.clearRect(0, 0, canvas.width, canvas.height)
drawGrid()
drawFood()
moveSnake()
drawSnake()
checkEat()
checkCollision()
loopId = setTimeout(() => {
gameLoop()
}, 180)
}
gameLoop()
document.addEventListener("keydown", ({ key }) => {
if (key == "ArrowRight" && direction !== "left") {
if (!(wallCollision || selfCollision)) {
direction = "right"
audioClique.play()
}
}
if (key == "ArrowLeft" && direction !== "right") {
if (!(wallCollision || selfCollision)) {
direction = "left"
audioClique.play()
}
}
if (key == "ArrowDown" && direction !== "up") {
if (!(wallCollision || selfCollision)) {
direction = "down"
audioClique.play()
}
}
if (key == "ArrowUp" && direction !== "down") {
if (!(wallCollision || selfCollision)) {
direction = "up"
audioClique.play()
}
}
if (key == "Enter" || key == "Return") {
if (wallCollision || selfCollision) {
buttonPlay.click() // Simula o clique no botão "Play"
}
}
})
buttonPlay.addEventListener("click", () => {
score.innerText = "00"
menu.style.display = "none"
canvas.style.filter = "none"
audioPlayed = false
snake = [initialPosition]
})