-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
439 lines (277 loc) · 12.2 KB
/
Copy pathapp.js
File metadata and controls
439 lines (277 loc) · 12.2 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// JavaScript source code
//global variables
let pacmanCurrentindex = 490
const width = 28
const squares = []
let powerpellet = document.querySelectorAll('.power-pellet')
const scoreDisplay = document.getElementById('score')
let score = 0
const directions = [-1, 1, width, -width]
//declaring ghosts globally
var t
setTimeout(t,10000)
t=document.addEventListener('keydown', () => {
class ghost {
constructor(className, startIndex, speed) {
this.className = className
this.startIndex = startIndex
this.speed = speed
this.currentIndex = startIndex
this.timerId = NaN
}
}
let ghosts =
[
new ghost('blinky', 444, 1150),
new ghost('pinky', 376, 800),
new ghost('inky', 351, 700),
new ghost('clyde', 379,900)
]
function moveGhosts(ghost) {
ghost.timerId = setInterval(function () {
//rules for motion of ghosts
// if move valid
let direction = directions[Math.floor(Math.random() * directions.length)]
if (!squares[ghost.currentIndex + direction].classList.contains('wall') && !squares[ghost.currentIndex + direction].classList.contains('ghosts')) {
//if (squares[ghost.currentIndex + direction].classList.contains('wall') || squares[ghost.currentIndex+direction)
squares[ghost.currentIndex].classList.remove(ghost.className, 'ghosts')
//move the ghosts in normal directions
ghost.currentIndex += direction*2
squares[ghost.currentIndex].classList.add(ghost.className, 'ghosts')
}
//else if not valid
else {
let direction = directions[Math.floor(Math.random() * directions.length)]
squares[ghost.currentIndex].classList.remove(ghost.className, 'ghosts')
ghost.currentIndex += direction
squares[ghost.currentIndex].classList.add(ghost.className, 'ghosts')
}
//find another direction
}, ghost.speed)
}
ghosts.forEach(ghost => {
squares[ghost.currentIndex].classList.add(ghost.className)
squares[ghost.currentIndex].classList.add('ghosts')
})
ghosts.forEach(ghost => {
moveGhosts(ghost)
})
})
const layout =
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 4, 0,
0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0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3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
document.addEventListener('DOMContentLoaded', () =>
{
/*
function shine()
{
let randomShinyColor = [
'blue',
'lightblue',
'black',
'darkblue'
]
powerpellet.backgroundColor = randomShinyColor[Math.floor(Math.random() * randomShinyColor.length)]
}
window.setInterval(shine(),100)
*/
const grid = document.querySelector('.grid')
//layout of the grid and the squares components
//character for the numbers in the array
//0 for blank
//1 for wall
//2 for pacman
//3 pac-dot
//4 power-pellet
//5 ghosts
//creating a container or grid for the game
function createBoard() {
for (let i = 0; i < layout.length; i++) {
const square = document.createElement('div')
grid.appendChild(square)
squares.push(square)
//adding layout to the board
if (layout[i] === 0) {
squares[i].classList.add('blank')
}
else if (layout[i] === 1)
{
squares[i].classList.add('wall')
}
else if (layout[i] === 3)
{
squares[i].classList.add('pac-dot')
}
else if (layout[i] === 4) {
squares[i].classList.add('power-pellet')
}
else if (layout[i] ===5) {
squares[i].classList.add('ghosts')
}
}
squares[pacmanCurrentindex].classList.add('pac-man')
}
createBoard()
//passing the number of pac-man to the array of layout
// squares[pacmanCurrentindex].classList.add('pac-man')
})
//making the start position of pac-man
/*
function startPosition() {
let randomPosition = [5, 7, 12, 34, 56, 67, 87, 120, 180, 250, 400]
pacmanCurrentindex = randomPosition[Math.floor(Math.random() * randomPosition.length)]
if (squares[pacmanCurrentindex].classList.contains('wall'))
pacmanCurrentindex=459
}
startPosition()
*/
//moving pac-man by using keycodes (can rely on instructions)
function movePacMan(e) {
squares[pacmanCurrentindex].classList.remove('pac-man')
//using keycodes for moving pacman
//browse for the keycodes ---- arrow left 37, arrow up 38 ,arrow right 39, arrow down 40,
switch (e.keyCode) {
//allowing movement in the whole row and the whole column
case 37:
if (pacmanCurrentindex - 1 !== '' && !squares[pacmanCurrentindex - 1].classList.contains('wall') && !squares[pacmanCurrentindex - 1].classList.contains('ghosts'))
pacmanCurrentindex--
/*if (squares[pacmanCurrentindex - 1] % width === 0)
pacmanCurrentindex = width
*/
//left exit
if ((pacmanCurrentindex - 1) === 363) {
pacmanCurrentindex = 391
}
break
case 38:
if (pacmanCurrentindex - width >= 0 && !squares[pacmanCurrentindex - width].classList.contains('wall') && !squares[pacmanCurrentindex - width].classList.contains('ghosts'))
pacmanCurrentindex -= width
break
case 39:
if (pacmanCurrentindex % width < width - 1 && !squares[pacmanCurrentindex + 1].classList.contains('wall') && !squares[pacmanCurrentindex + 1].classList.contains('ghosts'))
pacmanCurrentindex++
/* if (squares[pacmanCurrentindex - 1] % width===0)
pacmanCurrentindex +=width
*/
//right exit
if ((pacmanCurrentindex + 1) === 392) {
pacmanCurrentindex = 364
}
break
case 40:
if (pacmanCurrentindex + width < layout.length && !squares[pacmanCurrentindex + width].classList.contains('wall') && !squares[pacmanCurrentindex + width].classList.contains('ghosts'))
pacmanCurrentindex += width
break
}
squares[pacmanCurrentindex].classList.add('pac-man')
function pacDotEaten() {
//when pacman eats a pacDot
if (squares[pacmanCurrentindex].classList.contains('pac-dot')) {
/*score++
scoreDisplay.innerHTML = score*/
squares[pacmanCurrentindex].classList.remove('pac-dot')
}
}
pacDotEaten()
function powerPelletEaten() {
if (squares[pacmanCurrentindex].classList.contains('power-pellet'))
{
squares[pacmanCurrentindex].classList.remove('power-pellet')
squares[pacmanCurrentindex].classList.remove('pacman')
//squares[pacmanCurrentindex].style.backgroundColor='black'
}
}
powerPelletEaten()
}
//add event listeners to help execute the above code
document.addEventListener('keydown', movePacMan)
//creating objects (not OOP:OOP is for classes and objects of classes ) tutorial by w3schools
//creating a direct instance of an object
//create our ghost template
/* my way
Ghost(ghost1,34,1)
Ghost(ghost2,67,5)
Ghost(ghost3,54,10)
*/
//ania s way
//draw ghosts onto the grid
//adding ghosts by the forEach method
/*
ghosts.forEach(ghost => {
squares[ghost.currentIndex].classList.add(ghost.className)
squares[ghost.currentIndex].classList.add('ghosts')
})
*/
//ghosts moving randomly
//ghosts.forEach(ghost => moveGhost(ghost))
//function to move ghosts
/* will be fixed
function moveGhost(ghost)
{
const directions = [-1, +1, width, -width]
let direction = Math.floor(Math.random() * directions.length)
ghost.timerId = setInterval(function () {
// if ghost movement restrictions wall,ghost,
if (!squares[currentIndex + direction].classList.contains('wall') && !squares[currentIndex + direction].classList.contains('ghosts')) {
//removing all the ghost related classes
squares[currentIndex + direction].classList.remove(ghost.className, 'ghost', 'scared-ghost')
//new position
ghost.currentIndex += direction
//redraw the ghost in a new place
squares[currentIndex + direction].classList.add(ghost.className, 'ghost', 'scared-ghost')
}
else
{
direction = Math.floor(Math.random() * directions.length)
squares[currentIndex + direction].classList.remove(ghost.className, 'ghost', 'scared-ghost')
ghost.currentIndex += direction
squares[currentIndex + direction].classList.add(ghost.className, 'ghost', 'scared-ghost')
}
//else find another direction to go
},ghost.speed)
}
*/
//functions to do later
//noEntry() // prevent pacman from entering the maze of ghosts
//powerPelletEaten()
//checkForGameOver()
//checkForWin()
/*function pacDotEaten()
{
//when pacman eats a pacDot
if (squares[pacmanCurrentindex].classList.contains('pac-dot') )
{
squares[pacmanCurrentindex].classList.remove('pac-dot')
score++
scoreDisplay.innerHTML = score
}
}
*/