-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
378 lines (328 loc) · 12 KB
/
script.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// All gameboy and background elements var
const container = document.querySelector('.container')
const gameboy = container.querySelector('.gameboy')
const screen = document.querySelector('.screen')
const gameboyPivot = gameboy.querySelector('.pivot')
const gameboyFaces = Array.from(gameboy.querySelectorAll('.gameboy-face'))
const backgroundElements = container.querySelector('.background-elements')
const bButton = backgroundElements.querySelector('.b-button')
const bButtonPivot = bButton.querySelector('.b-button .pivot')
const bButtonZNegFace = bButtonPivot.querySelector('.b-button .zneg-face')
const backgroundButtons = Array.from(backgroundElements.querySelectorAll('.background-buttons'))
const aButton = backgroundElements.querySelector('.a-button')
const aButtonPivot = aButton.querySelector('.a-button .pivot')
const aButtonZNegFace = aButtonPivot.querySelector('.a-button .zneg-face')
const crossButtonPivot = backgroundElements.querySelector('.pivot')
const gameButtons = gameboy.querySelector('.game-buttons')
const activeAButton = gameButtons.querySelector('.active-a-button')
const activeBButton = gameButtons.querySelector('.active-b-button')
const crossFaces = Array.from(gameButtons.querySelectorAll('.cross-button-section'))
const activeCrossButton = gameButtons.querySelector('.active-cross-button')
const topCrossButton = activeCrossButton.querySelector('.top-button')
const leftCrossButton = activeCrossButton.querySelector('.left-button')
const rightCrossButton = activeCrossButton.querySelector('.right-button')
const bottomCrossButton = activeCrossButton.querySelector('.bottom-button')
const gameContainer = screen.querySelector('.game-container')
const scoreContent = screen.querySelector('.score')
const menu = document.querySelector('.menu')
const playButton = menu.querySelector('.play-button')
const replayButton = menu.querySelector('.replay-button')
const music = new Audio('ressources/background-music.wav')
let gameboyRotationAnimation
let gameboyRotationX = 0
let gameboyRotationY = 0
let rotationYVariation = 'plus'
const colorTheme = { blackAndWhite : {globalColor : '#000000',
borderScreenColor : '#ffffff',
screenColor : '#000000'},
yellow : {globalColor : '#ffee00',
borderScreenColor : '#ff9d00',
screenColor : '#ffe5a4'},
blue : {globalColor : '#00c3ff',
borderScreenColor : '#0048e3',
screenColor : '#c3f1ff'},
normal : {globalColor : '#4f00b0',
borderScreenColor : '#28186f',
screenColor : '#9656ff'}}
///// GAMEBOY PART /////
const blackAndWhiteButton = menu.querySelector('.black-and-white')
const yellowButton = menu.querySelector('.yellow')
const blueButton = menu.querySelector('.blue')
const normal = menu.querySelector('.normal')
let colorSelected = colorTheme.normal
blackAndWhiteButton.addEventListener('click', () =>
{
colorSelected = colorTheme.blackAndWhite
scoreContent.style.color = `${colorSelected.borderScreenColor}`
screen.style.background = `${colorSelected.borderScreenColor}`
activeAButton.style.background = `${colorSelected.borderScreenColor}`
activeBButton.style.background = `${colorSelected.borderScreenColor}`
for(const face of gameboyFaces)
{
face.style.background = `${colorSelected.globalColor}`
}
for(const crossFace of crossFaces)
{
crossFace.style.background = `${colorSelected.borderScreenColor}`
}
})
yellowButton.addEventListener('click', () =>
{
colorSelected = colorTheme.yellow
scoreContent.style.color = `${colorSelected.borderScreenColor}`
screen.style.background = `${colorSelected.borderScreenColor}`
activeAButton.style.background = `${colorSelected.borderScreenColor}`
activeBButton.style.background = `${colorSelected.borderScreenColor}`
for(const face of gameboyFaces)
{
face.style.background = `${colorSelected.globalColor}`
}
for(const crossFace of crossFaces)
{
crossFace.style.background = `${colorSelected.borderScreenColor}`
}
})
blueButton.addEventListener('click', () =>
{
colorSelected = colorTheme.blue
scoreContent.style.color = `${colorSelected.borderScreenColor}`
screen.style.background = `${colorSelected.borderScreenColor}`
activeAButton.style.background = `${colorSelected.borderScreenColor}`
activeBButton.style.background = `${colorSelected.borderScreenColor}`
for(const face of gameboyFaces)
{
face.style.background = `${colorSelected.globalColor}`
}
for(const crossFace of crossFaces)
{
crossFace.style.background = `${colorSelected.borderScreenColor}`
}
})
normal.addEventListener('click', () =>
{
colorSelected = colorTheme.normal
scoreContent.style.color = `${colorSelected.borderScreenColor}`
screen.style.background = `${colorSelected.borderScreenColor}`
activeAButton.style.background = `${colorSelected.borderScreenColor}`
activeBButton.style.background = `${colorSelected.borderScreenColor}`
for(const face of gameboyFaces)
{
face.style.background = `${colorSelected.globalColor}`
}
for(const crossFace of crossFaces)
{
crossFace.style.background = `${colorSelected.screenColor}`
}
})
// Display all the face on the decorations buttons to make the circle
const borderFaces = (button) =>
{
// Make 90 divs to make the round effect look real
for(i = 0; i < 90; i++)
{
const face = document.createElement('div')
face.classList.add('round-face')
face.style.width = `${(2 * Math.PI * 75) / 45}px`
const faceColor = '#19087c'
face.style.background = `${faceColor}`
const containerFace = document.createElement('div')
containerFace.classList.add('container-face')
containerFace.style.transform = `rotateZ(${i*4}deg) translateZ(20px)`
button.appendChild(containerFace)
containerFace.appendChild(face)
}
}
// Call our function for a button and b button
borderFaces(bButtonZNegFace)
borderFaces(aButtonZNegFace)
// Listen the click on the play button to lunch the sound and the interactions with the gameboy
playButton.addEventListener('click' , () =>
{
lunchGame()
})
replayButton.addEventListener('click' , () =>
{
lunchGame()
})
const lunchGame = () =>
{
menu.style.display = 'none'
gameContainer.style.background = `${colorSelected.screenColor}`
playButton.style.display = 'none'
gameboyPivot.style.transform = 'rotateX(0deg) rotateY(0deg)'
gameboy.style.transform = 'translateZ(0px)'
clearInterval(gameboyRotationAnimation)
music.loop = true
music.volume = 0.2
music.play()
}
// Gmae boy animation whe we are on the first page
gameboyRotationAnimation = setInterval(() => {
gameboyRotation()
}, 100);
const gameboyRotation = () =>
{
if(gameboyRotationX == 360)
gameboyRotationX = 0
if(gameboyRotationY == 20)
rotationYVariation = 'minus'
if(gameboyRotationY == -20)
rotationYVariation = 'plus'
if(rotationYVariation == 'minus')
gameboyRotationY = gameboyRotationY - 1
if(rotationYVariation == 'plus')
gameboyRotationY = gameboyRotationY + 1
gameboyRotationX = gameboyRotationX + 2
gameboyPivot.style.transform = `rotateX(${gameboyRotationY}deg) rotateY(${gameboyRotationX}deg)`
}
// INTERACTIONS PART /////
// All the var of the buttons on the gamboiy face
const width = 30
let currentIndex = 0
let appleIndex = 0
let currentSnake = [2,1,0]
let direction = 1
let previousDirection = 1
let score = 0
let speed = 0.9
let intervalTime = 0
let interval = 0
// Crete all ours div
for(let i=0; i<900; i++)
{
const block = document.createElement('div')
block.classList.add('blocs')
gameContainer.appendChild(block)
}
// Get all the blocks that we have previously set
const squares = Array.from(document.querySelectorAll('.blocs'))
// Create the fucntion to start teh game, we set up all our vars for the game
function startGame() {
randomApple()
direction = 1
intervalTime = 200
currentSnake = [2,1,0]
currentIndex = 0
currentSnake.forEach(index => squares[index].classList.add('snake'))
// Make our loop start
interval = setInterval(moveOutcomes, intervalTime)
}
// test all snake moves
function moveOutcomes()
{
// check if he hit a wall
if(
(currentSnake[0] + width >= (width * width) && direction == width) ||
(currentSnake[0] % width == width -1 && direction == 1) ||
(currentSnake[0] % width == 0 && direction == -1) ||
(currentSnake[0] - width < 0 && direction == -width) ||
squares[currentSnake[0] + direction].classList.contains('snake') && direction != - previousDirection)
{
// if he hit we get him back to menu
loseDisplay()
// And stop the loop
return clearInterval(interval)
}
// Mange to make the snake correctly with the tail that has to follow the head
const tail = currentSnake.pop()
squares[tail].classList.remove('snake')
currentSnake.unshift(currentSnake[0] + direction)
// Check if he eat an apple
if(squares[currentSnake[0]].classList.contains('apple'))
{
// Remove the apple
squares[currentSnake[0]].classList.remove('apple')
// Add a snake lenght
squares[tail].classList.add('snake')
currentSnake.push(tail)
// Create a new apple
randomApple()
// increase the score
score++
scoreContent.textContent = score
// Clear intervall to speed it up^right after
clearInterval(interval)
// Speed the interval
intervalTime = intervalTime * speed
// Restart the interval
interval = setInterval(moveOutcomes, intervalTime)
}
// Make the nsake move
squares[currentSnake[0]].classList.add('snake')
}
// Function that make randmolmy span an apple
function randomApple()
{
// Ask to make a random position for the apple as long as the apple dont spawn on the snake, if it does, the instreuction restart
do
{
appleIndex = Math.floor(Math.random() * squares.length)
}
while(squares[appleIndex].classList.contains('snake'))
squares[appleIndex].classList.add('apple')
}
// Set our controls
function control(e) {
if(e.key === 'ArrowRight' || e.key == 'd')
{
previousDirection = direction
direction = 1
}
else if (e.key === 'ArrowUp' || e.key == 'z')
{
previousDirection = direction
direction = -width
}
else if (e.key === 'ArrowLeft' || e.key == 'q')
{
previousDirection = direction
direction = -1
}
else if (e.key === 'ArrowDown' || e.key == 's')
{
previousDirection = direction
direction = +width
}
}
topCrossButton.addEventListener('click', (e) =>
{
console.log('click')
previousDirection = direction
direction = -width
})
leftCrossButton.addEventListener('click', (e) =>
{
previousDirection = direction
direction = -1
})
rightCrossButton.addEventListener('click', (e) =>
{
previousDirection = direction
direction = 1
})
bottomCrossButton.addEventListener('click', (e) =>
{
previousDirection = direction
direction = +width
})
document.addEventListener('keydown', control)
playButton.addEventListener('click', startGame)
replayButton.addEventListener('click', startGame)
const loseDisplay = () =>
{
menu.style.display = 'flex'
gameboyRotationX = 0
gameboyRotationY = 0
currentSnake.forEach(index => squares[index].classList.remove('snake'))
squares[appleIndex].classList.remove('apple')
score = 0
gameContainer.style.background = `url(ressources/snake-screen.jpg)`
scoreContent.textContent = ''
replayButton.style.display = 'flex'
gameboyPivot.style.transform = 'rotateX(0deg) rotateY(1deg)'
gameboy.style.transform = 'translateZ(-500px)'
gameboyRotationAnimation = setInterval(() => {
gameboyRotation()
}, 100);
screen.style.display = 'flex'
}