Skip to content

Commit 3f49e5b

Browse files
dijkstra progress
1 parent b514af1 commit 3f49e5b

File tree

2 files changed

+104
-46
lines changed

2 files changed

+104
-46
lines changed

ghost.js

+40-36
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class Ghost {
2121
this.imageY = imageY;
2222
this.imageHeight = imageHeight;
2323
this.imageWidth = imageWidth;
24-
this.loopCounter = 1;
2524
this.range = range;
26-
this.randomDirection = DIRECTION_BOTTOM;
25+
this.randomTargetIndex = parseInt(Math.random() * 4);
26+
this.target = randomTargetsForGhosts[this.randomTargetIndex];
2727
setInterval(() => {
2828
this.changeRandomDirection();
29-
}, 5000);
29+
}, 3000);
3030
}
3131

3232
isInRange() {
@@ -42,36 +42,31 @@ class Ghost {
4242
}
4343

4444
changeRandomDirection() {
45-
this.randomDirection = this.randomDirection % 4;
46-
this.randomDirection += parseInt(Math.random * 4);
47-
this.randomDirection = (this.randomDirection % 4) + 1;
48-
console.log("change");
45+
let addition = 1;
46+
this.randomTargetIndex += addition;
47+
this.randomTargetIndex = this.randomTargetIndex % 4;
4948
}
5049

51-
playRandomMove() {
52-
this.direction = this.randomDirection;
53-
this.moveForwards();
54-
if (this.checkCollisions()) {
55-
this.moveBackwards();
56-
this.changeRandomDirection();
57-
} else {
58-
this.moveBackwards();
59-
}
60-
}
50+
// playRandomMove() {
51+
// console.log("random ");
52+
// this.target = randomDirectionsForGhosts[this.randomDirectionIndex];
53+
// this.moveForwards();
54+
// if (this.checkCollisions()) {
55+
// this.moveBackwards();
56+
// this.changeRandomDirection();
57+
// } else {
58+
// this.moveBackwards();
59+
// }
60+
// }
6161

6262
moveProcess() {
63-
// console.log(this.loopCounter);
64-
// console.log(parseInt(oneBlockSize / this.speed));
65-
// console.log(this.loopCounter % parseInt(oneBlockSize / this.speed));
66-
6763
if (this.isInRange()) {
68-
this.changeDirectionIfPossible();
64+
this.target = pacman;
6965
} else {
70-
this.playRandomMove();
66+
this.target = randomTargetsForGhosts[this.randomTargetIndex];
7167
}
72-
68+
this.changeDirectionIfPossible();
7369
this.moveForwards();
74-
7570
if (this.checkCollisions()) {
7671
this.moveBackwards();
7772
return;
@@ -137,22 +132,23 @@ class Ghost {
137132
let tempDirection = this.direction;
138133
this.direction = this.calculateNewDirection(
139134
map,
140-
parseInt(pacman.x / oneBlockSize),
141-
parseInt(pacman.y / oneBlockSize)
135+
parseInt(this.target.x / oneBlockSize),
136+
parseInt(this.target.y / oneBlockSize)
142137
);
143138
if (typeof this.direction == "undefined") {
144-
console.log("undefined");
145139
this.direction = tempDirection;
146140
return;
147141
}
148-
this.loopCounter++;
149142
this.moveForwards();
150-
if (this.checkCollisions()) {
151-
this.moveBackwards();
152-
this.direction = tempDirection;
153-
} else {
143+
let addition = 0;
144+
while (this.checkCollisions()) {
145+
console.log("collided", addition);
154146
this.moveBackwards();
147+
this.direction = (tempDirection + addition) % 4;
148+
this.moveForwards();
149+
addition++;
155150
}
151+
this.moveBackwards();
156152
}
157153

158154
calculateNewDirection(map, destX, destY) {
@@ -168,8 +164,6 @@ class Ghost {
168164
moves: [],
169165
},
170166
];
171-
console.log("");
172-
173167
while (queue.length > 0) {
174168
let poped = queue.shift();
175169
if (poped.x == destX && poped.y == destY) {
@@ -225,7 +219,7 @@ class Ghost {
225219
) {
226220
let tempMoves = poped.moves.slice();
227221
tempMoves.push(DIRECTION_BOTTOM);
228-
queue.push({ x: poped.x - 1, y: poped.y + 1, moves: tempMoves });
222+
queue.push({ x: poped.x, y: poped.y + 1, moves: tempMoves });
229223
}
230224
return queue;
231225
}
@@ -269,6 +263,16 @@ class Ghost {
269263
this.height
270264
);
271265
canvasContext.restore();
266+
canvasContext.beginPath();
267+
canvasContext.strokeStyle = "red";
268+
canvasContext.arc(
269+
this.x + oneBlockSize / 2,
270+
this.y + oneBlockSize / 2,
271+
this.range * oneBlockSize,
272+
0,
273+
2 * Math.PI
274+
);
275+
canvasContext.stroke();
272276
}
273277
}
274278

pacman.js

+64-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ let ghostImageLocations = [
2121
{ x: 176, y: 121 },
2222
];
2323

24+
// Game variables
25+
let fps = 30;
26+
let pacman;
27+
let oneBlockSize = 20;
28+
let score = 0;
29+
let ghosts = [];
30+
let wallSpaceWidth = oneBlockSize / 1.6;
31+
let wallOffset = (oneBlockSize - wallSpaceWidth) / 2;
32+
let wallInnerColor = "black";
33+
console.log(wallSpaceWidth);
34+
console.log(wallOffset);
35+
2436
// we now create the map of the walls,
2537
// if 1 wall, if 0 not wall
2638
// 21 columns // 23 rows
@@ -50,6 +62,16 @@ let map = [
5062
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
5163
];
5264

65+
let randomTargetsForGhosts = [
66+
{ x: 1 * oneBlockSize, y: 1 * oneBlockSize },
67+
{ x: 1 * oneBlockSize, y: (map.length - 2) * oneBlockSize },
68+
{ x: (map[0].length - 2) * oneBlockSize, y: oneBlockSize },
69+
{
70+
x: (map[0].length - 2) * oneBlockSize,
71+
y: (map.length - 2) * oneBlockSize,
72+
},
73+
];
74+
5375
// for (let i = 0; i < map.length; i++) {
5476
// for (let j = 0; j < map[0].length; j++) {
5577
// map[i][j] = 2;
@@ -230,13 +252,6 @@ class Pacman {
230252
}
231253
}
232254

233-
// Game variables
234-
let fps = 30;
235-
let pacman;
236-
let oneBlockSize = 20;
237-
let score = 0;
238-
let ghosts = [];
239-
240255
let createNewPacman = () => {
241256
pacman = new Pacman(
242257
oneBlockSize,
@@ -335,7 +350,7 @@ let draw = () => {
335350

336351
let drawWalls = () => {
337352
for (let i = 0; i < map.length; i++) {
338-
for (let j = 0; j < map[i].length; j++) {
353+
for (let j = 0; j < map[0].length; j++) {
339354
if (map[i][j] == 1) {
340355
createRect(
341356
j * oneBlockSize,
@@ -344,6 +359,45 @@ let drawWalls = () => {
344359
oneBlockSize,
345360
"#342DCA"
346361
);
362+
if (j > 0 && map[i][j - 1] == 1) {
363+
createRect(
364+
j * oneBlockSize,
365+
i * oneBlockSize + wallOffset,
366+
wallSpaceWidth + wallOffset,
367+
wallSpaceWidth,
368+
wallInnerColor
369+
);
370+
}
371+
372+
if (j < map[0].length - 1 && map[i][j + 1] == 1) {
373+
createRect(
374+
j * oneBlockSize + wallOffset,
375+
i * oneBlockSize + wallOffset,
376+
wallSpaceWidth + wallOffset,
377+
wallSpaceWidth,
378+
wallInnerColor
379+
);
380+
}
381+
382+
if (i < map.length - 1 && map[i + 1][j] == 1) {
383+
createRect(
384+
j * oneBlockSize + wallOffset,
385+
i * oneBlockSize + wallOffset,
386+
wallSpaceWidth,
387+
wallSpaceWidth + wallOffset,
388+
wallInnerColor
389+
);
390+
}
391+
392+
if (i > 0 && map[i - 1][j] == 1) {
393+
createRect(
394+
j * oneBlockSize + wallOffset,
395+
i * oneBlockSize,
396+
wallSpaceWidth,
397+
wallSpaceWidth + wallOffset,
398+
wallInnerColor
399+
);
400+
}
347401
}
348402
}
349403
}
@@ -353,7 +407,7 @@ let createGhosts = () => {
353407
ghosts = [];
354408
for (let i = 0; i < ghostCount; i++) {
355409
let newGhost = new Ghost(
356-
9 * oneBlockSize + (i % 2 == 0 ? 0 : 2) * oneBlockSize,
410+
9 * oneBlockSize + (i % 2 == 0 ? 0 : 1) * oneBlockSize,
357411
10 * oneBlockSize + (i % 2 == 0 ? 0 : 1) * oneBlockSize,
358412
oneBlockSize,
359413
oneBlockSize,
@@ -362,7 +416,7 @@ let createGhosts = () => {
362416
ghostImageLocations[i % 4].y,
363417
124,
364418
116,
365-
9 + i * 3
419+
6 + i
366420
);
367421
ghosts.push(newGhost);
368422
}

0 commit comments

Comments
 (0)