Skip to content

Commit ccb29d6

Browse files
initial commit
0 parents  commit ccb29d6

11 files changed

+722
-0
lines changed

.idea/.gitignore

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/pacman.iml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
2.63 MB
Loading

animations.gif

642 Bytes
Loading

ghost.js

+284
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
class Ghost {
2+
constructor(
3+
x,
4+
y,
5+
width,
6+
height,
7+
speed,
8+
imageX,
9+
imageY,
10+
imageWidth,
11+
imageHeight,
12+
range
13+
) {
14+
this.x = x;
15+
this.y = y;
16+
this.width = width;
17+
this.height = height;
18+
this.speed = speed;
19+
this.direction = DIRECTION_RIGHT;
20+
this.imageX = imageX;
21+
this.imageY = imageY;
22+
this.imageHeight = imageHeight;
23+
this.imageWidth = imageWidth;
24+
this.loopCounter = 1;
25+
this.range = range;
26+
this.randomDirection = DIRECTION_BOTTOM;
27+
setInterval(() => {
28+
this.changeRandomDirection();
29+
}, 5000);
30+
}
31+
32+
isInRange() {
33+
let xDistance = Math.abs(pacman.getMapX() - this.getMapX());
34+
let yDistance = Math.abs(pacman.getMapY() - this.getMapY());
35+
if (
36+
Math.sqrt(xDistance * xDistance + yDistance * yDistance) <=
37+
this.range
38+
) {
39+
return true;
40+
}
41+
return false;
42+
}
43+
44+
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");
49+
}
50+
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+
}
61+
62+
moveProcess() {
63+
// console.log(this.loopCounter);
64+
// console.log(parseInt(oneBlockSize / this.speed));
65+
// console.log(this.loopCounter % parseInt(oneBlockSize / this.speed));
66+
67+
if (this.isInRange()) {
68+
this.changeDirectionIfPossible();
69+
} else {
70+
this.playRandomMove();
71+
}
72+
73+
this.moveForwards();
74+
75+
if (this.checkCollisions()) {
76+
this.moveBackwards();
77+
return;
78+
}
79+
}
80+
81+
moveBackwards() {
82+
switch (this.direction) {
83+
case 4: // Right
84+
this.x -= this.speed;
85+
break;
86+
case 3: // Up
87+
this.y += this.speed;
88+
break;
89+
case 2: // Left
90+
this.x += this.speed;
91+
break;
92+
case 1: // Bottom
93+
this.y -= this.speed;
94+
break;
95+
}
96+
}
97+
98+
moveForwards() {
99+
switch (this.direction) {
100+
case 4: // Right
101+
this.x += this.speed;
102+
break;
103+
case 3: // Up
104+
this.y -= this.speed;
105+
break;
106+
case 2: // Left
107+
this.x -= this.speed;
108+
break;
109+
case 1: // Bottom
110+
this.y += this.speed;
111+
break;
112+
}
113+
}
114+
115+
checkCollisions() {
116+
let isCollided = false;
117+
if (
118+
map[parseInt(this.y / oneBlockSize)][
119+
parseInt(this.x / oneBlockSize)
120+
] == 1 ||
121+
map[parseInt(this.y / oneBlockSize + 0.999)][
122+
parseInt(this.x / oneBlockSize)
123+
] == 1 ||
124+
map[parseInt(this.y / oneBlockSize)][
125+
parseInt(this.x / oneBlockSize + 0.9999)
126+
] == 1 ||
127+
map[parseInt(this.y / oneBlockSize + 0.9999)][
128+
parseInt(this.x / oneBlockSize + 0.9999)
129+
] == 1
130+
) {
131+
isCollided = true;
132+
}
133+
return isCollided;
134+
}
135+
136+
changeDirectionIfPossible() {
137+
let tempDirection = this.direction;
138+
this.direction = this.calculateNewDirection(
139+
map,
140+
parseInt(pacman.x / oneBlockSize),
141+
parseInt(pacman.y / oneBlockSize)
142+
);
143+
if (typeof this.direction == "undefined") {
144+
console.log("undefined");
145+
this.direction = tempDirection;
146+
return;
147+
}
148+
this.loopCounter++;
149+
this.moveForwards();
150+
if (this.checkCollisions()) {
151+
this.moveBackwards();
152+
this.direction = tempDirection;
153+
} else {
154+
this.moveBackwards();
155+
}
156+
}
157+
158+
calculateNewDirection(map, destX, destY) {
159+
let mp = [];
160+
for (let i = 0; i < map.length; i++) {
161+
mp[i] = map[i].slice();
162+
}
163+
164+
let queue = [
165+
{
166+
x: parseInt(this.x / oneBlockSize),
167+
y: parseInt(this.y / oneBlockSize),
168+
moves: [],
169+
},
170+
];
171+
172+
while (queue.length > 0) {
173+
let poped = queue.shift();
174+
if (poped.x == destX && poped.y == destY) {
175+
return poped.moves[0];
176+
} else {
177+
mp[poped.y][poped.x] = 1;
178+
let neighborList = this.addNeighbors(poped, mp);
179+
for (let i = 0; i < neighborList.length; i++) {
180+
queue.push(neighborList[i]);
181+
}
182+
}
183+
}
184+
185+
return 1; // direction
186+
}
187+
188+
addNeighbors(poped, mp) {
189+
let queue = [];
190+
let numOfRows = mp.length;
191+
let numOfColumns = mp[0].length;
192+
193+
if (
194+
poped.x - 1 >= 0 &&
195+
poped.x - 1 < numOfRows &&
196+
mp[poped.y][poped.x - 1] != 1
197+
) {
198+
let tempMoves = poped.moves.slice();
199+
tempMoves.push(DIRECTION_LEFT);
200+
queue.push({ x: poped.x - 1, y: poped.y, moves: tempMoves });
201+
}
202+
if (
203+
poped.x + 1 >= 0 &&
204+
poped.x + 1 < numOfRows &&
205+
mp[poped.y][poped.x + 1] != 1
206+
) {
207+
let tempMoves = poped.moves.slice();
208+
tempMoves.push(DIRECTION_RIGHT);
209+
queue.push({ x: poped.x + 1, y: poped.y, moves: tempMoves });
210+
}
211+
if (
212+
poped.y - 1 >= 0 &&
213+
poped.y - 1 < numOfColumns &&
214+
mp[poped.y - 1][poped.x] != 1
215+
) {
216+
let tempMoves = poped.moves.slice();
217+
tempMoves.push(DIRECTION_UP);
218+
queue.push({ x: poped.x, y: poped.y - 1, moves: tempMoves });
219+
}
220+
if (
221+
poped.y + 1 >= 0 &&
222+
poped.y + 1 < numOfColumns &&
223+
mp[poped.y + 1][poped.x] != 1
224+
) {
225+
let tempMoves = poped.moves.slice();
226+
tempMoves.push(DIRECTION_BOTTOM);
227+
queue.push({ x: poped.x - 1, y: poped.y + 1, moves: tempMoves });
228+
}
229+
return queue;
230+
}
231+
232+
getMapX() {
233+
let mapX = parseInt(this.x / oneBlockSize);
234+
return mapX;
235+
}
236+
237+
getMapY() {
238+
let mapY = parseInt(this.y / oneBlockSize);
239+
return mapY;
240+
}
241+
242+
getMapXRightSide() {
243+
let mapX = parseInt((this.x * 0.99 + oneBlockSize) / oneBlockSize);
244+
return mapX;
245+
}
246+
247+
getMapYRightSide() {
248+
let mapY = parseInt((this.y * 0.99 + oneBlockSize) / oneBlockSize);
249+
return mapY;
250+
}
251+
252+
changeAnimation() {
253+
this.currentFrame =
254+
this.currentFrame == this.frameCount ? 1 : this.currentFrame + 1;
255+
}
256+
257+
draw() {
258+
canvasContext.save();
259+
canvasContext.drawImage(
260+
ghostFrames,
261+
this.imageX,
262+
this.imageY,
263+
this.imageWidth,
264+
this.imageHeight,
265+
this.x,
266+
this.y,
267+
this.width,
268+
this.height
269+
);
270+
canvasContext.restore();
271+
}
272+
}
273+
274+
let updateGhosts = () => {
275+
for (let i = 0; i < ghosts.length; i++) {
276+
ghosts[i].moveProcess();
277+
}
278+
};
279+
280+
let drawGhosts = () => {
281+
for (let i = 0; i < ghosts.length; i++) {
282+
ghosts[i].draw();
283+
}
284+
};

ghost.png

11.7 KB
Loading

pacman.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Pacman</title>
9+
</head>
10+
11+
<body style="margin: 0; background-color: black;">
12+
<canvas id="canvas" width="500" height="500"></canvas>
13+
<div style="display:none;">
14+
<img id="animation" src="animations.gif" width="140" height="20">
15+
<img id="ghosts" src="ghost.png" width="140" height="20">
16+
</div>
17+
<script src="ghost.js"></script>
18+
<script src="pacman.js"></script>
19+
</body>
20+
21+
</html>

0 commit comments

Comments
 (0)