Java Pacman
最后修改于 2023 年 1 月 10 日
在本部分 Java 2D 游戏教程中,我们将创建一个简单的 Pacman 游戏克隆。源代码和图片可以在作者的 Github Java-Pacman-Game 存储库中找到。
Pacman 是一款街机游戏,最初由日本公司 Namco 于 1980 年开发。Pacman 成为了有史以来最受欢迎的街机游戏之一。
开发
以下代码示例是 Brian Postma 的 Pacman 游戏的翻版,可在 http://www.brianpostma.com 上找到。代码经过修改和简化,以便更容易理解。
游戏的目标是吃掉迷宫中的所有点并躲避幽灵。Pacman 以两种方式进行动画:它在迷宫中的位置和它的身体。我们用四张图片来制作它的身体动画,这取决于方向。动画用于制造 Pacman 张开和闭合嘴巴的错觉。迷宫由 15x15 个方格组成。迷宫的结构基于一个简单的整数数组。Pacman 有三条生命。我们也计算得分。
游戏包含两个文件:Board.java
和 Pacman.java
。
package com.zetcode; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Event; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.Timer; public class Board extends JPanel implements ActionListener { private Dimension d; private final Font smallFont = new Font("Helvetica", Font.BOLD, 14); private Image ii; private final Color dotColor = new Color(192, 192, 0); private Color mazeColor; private boolean inGame = false; private boolean dying = false; private final int BLOCK_SIZE = 24; private final int N_BLOCKS = 15; private final int SCREEN_SIZE = N_BLOCKS * BLOCK_SIZE; private final int PAC_ANIM_DELAY = 2; private final int PACMAN_ANIM_COUNT = 4; private final int MAX_GHOSTS = 12; private final int PACMAN_SPEED = 6; private int pacAnimCount = PAC_ANIM_DELAY; private int pacAnimDir = 1; private int pacmanAnimPos = 0; private int N_GHOSTS = 6; private int pacsLeft, score; private int[] dx, dy; private int[] ghost_x, ghost_y, ghost_dx, ghost_dy, ghostSpeed; private Image ghost; private Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down; private Image pacman3up, pacman3down, pacman3left, pacman3right; private Image pacman4up, pacman4down, pacman4left, pacman4right; private int pacman_x, pacman_y, pacmand_x, pacmand_y; private int req_dx, req_dy, view_dx, view_dy; private final short levelData[] = { 19, 26, 26, 26, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22, 21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 21, 0, 0, 0, 17, 16, 16, 24, 16, 16, 16, 16, 16, 16, 20, 17, 18, 18, 18, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 20, 17, 16, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 16, 24, 20, 25, 16, 16, 16, 24, 24, 28, 0, 25, 24, 24, 16, 20, 0, 21, 1, 17, 16, 20, 0, 0, 0, 0, 0, 0, 0, 17, 20, 0, 21, 1, 17, 16, 16, 18, 18, 22, 0, 19, 18, 18, 16, 20, 0, 21, 1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21, 1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21, 1, 17, 16, 16, 16, 16, 16, 18, 16, 16, 16, 16, 20, 0, 21, 1, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 21, 1, 25, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 18, 20, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 25, 24, 24, 24, 28 }; private final int validSpeeds[] = {1, 2, 3, 4, 6, 8}; private final int maxSpeed = 6; private int currentSpeed = 3; private short[] screenData; private Timer timer; public Board() { loadImages(); initVariables(); initBoard(); } private void initBoard() { addKeyListener(new TAdapter()); setFocusable(true); setBackground(Color.black); } private void initVariables() { screenData = new short[N_BLOCKS * N_BLOCKS]; mazeColor = new Color(5, 100, 5); d = new Dimension(400, 400); ghost_x = new int[MAX_GHOSTS]; ghost_dx = new int[MAX_GHOSTS]; ghost_y = new int[MAX_GHOSTS]; ghost_dy = new int[MAX_GHOSTS]; ghostSpeed = new int[MAX_GHOSTS]; dx = new int[4]; dy = new int[4]; timer = new Timer(40, this); timer.start(); } @Override public void addNotify() { super.addNotify(); initGame(); } private void doAnim() { pacAnimCount--; if (pacAnimCount <= 0) { pacAnimCount = PAC_ANIM_DELAY; pacmanAnimPos = pacmanAnimPos + pacAnimDir; if (pacmanAnimPos == (PACMAN_ANIM_COUNT - 1) || pacmanAnimPos == 0) { pacAnimDir = -pacAnimDir; } } } private void playGame(Graphics2D g2d) { if (dying) { death(); } else { movePacman(); drawPacman(g2d); moveGhosts(g2d); checkMaze(); } } private void showIntroScreen(Graphics2D g2d) { g2d.setColor(new Color(0, 32, 48)); g2d.fillRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50); g2d.setColor(Color.white); g2d.drawRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50); String s = "Press s to start."; Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = this.getFontMetrics(small); g2d.setColor(Color.white); g2d.setFont(small); g2d.drawString(s, (SCREEN_SIZE - metr.stringWidth(s)) / 2, SCREEN_SIZE / 2); } private void drawScore(Graphics2D g) { int i; String s; g.setFont(smallFont); g.setColor(new Color(96, 128, 255)); s = "Score: " + score; g.drawString(s, SCREEN_SIZE / 2 + 96, SCREEN_SIZE + 16); for (i = 0; i < pacsLeft; i++) { g.drawImage(pacman3left, i * 28 + 8, SCREEN_SIZE + 1, this); } } private void checkMaze() { short i = 0; boolean finished = true; while (i < N_BLOCKS * N_BLOCKS && finished) { if ((screenData[i] & 48) != 0) { finished = false; } i++; } if (finished) { score += 50; if (N_GHOSTS < MAX_GHOSTS) { N_GHOSTS++; } if (currentSpeed < maxSpeed) { currentSpeed++; } initLevel(); } } private void death() { pacsLeft--; if (pacsLeft == 0) { inGame = false; } continueLevel(); } private void moveGhosts(Graphics2D g2d) { short i; int pos; int count; for (i = 0; i < N_GHOSTS; i++) { if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) { pos = ghost_x[i] / BLOCK_SIZE + N_BLOCKS * (int) (ghost_y[i] / BLOCK_SIZE); count = 0; if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) { dx[count] = -1; dy[count] = 0; count++; } if ((screenData[pos] & 2) == 0 && ghost_dy[i] != 1) { dx[count] = 0; dy[count] = -1; count++; } if ((screenData[pos] & 4) == 0 && ghost_dx[i] != -1) { dx[count] = 1; dy[count] = 0; count++; } if ((screenData[pos] & 8) == 0 && ghost_dy[i] != -1) { dx[count] = 0; dy[count] = 1; count++; } if (count == 0) { if ((screenData[pos] & 15) == 15) { ghost_dx[i] = 0; ghost_dy[i] = 0; } else { ghost_dx[i] = -ghost_dx[i]; ghost_dy[i] = -ghost_dy[i]; } } else { count = (int) (Math.random() * count); if (count > 3) { count = 3; } ghost_dx[i] = dx[count]; ghost_dy[i] = dy[count]; } } ghost_x[i] = ghost_x[i] + (ghost_dx[i] * ghostSpeed[i]); ghost_y[i] = ghost_y[i] + (ghost_dy[i] * ghostSpeed[i]); drawGhost(g2d, ghost_x[i] + 1, ghost_y[i] + 1); 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; } } } private void drawGhost(Graphics2D g2d, int x, int y) { g2d.drawImage(ghost, x, y, this); } private void movePacman() { int pos; short ch; if (req_dx == -pacmand_x && req_dy == -pacmand_y) { pacmand_x = req_dx; pacmand_y = req_dy; view_dx = pacmand_x; view_dy = pacmand_y; } if (pacman_x % BLOCK_SIZE == 0 && pacman_y % BLOCK_SIZE == 0) { pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE); ch = screenData[pos]; if ((ch & 16) != 0) { screenData[pos] = (short) (ch & 15); score++; } if (req_dx != 0 || req_dy != 0) { if (!((req_dx == -1 && req_dy == 0 && (ch & 1) != 0) || (req_dx == 1 && req_dy == 0 && (ch & 4) != 0) || (req_dx == 0 && req_dy == -1 && (ch & 2) != 0) || (req_dx == 0 && req_dy == 1 && (ch & 8) != 0))) { pacmand_x = req_dx; pacmand_y = req_dy; view_dx = pacmand_x; view_dy = pacmand_y; } } // Check for standstill 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; } } pacman_x = pacman_x + PACMAN_SPEED * pacmand_x; pacman_y = pacman_y + PACMAN_SPEED * pacmand_y; } 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); } } private void drawPacmanUp(Graphics2D g2d) { switch (pacmanAnimPos) { case 1: g2d.drawImage(pacman2up, pacman_x + 1, pacman_y + 1, this); break; case 2: g2d.drawImage(pacman3up, pacman_x + 1, pacman_y + 1, this); break; case 3: g2d.drawImage(pacman4up, pacman_x + 1, pacman_y + 1, this); break; default: g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); break; } } private void drawPacmanDown(Graphics2D g2d) { switch (pacmanAnimPos) { case 1: g2d.drawImage(pacman2down, pacman_x + 1, pacman_y + 1, this); break; case 2: g2d.drawImage(pacman3down, pacman_x + 1, pacman_y + 1, this); break; case 3: g2d.drawImage(pacman4down, pacman_x + 1, pacman_y + 1, this); break; default: g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); break; } } private void drawPacnanLeft(Graphics2D g2d) { switch (pacmanAnimPos) { case 1: g2d.drawImage(pacman2left, pacman_x + 1, pacman_y + 1, this); break; case 2: g2d.drawImage(pacman3left, pacman_x + 1, pacman_y + 1, this); break; case 3: g2d.drawImage(pacman4left, pacman_x + 1, pacman_y + 1, this); break; default: g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); break; } } private void drawPacmanRight(Graphics2D g2d) { switch (pacmanAnimPos) { case 1: g2d.drawImage(pacman2right, pacman_x + 1, pacman_y + 1, this); break; case 2: g2d.drawImage(pacman3right, pacman_x + 1, pacman_y + 1, this); break; case 3: g2d.drawImage(pacman4right, pacman_x + 1, pacman_y + 1, this); break; default: g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this); break; } } private void drawMaze(Graphics2D g2d) { short i = 0; int x, y; for (y = 0; y < SCREEN_SIZE; y += BLOCK_SIZE) { for (x = 0; x < SCREEN_SIZE; x += BLOCK_SIZE) { g2d.setColor(mazeColor); g2d.setStroke(new BasicStroke(2)); if ((screenData[i] & 1) != 0) { g2d.drawLine(x, y, x, y + BLOCK_SIZE - 1); } if ((screenData[i] & 2) != 0) { g2d.drawLine(x, y, x + BLOCK_SIZE - 1, y); } if ((screenData[i] & 4) != 0) { g2d.drawLine(x + BLOCK_SIZE - 1, y, x + BLOCK_SIZE - 1, y + BLOCK_SIZE - 1); } if ((screenData[i] & 8) != 0) { g2d.drawLine(x, y + BLOCK_SIZE - 1, x + BLOCK_SIZE - 1, y + BLOCK_SIZE - 1); } if ((screenData[i] & 16) != 0) { g2d.setColor(dotColor); g2d.fillRect(x + 11, y + 11, 2, 2); } i++; } } } private void initGame() { pacsLeft = 3; score = 0; initLevel(); N_GHOSTS = 6; currentSpeed = 3; } private void initLevel() { int i; for (i = 0; i < N_BLOCKS * N_BLOCKS; i++) { screenData[i] = levelData[i]; } continueLevel(); } private void continueLevel() { short i; int dx = 1; int random; for (i = 0; i < N_GHOSTS; i++) { ghost_y[i] = 4 * BLOCK_SIZE; ghost_x[i] = 4 * BLOCK_SIZE; ghost_dy[i] = 0; ghost_dx[i] = dx; dx = -dx; random = (int) (Math.random() * (currentSpeed + 1)); if (random > currentSpeed) { random = currentSpeed; } ghostSpeed[i] = validSpeeds[random]; } pacman_x = 7 * BLOCK_SIZE; pacman_y = 11 * BLOCK_SIZE; pacmand_x = 0; pacmand_y = 0; req_dx = 0; req_dy = 0; view_dx = -1; view_dy = 0; dying = false; } private void loadImages() { ghost = new ImageIcon("images/ghost.png").getImage(); pacman1 = new ImageIcon("images/pacman.png").getImage(); pacman2up = new ImageIcon("images/up1.png").getImage(); pacman3up = new ImageIcon("images/up2.png").getImage(); pacman4up = new ImageIcon("images/up3.png").getImage(); pacman2down = new ImageIcon("images/down1.png").getImage(); pacman3down = new ImageIcon("images/down2.png").getImage(); pacman4down = new ImageIcon("images/down3.png").getImage(); pacman2left = new ImageIcon("images/left1.png").getImage(); pacman3left = new ImageIcon("images/left2.png").getImage(); pacman4left = new ImageIcon("images/left3.png").getImage(); pacman2right = new ImageIcon("images/right1.png").getImage(); pacman3right = new ImageIcon("images/right2.png").getImage(); pacman4right = new ImageIcon("images/right3.png").getImage(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); doDrawing(g); } private void doDrawing(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.black); g2d.fillRect(0, 0, d.width, d.height); drawMaze(g2d); drawScore(g2d); doAnim(); if (inGame) { playGame(g2d); } else { showIntroScreen(g2d); } g2d.drawImage(ii, 5, 5, this); Toolkit.getDefaultToolkit().sync(); g2d.dispose(); } class TAdapter extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (inGame) { if (key == KeyEvent.VK_LEFT) { req_dx = -1; req_dy = 0; } else if (key == KeyEvent.VK_RIGHT) { req_dx = 1; req_dy = 0; } else if (key == KeyEvent.VK_UP) { req_dx = 0; req_dy = -1; } else if (key == KeyEvent.VK_DOWN) { req_dx = 0; req_dy = 1; } else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) { inGame = false; } else if (key == KeyEvent.VK_PAUSE) { if (timer.isRunning()) { timer.stop(); } else { timer.start(); } } } else { if (key == 's' || key == 'S') { inGame = true; initGame(); } } } @Override public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == Event.LEFT || key == Event.RIGHT || key == Event.UP || key == Event.DOWN) { req_dx = 0; req_dy = 0; } } } @Override public void actionPerformed(ActionEvent e) { repaint(); } }
Pacman 使用光标键控制。Esc 键结束游戏,Pause 键暂停游戏。
private int pacman_x, pacman_y, pacmand_x, pacmand_y;
前两个变量存储 Pacman 精灵的 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
变量,该变量决定绘制哪个 Pacman 图像。有四张 Pacman 图像。还有一个 PAC_ANIM_DELAY
常量,它使动画稍微慢一些。否则 Pacman 会太快地张开嘴。
boolean finished = true; while (i < N_BLOCKS * N_BLOCKS && finished) { if ((screenData[i] & 48) != 0) { finished = false; } i++; }
这段代码是 checkMaze()
方法的一部分。它检查 Pacman 是否还有剩余的点可以吃。数字 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; }
如果幽灵与 Pacman 发生碰撞,Pacman 就会死亡。
接下来我们将检查 movePacman()
方法。req_dx
和 req_dy
变量在 TAdapter
内部类中确定。这些变量由光标键控制。
if ((ch & 16) != 0) { screenData[pos] = (short) (ch & 15); score++; }
如果 Pacman 移动到有点的位置,我们将其从迷宫中移除并增加分数。
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; }
如果 Pacman 无法沿当前方向继续移动,它就会停止。
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); } }
Pacman 有四个可能的方向。所有方向都有四张图像。这些图像用于制作 Pacman 张开和闭合嘴巴的动画。
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); }
如果数字的第一个位被设置,我们就绘制左边框。
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(() -> { var ex = new Pacman(); ex.setVisible(true); }); } }
这是一个带有 main 方法的 Pacman 文件。

这就是 Pacman 游戏。