原文: https://zetcode.com/tutorials/javagamestutorial/pacman/

在 Java 2D 游戏教程的这一部分中,我们将创建一个简单的 Pacman 游戏克隆。

Pacman 是一款街机游戏,最初由日本公司 Namco 在 1980 年开发。Pacman 成为有史以来最受欢迎的街机游戏之一。

开发

以下代码示例是 Brian Postma 对 Pacman 游戏的翻版,可从 http://www.brianpostma.com 获得。 修改并简化了代码,以便于理解。

游戏的目标是收集迷宫中的所有点并避开鬼魂。 吃豆人的动画制作有两种方式:在迷宫中的位置和身体。 我们根据方向为他的身体制作四幅图像。 该动画用于创建吃豆人张开和闭合嘴巴的幻觉。 迷宫由15x15正方形组成。 迷宫的结构基于简单的整数数组。 吃豆人有三命。 我们还计算分数。

游戏包含两个文件:Board.javaPacman.java

Board.java

  1. package com.zetcode;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. import java.awt.Image;
  11. import java.awt.Toolkit;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.KeyAdapter;
  15. import java.awt.event.KeyEvent;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JPanel;
  18. import javax.swing.Timer;
  19. public class Board extends JPanel implements ActionListener {
  20. private Dimension d;
  21. private final Font smallFont = new Font("Helvetica", Font.BOLD, 14);
  22. private Image ii;
  23. private final Color dotColor = new Color(192, 192, 0);
  24. private Color mazeColor;
  25. private boolean inGame = false;
  26. private boolean dying = false;
  27. private final int BLOCK_SIZE = 24;
  28. private final int N_BLOCKS = 15;
  29. private final int SCREEN_SIZE = N_BLOCKS * BLOCK_SIZE;
  30. private final int PAC_ANIM_DELAY = 2;
  31. private final int PACMAN_ANIM_COUNT = 4;
  32. private final int MAX_GHOSTS = 12;
  33. private final int PACMAN_SPEED = 6;
  34. private int pacAnimCount = PAC_ANIM_DELAY;
  35. private int pacAnimDir = 1;
  36. private int pacmanAnimPos = 0;
  37. private int N_GHOSTS = 6;
  38. private int pacsLeft, score;
  39. private int[] dx, dy;
  40. private int[] ghost_x, ghost_y, ghost_dx, ghost_dy, ghostSpeed;
  41. private Image ghost;
  42. private Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down;
  43. private Image pacman3up, pacman3down, pacman3left, pacman3right;
  44. private Image pacman4up, pacman4down, pacman4left, pacman4right;
  45. private int pacman_x, pacman_y, pacmand_x, pacmand_y;
  46. private int req_dx, req_dy, view_dx, view_dy;
  47. private final short levelData[] = {
  48. 19, 26, 26, 26, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22,
  49. 21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  50. 21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  51. 21, 0, 0, 0, 17, 16, 16, 24, 16, 16, 16, 16, 16, 16, 20,
  52. 17, 18, 18, 18, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 20,
  53. 17, 16, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 16, 24, 20,
  54. 25, 16, 16, 16, 24, 24, 28, 0, 25, 24, 24, 16, 20, 0, 21,
  55. 1, 17, 16, 20, 0, 0, 0, 0, 0, 0, 0, 17, 20, 0, 21,
  56. 1, 17, 16, 16, 18, 18, 22, 0, 19, 18, 18, 16, 20, 0, 21,
  57. 1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21,
  58. 1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21,
  59. 1, 17, 16, 16, 16, 16, 16, 18, 16, 16, 16, 16, 20, 0, 21,
  60. 1, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 21,
  61. 1, 25, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 18, 20,
  62. 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 25, 24, 24, 24, 28
  63. };
  64. private final int validSpeeds[] = {1, 2, 3, 4, 6, 8};
  65. private final int maxSpeed = 6;
  66. private int currentSpeed = 3;
  67. private short[] screenData;
  68. private Timer timer;
  69. public Board() {
  70. loadImages();
  71. initVariables();
  72. initBoard();
  73. }
  74. private void initBoard() {
  75. addKeyListener(new TAdapter());
  76. setFocusable(true);
  77. setBackground(Color.black);
  78. }
  79. private void initVariables() {
  80. screenData = new short[N_BLOCKS * N_BLOCKS];
  81. mazeColor = new Color(5, 100, 5);
  82. d = new Dimension(400, 400);
  83. ghost_x = new int[MAX_GHOSTS];
  84. ghost_dx = new int[MAX_GHOSTS];
  85. ghost_y = new int[MAX_GHOSTS];
  86. ghost_dy = new int[MAX_GHOSTS];
  87. ghostSpeed = new int[MAX_GHOSTS];
  88. dx = new int[4];
  89. dy = new int[4];
  90. timer = new Timer(40, this);
  91. timer.start();
  92. }
  93. @Override
  94. public void addNotify() {
  95. super.addNotify();
  96. initGame();
  97. }
  98. private void doAnim() {
  99. pacAnimCount--;
  100. if (pacAnimCount <= 0) {
  101. pacAnimCount = PAC_ANIM_DELAY;
  102. pacmanAnimPos = pacmanAnimPos + pacAnimDir;
  103. if (pacmanAnimPos == (PACMAN_ANIM_COUNT - 1) || pacmanAnimPos == 0) {
  104. pacAnimDir = -pacAnimDir;
  105. }
  106. }
  107. }
  108. private void playGame(Graphics2D g2d) {
  109. if (dying) {
  110. death();
  111. } else {
  112. movePacman();
  113. drawPacman(g2d);
  114. moveGhosts(g2d);
  115. checkMaze();
  116. }
  117. }
  118. private void showIntroScreen(Graphics2D g2d) {
  119. g2d.setColor(new Color(0, 32, 48));
  120. g2d.fillRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50);
  121. g2d.setColor(Color.white);
  122. g2d.drawRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50);
  123. String s = "Press s to start.";
  124. Font small = new Font("Helvetica", Font.BOLD, 14);
  125. FontMetrics metr = this.getFontMetrics(small);
  126. g2d.setColor(Color.white);
  127. g2d.setFont(small);
  128. g2d.drawString(s, (SCREEN_SIZE - metr.stringWidth(s)) / 2, SCREEN_SIZE / 2);
  129. }
  130. private void drawScore(Graphics2D g) {
  131. int i;
  132. String s;
  133. g.setFont(smallFont);
  134. g.setColor(new Color(96, 128, 255));
  135. s = "Score: " + score;
  136. g.drawString(s, SCREEN_SIZE / 2 + 96, SCREEN_SIZE + 16);
  137. for (i = 0; i < pacsLeft; i++) {
  138. g.drawImage(pacman3left, i * 28 + 8, SCREEN_SIZE + 1, this);
  139. }
  140. }
  141. private void checkMaze() {
  142. short i = 0;
  143. boolean finished = true;
  144. while (i < N_BLOCKS * N_BLOCKS && finished) {
  145. if ((screenData[i] & 48) != 0) {
  146. finished = false;
  147. }
  148. i++;
  149. }
  150. if (finished) {
  151. score += 50;
  152. if (N_GHOSTS < MAX_GHOSTS) {
  153. N_GHOSTS++;
  154. }
  155. if (currentSpeed < maxSpeed) {
  156. currentSpeed++;
  157. }
  158. initLevel();
  159. }
  160. }
  161. private void death() {
  162. pacsLeft--;
  163. if (pacsLeft == 0) {
  164. inGame = false;
  165. }
  166. continueLevel();
  167. }
  168. private void moveGhosts(Graphics2D g2d) {
  169. short i;
  170. int pos;
  171. int count;
  172. for (i = 0; i < N_GHOSTS; i++) {
  173. if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) {
  174. pos = ghost_x[i] / BLOCK_SIZE + N_BLOCKS * (int) (ghost_y[i] / BLOCK_SIZE);
  175. count = 0;
  176. if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) {
  177. dx[count] = -1;
  178. dy[count] = 0;
  179. count++;
  180. }
  181. if ((screenData[pos] & 2) == 0 && ghost_dy[i] != 1) {
  182. dx[count] = 0;
  183. dy[count] = -1;
  184. count++;
  185. }
  186. if ((screenData[pos] & 4) == 0 && ghost_dx[i] != -1) {
  187. dx[count] = 1;
  188. dy[count] = 0;
  189. count++;
  190. }
  191. if ((screenData[pos] & 8) == 0 && ghost_dy[i] != -1) {
  192. dx[count] = 0;
  193. dy[count] = 1;
  194. count++;
  195. }
  196. if (count == 0) {
  197. if ((screenData[pos] & 15) == 15) {
  198. ghost_dx[i] = 0;
  199. ghost_dy[i] = 0;
  200. } else {
  201. ghost_dx[i] = -ghost_dx[i];
  202. ghost_dy[i] = -ghost_dy[i];
  203. }
  204. } else {
  205. count = (int) (Math.random() * count);
  206. if (count > 3) {
  207. count = 3;
  208. }
  209. ghost_dx[i] = dx[count];
  210. ghost_dy[i] = dy[count];
  211. }
  212. }
  213. ghost_x[i] = ghost_x[i] + (ghost_dx[i] * ghostSpeed[i]);
  214. ghost_y[i] = ghost_y[i] + (ghost_dy[i] * ghostSpeed[i]);
  215. drawGhost(g2d, ghost_x[i] + 1, ghost_y[i] + 1);
  216. if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12)
  217. && pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12)
  218. && inGame) {
  219. dying = true;
  220. }
  221. }
  222. }
  223. private void drawGhost(Graphics2D g2d, int x, int y) {
  224. g2d.drawImage(ghost, x, y, this);
  225. }
  226. private void movePacman() {
  227. int pos;
  228. short ch;
  229. if (req_dx == -pacmand_x && req_dy == -pacmand_y) {
  230. pacmand_x = req_dx;
  231. pacmand_y = req_dy;
  232. view_dx = pacmand_x;
  233. view_dy = pacmand_y;
  234. }
  235. if (pacman_x % BLOCK_SIZE == 0 && pacman_y % BLOCK_SIZE == 0) {
  236. pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE);
  237. ch = screenData[pos];
  238. if ((ch & 16) != 0) {
  239. screenData[pos] = (short) (ch & 15);
  240. score++;
  241. }
  242. if (req_dx != 0 || req_dy != 0) {
  243. if (!((req_dx == -1 && req_dy == 0 && (ch & 1) != 0)
  244. || (req_dx == 1 && req_dy == 0 && (ch & 4) != 0)
  245. || (req_dx == 0 && req_dy == -1 && (ch & 2) != 0)
  246. || (req_dx == 0 && req_dy == 1 && (ch & 8) != 0))) {
  247. pacmand_x = req_dx;
  248. pacmand_y = req_dy;
  249. view_dx = pacmand_x;
  250. view_dy = pacmand_y;
  251. }
  252. }
  253. // Check for standstill
  254. if ((pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0)
  255. || (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0)
  256. || (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0)
  257. || (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) {
  258. pacmand_x = 0;
  259. pacmand_y = 0;
  260. }
  261. }
  262. pacman_x = pacman_x + PACMAN_SPEED * pacmand_x;
  263. pacman_y = pacman_y + PACMAN_SPEED * pacmand_y;
  264. }
  265. private void drawPacman(Graphics2D g2d) {
  266. if (view_dx == -1) {
  267. drawPacnanLeft(g2d);
  268. } else if (view_dx == 1) {
  269. drawPacmanRight(g2d);
  270. } else if (view_dy == -1) {
  271. drawPacmanUp(g2d);
  272. } else {
  273. drawPacmanDown(g2d);
  274. }
  275. }
  276. private void drawPacmanUp(Graphics2D g2d) {
  277. switch (pacmanAnimPos) {
  278. case 1:
  279. g2d.drawImage(pacman2up, pacman_x + 1, pacman_y + 1, this);
  280. break;
  281. case 2:
  282. g2d.drawImage(pacman3up, pacman_x + 1, pacman_y + 1, this);
  283. break;
  284. case 3:
  285. g2d.drawImage(pacman4up, pacman_x + 1, pacman_y + 1, this);
  286. break;
  287. default:
  288. g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  289. break;
  290. }
  291. }
  292. private void drawPacmanDown(Graphics2D g2d) {
  293. switch (pacmanAnimPos) {
  294. case 1:
  295. g2d.drawImage(pacman2down, pacman_x + 1, pacman_y + 1, this);
  296. break;
  297. case 2:
  298. g2d.drawImage(pacman3down, pacman_x + 1, pacman_y + 1, this);
  299. break;
  300. case 3:
  301. g2d.drawImage(pacman4down, pacman_x + 1, pacman_y + 1, this);
  302. break;
  303. default:
  304. g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  305. break;
  306. }
  307. }
  308. private void drawPacnanLeft(Graphics2D g2d) {
  309. switch (pacmanAnimPos) {
  310. case 1:
  311. g2d.drawImage(pacman2left, pacman_x + 1, pacman_y + 1, this);
  312. break;
  313. case 2:
  314. g2d.drawImage(pacman3left, pacman_x + 1, pacman_y + 1, this);
  315. break;
  316. case 3:
  317. g2d.drawImage(pacman4left, pacman_x + 1, pacman_y + 1, this);
  318. break;
  319. default:
  320. g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  321. break;
  322. }
  323. }
  324. private void drawPacmanRight(Graphics2D g2d) {
  325. switch (pacmanAnimPos) {
  326. case 1:
  327. g2d.drawImage(pacman2right, pacman_x + 1, pacman_y + 1, this);
  328. break;
  329. case 2:
  330. g2d.drawImage(pacman3right, pacman_x + 1, pacman_y + 1, this);
  331. break;
  332. case 3:
  333. g2d.drawImage(pacman4right, pacman_x + 1, pacman_y + 1, this);
  334. break;
  335. default:
  336. g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  337. break;
  338. }
  339. }
  340. private void drawMaze(Graphics2D g2d) {
  341. short i = 0;
  342. int x, y;
  343. for (y = 0; y < SCREEN_SIZE; y += BLOCK_SIZE) {
  344. for (x = 0; x < SCREEN_SIZE; x += BLOCK_SIZE) {
  345. g2d.setColor(mazeColor);
  346. g2d.setStroke(new BasicStroke(2));
  347. if ((screenData[i] & 1) != 0) {
  348. g2d.drawLine(x, y, x, y + BLOCK_SIZE - 1);
  349. }
  350. if ((screenData[i] & 2) != 0) {
  351. g2d.drawLine(x, y, x + BLOCK_SIZE - 1, y);
  352. }
  353. if ((screenData[i] & 4) != 0) {
  354. g2d.drawLine(x + BLOCK_SIZE - 1, y, x + BLOCK_SIZE - 1,
  355. y + BLOCK_SIZE - 1);
  356. }
  357. if ((screenData[i] & 8) != 0) {
  358. g2d.drawLine(x, y + BLOCK_SIZE - 1, x + BLOCK_SIZE - 1,
  359. y + BLOCK_SIZE - 1);
  360. }
  361. if ((screenData[i] & 16) != 0) {
  362. g2d.setColor(dotColor);
  363. g2d.fillRect(x + 11, y + 11, 2, 2);
  364. }
  365. i++;
  366. }
  367. }
  368. }
  369. private void initGame() {
  370. pacsLeft = 3;
  371. score = 0;
  372. initLevel();
  373. N_GHOSTS = 6;
  374. currentSpeed = 3;
  375. }
  376. private void initLevel() {
  377. int i;
  378. for (i = 0; i < N_BLOCKS * N_BLOCKS; i++) {
  379. screenData[i] = levelData[i];
  380. }
  381. continueLevel();
  382. }
  383. private void continueLevel() {
  384. short i;
  385. int dx = 1;
  386. int random;
  387. for (i = 0; i < N_GHOSTS; i++) {
  388. ghost_y[i] = 4 * BLOCK_SIZE;
  389. ghost_x[i] = 4 * BLOCK_SIZE;
  390. ghost_dy[i] = 0;
  391. ghost_dx[i] = dx;
  392. dx = -dx;
  393. random = (int) (Math.random() * (currentSpeed + 1));
  394. if (random > currentSpeed) {
  395. random = currentSpeed;
  396. }
  397. ghostSpeed[i] = validSpeeds[random];
  398. }
  399. pacman_x = 7 * BLOCK_SIZE;
  400. pacman_y = 11 * BLOCK_SIZE;
  401. pacmand_x = 0;
  402. pacmand_y = 0;
  403. req_dx = 0;
  404. req_dy = 0;
  405. view_dx = -1;
  406. view_dy = 0;
  407. dying = false;
  408. }
  409. private void loadImages() {
  410. ghost = new ImageIcon("images/ghost.png").getImage();
  411. pacman1 = new ImageIcon("images/pacman.png").getImage();
  412. pacman2up = new ImageIcon("images/up1.png").getImage();
  413. pacman3up = new ImageIcon("images/up2.png").getImage();
  414. pacman4up = new ImageIcon("images/up3.png").getImage();
  415. pacman2down = new ImageIcon("images/down1.png").getImage();
  416. pacman3down = new ImageIcon("images/down2.png").getImage();
  417. pacman4down = new ImageIcon("images/down3.png").getImage();
  418. pacman2left = new ImageIcon("images/left1.png").getImage();
  419. pacman3left = new ImageIcon("images/left2.png").getImage();
  420. pacman4left = new ImageIcon("images/left3.png").getImage();
  421. pacman2right = new ImageIcon("images/right1.png").getImage();
  422. pacman3right = new ImageIcon("images/right2.png").getImage();
  423. pacman4right = new ImageIcon("images/right3.png").getImage();
  424. }
  425. @Override
  426. public void paintComponent(Graphics g) {
  427. super.paintComponent(g);
  428. doDrawing(g);
  429. }
  430. private void doDrawing(Graphics g) {
  431. Graphics2D g2d = (Graphics2D) g;
  432. g2d.setColor(Color.black);
  433. g2d.fillRect(0, 0, d.width, d.height);
  434. drawMaze(g2d);
  435. drawScore(g2d);
  436. doAnim();
  437. if (inGame) {
  438. playGame(g2d);
  439. } else {
  440. showIntroScreen(g2d);
  441. }
  442. g2d.drawImage(ii, 5, 5, this);
  443. Toolkit.getDefaultToolkit().sync();
  444. g2d.dispose();
  445. }
  446. class TAdapter extends KeyAdapter {
  447. @Override
  448. public void keyPressed(KeyEvent e) {
  449. int key = e.getKeyCode();
  450. if (inGame) {
  451. if (key == KeyEvent.VK_LEFT) {
  452. req_dx = -1;
  453. req_dy = 0;
  454. } else if (key == KeyEvent.VK_RIGHT) {
  455. req_dx = 1;
  456. req_dy = 0;
  457. } else if (key == KeyEvent.VK_UP) {
  458. req_dx = 0;
  459. req_dy = -1;
  460. } else if (key == KeyEvent.VK_DOWN) {
  461. req_dx = 0;
  462. req_dy = 1;
  463. } else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) {
  464. inGame = false;
  465. } else if (key == KeyEvent.VK_PAUSE) {
  466. if (timer.isRunning()) {
  467. timer.stop();
  468. } else {
  469. timer.start();
  470. }
  471. }
  472. } else {
  473. if (key == 's' || key == 'S') {
  474. inGame = true;
  475. initGame();
  476. }
  477. }
  478. }
  479. @Override
  480. public void keyReleased(KeyEvent e) {
  481. int key = e.getKeyCode();
  482. if (key == Event.LEFT || key == Event.RIGHT
  483. || key == Event.UP || key == Event.DOWN) {
  484. req_dx = 0;
  485. req_dy = 0;
  486. }
  487. }
  488. }
  489. @Override
  490. public void actionPerformed(ActionEvent e) {
  491. repaint();
  492. }
  493. }

