-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
178 lines (160 loc) · 5.82 KB
/
Copy pathapp.js
File metadata and controls
178 lines (160 loc) · 5.82 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
import {
layout
} from './layout.js';
const width = 28;
var score = Number(document.querySelector('.score').innerHTML)
var pacmanCurrentIndex = 490;
var gameIsStarted = false;
var squares = []
document.addEventListener('DOMContentLoaded', () => {
const grid = document.querySelector('.grid');
function InitialLayout() {
layout.forEach((element, index) => {
const square = document.createElement('div');
grid.appendChild(square);
squares.push(square);
if (element === 0) {
squares[index].classList.add('pac-dot')
} else if (element === 1) {
squares[index].classList.add('wall')
} else if (element === 3) {
squares[index].classList.add('power-pellet')
}
});
}
InitialLayout();
class Ghost {
constructor(className, startIndex, speed) {
this.className = className;
this.startIndex = startIndex;
this.speed = speed;
this.currentIndex = startIndex;
this.interval = this.speed;
}
}
squares[pacmanCurrentIndex].classList.add('pac-man')
document.addEventListener('keyup', movePacman);
var ghosts = [
new Ghost('blinky', 348, 250),
new Ghost('inky', 351, 300),
new Ghost('clyde', 379, 500)
]
ghosts.forEach(ghost => {
squares[ghost.currentIndex].classList.add(ghost.className)
squares[ghost.currentIndex].classList.add('ghost')
})
document.getElementById("startButton").addEventListener("click", function () {
startGame(ghosts)
});
document.getElementById("stopButton").addEventListener("click", function () {
stopGame(ghosts);
});
})
function stopGame(ghosts) {
if (gameIsStarted) {
gameIsStarted = false;
ghosts.forEach(ghost => {
clearInterval(ghost.interval)
})
}
}
function startGame(ghosts) {
if (!gameIsStarted) {
gameIsStarted = true
ghosts.forEach(ghost => {
moveGhost(ghost, ghosts)
})
}
}
function movePacman(e) {
if (!gameIsStarted) {
return
}
squares[pacmanCurrentIndex].classList.remove('pac-man')
squares[pacmanCurrentIndex].classList.remove('to-right')
squares[pacmanCurrentIndex].classList.remove('to-left')
squares[pacmanCurrentIndex].classList.remove('to-down')
squares[pacmanCurrentIndex].classList.remove('to-up')
switch (e.keyCode) {
case 37:
if (
pacmanCurrentIndex % width !== 0 &&
!squares[pacmanCurrentIndex - 1].classList.contains('wall') &&
!squares[pacmanCurrentIndex - 1].classList.contains('ghost-lair')
)
pacmanCurrentIndex -= 1
squares[pacmanCurrentIndex].classList.add('to-left')
if (squares[pacmanCurrentIndex - 1] === squares[363]) {
pacmanCurrentIndex = 391
}
break
case 38:
if (
pacmanCurrentIndex - width >= 0 &&
!squares[pacmanCurrentIndex - width].classList.contains('wall') &&
!squares[pacmanCurrentIndex - width].classList.contains('ghost-lair')
)
pacmanCurrentIndex -= width
squares[pacmanCurrentIndex].classList.add('to-up')
break
case 39:
if (
pacmanCurrentIndex % width < width - 1 &&
!squares[pacmanCurrentIndex + 1].classList.contains('wall') &&
!squares[pacmanCurrentIndex + 1].classList.contains('ghost-lair')
)
pacmanCurrentIndex += 1
squares[pacmanCurrentIndex].classList.add('to-right')
if (squares[pacmanCurrentIndex + 1] === squares[392]) {
pacmanCurrentIndex = 364
}
break
case 40:
if (
pacmanCurrentIndex + width < width * width &&
!squares[pacmanCurrentIndex + width].classList.contains('wall') &&
!squares[pacmanCurrentIndex + width].classList.contains('ghost-lair')
)
pacmanCurrentIndex += width
squares[pacmanCurrentIndex].classList.add('to-down')
break
}
squares[pacmanCurrentIndex].classList.add('pac-man')
pacDotEaten()
//checkForWin()
}
function pacDotEaten() {
if (squares[pacmanCurrentIndex].classList.contains('pac-dot')) {
score += 1
document.querySelector('.score').innerHTML = score;
squares[pacmanCurrentIndex].classList.remove('pac-dot')
} else if (squares[pacmanCurrentIndex].classList.contains('power-pellet')) {
score += 3;
document.querySelector('.score').innerHTML = score;
squares[pacmanCurrentIndex].classList.remove('power-pellet')
}
}
function moveGhost(ghost, ghosts) {
if (!gameIsStarted) {
return
}
let directions = [1, -1, width, -width]
let ghostNextMove = Math.floor(Math.random() * directions.length);
ghost.interval = setInterval(() => {
checkForGameOver(ghost, ghosts)
if (!squares[ghost.currentIndex + directions[ghostNextMove]].classList.contains('ghost') &&
!squares[ghost.currentIndex + directions[ghostNextMove]].classList.contains('wall')) {
squares[ghost.currentIndex].classList.remove(ghost.className)
squares[ghost.currentIndex].classList.remove('ghost')
ghost.currentIndex += directions[ghostNextMove]
squares[ghost.currentIndex].classList.add(ghost.className, 'ghost')
} else {
ghostNextMove = Math.floor(Math.random() * directions.length);
}
}, ghost.speed)
}
function checkForGameOver(ghost, ghosts) {
if (ghost.currentIndex == pacmanCurrentIndex) {
alert('You can continue')
}
}