-
Notifications
You must be signed in to change notification settings - Fork 1
/
main03.js
372 lines (323 loc) · 8.84 KB
/
main03.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
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
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
var screen = {
width: 40 * 3,
height: 41 * 3,
zoom: 6,
scale: 3
};
canvas.width = screen.width;
canvas.height = screen.height;
canvas.style.width = (screen.width * screen.zoom) + 'px';
canvas.style.height = (screen.height * screen.zoom) + 'px';
canvas.style['image-rendering'] = 'pixelated';
const context = canvas.getContext('2d');
function clear() {
context.fillStyle = 'black';
context.fillRect(0, 0, screen.width, screen.height);
}
function pixel(x, y, color = 'red') {
context.fillStyle = color;
context.fillRect(x * screen.scale + 1, y * screen.scale + 1, 1, 1);
}
function cell(x, y, color = 'red') {
context.fillStyle = color;
context.fillRect(x * screen.scale, y * screen.scale, screen.scale, screen.scale);
}
const FRAMES_EAT_GHOSTS = 40;
let mapStrings =
`BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
B B BB
B BBBBBBB BBBBBBBB B BBBBBBBB BBBBBBB BB
B BBBBBBB BBBBBBBB B BBBBBBBB BBBBBBB BB
BPBBBBBBB BBBBBBBB B BBBBBBBB BBBBBBBPBB
B BBBBBBB BBBBBBBB B BBBBBBBB BBBBBBB BB
B BB
B BBBBBBB BBBBB BBBBBBB BBBBB BBBBBBB BB
B BBBBBBB BBBBB BBBBBBB BBBBB BBBBBBB BB
B BBBB BB B BB BBBB BB
B BB BB BBBBB B BBBBB BB BB BB
B BBBBBBB BB BBBBB B BBBBB BB BBBBBBB BB
B BB BBBBB B BBBBB BB BB
B BBBB BB BB BBBB BB
BBBBBBBBB BB BB BBB BBB BB BB BBBBBBBBBB
BBBBBBBBB BB B B BB BBBBBBBBBB
BBBBBBBBBBBB BB B B BB BBBBBBBBBBBBB
BBBBBBBBBBBB BB B B BB BBBBBBBBBBBBB
BBBBBBBBBBBB BB BBB BBB BB BBBBBBBBBBBBB
BBBBBBBBBBBB BBBBBBBBBBBBB
BBBBBBBBBBBB BB BBBBBBB BB BBBBBBBBBBBBB
BBBBBBBBBBBB BB BBBBBBB BB BBBBBBBBBBBBB
BBBBBBBBBBBB BB BB BBBBBBBBBBBBB
BBBBBBBBBBBB BB BBB BBB BB BBBBBBBBBBBBB
BBBBBBBBBBBB BB BBB BBB BB BBBBBBBBBBBBB
B BB
B BBBBBBB BB BBBBBBBBBBBBB BB BBBBBBB BB
B BBBBBBB BB BBBBBBBBBBBBB BB BBBBBBB BB
B BB BB BBB BB BB BB
BBBBBB BB BB BBBB BBB BBBB BB BB BBBBBBB
BBBBBB BB BB BBBB BBB BBBB BB BB BBBBBBB
B BB BB BBBB BBB BBBB BB BB BB
B BBBB BB BB BB BB BBBB BB
B BBBB BB BBBBBBBBBBBBB BB BBBB BB
BPBBBB BBBBB BBBBBBBBBBBBB BBBBB BBBBPBB
B BBBB BBB BBBB BB
B BBBB BBBBBBBBBB BBB BBBBBBBBBB BBBB BB
B BBBB BBBBBBBBBB BBB BBBBBBBBBB BBBB BB
B BB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB`.split('\n');
let mapNumbers = [];
for (let row = 0; row < mapStrings.length; row++) {
let strRow = mapStrings[row];
let newRow = [];
for (let col = 0; col < strRow.length; col++) {
if (strRow[col] == 'B') {
newRow.push(1)
} else {
newRow.push(0)
}
}
mapNumbers.push(newRow);
}
class Entity {
constructor() {
this.x = 0;
this.y = 0;
this.color = 'gray';
}
render() {
pixel(this.x, this.y, this.color);
}
update() {
}
}
const dir_x = [1, 0, -1, 0];
const dir_y = [0, 1, 0, -1];
class Pacman extends Entity {
constructor() {
super();
this.color = 'yellow';
this.dx = 1;
this.dy = 0;
this.eatGhosts = 0;
}
render() {
cell(this.x, this.y, this.color);
}
update() {
if (keyLeft) {
this.dx = -1;
this.dy = 0;
} else if (keyRight) {
this.dx = 1;
this.dy = 0;
} else if (keyUp) {
this.dx = 0;
this.dy = -1;
} else if (keyDown) {
this.dx = 0;
this.dy = 1;
}
const x_old = this.x;
const y_old = this.y;
const x_new = x_old + this.dx;
const y_new = y_old + this.dy;
if (y_new < 0 || y_new >= mapNumbers.length
|| mapNumbers[y_new][x_new] !== 1) {
this.x = x_new;
this.y = y_new;
}
if (this.eatGhosts > 0) {
this.eatGhosts--;
}
for (let i=0;i<entities.length;i++) {
if (entities[i].x == this.x &&
entities[i].y == this.y &&
(entities[i] instanceof Fruit
|| entities[i] instanceof Ghost
&& this.eatGhosts
)) {
if (entities[i].yummy) {
this.eatGhosts = FRAMES_EAT_GHOSTS;
}
remove(entities[i]);
}
}
}
}
const dir_love = [0, 0, 0];
class Ghost extends Entity {
constructor() {
super();
this.color = 'red';
this.direction = 0;
}
render() {
cell(this.x, this.y, this.color);
}
update() {
if (pacman.eatGhosts) {
this.color = 'aqua';
} else {
this.color = 'red';
}
dir_love[0] = (this.direction + 3) % 4;
dir_love[1] = this.direction;
dir_love[2] = (this.direction + 1) % 4;
dir_love[3] = (this.direction + 2) % 4;
if (Math.random() < 0.2) {
dir_love[0] = (this.direction + 1) % 4;
dir_love[1] = this.direction;
dir_love[2] = (this.direction + 3) % 4;
dir_love[3] = (this.direction + 2) % 4;
}
for (let i = 0; i < 4; i++) {
const dir = dir_love[i];
const dx = dir_x[dir];
const dy = dir_y[dir];
const x_old = this.x;
const y_old = this.y;
const x_new = x_old + dx;
const y_new = y_old + dy;
if (y_new < 0 || y_new >= mapNumbers.length
|| mapNumbers[y_new][x_new] !== 1) {
this.x = x_new;
this.y = y_new;
this.direction = dir;
break;
}
}
for (let i=0;i<entities.length;i++) {
if (entities[i].x == this.x &&
entities[i].y == this.y &&
entities[i] instanceof Pacman &&
!entities[i].eatGhosts) {
remove(entities[i]);
}
}
}
}
class Map extends Entity {
render() {
const rows = mapNumbers.length;
const cols = mapNumbers[0].length;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
if (mapNumbers[row][col] !== 0) {
cell(this.x + col, this.y + row, this.color);
}
}
}
}
}
class Fruit extends Entity {
constructor() {
super();
this.color = 'white';
this.yummy = 0;
}
makeyummy() {
this.yummy = 1;
this.color = 'lime';
}
}
let entities = [];
function add(e) {
entities.push(e);
}
function remove(e) {
const ind = entities.indexOf(e);
if (ind >= 0) {
entities.splice(ind, 1);
}
}
let map = new Map();
add(map);
let pacman = new Pacman();
add(pacman);
pacman.x = 1;
pacman.y = 2;
const maxW = screen.width / screen.scale;
const maxH = screen.height / screen.scale;
for (let j = 0; j < maxH; j++) {
for (let i = 0; i < maxW; i++) {
if (j>=0 &&
j<mapNumbers.length &&
mapNumbers[j][i]===0) {
let fruit = new Fruit();
fruit.x = i;
fruit.y = j;
if (mapStrings[j][i] === 'P') {
fruit.makeyummy();
}
add(fruit);
}
}
}
for (let i = 0; i < 20; i++) {
let x = Math.floor(Math.random() * maxW);
let y = Math.floor(Math.random() * maxH);
if (y < mapNumbers.length && mapNumbers[y][x] === 1) {
continue;
}
let ghost = new Ghost();
ghost.x = x;
ghost.y = y;
add(ghost);
}
function frame() {
for (let i = 0; i < entities.length; i++) {
entities[i].update();
}
clear();
for (let i = 0; i < entities.length; i++) {
entities[i].render();
}
}
let keyRight = false;
let keyUp = false;
let keyLeft = false;
let keyDown = false;
document.addEventListener('keydown', function (event) {
switch (event.which) {
case 39:
case 68:
keyRight = true;
break;
case 38:
case 87:
keyUp = true;
break;
case 37:
case 65:
keyLeft = true;
break;
case 40:
case 83:
keyDown = true;
break;
}
});
document.addEventListener('keyup', function (event) {
switch (event.which) {
case 39:
case 68:
keyRight = false;
break;
case 38:
case 87:
keyUp = false;
break;
case 37:
case 65:
keyLeft = false;
break;
case 40:
case 83:
keyDown = false;
break;
}
});
frame();
setInterval(frame, 250);