用光标键控制吃豆人。 Esc 键完成游戏,暂停键暂停游戏。

private int pacman_x, pacman_y, pacmand_x, pacmand_y;

前两个变量存储吃豆子精灵的 x 和 y 坐标。 最后两个变量是水平和垂直方向的增量变化。

private final short levelData[] = {
    19, 26, 26, 26, 18, 18, 18, 18, ...
};

这些数字组成了迷宫。 它们提供了信息,我们可以据此创建角点。 1 号是左上角。 数字 2、4 和 8 分别代表顶角,右角和底角。 16 号是重点。 可以添加这些数字,例如左上角的数字 19 表示正方形将具有顶部和左侧边框以及一个点(16 + 2 + 1)。

private void doAnim() {

    pacAnimCount--;

    if (pacAnimCount <= 0) {
        pacAnimCount = PAC_ANIM_DELAY;
        pacmanAnimPos = pacmanAnimPos + pacAnimDir;

        if (pacmanAnimPos == (PACMAN_ANIM_COUNT - 1) || pacmanAnimPos == 0) {
            pacAnimDir = -pacAnimDir;
        }
    }
}

doAnim()pacmanAnimPos变量进行计数,该变量确定绘制哪种吃豆子图像。 有四个吃豆人图像。 还有一个PAC_ANIM_DELAY常数,它会使动画变慢。 否则,吃豆人会太快张开嘴。

