-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
239 lines (213 loc) · 9.54 KB
/
app.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
document.addEventListener('DOMContentLoaded', ()=> {
const scoreDisplay=document.getElementById('score')
const width=28
let score=0
const grid=document.querySelector('.grid')
//0- pac-dots
//1- wall
//2-gost-lair
//3-power-pellet
//4-empty
const layout = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 3, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 3, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 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, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 3, 1,
1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1,
1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
1, 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, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
]
//create board
const squares=[]
function createBoard(){
for (let i=0;i<layout.length;i++){
const square=document.createElement('div')
square.id = i;
grid.appendChild(square)
squares.push(square)
//add layout to square
if(layout[i]===0){
squares[i].classList.add('pac-dot')
}
if(layout[i]===1){
squares[i].classList.add('wall')
}
if(layout[i]===2){
squares[i].classList.add('gost-lair')
}
if(layout[i]===3){
squares[i].classList.add('power-pellet')
}
}
}
createBoard()
//create characters
// draw pac man to the board
let pacmanCurrentIndex=490
squares[pacmanCurrentIndex].classList.add('pac-man')
function movePacman(e){
squares[pacmanCurrentIndex].classList.remove('pac-man')
switch (e.key){
case 'ArrowLeft':
if( pacmanCurrentIndex % width !==0 &&
!squares[pacmanCurrentIndex -1].classList.contains('wall') &&
!squares[pacmanCurrentIndex -1].classList.contains('ghost-lair')){
pacmanCurrentIndex -= 1
}
if(squares[pacmanCurrentIndex -1] === squares[363]){
pacmanCurrentIndex = 391
}
break
case 'ArrowRight':
if ( pacmanCurrentIndex % width < width -1 &&
!squares[pacmanCurrentIndex +1].classList.contains('wall') &&
!squares[pacmanCurrentIndex +1].classList.contains('ghost-lair')
){
pacmanCurrentIndex +=1
}
if(squares[pacmanCurrentIndex +1] === squares[392]){
pacmanCurrentIndex = 364
}
break
case 'ArrowUp':
if(
pacmanCurrentIndex - width >= 0 &&
!squares[pacmanCurrentIndex - width].classList.contains('wall') &&
!squares[pacmanCurrentIndex - width].classList.contains('ghost-lair')
){
pacmanCurrentIndex -= width
}
break
case 'ArrowDown':
if(
pacmanCurrentIndex + width < width * width &&
!squares[pacmanCurrentIndex + width].classList.contains('wall') &&
!squares[pacmanCurrentIndex + width].classList.contains('ghost-lair')
){
pacmanCurrentIndex +=width
}
break
}
squares[pacmanCurrentIndex].classList.add('pac-man')
pacDotEaten()
powerPelletEaten()
// checkForGameOver()
checkForWin()
}
document.addEventListener('keyup',movePacman)
//what Happens When Eaten a pacdot
function pacDotEaten(){
if(squares[pacmanCurrentIndex].classList.contains('pac-dot')){
score++
scoreDisplay.innerHTML = score
squares[pacmanCurrentIndex].classList.remove('pac-dot')
}
}
//what Happens When Eaten a powerPelletEaten
function powerPelletEaten(){
if(squares[pacmanCurrentIndex].classList.contains('power-pellet')){
score+=10
scoreDisplay.innerHTML = score
ghosts.forEach(ghost =>ghost.isScared = true)
setTimeout(unScareGhost, 10000)
squares[pacmanCurrentIndex].classList.remove('power-pellet')
}
}
//make gost stop flash
function unScareGhost(){
ghosts.forEach(ghost=>ghost.isScared = false)
}
//create ghosts using constructor
class Ghost{
constructor(className,startIndex,speed) {
this.className = className
this.startIndex = startIndex
this.speed = speed
this.currentIndex = startIndex
this.isScared = false
this.timerId = NaN
}
}
const ghosts = [
new Ghost ('blinky1',348,258),
new Ghost ('pinky',378,400),
new Ghost ('inky',351,300),
new Ghost ('clyde',379,500)
]
//draw ghosts in the board
ghosts.forEach(ghost => {
squares[ghost.currentIndex].classList.add(ghost.className,'ghost')
})
//move ghosts randomly
ghosts.forEach(ghost => moveGhost(ghost))
function moveGhost(ghost){
//console.log(ghost)
const directions = [-1,+1,-width,+width]
let direction=directions[Math.floor(Math.random() * directions.length)]
ghost.timerId = setInterval(function (){
//no ghost no wall
if(
!squares[ghost.currentIndex + direction].classList.contains('ghost') &&
!squares[ghost.currentIndex + direction].classList.contains('wall')
){
squares[ghost.currentIndex].classList.remove(ghost.className,'ghost','scared-ghost')
ghost.currentIndex += direction
squares[ghost.currentIndex].classList.add(ghost.className,'ghost')
}else direction=directions[Math.floor(Math.random() * directions.length)]
//only if ghost is scared
if(ghost.isScared){
squares[ghost.currentIndex].classList.add('scared-ghost')
}
//if ghost is scared and pacman on it
if(ghost.isScared && squares[ghost.currentIndex].classList.contains('pac-man')){
ghost.isScared = false
squares[ghost.currentIndex].classList.remove(ghost.className,'ghost','scared-ghost')
ghost.currentIndex = ghost.startIndex
score +=100
scoreDisplay.innerHTML = score
squares[ghost.currentIndex].classList.add(ghost.className, 'ghost')
}
//check for game over
checkForGameOver()
},ghost.speed)
//check for game over
function checkForGameOver(){
if(squares[pacmanCurrentIndex].classList.contains('ghost') &&
!squares[pacmanCurrentIndex].classList.contains('scared-ghost'))
{
ghosts.forEach(ghost => clearInterval(ghost.timerId))
document.removeEventListener('keyup',movePacman)
setTimeout(function (){alert('Game Over')},500)
}
}
}
function checkForWin()
{
if(score >= 274){
ghosts.forEach(ghost=> clearInterval(ghost.timerId))
document.removeEventListener('keyup',movePacman)
setTimeout(function (){alert('You Win')},500)
}
}
})