-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.js
172 lines (158 loc) · 4.5 KB
/
pacman.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
class Pacman {
constructor(x, y, width, height, speed) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.direction = 4;
this.nextDirection = 4;
this.frameCount = 7;
this.currentFrame = 1;
setInterval(() => {
this.changeAnimation();
}, 100);
}
moveProcess() {
this.changeDirectionIfPossible();
this.moveForwards();
if (this.checkCollisions()) {
this.moveBackwards();
return;
}
}
eat() {
for (let i = 0; i < map.length; i++) {
for (let j = 0; j < map[0].length; j++) {
if (
map[i][j] == 2 &&
this.getMapX() == j &&
this.getMapY() == i
) {
map[i][j] = 3;
score++;
}
}
}
}
moveBackwards() {
switch (this.direction) {
case DIRECTION_RIGHT: // Right
this.x -= this.speed;
break;
case DIRECTION_UP: // Up
this.y += this.speed;
break;
case DIRECTION_LEFT: // Left
this.x += this.speed;
break;
case DIRECTION_BOTTOM: // Bottom
this.y -= this.speed;
break;
}
}
moveForwards() {
switch (this.direction) {
case DIRECTION_RIGHT: // Right
this.x += this.speed;
break;
case DIRECTION_UP: // Up
this.y -= this.speed;
break;
case DIRECTION_LEFT: // Left
this.x -= this.speed;
break;
case DIRECTION_BOTTOM: // Bottom
this.y += this.speed;
break;
}
}
checkCollisions() {
let isCollided = false;
if (
map[parseInt(this.y / oneBlockSize)][
parseInt(this.x / oneBlockSize)
] == 1 ||
map[parseInt(this.y / oneBlockSize + 0.9999)][
parseInt(this.x / oneBlockSize)
] == 1 ||
map[parseInt(this.y / oneBlockSize)][
parseInt(this.x / oneBlockSize + 0.9999)
] == 1 ||
map[parseInt(this.y / oneBlockSize + 0.9999)][
parseInt(this.x / oneBlockSize + 0.9999)
] == 1
) {
isCollided = true;
}
return isCollided;
}
checkGhostCollision(ghosts) {
for (let i = 0; i < ghosts.length; i++) {
let ghost = ghosts[i];
if (
ghost.getMapX() == this.getMapX() &&
ghost.getMapY() == this.getMapY()
) {
return true;
}
}
return false;
}
changeDirectionIfPossible() {
if (this.direction == this.nextDirection) return;
let tempDirection = this.direction;
this.direction = this.nextDirection;
this.moveForwards();
if (this.checkCollisions()) {
this.moveBackwards();
this.direction = tempDirection;
} else {
this.moveBackwards();
}
}
getMapX() {
let mapX = parseInt(this.x / oneBlockSize);
return mapX;
}
getMapY() {
let mapY = parseInt(this.y / oneBlockSize);
return mapY;
}
getMapXRightSide() {
let mapX = parseInt((this.x * 0.99 + oneBlockSize) / oneBlockSize);
return mapX;
}
getMapYRightSide() {
let mapY = parseInt((this.y * 0.99 + oneBlockSize) / oneBlockSize);
return mapY;
}
changeAnimation() {
this.currentFrame =
this.currentFrame == this.frameCount ? 1 : this.currentFrame + 1;
}
draw() {
canvasContext.save();
canvasContext.translate(
this.x + oneBlockSize / 2,
this.y + oneBlockSize / 2
);
canvasContext.rotate((this.direction * 90 * Math.PI) / 180);
canvasContext.translate(
-this.x - oneBlockSize / 2,
-this.y - oneBlockSize / 2
);
canvasContext.drawImage(
pacmanFrames,
(this.currentFrame - 1) * oneBlockSize,
0,
oneBlockSize,
oneBlockSize,
this.x,
this.y,
this.width,
this.height
);
canvasContext.restore();
}
}