Advertisement
LegendSujay2019

Making Pacman game in java

Nov 8th, 2020
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.62 KB | None | 0 0
  1. //Java Pacman//
  2. //The game consists of two files: Board.java and Pacman.java.
  3.  
  4. com/zetcode/Board.java//
  5. package com.zetcode;
  6.  
  7. import java.awt.BasicStroke;
  8. import java.awt.Color;
  9. import java.awt.Dimension;
  10. import java.awt.Event;
  11. import java.awt.Font;
  12. import java.awt.FontMetrics;
  13. import java.awt.Graphics;
  14. import java.awt.Graphics2D;
  15. import java.awt.Image;
  16. import java.awt.Toolkit;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.KeyAdapter;
  20. import java.awt.event.KeyEvent;
  21.  
  22. import javax.swing.ImageIcon;
  23. import javax.swing.JPanel;
  24. import javax.swing.Timer;
  25.  
  26. public class Board extends JPanel implements ActionListener {
  27.  
  28.     private Dimension d;
  29.     private final Font smallFont = new Font("Helvetica", Font.BOLD, 14);
  30.  
  31.     private Image ii;
  32.     private final Color dotColor = new Color(192, 192, 0);
  33.     private Color mazeColor;
  34.  
  35.     private boolean inGame = false;
  36.     private boolean dying = false;
  37.  
  38.     private final int BLOCK_SIZE = 24;
  39.     private final int N_BLOCKS = 15;
  40.     private final int SCREEN_SIZE = N_BLOCKS * BLOCK_SIZE;
  41.     private final int PAC_ANIM_DELAY = 2;
  42.     private final int PACMAN_ANIM_COUNT = 4;
  43.     private final int MAX_GHOSTS = 12;
  44.     private final int PACMAN_SPEED = 6;
  45.  
  46.     private int pacAnimCount = PAC_ANIM_DELAY;
  47.     private int pacAnimDir = 1;
  48.     private int pacmanAnimPos = 0;
  49.     private int N_GHOSTS = 6;
  50.     private int pacsLeft, score;
  51.     private int[] dx, dy;
  52.     private int[] ghost_x, ghost_y, ghost_dx, ghost_dy, ghostSpeed;
  53.  
  54.     private Image ghost;
  55.     private Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down;
  56.     private Image pacman3up, pacman3down, pacman3left, pacman3right;
  57.     private Image pacman4up, pacman4down, pacman4left, pacman4right;
  58.  
  59.     private int pacman_x, pacman_y, pacmand_x, pacmand_y;
  60.     private int req_dx, req_dy, view_dx, view_dy;
  61.  
  62.     private final short levelData[] = {
  63.         19, 26, 26, 26, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22,
  64.         21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  65.         21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
  66.         21, 0, 0, 0, 17, 16, 16, 24, 16, 16, 16, 16, 16, 16, 20,
  67.         17, 18, 18, 18, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 20,
  68.         17, 16, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 16, 24, 20,
  69.         25, 16, 16, 16, 24, 24, 28, 0, 25, 24, 24, 16, 20, 0, 21,
  70.         1, 17, 16, 20, 0, 0, 0, 0, 0, 0, 0, 17, 20, 0, 21,
  71.         1, 17, 16, 16, 18, 18, 22, 0, 19, 18, 18, 16, 20, 0, 21,
  72.         1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21,
  73.         1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21,
  74.         1, 17, 16, 16, 16, 16, 16, 18, 16, 16, 16, 16, 20, 0, 21,
  75.         1, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 21,
  76.         1, 25, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 18, 20,
  77.         9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 25, 24, 24, 24, 28
  78.     };
  79.  
  80.     private final int validSpeeds[] = {1, 2, 3, 4, 6, 8};
  81.     private final int maxSpeed = 6;
  82.  
  83.     private int currentSpeed = 3;
  84.     private short[] screenData;
  85.     private Timer timer;
  86.  
  87.     public Board() {
  88.  
  89.         loadImages();
  90.         initVariables();
  91.         initBoard();
  92.     }
  93.    
  94.     private void initBoard() {
  95.        
  96.         addKeyListener(new TAdapter());
  97.  
  98.         setFocusable(true);
  99.  
  100.         setBackground(Color.black);
  101.     }
  102.  
  103.     private void initVariables() {
  104.  
  105.         screenData = new short[N_BLOCKS * N_BLOCKS];
  106.         mazeColor = new Color(5, 100, 5);
  107.         d = new Dimension(400, 400);
  108.         ghost_x = new int[MAX_GHOSTS];
  109.         ghost_dx = new int[MAX_GHOSTS];
  110.         ghost_y = new int[MAX_GHOSTS];
  111.         ghost_dy = new int[MAX_GHOSTS];
  112.         ghostSpeed = new int[MAX_GHOSTS];
  113.         dx = new int[4];
  114.         dy = new int[4];
  115.        
  116.         timer = new Timer(40, this);
  117.         timer.start();
  118.     }
  119.  
  120.     @Override
  121.     public void addNotify() {
  122.         super.addNotify();
  123.  
  124.         initGame();
  125.     }
  126.  
  127.     private void doAnim() {
  128.  
  129.         pacAnimCount--;
  130.  
  131.         if (pacAnimCount <= 0) {
  132.             pacAnimCount = PAC_ANIM_DELAY;
  133.             pacmanAnimPos = pacmanAnimPos + pacAnimDir;
  134.  
  135.             if (pacmanAnimPos == (PACMAN_ANIM_COUNT - 1) || pacmanAnimPos == 0) {
  136.                 pacAnimDir = -pacAnimDir;
  137.             }
  138.         }
  139.     }
  140.  
  141.     private void playGame(Graphics2D g2d) {
  142.  
  143.         if (dying) {
  144.  
  145.             death();
  146.  
  147.         } else {
  148.  
  149.             movePacman();
  150.             drawPacman(g2d);
  151.             moveGhosts(g2d);
  152.             checkMaze();
  153.         }
  154.     }
  155.  
  156.     private void showIntroScreen(Graphics2D g2d) {
  157.  
  158.         g2d.setColor(new Color(0, 32, 48));
  159.         g2d.fillRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50);
  160.         g2d.setColor(Color.white);
  161.         g2d.drawRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50);
  162.  
  163.         String s = "Press s to start.";
  164.         Font small = new Font("Helvetica", Font.BOLD, 14);
  165.         FontMetrics metr = this.getFontMetrics(small);
  166.  
  167.         g2d.setColor(Color.white);
  168.         g2d.setFont(small);
  169.         g2d.drawString(s, (SCREEN_SIZE - metr.stringWidth(s)) / 2, SCREEN_SIZE / 2);
  170.     }
  171.  
  172.     private void drawScore(Graphics2D g) {
  173.  
  174.         int i;
  175.         String s;
  176.  
  177.         g.setFont(smallFont);
  178.         g.setColor(new Color(96, 128, 255));
  179.         s = "Score: " + score;
  180.         g.drawString(s, SCREEN_SIZE / 2 + 96, SCREEN_SIZE + 16);
  181.  
  182.         for (i = 0; i < pacsLeft; i++) {
  183.             g.drawImage(pacman3left, i * 28 + 8, SCREEN_SIZE + 1, this);
  184.         }
  185.     }
  186.  
  187.     private void checkMaze() {
  188.  
  189.         short i = 0;
  190.         boolean finished = true;
  191.  
  192.         while (i < N_BLOCKS * N_BLOCKS && finished) {
  193.  
  194.             if ((screenData[i] & 48) != 0) {
  195.                 finished = false;
  196.             }
  197.  
  198.             i++;
  199.         }
  200.  
  201.         if (finished) {
  202.  
  203.             score += 50;
  204.  
  205.             if (N_GHOSTS < MAX_GHOSTS) {
  206.                 N_GHOSTS++;
  207.             }
  208.  
  209.             if (currentSpeed < maxSpeed) {
  210.                 currentSpeed++;
  211.             }
  212.  
  213.             initLevel();
  214.         }
  215.     }
  216.  
  217.     private void death() {
  218.  
  219.         pacsLeft--;
  220.  
  221.         if (pacsLeft == 0) {
  222.             inGame = false;
  223.         }
  224.  
  225.         continueLevel();
  226.     }
  227.  
  228.     private void moveGhosts(Graphics2D g2d) {
  229.  
  230.         short i;
  231.         int pos;
  232.         int count;
  233.  
  234.         for (i = 0; i < N_GHOSTS; i++) {
  235.             if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) {
  236.                 pos = ghost_x[i] / BLOCK_SIZE + N_BLOCKS * (int) (ghost_y[i] / BLOCK_SIZE);
  237.  
  238.                 count = 0;
  239.  
  240.                 if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) {
  241.                     dx[count] = -1;
  242.                     dy[count] = 0;
  243.                     count++;
  244.                 }
  245.  
  246.                 if ((screenData[pos] & 2) == 0 && ghost_dy[i] != 1) {
  247.                     dx[count] = 0;
  248.                     dy[count] = -1;
  249.                     count++;
  250.                 }
  251.  
  252.                 if ((screenData[pos] & 4) == 0 && ghost_dx[i] != -1) {
  253.                     dx[count] = 1;
  254.                     dy[count] = 0;
  255.                     count++;
  256.                 }
  257.  
  258.                 if ((screenData[pos] & 8) == 0 && ghost_dy[i] != -1) {
  259.                     dx[count] = 0;
  260.                     dy[count] = 1;
  261.                     count++;
  262.                 }
  263.  
  264.                 if (count == 0) {
  265.  
  266.                     if ((screenData[pos] & 15) == 15) {
  267.                         ghost_dx[i] = 0;
  268.                         ghost_dy[i] = 0;
  269.                     } else {
  270.                         ghost_dx[i] = -ghost_dx[i];
  271.                         ghost_dy[i] = -ghost_dy[i];
  272.                     }
  273.  
  274.                 } else {
  275.  
  276.                     count = (int) (Math.random() * count);
  277.  
  278.                     if (count > 3) {
  279.                         count = 3;
  280.                     }
  281.  
  282.                     ghost_dx[i] = dx[count];
  283.                     ghost_dy[i] = dy[count];
  284.                 }
  285.  
  286.             }
  287.  
  288.             ghost_x[i] = ghost_x[i] + (ghost_dx[i] * ghostSpeed[i]);
  289.             ghost_y[i] = ghost_y[i] + (ghost_dy[i] * ghostSpeed[i]);
  290.             drawGhost(g2d, ghost_x[i] + 1, ghost_y[i] + 1);
  291.  
  292.             if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12)
  293.                     && pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12)
  294.                     && inGame) {
  295.  
  296.                 dying = true;
  297.             }
  298.         }
  299.     }
  300.  
  301.     private void drawGhost(Graphics2D g2d, int x, int y) {
  302.  
  303.         g2d.drawImage(ghost, x, y, this);
  304.     }
  305.  
  306.     private void movePacman() {
  307.  
  308.         int pos;
  309.         short ch;
  310.  
  311.         if (req_dx == -pacmand_x && req_dy == -pacmand_y) {
  312.             pacmand_x = req_dx;
  313.             pacmand_y = req_dy;
  314.             view_dx = pacmand_x;
  315.             view_dy = pacmand_y;
  316.         }
  317.  
  318.         if (pacman_x % BLOCK_SIZE == 0 && pacman_y % BLOCK_SIZE == 0) {
  319.             pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE);
  320.             ch = screenData[pos];
  321.  
  322.             if ((ch & 16) != 0) {
  323.                 screenData[pos] = (short) (ch & 15);
  324.                 score++;
  325.             }
  326.  
  327.             if (req_dx != 0 || req_dy != 0) {
  328.                 if (!((req_dx == -1 && req_dy == 0 && (ch & 1) != 0)
  329.                         || (req_dx == 1 && req_dy == 0 && (ch & 4) != 0)
  330.                         || (req_dx == 0 && req_dy == -1 && (ch & 2) != 0)
  331.                         || (req_dx == 0 && req_dy == 1 && (ch & 8) != 0))) {
  332.                     pacmand_x = req_dx;
  333.                     pacmand_y = req_dy;
  334.                     view_dx = pacmand_x;
  335.                     view_dy = pacmand_y;
  336.                 }
  337.             }
  338.  
  339.             // Check for standstill
  340.             if ((pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0)
  341.                     || (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0)
  342.                     || (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0)
  343.                     || (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) {
  344.                 pacmand_x = 0;
  345.                 pacmand_y = 0;
  346.             }
  347.         }
  348.         pacman_x = pacman_x + PACMAN_SPEED * pacmand_x;
  349.         pacman_y = pacman_y + PACMAN_SPEED * pacmand_y;
  350.     }
  351.  
  352.     private void drawPacman(Graphics2D g2d) {
  353.  
  354.         if (view_dx == -1) {
  355.             drawPacnanLeft(g2d);
  356.         } else if (view_dx == 1) {
  357.             drawPacmanRight(g2d);
  358.         } else if (view_dy == -1) {
  359.             drawPacmanUp(g2d);
  360.         } else {
  361.             drawPacmanDown(g2d);
  362.         }
  363.     }
  364.  
  365.     private void drawPacmanUp(Graphics2D g2d) {
  366.  
  367.         switch (pacmanAnimPos) {
  368.             case 1:
  369.                 g2d.drawImage(pacman2up, pacman_x + 1, pacman_y + 1, this);
  370.                 break;
  371.             case 2:
  372.                 g2d.drawImage(pacman3up, pacman_x + 1, pacman_y + 1, this);
  373.                 break;
  374.             case 3:
  375.                 g2d.drawImage(pacman4up, pacman_x + 1, pacman_y + 1, this);
  376.                 break;
  377.             default:
  378.                 g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  379.                 break;
  380.         }
  381.     }
  382.  
  383.     private void drawPacmanDown(Graphics2D g2d) {
  384.  
  385.         switch (pacmanAnimPos) {
  386.             case 1:
  387.                 g2d.drawImage(pacman2down, pacman_x + 1, pacman_y + 1, this);
  388.                 break;
  389.             case 2:
  390.                 g2d.drawImage(pacman3down, pacman_x + 1, pacman_y + 1, this);
  391.                 break;
  392.             case 3:
  393.                 g2d.drawImage(pacman4down, pacman_x + 1, pacman_y + 1, this);
  394.                 break;
  395.             default:
  396.                 g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  397.                 break;
  398.         }
  399.     }
  400.  
  401.     private void drawPacnanLeft(Graphics2D g2d) {
  402.  
  403.         switch (pacmanAnimPos) {
  404.             case 1:
  405.                 g2d.drawImage(pacman2left, pacman_x + 1, pacman_y + 1, this);
  406.                 break;
  407.             case 2:
  408.                 g2d.drawImage(pacman3left, pacman_x + 1, pacman_y + 1, this);
  409.                 break;
  410.             case 3:
  411.                 g2d.drawImage(pacman4left, pacman_x + 1, pacman_y + 1, this);
  412.                 break;
  413.             default:
  414.                 g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  415.                 break;
  416.         }
  417.     }
  418.  
  419.     private void drawPacmanRight(Graphics2D g2d) {
  420.  
  421.         switch (pacmanAnimPos) {
  422.             case 1:
  423.                 g2d.drawImage(pacman2right, pacman_x + 1, pacman_y + 1, this);
  424.                 break;
  425.             case 2:
  426.                 g2d.drawImage(pacman3right, pacman_x + 1, pacman_y + 1, this);
  427.                 break;
  428.             case 3:
  429.                 g2d.drawImage(pacman4right, pacman_x + 1, pacman_y + 1, this);
  430.                 break;
  431.             default:
  432.                 g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
  433.                 break;
  434.         }
  435.     }
  436.  
  437.     private void drawMaze(Graphics2D g2d) {
  438.  
  439.         short i = 0;
  440.         int x, y;
  441.  
  442.         for (y = 0; y < SCREEN_SIZE; y += BLOCK_SIZE) {
  443.             for (x = 0; x < SCREEN_SIZE; x += BLOCK_SIZE) {
  444.  
  445.                 g2d.setColor(mazeColor);
  446.                 g2d.setStroke(new BasicStroke(2));
  447.  
  448.                 if ((screenData[i] & 1) != 0) {
  449.                     g2d.drawLine(x, y, x, y + BLOCK_SIZE - 1);
  450.                 }
  451.  
  452.                 if ((screenData[i] & 2) != 0) {
  453.                     g2d.drawLine(x, y, x + BLOCK_SIZE - 1, y);
  454.                 }
  455.  
  456.                 if ((screenData[i] & 4) != 0) {
  457.                     g2d.drawLine(x + BLOCK_SIZE - 1, y, x + BLOCK_SIZE - 1,
  458.                             y + BLOCK_SIZE - 1);
  459.                 }
  460.  
  461.                 if ((screenData[i] & 8) != 0) {
  462.                     g2d.drawLine(x, y + BLOCK_SIZE - 1, x + BLOCK_SIZE - 1,
  463.                             y + BLOCK_SIZE - 1);
  464.                 }
  465.  
  466.                 if ((screenData[i] & 16) != 0) {
  467.                     g2d.setColor(dotColor);
  468.                     g2d.fillRect(x + 11, y + 11, 2, 2);
  469.                 }
  470.  
  471.                 i++;
  472.             }
  473.         }
  474.     }
  475.  
  476.     private void initGame() {
  477.  
  478.         pacsLeft = 3;
  479.         score = 0;
  480.         initLevel();
  481.         N_GHOSTS = 6;
  482.         currentSpeed = 3;
  483.     }
  484.  
  485.     private void initLevel() {
  486.  
  487.         int i;
  488.         for (i = 0; i < N_BLOCKS * N_BLOCKS; i++) {
  489.             screenData[i] = levelData[i];
  490.         }
  491.  
  492.         continueLevel();
  493.     }
  494.  
  495.     private void continueLevel() {
  496.  
  497.         short i;
  498.         int dx = 1;
  499.         int random;
  500.  
  501.         for (i = 0; i < N_GHOSTS; i++) {
  502.  
  503.             ghost_y[i] = 4 * BLOCK_SIZE;
  504.             ghost_x[i] = 4 * BLOCK_SIZE;
  505.             ghost_dy[i] = 0;
  506.             ghost_dx[i] = dx;
  507.             dx = -dx;
  508.             random = (int) (Math.random() * (currentSpeed + 1));
  509.  
  510.             if (random > currentSpeed) {
  511.                 random = currentSpeed;
  512.             }
  513.  
  514.             ghostSpeed[i] = validSpeeds[random];
  515.         }
  516.  
  517.         pacman_x = 7 * BLOCK_SIZE;
  518.         pacman_y = 11 * BLOCK_SIZE;
  519.         pacmand_x = 0;
  520.         pacmand_y = 0;
  521.         req_dx = 0;
  522.         req_dy = 0;
  523.         view_dx = -1;
  524.         view_dy = 0;
  525.         dying = false;
  526.     }
  527.  
  528.     private void loadImages() {
  529.  
  530.         ghost = new ImageIcon("images/ghost.png").getImage();
  531.         pacman1 = new ImageIcon("images/pacman.png").getImage();
  532.         pacman2up = new ImageIcon("images/up1.png").getImage();
  533.         pacman3up = new ImageIcon("images/up2.png").getImage();
  534.         pacman4up = new ImageIcon("images/up3.png").getImage();
  535.         pacman2down = new ImageIcon("images/down1.png").getImage();
  536.         pacman3down = new ImageIcon("images/down2.png").getImage();
  537.         pacman4down = new ImageIcon("images/down3.png").getImage();
  538.         pacman2left = new ImageIcon("images/left1.png").getImage();
  539.         pacman3left = new ImageIcon("images/left2.png").getImage();
  540.         pacman4left = new ImageIcon("images/left3.png").getImage();
  541.         pacman2right = new ImageIcon("images/right1.png").getImage();
  542.         pacman3right = new ImageIcon("images/right2.png").getImage();
  543.         pacman4right = new ImageIcon("images/right3.png").getImage();
  544.  
  545.     }
  546.  
  547.     @Override
  548.     public void paintComponent(Graphics g) {
  549.         super.paintComponent(g);
  550.  
  551.         doDrawing(g);
  552.     }
  553.  
  554.     private void doDrawing(Graphics g) {
  555.  
  556.         Graphics2D g2d = (Graphics2D) g;
  557.  
  558.         g2d.setColor(Color.black);
  559.         g2d.fillRect(0, 0, d.width, d.height);
  560.  
  561.         drawMaze(g2d);
  562.         drawScore(g2d);
  563.         doAnim();
  564.  
  565.         if (inGame) {
  566.             playGame(g2d);
  567.         } else {
  568.             showIntroScreen(g2d);
  569.         }
  570.  
  571.         g2d.drawImage(ii, 5, 5, this);
  572.         Toolkit.getDefaultToolkit().sync();
  573.         g2d.dispose();
  574.     }
  575.  
  576.     class TAdapter extends KeyAdapter {
  577.  
  578.         @Override
  579.         public void keyPressed(KeyEvent e) {
  580.  
  581.             int key = e.getKeyCode();
  582.  
  583.             if (inGame) {
  584.                 if (key == KeyEvent.VK_LEFT) {
  585.                     req_dx = -1;
  586.                     req_dy = 0;
  587.                 } else if (key == KeyEvent.VK_RIGHT) {
  588.                     req_dx = 1;
  589.                     req_dy = 0;
  590.                 } else if (key == KeyEvent.VK_UP) {
  591.                     req_dx = 0;
  592.                     req_dy = -1;
  593.                 } else if (key == KeyEvent.VK_DOWN) {
  594.                     req_dx = 0;
  595.                     req_dy = 1;
  596.                 } else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) {
  597.                     inGame = false;
  598.                 } else if (key == KeyEvent.VK_PAUSE) {
  599.                     if (timer.isRunning()) {
  600.                         timer.stop();
  601.                     } else {
  602.                         timer.start();
  603.                     }
  604.                 }
  605.             } else {
  606.                 if (key == 's' || key == 'S') {
  607.                     inGame = true;
  608.                     initGame();
  609.                 }
  610.             }
  611.         }
  612.  
  613.         @Override
  614.         public void keyReleased(KeyEvent e) {
  615.  
  616.             int key = e.getKeyCode();
  617.  
  618.             if (key == Event.LEFT || key == Event.RIGHT
  619.                     || key == Event.UP || key == Event.DOWN) {
  620.                 req_dx = 0;
  621.                 req_dy = 0;
  622.             }
  623.         }
  624.     }
  625.  
  626.     @Override
  627.     public void actionPerformed(ActionEvent e) {
  628.  
  629.         repaint();
  630.     }
  631. }
  632. //The Pacman is controlled with the cursor keys. The Esc key finishes the game, the Pause key pauses it.//
  633. private int pacman_x, pacman_y, pacmand_x, pacmand_y;
  634. //The first two variables store the x and y coordinates of the Pacman sprite. The last two variables are the delta changes in horizontal and vertical directions.//
  635. private final short levelData[] = {
  636.     19, 26, 26, 26, 18, 18, 18, 18, ...
  637. };
  638. //These numbers make up the maze. They provide information out of which we create the corners and the points. Number 1 is a left corner. Numbers 2, 4 and 8 represent top, right, and bottom corners respectively. Number 16 is a point. These numbers can be added, for example number 19 in the upper left corner means that the square will have top and left borders and a point (16 + 2 + 1).//
  639. private void doAnim() {
  640.  
  641.     pacAnimCount--;
  642.  
  643.     if (pacAnimCount <= 0) {
  644.         pacAnimCount = PAC_ANIM_DELAY;
  645.         pacmanAnimPos = pacmanAnimPos + pacAnimDir;
  646.  
  647.         if (pacmanAnimPos == (PACMAN_ANIM_COUNT - 1) || pacmanAnimPos == 0) {
  648.             pacAnimDir = -pacAnimDir;
  649.         }
  650.     }
  651. }
  652. //The doAnim() counts the pacmanAnimPos variable which determines what pacman image is drawn. There are four pacman images. There is also a PAC_ANIM_DELAY constant which makes the animation a bit slower. Otherwise the pacman would open his mouth too fast.//
  653.  
  654. boolean finished = true;
  655.  
  656. while (i < N_BLOCKS * N_BLOCKS && finished) {
  657.  
  658.     if ((screenData[i] & 48) != 0) {
  659.         finished = false;
  660.     }
  661.  
  662.     i++;
  663. }
  664. //This code is part of the checkMaze() method. It checks if there are any points left for the Pacman to eat. Number 16 stands for a point. If all points are consumed, we move to the next level. (In our case, we just restart the game.)
  665.  
  666. Next we will examine the moveGhosts() method. The ghosts move one square and then decide if they change the direction.//
  667. if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) {
  668. //We continue only if we have finished moving one square.//
  669. pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE);
  670. //This line determines where the ghost is located; in which position/square. There are 225 theoretical positions. (A ghost cannot move over walls.)//
  671. if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) {
  672.     dx[count] = -1;
  673.     dy[count] = 0;
  674.     count++;
  675. }
  676. //If there is no obstacle on the left and the ghost is not already moving to the right, the ghost will move to the left. What does this code really mean? If the ghost enters a tunnel, he will continue in the same direction until he is out of the tunnel. Moving of ghosts is partly random. We do not apply this randomness inside long tunnels because the ghost might get stuck there.//
  677. if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12)
  678.         && pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12)
  679.         && inGame) {
  680.  
  681.     dying = true;
  682. }
  683. //If there is a collision between ghosts and Pacman, Pacman dies.
  684.  
  685. Next we are going to examine the movePacman() method. The req_dx and req_dy variables are determined in the TAdapter inner class. These variables are controlled with cursor keys.//
  686.  
  687. if ((ch & 16) != 0) {
  688.     screenData[pos] = (short) (ch & 15);
  689.     score++;
  690. }
  691. //If the pacman moves to a position with a point, we remove it from the maze and increase the score value.//
  692.  
  693. if ((pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0)
  694.         || (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0)
  695.         || (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0)
  696.         || (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) {
  697.     pacmand_x = 0;
  698.     pacmand_y = 0;
  699. }
  700. //The Pacman stops if he cannot move further it his current direction.//
  701.  
  702. private void drawPacman(Graphics2D g2d) {
  703.  
  704.     if (view_dx == -1) {
  705.         drawPacnanLeft(g2d);
  706.     } else if (view_dx == 1) {
  707.         drawPacmanRight(g2d);
  708.     } else if (view_dy == -1) {
  709.         drawPacmanUp(g2d);
  710.     } else {
  711.         drawPacmanDown(g2d);
  712.     }
  713. }
  714. //There are four possible directions for a Pacman. There are four images for all directions. The images are used to animate Pacman opening and closing his mouth.
  715.  
  716. The drawMaze() method draws the maze out of the numbers in the screenData array. Number 1 is a left border, 2 is a top border, 4 is a right border, 8 is a bottom border and 16 is a point. We simply go through all 225 squares in the maze. For example we have 9 in the screenData array. We have the first bit (1) and the fourth bit (8) set. So we draw a bottom and a left border on this particular square.//
  717. if ((screenData[i] & 1) != 0) {
  718.     g2d.drawLine(x, y, x, y + BLOCK_SIZE - 1);
  719. }
  720. //We draw a left border if the first bit of a number is set.//
  721.  
  722. //com/zetcode/Pacman.java//
  723. //package com.zetcode;//
  724.  
  725. import java.awt.EventQueue;
  726. import javax.swing.JFrame;
  727.  
  728. public class Pacman extends JFrame {
  729.  
  730.     public Pacman() {
  731.        
  732.         initUI();
  733.     }
  734.    
  735.     private void initUI() {
  736.        
  737.         add(new Board());
  738.        
  739.         setTitle("Pacman");
  740.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  741.         setSize(380, 420);
  742.         setLocationRelativeTo(null);
  743.     }
  744.  
  745.     public static void main(String[] args) {
  746.  
  747.         EventQueue.invokeLater(() -> {
  748.  
  749.             var ex = new Pacman();
  750.             ex.setVisible(true);
  751.         });
  752.     }
  753. }
  754. //This is a Pacman file with a main method.//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement