Skip to content

Commit 4d27d9a

Browse files
ghosts bug solved
1 parent b5cb906 commit 4d27d9a

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

game.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ let drawWalls = () => {
228228

229229
let createGhosts = () => {
230230
ghosts = [];
231-
for (let i = 0; i < ghostCount; i++) {
231+
for (let i = 0; i < ghostCount * 2; i++) {
232232
let newGhost = new Ghost(
233233
9 * oneBlockSize + (i % 2 == 0 ? 0 : 1) * oneBlockSize,
234234
10 * oneBlockSize + (i % 2 == 0 ? 0 : 1) * oneBlockSize,

ghost.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Ghost {
2626
this.target = randomTargetsForGhosts[this.randomTargetIndex];
2727
setInterval(() => {
2828
this.changeRandomDirection();
29-
}, 3000);
29+
}, 10000);
3030
}
3131

3232
isInRange() {
@@ -127,17 +127,27 @@ class Ghost {
127127
this.direction = tempDirection;
128128
return;
129129
}
130+
if (
131+
this.getMapY() != this.getMapYRightSide() &&
132+
(this.direction == DIRECTION_LEFT ||
133+
this.direction == DIRECTION_RIGHT)
134+
) {
135+
this.direction = DIRECTION_UP;
136+
}
137+
if (
138+
this.getMapX() != this.getMapXRightSide() &&
139+
this.direction == DIRECTION_UP
140+
) {
141+
this.direction = DIRECTION_LEFT;
142+
}
130143
this.moveForwards();
131144
if (this.checkCollisions()) {
145+
this.moveBackwards();
146+
this.direction = tempDirection;
147+
} else {
148+
this.moveBackwards();
132149
}
133-
// let addition = 0;
134-
// while (this.checkCollisions()) {
135-
// this.moveBackwards();
136-
// this.direction = (tempDirection + addition) % 4;
137-
// this.moveForwards();
138-
// addition++;
139-
// }
140-
this.moveBackwards();
150+
console.log(this.direction);
141151
}
142152

143153
calculateNewDirection(map, destX, destY) {

0 commit comments

Comments
 (0)