├── src ├── resources │ └── images │ │ ├── file.txt │ │ ├── up1.png │ │ ├── up2.png │ │ ├── up3.png │ │ ├── down1.png │ │ ├── down2.png │ │ ├── down3.png │ │ ├── ghost.png │ │ ├── left1.png │ │ ├── left2.png │ │ ├── left3.png │ │ ├── pacman.png │ │ ├── right1.png │ │ ├── right2.png │ │ └── right3.png └── com │ └── zetcode │ ├── Pacman.java │ └── Board.java ├── README.md └── LICENSE /src/resources/images/file.txt: -------------------------------------------------------------------------------- 1 | - 2 | -------------------------------------------------------------------------------- /src/resources/images/up1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/up1.png -------------------------------------------------------------------------------- /src/resources/images/up2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/up2.png -------------------------------------------------------------------------------- /src/resources/images/up3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/up3.png -------------------------------------------------------------------------------- /src/resources/images/down1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/down1.png -------------------------------------------------------------------------------- /src/resources/images/down2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/down2.png -------------------------------------------------------------------------------- /src/resources/images/down3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/down3.png -------------------------------------------------------------------------------- /src/resources/images/ghost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/ghost.png -------------------------------------------------------------------------------- /src/resources/images/left1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/left1.png -------------------------------------------------------------------------------- /src/resources/images/left2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/left2.png -------------------------------------------------------------------------------- /src/resources/images/left3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/left3.png -------------------------------------------------------------------------------- /src/resources/images/pacman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/pacman.png -------------------------------------------------------------------------------- /src/resources/images/right1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/right1.png -------------------------------------------------------------------------------- /src/resources/images/right2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/right2.png -------------------------------------------------------------------------------- /src/resources/images/right3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Java-Pacman-Game/HEAD/src/resources/images/right3.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-Pacman-Game 2 | Source code of the Java Pacman game 3 | 4 | https://zetcode.com/javagames/pacman/ 5 | 6 | Java games programming e-book: https://zetcode.com/ebooks/javagames/ 7 | -------------------------------------------------------------------------------- /src/com/zetcode/Pacman.java: -------------------------------------------------------------------------------- 1 | package com.zetcode; 2 | 3 | import java.awt.EventQueue; 4 | import javax.swing.JFrame; 5 | 6 | public class Pacman extends JFrame { 7 | 8 | public Pacman() { 9 | 10 | initUI(); 11 | } 12 | 13 | private void initUI() { 14 | 15 | add(new Board()); 16 | 17 | setTitle("Pacman"); 18 | setDefaultCloseOperation(EXIT_ON_CLOSE); 19 | setSize(380, 420); 20 | setLocationRelativeTo(null); 21 | } 22 | 23 | public static void main(String[] args) { 24 | 25 | EventQueue.invokeLater(() -> { 26 | 27 | var ex = new Pacman(); 28 | ex.setVisible(true); 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2021, Jan Bodnar 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /src/com/zetcode/Board.java: -------------------------------------------------------------------------------- 1 | package com.zetcode; 2 | 3 | import java.awt.BasicStroke; 4 | import java.awt.Color; 5 | import java.awt.Dimension; 6 | import java.awt.Event; 7 | import java.awt.Font; 8 | import java.awt.FontMetrics; 9 | import java.awt.Graphics; 10 | import java.awt.Graphics2D; 11 | import java.awt.Image; 12 | import java.awt.Toolkit; 13 | import java.awt.event.ActionEvent; 14 | import java.awt.event.ActionListener; 15 | import java.awt.event.KeyAdapter; 16 | import java.awt.event.KeyEvent; 17 | 18 | import javax.swing.ImageIcon; 19 | import javax.swing.JPanel; 20 | import javax.swing.Timer; 21 | 22 | public class Board extends JPanel implements ActionListener { 23 | 24 | private Dimension d; 25 | private final Font smallFont = new Font("Helvetica", Font.BOLD, 14); 26 | 27 | private Image ii; 28 | private final Color dotColor = new Color(192, 192, 0); 29 | private Color mazeColor; 30 | 31 | private boolean inGame = false; 32 | private boolean dying = false; 33 | 34 | private final int BLOCK_SIZE = 24; 35 | private final int N_BLOCKS = 15; 36 | private final int SCREEN_SIZE = N_BLOCKS * BLOCK_SIZE; 37 | private final int PAC_ANIM_DELAY = 2; 38 | private final int PACMAN_ANIM_COUNT = 4; 39 | private final int MAX_GHOSTS = 12; 40 | private final int PACMAN_SPEED = 6; 41 | 42 | private int pacAnimCount = PAC_ANIM_DELAY; 43 | private int pacAnimDir = 1; 44 | private int pacmanAnimPos = 0; 45 | private int N_GHOSTS = 6; 46 | private int pacsLeft, score; 47 | private int[] dx, dy; 48 | private int[] ghost_x, ghost_y, ghost_dx, ghost_dy, ghostSpeed; 49 | 50 | private Image ghost; 51 | private Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down; 52 | private Image pacman3up, pacman3down, pacman3left, pacman3right; 53 | private Image pacman4up, pacman4down, pacman4left, pacman4right; 54 | 55 | private int pacman_x, pacman_y, pacmand_x, pacmand_y; 56 | private int req_dx, req_dy, view_dx, view_dy; 57 | 58 | private final short levelData[] = { 59 | 19, 26, 26, 26, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22, 60 | 21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 61 | 21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 62 | 21, 0, 0, 0, 17, 16, 16, 24, 16, 16, 16, 16, 16, 16, 20, 63 | 17, 18, 18, 18, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 20, 64 | 17, 16, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 16, 24, 20, 65 | 25, 16, 16, 16, 24, 24, 28, 0, 25, 24, 24, 16, 20, 0, 21, 66 | 1, 17, 16, 20, 0, 0, 0, 0, 0, 0, 0, 17, 20, 0, 21, 67 | 1, 17, 16, 16, 18, 18, 22, 0, 19, 18, 18, 16, 20, 0, 21, 68 | 1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21, 69 | 1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21, 70 | 1, 17, 16, 16, 16, 16, 16, 18, 16, 16, 16, 16, 20, 0, 21, 71 | 1, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 21, 72 | 1, 25, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 18, 20, 73 | 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 25, 24, 24, 24, 28 74 | }; 75 | 76 | private final int validSpeeds[] = {1, 2, 3, 4, 6, 8}; 77 | private final int maxSpeed = 6; 78 | 79 | private int currentSpeed = 3; 80 | private short[] screenData; 81 | private Timer timer; 82 | 83 | public Board() { 84 | 85 | loadImages(); 86 | initVariables(); 87 | initBoard(); 88 | } 89 | 90 | private void initBoard() { 91 | 92 | addKeyListener(new TAdapter()); 93 | 94 | setFocusable(true); 95 | 96 | setBackground(Color.black); 97 | } 98 | 99 | private void initVariables() { 100 | 101 | screenData = new short[N_BLOCKS * N_BLOCKS]; 102 | mazeColor = new Color(5, 100, 5); 103 | d = new Dimension(400, 400); 104 | ghost_x = new int[MAX_GHOSTS]; 105 | ghost_dx = new int[MAX_GHOSTS]; 106 | ghost_y = new int[MAX_GHOSTS]; 107 | ghost_dy = new int[MAX_GHOSTS]; 108 | ghostSpeed = new int[MAX_GHOSTS]; 109 | dx = new int[4]; 110 | dy = new int[4]; 111 | 112 | timer = new Timer(40, this); 113 | timer.start(); 114 | } 115 | 116 | @Override 117 | public void addNotify() { 118 | super.addNotify(); 119 | 120 | initGame(); 121 | } 122 | 123 | private void doAnim() { 124 | 125 | pacAnimCount--; 126 | 127 | if (pacAnimCount <= 0) { 128 | pacAnimCount = PAC_ANIM_DELAY; 129 | pacmanAnimPos = pacmanAnimPos + pacAnimDir; 130 | 131 | if (pacmanAnimPos == (PACMAN_ANIM_COUNT - 1) || pacmanAnimPos == 0) { 132 | pacAnimDir = -pacAnimDir; 133 | } 134 | } 135 | } 136 | 137 | private void playGame(Graphics2D g2d) { 138 | 139 | if (dying) { 140 | 141 | death(); 142 | 143 | } else { 144 | 145 | movePacman(); 146 | drawPacman(g2d); 147 | moveGhosts(g2d); 148 | checkMaze(); 149 | } 150 | } 151 | 152 | private void showIntroScreen(Graphics2D g2d) { 153 | 154 | g2d.setColor(new Color(0, 32, 48)); 155 | g2d.fillRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50); 156 | g2d.setColor(Color.white); 157 | g2d.drawRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50); 158 | 159 | String s = "Press s to start."; 160 | Font small = new Font("Helvetica", Font.BOLD, 14); 161 | FontMetrics metr = this.getFontMetrics(small); 162 | 163 | g2d.setColor(Color.white); 164 | g2d.setFont(small); 165 | g2d.drawString(s, (SCREEN_SIZE - metr.stringWidth(s)) / 2, SCREEN_SIZE / 2); 166 | } 167 | 168 | private void drawScore(Graphics2D g) { 169 | 170 | int i; 171 | String s; 172 | 173 | g.setFont(smallFont); 174 | g.setColor(new Color(96, 128, 255)); 175 | s = "Score: " + score; 176 | g.drawString(s, SCREEN_SIZE / 2 + 96, SCREEN_SIZE + 16); 177 | 178 | for (i = 0; i < pacsLeft; i++) { 179 | g.drawImage(pacman3left, i * 28 + 8, SCREEN_SIZE + 1, this); 180 | } 181 | } 182 | 183 | private void checkMaze() { 184 | 185 | short i = 0; 186 | boolean finished = true; 187 | 188 | while (i < N_BLOCKS * N_BLOCKS && finished) { 189 | 190 | if ((screenData[i] & 48) != 0) { 191 | finished = false; 192 | } 193 | 194 | i++; 195 | } 196 | 197 | if (finished) { 198 | 199 | score += 50; 200 | 201 | if (N_GHOSTS < MAX_GHOSTS) { 202 | N_GHOSTS++; 203 | } 204 | 205 | if (currentSpeed < maxSpeed) { 206 | currentSpeed++; 207 | } 208 | 209 | initLevel(); 210 | } 211 | } 212 | 213 | private void death() { 214 | 215 | pacsLeft--; 216 | 217 | if (pacsLeft == 0) { 218 | inGame = false; 219 | } 220 | 221 | continueLevel(); 222 | } 223 | 224 | private void moveGhosts(Graphics2D g2d) { 225 | 226 | short i; 227 | int pos; 228 | int count; 229 | 230 | for (i = 0; i < N_GHOSTS; i++) { 231 | if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) { 232 | pos = ghost_x[i] / BLOCK_SIZE + N_BLOCKS * (int) (ghost_y[i] / BLOCK_SIZE); 233 | 234 | count = 0; 235 | 236 | if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) { 237 | dx[count] = -1; 238 | dy[count] = 0; 239 | count++; 240 | } 241 | 242 | if ((screenData[pos] & 2) == 0 && ghost_dy[i] != 1) { 243 | dx[count] = 0; 244 | dy[count] = -1; 245 | count++; 246 | } 247 | 248 | if ((screenData[pos] & 4) == 0 && ghost_dx[i] != -1) { 249 | dx[count] = 1; 250 | dy[count] = 0; 251 | count++; 252 | } 253 | 254 | if ((screenData[pos] & 8) == 0 && ghost_dy[i] != -1) { 255 | dx[count] = 0; 256 | dy[count] = 1; 257 | count++; 258 | } 259 | 260 | if (count == 0) { 261 | 262 | if ((screenData[pos] & 15) == 15) { 263 | ghost_dx[i] = 0; 264 | ghost_dy[i] = 0; 265 | } else { 266 | ghost_dx[i] = -ghost_dx[i]; 267 | ghost_dy[i] = -ghost_dy[i]; 268 | } 269 | 270 | } else { 271 | 272 | count = (int) (Math.random() * count); 273 | 274 | if (count > 3) { 275 | count = 3; 276 | } 277 | 278 | ghost_dx[i] = dx[count]; 279 | ghost_dy[i] = dy[count]; 280 | } 281 | 282 | } 283 | 284 | ghost_x[i] = ghost_x[i] + (ghost_dx[i] * ghostSpeed[i]); 285 | ghost_y[i] = ghost_y[i] + (ghost_dy[i] * ghostSpeed[i]); 286 | drawGhost(g2d, ghost_x[i] + 1, ghost_y[i] + 1); 287 | 288 | if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12) 289 | && pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12) 290 | && inGame) { 291 | 292 | dying = true; 293 | } 294 | } 295 | } 296 | 297 | private void drawGhost(Graphics2D g2d, int x, int y) { 298 | 299 | g2d.drawImage(ghost, x, y, this); 300 | } 301 | 302 | private void movePacman() { 303 | 304 | int pos; 305 | short ch; 306 | 307 | if (req_dx == -pacmand_x && req_dy == -pacmand_y) { 308 | pacmand_x = req_dx; 309 | pacmand_y = req_dy; 310 | view_dx = pacmand_x; 311 | view_dy = pacmand_y; 312 | } 313 | 314 | if (pacman_x % BLOCK_SIZE == 0 && pacman_y % BLOCK_SIZE == 0) { 315 | pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE); 316 | ch = screenData[pos]; 317 | 318 | if ((ch & 16) != 0) { 319 | screenData[pos] = (short) (ch & 15); 320 | score++; 321 | } 322 | 323 | if (req_dx != 0 || req_dy != 0) { 324 | if (!((req_dx == -1 && req_dy == 0 && (ch & 1) != 0) 325 | || (req_dx == 1 && req_dy == 0 && (ch & 4) != 0) 326 | || (req_dx == 0 && req_dy == -1 && (ch & 2) != 0) 327 | || (req_dx == 0 && req_dy == 1 && (ch & 8) != 0))) { 328 | pacmand_x = req_dx; 329 | pacmand_y = req_dy; 330 | view_dx = pacmand_x; 331 | view_dy = pacmand_y; 332 | } 333 | } 334 | 335 | // Check for standstill 336 | if ((pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0) 337 | || (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0) 338 | || (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0) 339 | || (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) { 340 | pacmand_x = 0; 341 | pacmand_y = 0; 342 | } 343 | } 344 | pacman_x = pacman_x + PACMAN_SPEED * pacmand_x; 345 | pacman_y = pacman_y + PACMAN_SPEED * pacmand_y; 346 | } 347 | 348 | private void drawPacman(Graphics2D g2d) { 349 | 350 | if (view_dx == -1) { 351 | drawPacnanLeft(g2d); 352 | } else if (view_dx == 1) { 353 | drawPacmanRight(g2d); 354 | } else if (view_dy == -1) { 355 | drawPacmanUp(g2d); 356 | } else { 357 | drawPacmanDown(g2d); 358 | } 359 | } 360 | 361 | private void drawPacmanUp(Graphics2D g2d) { 362 | 363 | switch (pacmanAnimPos) { 364 | case 1: 365 | g2d.drawImage(pacman2up, pacman_x + 1, pacman_y + 1, this); 366 | break; 367 | case 2: 368 | g2d.drawImage(pacman3up, pacman_x + 1, pacman_y + 1, this); 369 | break; 370 | case 3: 371 | g2d.drawImage(pacman4up, pacman_x + 1, pacman_y + 1, this); 372 | break; 373 | default: 374 | g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); 375 | break; 376 | } 377 | } 378 | 379 | private void drawPacmanDown(Graphics2D g2d) { 380 | 381 | switch (pacmanAnimPos) { 382 | case 1: 383 | g2d.drawImage(pacman2down, pacman_x + 1, pacman_y + 1, this); 384 | break; 385 | case 2: 386 | g2d.drawImage(pacman3down, pacman_x + 1, pacman_y + 1, this); 387 | break; 388 | case 3: 389 | g2d.drawImage(pacman4down, pacman_x + 1, pacman_y + 1, this); 390 | break; 391 | default: 392 | g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); 393 | break; 394 | } 395 | } 396 | 397 | private void drawPacnanLeft(Graphics2D g2d) { 398 | 399 | switch (pacmanAnimPos) { 400 | case 1: 401 | g2d.drawImage(pacman2left, pacman_x + 1, pacman_y + 1, this); 402 | break; 403 | case 2: 404 | g2d.drawImage(pacman3left, pacman_x + 1, pacman_y + 1, this); 405 | break; 406 | case 3: 407 | g2d.drawImage(pacman4left, pacman_x + 1, pacman_y + 1, this); 408 | break; 409 | default: 410 | g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); 411 | break; 412 | } 413 | } 414 | 415 | private void drawPacmanRight(Graphics2D g2d) { 416 | 417 | switch (pacmanAnimPos) { 418 | case 1: 419 | g2d.drawImage(pacman2right, pacman_x + 1, pacman_y + 1, this); 420 | break; 421 | case 2: 422 | g2d.drawImage(pacman3right, pacman_x + 1, pacman_y + 1, this); 423 | break; 424 | case 3: 425 | g2d.drawImage(pacman4right, pacman_x + 1, pacman_y + 1, this); 426 | break; 427 | default: 428 | g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); 429 | break; 430 | } 431 | } 432 | 433 | private void drawMaze(Graphics2D g2d) { 434 | 435 | short i = 0; 436 | int x, y; 437 | 438 | for (y = 0; y < SCREEN_SIZE; y += BLOCK_SIZE) { 439 | for (x = 0; x < SCREEN_SIZE; x += BLOCK_SIZE) { 440 | 441 | g2d.setColor(mazeColor); 442 | g2d.setStroke(new BasicStroke(2)); 443 | 444 | if ((screenData[i] & 1) != 0) { 445 | g2d.drawLine(x, y, x, y + BLOCK_SIZE - 1); 446 | } 447 | 448 | if ((screenData[i] & 2) != 0) { 449 | g2d.drawLine(x, y, x + BLOCK_SIZE - 1, y); 450 | } 451 | 452 | if ((screenData[i] & 4) != 0) { 453 | g2d.drawLine(x + BLOCK_SIZE - 1, y, x + BLOCK_SIZE - 1, 454 | y + BLOCK_SIZE - 1); 455 | } 456 | 457 | if ((screenData[i] & 8) != 0) { 458 | g2d.drawLine(x, y + BLOCK_SIZE - 1, x + BLOCK_SIZE - 1, 459 | y + BLOCK_SIZE - 1); 460 | } 461 | 462 | if ((screenData[i] & 16) != 0) { 463 | g2d.setColor(dotColor); 464 | g2d.fillRect(x + 11, y + 11, 2, 2); 465 | } 466 | 467 | i++; 468 | } 469 | } 470 | } 471 | 472 | private void initGame() { 473 | 474 | pacsLeft = 3; 475 | score = 0; 476 | initLevel(); 477 | N_GHOSTS = 6; 478 | currentSpeed = 3; 479 | } 480 | 481 | private void initLevel() { 482 | 483 | int i; 484 | for (i = 0; i < N_BLOCKS * N_BLOCKS; i++) { 485 | screenData[i] = levelData[i]; 486 | } 487 | 488 | continueLevel(); 489 | } 490 | 491 | private void continueLevel() { 492 | 493 | short i; 494 | int dx = 1; 495 | int random; 496 | 497 | for (i = 0; i < N_GHOSTS; i++) { 498 | 499 | ghost_y[i] = 4 * BLOCK_SIZE; 500 | ghost_x[i] = 4 * BLOCK_SIZE; 501 | ghost_dy[i] = 0; 502 | ghost_dx[i] = dx; 503 | dx = -dx; 504 | random = (int) (Math.random() * (currentSpeed + 1)); 505 | 506 | if (random > currentSpeed) { 507 | random = currentSpeed; 508 | } 509 | 510 | ghostSpeed[i] = validSpeeds[random]; 511 | } 512 | 513 | pacman_x = 7 * BLOCK_SIZE; 514 | pacman_y = 11 * BLOCK_SIZE; 515 | pacmand_x = 0; 516 | pacmand_y = 0; 517 | req_dx = 0; 518 | req_dy = 0; 519 | view_dx = -1; 520 | view_dy = 0; 521 | dying = false; 522 | } 523 | 524 | private void loadImages() { 525 | 526 | ghost = new ImageIcon("src/resources/images/ghost.png").getImage(); 527 | pacman1 = new ImageIcon("src/resources/images/pacman.png").getImage(); 528 | pacman2up = new ImageIcon("src/resources/images/up1.png").getImage(); 529 | pacman3up = new ImageIcon("src/resources/images/up2.png").getImage(); 530 | pacman4up = new ImageIcon("src/resources/images/up3.png").getImage(); 531 | pacman2down = new ImageIcon("src/resources/images/down1.png").getImage(); 532 | pacman3down = new ImageIcon("src/resources/images/down2.png").getImage(); 533 | pacman4down = new ImageIcon("src/resources/images/down3.png").getImage(); 534 | pacman2left = new ImageIcon("src/resources/images/left1.png").getImage(); 535 | pacman3left = new ImageIcon("src/resources/images/left2.png").getImage(); 536 | pacman4left = new ImageIcon("src/resources/images/left3.png").getImage(); 537 | pacman2right = new ImageIcon("src/resources/images/right1.png").getImage(); 538 | pacman3right = new ImageIcon("src/resources/images/right2.png").getImage(); 539 | pacman4right = new ImageIcon("src/resources/images/right3.png").getImage(); 540 | 541 | } 542 | 543 | @Override 544 | public void paintComponent(Graphics g) { 545 | super.paintComponent(g); 546 | 547 | doDrawing(g); 548 | } 549 | 550 | private void doDrawing(Graphics g) { 551 | 552 | Graphics2D g2d = (Graphics2D) g; 553 | 554 | g2d.setColor(Color.black); 555 | g2d.fillRect(0, 0, d.width, d.height); 556 | 557 | drawMaze(g2d); 558 | drawScore(g2d); 559 | doAnim(); 560 | 561 | if (inGame) { 562 | playGame(g2d); 563 | } else { 564 | showIntroScreen(g2d); 565 | } 566 | 567 | g2d.drawImage(ii, 5, 5, this); 568 | Toolkit.getDefaultToolkit().sync(); 569 | g2d.dispose(); 570 | } 571 | 572 | class TAdapter extends KeyAdapter { 573 | 574 | @Override 575 | public void keyPressed(KeyEvent e) { 576 | 577 | int key = e.getKeyCode(); 578 | 579 | if (inGame) { 580 | if (key == KeyEvent.VK_LEFT) { 581 | req_dx = -1; 582 | req_dy = 0; 583 | } else if (key == KeyEvent.VK_RIGHT) { 584 | req_dx = 1; 585 | req_dy = 0; 586 | } else if (key == KeyEvent.VK_UP) { 587 | req_dx = 0; 588 | req_dy = -1; 589 | } else if (key == KeyEvent.VK_DOWN) { 590 | req_dx = 0; 591 | req_dy = 1; 592 | } else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) { 593 | inGame = false; 594 | } else if (key == KeyEvent.VK_PAUSE) { 595 | if (timer.isRunning()) { 596 | timer.stop(); 597 | } else { 598 | timer.start(); 599 | } 600 | } 601 | } else { 602 | if (key == 's' || key == 'S') { 603 | inGame = true; 604 | initGame(); 605 | } 606 | } 607 | } 608 | 609 | @Override 610 | public void keyReleased(KeyEvent e) { 611 | 612 | int key = e.getKeyCode(); 613 | 614 | if (key == Event.LEFT || key == Event.RIGHT 615 | || key == Event.UP || key == Event.DOWN) { 616 | req_dx = 0; 617 | req_dy = 0; 618 | } 619 | } 620 | } 621 | 622 | @Override 623 | public void actionPerformed(ActionEvent e) { 624 | 625 | repaint(); 626 | } 627 | } 628 | --------------------------------------------------------------------------------