boolean finished = true;

while (i < N_BLOCKS * N_BLOCKS && finished) {

    if ((screenData[i] & 48) != 0) {
        finished = false;
    }

    i++;
}

此代码是checkMaze()方法的一部分。 它检查吃豆人是否还有剩余的食物要吃。 数字 16 代表一个点。 如果所有积分都被消耗掉了,我们将进入下一个层次。 (在我们的例子中,我们只是重新启动游戏。)

接下来,我们将研究moveGhosts()方法。 鬼魂移动一个正方形,然后决定是否改变方向。

if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) {

我们仅在完成移动一个正方形后才继续。

pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE);

这条线确定了幻影的位置; 在哪个位置/正方形。 有 225 个理论职位。 (鬼不能在墙上移动。)

if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) {
    dx[count] = -1;
    dy[count] = 0;
    count++;
}

如果左侧没有障碍物并且幻影尚未向右移动,则幻影将向左移动。 该代码的真正含义是什么? 如果幽灵进入隧道,他将朝着同一方向继续前进,直到他离开隧道。 鬼影的移动部分是随机的。 我们不会在长隧道中应用这种随机性,因为幽灵可能会卡在那里。

if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12)
        && pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12)
        && inGame) {

    dying = true;
}

如果幽灵和吃豆人之间发生碰撞,吃豆人会死。

