-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
337 lines (301 loc) · 9.46 KB
/
game.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
/// Load custom font
var emulogic = new FontFace('emulogic', 'url(assets/emulogic.ttf)');
emulogic.load().then( () => {
document.fonts.add(emulogic)
})
const BORDER_WALL_WIDTH = 1.5
const canvas = document.getElementById("canvas")
const canvasContext = canvas.getContext("2d")
const pacmanFrames = document.getElementById("animation")
const ghostFrames = document.getElementById("ghosts")
const GHOST_LIMIT_SPAWN = 4
const LIVES_TO_START = 3
let createRect = (x, y, width, heigth, color) => {
canvasContext.fillStyle = color
canvasContext.fillRect(x, y, width, heigth)
}
let fps = 30
let oneBlockSize = 20
let wallColor = "#DEAC87"
let wallSpaceWidth = oneBlockSize / BORDER_WALL_WIDTH
let wallOffset = (oneBlockSize - wallSpaceWidth) / 2
let wallInnerColor = "#000"
let foodColor = "#F02011"
let score = 0
let ghosts = []
let lives = 1
let foodCount = 0
const DIRECTION_RIGHT = 4
const DIRETCION_UP = 3
const DIRECTION_LEFT = 2
const DIRECTION_BOTTOM = 1
/// positions of ghosts in ghost.png
let ghostLocations = [
{ x:0, y:0 },
{ x:176, y:0},
{ x:0, y:121},
{ x:176, y:121},
]
/*-----------------------------
* 1 => wall
* 0 => can walk
* 2 => outside
-----------------------------*/
let map = [
[1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1],//23
[1,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, 0,0,0,0,0,1],//22
[1,0,1,1,1, 0,1,1,1,0, 1,0,1,1,1, 0,1,1,1,0,1],//21
[1,0,1,1,1, 0,1,1,1,0, 1,0,1,1,1, 0,1,1,1,0,1],//20
[1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,1],//19
[1,0,1,1,1, 0,1,0,1,1, 1,1,1,0,1, 0,1,1,1,0,1],//18
[1,0,0,0,0, 0,1,0,0,0, 1,0,0,0,1, 0,0,0,0,0,1],//17
[1,1,1,1,1, 0,1,1,1,0, 1,0,1,1,1, 0,1,1,1,1,1],//16
[2,2,2,2,1, 0,1,0,0,0, 0,0,0,0,1, 0,1,2,2,2,2],//15
[1,1,1,1,1, 0,1,0,1,1, 0,1,1,0,1, 0,1,1,1,1,1],//14
[0,0,0,0,0, 0,1,0,1,0, 0,0,1,0,1, 0,0,0,0,0,0],//13
[1,1,1,1,1, 0,1,0,1,0, 0,0,1,0,1, 0,1,1,1,1,1],//12
[2,2,2,2,1, 0,1,0,1,1, 1,1,1,0,1, 0,1,2,2,2,2],//11
[2,2,2,2,1, 0,1,0,0,0, 0,0,0,0,1, 0,1,2,2,2,2],//10
[1,1,1,1,1, 0,0,0,1,1, 1,1,1,0,0, 0,1,1,1,1,1],//9
[1,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, 0,0,0,0,0,1],//8
[1,0,1,1,1, 0,1,1,1,0, 1,0,1,1,1, 0,1,1,1,0,1],//7
[1,0,0,0,1, 0,0,0,0,0, 0,0,0,0,0, 0,1,0,0,0,1],//6
[1,1,0,0,1, 0,1,0,1,1, 1,1,1,0,1, 0,1,0,0,1,1],//5
[1,0,0,0,0, 0,1,0,0,0, 1,0,0,0,1, 0,0,0,0,0,1],//4
[1,0,1,1,1, 1,1,1,1,0, 1,0,1,1,1, 1,1,1,1,0,1],//3
[1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,1],//2
[1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1],//1
]
let createTotalFood = () => {
for (let i = 0; i < map.length; i ++) {
for(let j = 0; j < map[0].length; j++) {
if(map[i][j] == 0) {
foodCount ++ // IMPORTANT TO COUNT FOOD
}
}
}
}
// set this after map for reference use
let randomTargetsForGhosts = [
{x:1 * oneBlockSize, y: 1 * oneBlockSize },
{x:1 * oneBlockSize, y: (map.length - 2) * oneBlockSize },
{x:1 * (map[0].length -2) * oneBlockSize, y: oneBlockSize },
{
x:(map[0].length -2 ) * oneBlockSize,
y: (map.length - 2) * oneBlockSize
}
]
let gameLoop = () => {
draw() // draw before - becouse
update ()
}
let update = () => {
console.log(foodCount)
pacman.moveProcess()
pacman.eat()
for (let i = 0; i < ghosts.length; i++) {
ghosts[i].moveProcess()
}
if(pacman.checkGhostCollision()) {
console.log('pegou')
restartGame()
}
if(score >= foodCount) {
drawWin()
clearInterval(gameInterval)
}
}
let drawWin = () => {
canvasContext.font ="20px Emulogic"
canvasContext.fillStyle = "white"
canvasContext.fillText("You Win!!",120, 230)
}
let restartGame = () => {
createNewPacman()
createGhosts()
lives--
if(lives == 0) {
gameOver()
}
}
let gameOver = () =>{
clearInterval(gameInterval)
drawGameOver()
}
let drawGameOver = () => {
canvasContext.font ="20px Emulogic"
canvasContext.fillStyle = "white"
canvasContext.fillText("Game Over",120, 230)
canvasContext.fillText("Press ENTER to restart",10, 280)
}
let drawLives = () => {
// draw text
canvasContext.font ="20px Emulogic"
canvasContext.fillStyle = "white"
canvasContext.fillText("Lives: ", 220, oneBlockSize * (map.length + 1.5))
//draw live as images
for (let i = 0; i < lives; i++) {
canvasContext.drawImage(pacmanFrames,
2 * oneBlockSize,
0,
oneBlockSize,
oneBlockSize,
350 + i * oneBlockSize,
oneBlockSize * map.length + 10,
oneBlockSize,
oneBlockSize);
}
}
let drawFoods = () => {
for (let i = 0; i < map.length; i ++) {
for(let j = 0; j < map[0].length; j++) {
if(map[i][j] == 0) { // create food only in path pacman can walk
createRect(
j * oneBlockSize + oneBlockSize / 3,
i * oneBlockSize + oneBlockSize / 3,
oneBlockSize / 5,
oneBlockSize / 5,
foodColor
)
}
}
}
}
let drawScore = () => {
canvasContext.font = "bold 20px Emulogic"
canvasContext.fillStyle = "white"
canvasContext.fillText("Score: " + score,
5,
oneBlockSize * (map.length + 1) + 10
)
}
let drawGhosts = () => {
for (let i = 0; i< ghosts.length; i++) {
ghosts[i].draw()
}
}
let draw = () => {
createRect(0, 0, canvas.width, canvas.height, "#000") // workaround to do not show multiples pacmans
drawWalls()
drawFoods()
pacman.draw()
drawScore()
drawGhosts()
drawLives()
}
let gameInterval = setInterval(gameLoop, 1000 / fps);
let drawWalls = () => {
for( let i = 0; i < map.length; i++) {
for(let j = 0; j < map[0].length; j++) {
// create raw walls
if(map[i][j] == 1) {
createRect(
j* oneBlockSize,
i* oneBlockSize,
oneBlockSize,
oneBlockSize,
wallColor
)
}
if( j > 0 && map[i][ j - 1] == 1) {
createRect(
j * oneBlockSize,
i * oneBlockSize + wallOffset,
wallSpaceWidth + wallOffset,
wallSpaceWidth,
wallInnerColor)
}
if( j < map[0].length -1 && map[i][ j + 1] == 1) {
createRect(
j * oneBlockSize + wallOffset,
i * oneBlockSize + wallOffset,
wallSpaceWidth + wallOffset,
wallSpaceWidth,
wallInnerColor)
}
if( i > 0 && map[i - 1][ j ] == 1) {
createRect(
j * oneBlockSize + wallOffset,
i * oneBlockSize ,
wallSpaceWidth,
wallSpaceWidth + wallOffset,
wallInnerColor)
}
if( i < map.length - 1 && map[i + 1][ j ] == 1) {
createRect(
j * oneBlockSize + wallOffset,
i * oneBlockSize + wallOffset,
wallSpaceWidth ,
wallSpaceWidth + wallOffset,
wallInnerColor)
}
}
}
}
let createNewPacman = () => {
pacman = new Pacman(
oneBlockSize,
oneBlockSize ,
oneBlockSize,
oneBlockSize,
oneBlockSize / 5)
}
let createGhosts = () => {
ghosts = []
for (let i = 0; i < GHOST_LIMIT_SPAWN; i++ ) {
let newGhost = new Ghost( 9 * oneBlockSize + (i % 2 == 0 ? 0 : 1) * oneBlockSize,
10 * oneBlockSize + (i%2 == 0 ? 0 : 1) * oneBlockSize,
oneBlockSize,
oneBlockSize,
pacman.speed / 2,
ghostLocations[i % 4].x,
ghostLocations[i % 4].y,
124,
116,
6+i
)
ghosts.push(newGhost)
}
}
let getDefaultLifesToStart = () => {
return LIVES_TO_START
}
let resetScore = () => {
score = 0
}
let startGame = () => {
lives = getDefaultLifesToStart()
createNewPacman()
createGhosts()
createTotalFood()
gameLoop()
}
//------------------------- START MAIN ------------------
startGame()
//----------------------------------------------------
/**
* Configure keys
* //https://keyjs.dev/
*/
window.addEventListener("keydown", (event) => {
let k = event.keyCode
console.log(k)
setTimeout(() => {
if(k == 37 || k == 65) { //left
pacman.nextDirection = DIRECTION_LEFT
} else if(k == 38 || k == 87) { //up
pacman.nextDirection = DIRETCION_UP
} else if(k == 39 || k == 68) { //right
pacman.nextDirection = DIRECTION_RIGHT
} else if(k == 40 || k == 83) { //bottom
pacman.nextDirection = DIRECTION_BOTTOM
}
// restart game
if(lives == 0 && k == 13) {
resetScore()
drawFoods()
startGame()
gameInterval = setInterval(gameLoop, 1000 / fps);
}
}, 1);
})