-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPacMan.java
355 lines (316 loc) · 11.4 KB
/
PacMan.java
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
import java.awt.*;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Random;
import javax.swing.*;
public class PacMan extends JPanel implements ActionListener, KeyListener {
class Block {
int x;
int y;
int width;
int height;
Image image;
int startX;
int startY;
char direction = 'U'; // U D L R
int velocityX = 0;
int velocityY = 0;
Block(Image image, int x, int y, int width, int height) {
this.image = image;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.startX = x;
this.startY = y;
}
void updateDirection(char direction) {
char prevDirection = this.direction;
this.direction = direction;
updateVelocity();
this.x += this.velocityX;
this.y += this.velocityY;
for (Block wall : walls) {
if (collision(this, wall)) {
this.x -= this.velocityX;
this.y -= this.velocityY;
this.direction = prevDirection;
updateVelocity();
}
}
}
void updateVelocity() {
if (this.direction == 'U') {
this.velocityX = 0;
this.velocityY = -tileSize/4;
}
else if (this.direction == 'D') {
this.velocityX = 0;
this.velocityY = tileSize/4;
}
else if (this.direction == 'L') {
this.velocityX = -tileSize/4;
this.velocityY = 0;
}
else if (this.direction == 'R') {
this.velocityX = tileSize/4;
this.velocityY = 0;
}
}
void reset() {
this.x = this.startX;
this.y = this.startY;
}
}
private int rowCount = 21;
private int columnCount = 19;
private int tileSize = 32;
private int boardWidth = columnCount * tileSize;
private int boardHeight = rowCount * tileSize;
private Image wallImage;
private Image blueGhostImage;
private Image orangeGhostImage;
private Image pinkGhostImage;
private Image redGhostImage;
private Image pacmanUpImage;
private Image pacmanDownImage;
private Image pacmanLeftImage;
private Image pacmanRightImage;
//X = wall, O = skip, P = pac man, ' ' = food
//Ghosts: b = blue, o = orange, p = pink, r = red
private String[] tileMap = {
"XXXXXXXXXXXXXXXXXXX",
"X X X",
"X XX XXX X XXX XX X",
"X X",
"X XX X XXXXX X XX X",
"X X X X",
"XXXX XXXX XXXX XXXX",
"OOOX X X XOOO",
"XXXX X XXrXX X XXXX",
"O bpo O",
"XXXX X XXXXX X XXXX",
"OOOX X X XOOO",
"XXXX X XXXXX X XXXX",
"X X X",
"X XX XXX X XXX XX X",
"X X P X X",
"XX X X XXXXX X X XX",
"X X X X X",
"X XXXXXX X XXXXXX X",
"X X",
"XXXXXXXXXXXXXXXXXXX"
};
HashSet<Block> walls;
HashSet<Block> foods;
HashSet<Block> ghosts;
Block pacman;
Timer gameLoop;
char[] directions = {'U', 'D', 'L', 'R'}; //up down left right
Random random = new Random();
int score = 0;
int lives = 3;
boolean gameOver = false;
PacMan() {
setPreferredSize(new Dimension(boardWidth, boardHeight));
setBackground(Color.BLACK);
addKeyListener(this);
setFocusable(true);
//load images
wallImage = new ImageIcon(getClass().getResource("./wall.png")).getImage();
blueGhostImage = new ImageIcon(getClass().getResource("./blueGhost.png")).getImage();
orangeGhostImage = new ImageIcon(getClass().getResource("./orangeGhost.png")).getImage();
pinkGhostImage = new ImageIcon(getClass().getResource("./pinkGhost.png")).getImage();
redGhostImage = new ImageIcon(getClass().getResource("./redGhost.png")).getImage();
pacmanUpImage = new ImageIcon(getClass().getResource("./pacmanUp.png")).getImage();
pacmanDownImage = new ImageIcon(getClass().getResource("./pacmanDown.png")).getImage();
pacmanLeftImage = new ImageIcon(getClass().getResource("./pacmanLeft.png")).getImage();
pacmanRightImage = new ImageIcon(getClass().getResource("./pacmanRight.png")).getImage();
loadMap();
for (Block ghost : ghosts) {
char newDirection = directions[random.nextInt(4)];
ghost.updateDirection(newDirection);
}
//how long it takes to start timer, milliseconds gone between frames
gameLoop = new Timer(50, this); //20fps (1000/50)
gameLoop.start();
}
public void loadMap() {
walls = new HashSet<Block>();
foods = new HashSet<Block>();
ghosts = new HashSet<Block>();
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < columnCount; c++) {
String row = tileMap[r];
char tileMapChar = row.charAt(c);
int x = c*tileSize;
int y = r*tileSize;
if (tileMapChar == 'X') { //block wall
Block wall = new Block(wallImage, x, y, tileSize, tileSize);
walls.add(wall);
}
else if (tileMapChar == 'b') { //blue ghost
Block ghost = new Block(blueGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}
else if (tileMapChar == 'o') { //orange ghost
Block ghost = new Block(orangeGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}
else if (tileMapChar == 'p') { //pink ghost
Block ghost = new Block(pinkGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}
else if (tileMapChar == 'r') { //red ghost
Block ghost = new Block(redGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}
else if (tileMapChar == 'P') { //pacman
pacman = new Block(pacmanRightImage, x, y, tileSize, tileSize);
}
else if (tileMapChar == ' ') { //food
Block food = new Block(null, x + 14, y + 14, 4, 4);
foods.add(food);
}
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
g.drawImage(pacman.image, pacman.x, pacman.y, pacman.width, pacman.height, null);
for (Block ghost : ghosts) {
g.drawImage(ghost.image, ghost.x, ghost.y, ghost.width, ghost.height, null);
}
for (Block wall : walls) {
g.drawImage(wall.image, wall.x, wall.y, wall.width, wall.height, null);
}
g.setColor(Color.WHITE);
for (Block food : foods) {
g.fillRect(food.x, food.y, food.width, food.height);
}
//score
g.setFont(new Font("Arial", Font.PLAIN, 18));
if (gameOver) {
g.drawString("Game Over: " + String.valueOf(score), tileSize/2, tileSize/2);
}
else {
g.drawString("x" + String.valueOf(lives) + " Score: " + String.valueOf(score), tileSize/2, tileSize/2);
}
}
public void move() {
pacman.x += pacman.velocityX;
pacman.y += pacman.velocityY;
//check wall collisions
for (Block wall : walls) {
if (collision(pacman, wall)) {
pacman.x -= pacman.velocityX;
pacman.y -= pacman.velocityY;
break;
}
}
//check ghost collisions
for (Block ghost : ghosts) {
if (collision(ghost, pacman)) {
lives -= 1;
if (lives == 0) {
gameOver = true;
return;
}
resetPositions();
}
if (ghost.y == tileSize*9 && ghost.direction != 'U' && ghost.direction != 'D') {
ghost.updateDirection('U');
}
ghost.x += ghost.velocityX;
ghost.y += ghost.velocityY;
for (Block wall : walls) {
if (collision(ghost, wall) || ghost.x <= 0 || ghost.x + ghost.width >= boardWidth) {
ghost.x -= ghost.velocityX;
ghost.y -= ghost.velocityY;
char newDirection = directions[random.nextInt(4)];
ghost.updateDirection(newDirection);
}
}
}
//check food collision
Block foodEaten = null;
for (Block food : foods) {
if (collision(pacman, food)) {
foodEaten = food;
score += 10;
}
}
foods.remove(foodEaten);
if (foods.isEmpty()) {
loadMap();
resetPositions();
}
}
public boolean collision(Block a, Block b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
public void resetPositions() {
pacman.reset();
pacman.velocityX = 0;
pacman.velocityY = 0;
for (Block ghost : ghosts) {
ghost.reset();
char newDirection = directions[random.nextInt(4)];
ghost.updateDirection(newDirection);
}
}
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
if (gameOver) {
gameLoop.stop();
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {
if (gameOver) {
loadMap();
resetPositions();
lives = 3;
score = 0;
gameOver = false;
gameLoop.start();
}
// System.out.println("KeyEvent: " + e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_UP) {
pacman.updateDirection('U');
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
pacman.updateDirection('D');
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
pacman.updateDirection('L');
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
pacman.updateDirection('R');
}
if (pacman.direction == 'U') {
pacman.image = pacmanUpImage;
}
else if (pacman.direction == 'D') {
pacman.image = pacmanDownImage;
}
else if (pacman.direction == 'L') {
pacman.image = pacmanLeftImage;
}
else if (pacman.direction == 'R') {
pacman.image = pacmanRightImage;
}
}
}