接下来,我们将研究movePacman()方法。 req_dxreq_dy变量在TAdapter内部类中确定。 这些变量由光标键控制。

if ((ch & 16) != 0) {
    screenData[pos] = (short) (ch & 15);
    score++;
}

如果吃豆人移动到带点的位置,我们将其从迷宫中移出并增加得分值。

if ((pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0)
        || (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0)
        || (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0)
        || (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) {
    pacmand_x = 0;
    pacmand_y = 0;
}

如果吃豆子无法按当前方向继续前进,则会停下来。

private void drawPacman(Graphics2D g2d) {

    if (view_dx == -1) {
        drawPacnanLeft(g2d);
    } else if (view_dx == 1) {
        drawPacmanRight(g2d);
    } else if (view_dy == -1) {
        drawPacmanUp(g2d);
    } else {
        drawPacmanDown(g2d);
    }
}

吃豆人有四个可能的方向。 所有方向都有四个图像。 这些图像用于为吃豆人张嘴和闭嘴动画。

drawMaze()方法从screenData数组中的数字中提取迷宫。 数字 1 是左侧边框,2 是顶部边框,4 是右侧边框,8 是底部边框,16 是点。 我们只需在迷宫中浏览所有 225 平方。 例如,在screenData数组中有 9 个。 我们设置了第一位(1)和第四位(8)。 因此,我们在此特定正方形上绘制了底部和左侧边框。

if ((screenData[i] & 1) != 0) { 
    g2d.drawLine(x, y, x, y + BLOCK_SIZE - 1);
}

如果设置了数字的第一位,我们将绘制左边框。

Pacman.java

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JFrame;

public class Pacman extends JFrame {

    public Pacman() {

        initUI();
    }

    private void initUI() {

        add(new Board());

        setTitle("Pacman");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(380, 420);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            Pacman ex = new Pacman();
            ex.setVisible(true);
        });
    }
}

这是带有main方法的 Pacman 文件。

Java 吃豆人 - 图1

图:吃豆人

这是吃豆子游戏。