-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
246 lines (214 loc) · 6.88 KB
/
main.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
// set up canvas
const canvas = document.querySelector('#game');
const ctx = canvas.getContext("2d");
let canvasSize = canvas.width;
const squareSize = (canvasSize / 24);
let fruitsEaten = 0;
let multiplier = 1;
// localStorage
let hiScore = localStorage.getItem('hiScore');
// Snake is an array
let snake = [];
snake[0] = {
x: (canvasSize / 2),
y: (canvasSize / 2)
}
// food is an object
let food = {
x: Math.floor((Math.random() * 24)) * squareSize,
y: Math.floor((Math.random() * 24)) * squareSize
}
// SFXs
const eatAudio = new Audio('sfx/eat.wav');
const dieAudio = new Audio('sfx/die.mp3');
// face direction
let face;
let upButton = document.querySelector('#upButton');
let leftButton = document.querySelector('#leftButton');
let rightButton = document.querySelector('#rightButton');
let downButton = document.querySelector('#downButton');
upButton.onclick = () => {
if (face != "DOWN") face = 'UP'
}
leftButton.onclick = () => {
if (face != "RIGHT") face = 'LEFT'
}
rightButton.onclick = () => {
if (face != "LEFT") face = 'RIGHT'
}
downButton.onclick = () => {
if (face != "UP") face = 'DOWN'
}
document.addEventListener('keydown', faceDirection);
let cheat = 'OFF'
function faceDirection(event) {
let key = event.keyCode;
if (key == 37 && face != 'RIGHT') {
face = 'LEFT'
} else if (key == 38 && face != "DOWN") {
face = 'UP'
}
else if (key == 39 && face != "LEFT") {
face = 'RIGHT'
}
else if (key == 40 && face != "UP") {
face = 'DOWN'
}
else if (key == 32) {
// cheat == 'OFF' ? cheat = 'ON' : cheat = 'OFF'
}
}
// Checks if snakeHead position = snakeBody position, if it is, GameOver
function bumpedInto(head, snake) {
for (let i = 0; i < snake.length; i++) {
if (head.x == snake[i].x && head.y == snake[i].y) {
return true
}
}
}
// generates food randomly
function foodGen(snake) {
food = {
x: Math.floor((Math.random() * 24)) * squareSize,
y: Math.floor((Math.random() * 24)) * squareSize
}
for (let i = 0; i < snake.length; i++) {
if (food.x == snake[i].x && food.y == snake[i].y) {
food = {
x: Math.floor((Math.random() * 24)) * squareSize,
y: Math.floor((Math.random() * 24)) * squareSize
}
}
}
}
// main function
function draw() {
ctx.fillStyle = "#f0f7da"
ctx.fillRect(0, 0, canvasSize, canvasSize)
//draw snake
for (let i = 0; i < snake.length; i++) {
ctx.fillStyle = (i == 0) ? "#234d20" : "#36802d ";
ctx.fillRect(snake[i].x, snake[i].y, squareSize, squareSize);
//better snake ?
ctx.strokeStyle = '#f0f7da'
ctx.strokeRect(snake[i].x, snake[i].y, squareSize, squareSize);
}
//draw food
ctx.fillStyle = '#ff3232'
ctx.strokeStyle = 'red'
ctx.strokeRect(food.x, food.y, squareSize, squareSize)
ctx.fillRect(food.x, food.y, squareSize, squareSize)
let snakeHeadX = snake[0].x;
let snakeHeadY = snake[0].y;
// snake's direction
(face == 'LEFT') ? snakeHeadX -= squareSize : '';
(face == 'UP') ? snakeHeadY -= squareSize : '';
(face == 'RIGHT') ? snakeHeadX += squareSize : '';
(face == 'DOWN') ? snakeHeadY += squareSize : '';
// points System
let score = ((fruitsEaten * 10) * multiplier);
let domScore = document.querySelector('#score');
let domHiScore = document.querySelector('#hiScore');
// checks if snakeHead = FoodPosition
if (snakeHeadX == food.x && snakeHeadY == food.y) {
fruitsEaten++
eatAudio.play()
foodGen(snake)
} else if (cheat == 'OFF') {
localStorage.getItem('hiScore') == null ? hiScore = 0 : '';
// delete snake's tail Position
snake.pop();
}
// new Hi-score system, basically
score > hiScore ? hiScore = score : '';
localStorage.setItem('hiScore', hiScore)
domScore.textContent = `Score:${score}`
domHiScore.textContent = localStorage.getItem('hiScore')
let newSnakeHeadPosition = {
x: snakeHeadX,
y: snakeHeadY
}
// basic
if (snakeHeadX < 0 || snakeHeadX >= canvasSize ||
snakeHeadY < 0 || snakeHeadY >= canvasSize ||
bumpedInto(newSnakeHeadPosition, snake)) {
gameOver()
dieAudio.play()
}
// SnakeHead new position
snake.unshift(newSnakeHeadPosition)
}
//refresh game every 100ms
let gameOn = setInterval(draw, 100);
// dpad movement system
let dPadCenterDefault = document.querySelector('#centerDefault')
let dPadCenterRetry = document.querySelector('#centerRetry')
let gameOverScreen = document.querySelector('#gameOver')
let pcCenterRetry = document.querySelector('#centerRetry2')
//game over...
function gameOver() {
clearInterval(gameOn)
dPadCenterDefault.style.display = 'none';
dPadCenterRetry.style.display = 'table-cell';
gameOverScreen.style.display = 'flex'
console.log('over')
dPadCenterRetry.onclick = () => {
reset()
}
pcCenterRetry.onclick = () => {
reset()
}
}
// reset game
function reset() {
snake = [];
snake[0] = {
x: (canvasSize / 2),
y: (canvasSize / 2)
}
fruitsEaten = 0;
score = 0;
food = {
x: Math.floor((Math.random() * 24)) * squareSize,
y: Math.floor((Math.random() * 24)) * squareSize
}
gameOn = setInterval(draw, 100);
face = 'STOP';
dPadCenterDefault.style.display = 'table-cell';
dPadCenterRetry.style.display = 'none';
gameOverScreen.style.display = 'none'
}
// Detects mobile devices
if ( !/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(window.navigator.userAgent.toLowerCase())) {
console.log('not mobile')
let dPad = document.querySelector('section')
dPad.style.display = 'none'
let howtoPlay = document.querySelector('#howToPlayPc')
howtoPlay.style.display = 'flex';
// setTimeout(timeOutModal = () => {howtoPlay.style.display = 'none'}, 10000);
pcCenterRetry.style.display = 'flex';
};
// Detects if is iOS
const isIos = () => {
let userAgent = window.navigator.userAgent.toLowerCase();
return /iphone|ipad|ipod/.test(userAgent);
}
// Detects if device is in standalone mode
const isInStandaloneMode = () => ('standalone' in window.navigator) && (window.navigator.standalone);
// Checks if should display install popup notification:
if (isIos() && !isInStandaloneMode()) {
let showModal = document.querySelector('.iosAlertBoxWebApp');
showModal.style.animation = 'show 8s ease-in-out 2s'
showModal.style.display = 'flex'
setTimeout(timeOutModal = () => { showModal.style.display = 'none' }, 10000)
}
// Register serviceworker
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('service-worker.js')
.then(function () {
console.log('Service Worker Registered');
}, function (error) {
console.error(error);
});
}