Vla_DOS

map

May 18th, 2023
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.51 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.KeyEvent;
  4. import javax.swing.table.DefaultTableModel;
  5. import javax.swing.table.DefaultTableCellRenderer;
  6.  
  7. public class PacmanMap extends JFrame {
  8.     private static final int CELL_SIZE = 25;
  9.     private static final int ROWS = 31;
  10.     private static final int COLUMNS = 28;
  11.     private JTable mapTable;
  12.     private DefaultTableModel tableModel;
  13.     private int pacmanRow = (ROWS / 2) + 2;
  14.     private int pacmanColumn = (COLUMNS / 2) +4;
  15.     private int ghostRow = (ROWS / 2) + 2;
  16.     private int ghostColumn = (COLUMNS / 2) - 5;
  17.     final boolean W=false; // Wall.
  18.     final boolean F=true; // Crossroads with food
  19.     final boolean E=false; // Empty crossroads
  20.     private boolean obstacleMap[][] = {
  21.             {W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W},
  22.             {W,F,F,F,F,F,F,F,F,F,F,F,F,W,W,F,F,F,F,F,F,F,F,F,F,F,F,W},
  23.             {W,F,W,W,W,W,F,W,W,W,W,W,F,W,W,F,W,W,W,W,W,F,W,W,W,W,F,W},
  24.             {W,F,W,W,W,W,F,W,W,W,W,W,F,W,W,F,W,W,W,W,W,F,W,W,W,W,F,W},
  25.             {W,F,W,W,W,W,F,W,W,W,W,W,F,W,W,F,W,W,W,W,W,F,W,W,W,W,F,W},
  26.             {W,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,W},
  27.             {W,F,W,W,W,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,W,W,W,F,W},
  28.             {W,F,W,W,W,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,W,W,W,F,W},
  29.             {W,F,F,F,F,F,F,W,W,F,F,F,F,W,W,F,F,F,F,W,W,F,F,F,F,F,F,W},
  30.             {W,W,W,W,W,W,F,W,W,W,W,W,F,W,W,F,W,W,W,W,W,F,W,W,W,W,W,W},
  31.             {E,E,E,E,E,W,F,W,W,W,W,W,F,W,W,F,W,W,W,W,W,F,W,E,E,E,E,E},
  32.             {E,E,E,E,E,W,F,W,W,F,F,F,F,F,F,F,F,F,F,W,W,F,W,E,E,E,E,E},
  33.             {E,E,E,E,E,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,E,E,E,E,E},
  34.             {W,W,W,W,W,W,F,W,W,F,W,E,E,E,E,E,E,W,F,W,W,F,W,W,W,W,W,W},
  35.             {F,F,F,F,F,F,F,F,F,F,W,E,E,E,E,E,E,W,F,F,F,F,F,F,F,F,F,F},
  36.             {W,W,W,W,W,W,F,W,W,F,W,E,E,E,E,E,E,W,F,W,W,F,W,W,W,W,W,W},
  37.             {E,E,E,E,E,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,E,E,E,E,E},
  38.             {E,E,E,E,E,W,F,W,W,F,F,F,F,F,F,F,F,F,F,W,W,F,W,E,E,E,E,E},
  39.             {E,E,E,E,E,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,E,E,E,E,E},
  40.             {W,W,W,W,W,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,W,W,W,W,W},
  41.             {W,F,F,F,F,F,F,F,F,F,F,F,F,W,W,F,F,F,F,F,F,F,F,F,F,F,F,W},
  42.             {W,F,W,W,W,W,F,W,W,W,W,W,F,W,W,F,W,W,W,W,W,F,W,W,W,W,F,W},
  43.             {W,F,W,W,W,W,F,W,W,W,W,W,F,W,W,F,W,W,W,W,W,F,W,W,W,W,F,W},
  44.             {W,F,F,F,W,W,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,W,W,F,F,F,W},
  45.             {W,W,W,F,W,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,W,F,W,W,W},
  46.             {W,W,W,F,W,W,F,W,W,F,W,W,W,W,W,W,W,W,F,W,W,F,W,W,F,W,W,W},
  47.             {W,F,F,F,F,F,F,W,W,F,F,F,F,W,W,F,F,F,F,W,W,F,F,F,F,F,F,W},
  48.             {W,F,W,W,W,W,W,W,W,W,W,W,F,W,W,F,W,W,W,W,W,W,W,W,W,W,F,W},
  49.             {W,F,W,W,W,W,W,W,W,W,W,W,F,W,W,F,W,W,W,W,W,W,W,W,W,W,F,W},
  50.             {W,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,W},
  51.             {W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W}
  52.     };
  53.  
  54.     private int counter = 1;
  55.     private int countPoint = 0;
  56.     private JLabel counterLabel;
  57.     private JLabel livesLabel; // Додаємо JLabel для відображення кількості життів
  58.  
  59.     Enemy enemyBlueRight;
  60.     Enemy enemyRedLeft;
  61.     Enemy enemyOrangeRight;
  62.     public PacmanMap()  {
  63.         initUI();
  64.         ImageIcon imageIconRedLeft = new ImageIcon(getClass().getResource("img/" + "RedLeft.png"));
  65.         ImageIcon imageIconBlueRight = new ImageIcon(getClass().getResource("img/" + "BlueRight.png"));
  66.             enemyBlueRight = new Enemy(ghostRow - 6, ghostColumn, obstacleMap, 300, tableModel, imageIconBlueRight, pacmanRow, pacmanColumn, "enemyBlue");
  67.         Thread enemyThreadBlueRight = new Thread(enemyBlueRight);
  68.         enemyThreadBlueRight.start();
  69.  
  70.         enemyRedLeft = new Enemy(ghostRow+3, ghostColumn, obstacleMap, 300, tableModel, imageIconRedLeft, pacmanRow, pacmanColumn, "enemyRed");
  71.         Thread enemyThreadenemyRedLeft = new Thread(enemyRedLeft);
  72.         enemyThreadenemyRedLeft.start();
  73.  
  74.         ImageIcon imageIconOrangeRight = new ImageIcon(getClass().getResource("img/" + "OrangeRight.png"));
  75.         enemyOrangeRight = new Enemy(ghostRow-7, ghostColumn + 7, obstacleMap, 300, tableModel, imageIconOrangeRight, pacmanRow, pacmanColumn, "enemyOrange");
  76.         Thread enemyThreadOrangeRight = new Thread(enemyOrangeRight);
  77.         enemyThreadOrangeRight.start();
  78.     }
  79.  
  80.     private void initUI(){
  81.         tableModel = new DefaultTableModel(ROWS, COLUMNS);
  82.         ImageIcon photo = new ImageIcon(getClass().getResource("img/" +"PacmanRight.png"));
  83.         Image image = photo.getImage();
  84.  
  85.         for (int row = 0; row < ROWS; row++) {
  86.             for (int column = 0; column < COLUMNS; column++) {
  87.                 if (!obstacleMap[row][column]) {
  88.                     tableModel.setValueAt("X", row, column);
  89.                 }
  90.                 else if(obstacleMap[row][column]) {
  91.                     tableModel.setValueAt("*", row, column);
  92.                     countPoint++;
  93.                 }
  94.             }
  95.         }
  96.  
  97.         tableModel.setValueAt(image, pacmanRow, pacmanColumn);
  98.         mapTable = new JTable(tableModel);
  99.         mapTable.setGridColor(Color.black);
  100.         mapTable.setRowHeight(CELL_SIZE);
  101.         for (int column = 0; column < COLUMNS; column++) {
  102.             mapTable.getColumnModel().getColumn(column).setPreferredWidth(CELL_SIZE);
  103.         }
  104.         mapTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
  105.             @Override
  106.             public void setValue(Object value) {
  107.  
  108.                 if (value instanceof Image) {
  109.                     ImageIcon icon = new ImageIcon((Image)value);
  110.                     setIcon(icon);
  111.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  112.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  113.                     setBackground(new Color(13,1,35));
  114.                     setText(null);
  115.                 }
  116.                 else if (value instanceof String && value.equals("X")) {
  117.                     setBackground(new Color(80,111,214));
  118.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  119.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  120.                     setText("X");
  121.                     setIcon(null);
  122.                 }
  123.                 else if (value instanceof String && ((String) value).contains("enemy")) {
  124.                     String nameEnemy = "";
  125.                     ImageIcon imageIcon;
  126.                     if((value).equals("enemyOrange")){
  127.                         imageIcon = new ImageIcon(getClass().getResource("img/" + "OrangeRight.png"));
  128.                         setIcon(imageIcon);
  129.                         nameEnemy = "enemyOrange";
  130.                     }
  131.                     else if((value).equals("enemyRed")){
  132.                         imageIcon = new ImageIcon(getClass().getResource("img/" + "RedLeft.png"));
  133.                         setIcon(imageIcon);
  134.                         nameEnemy = "enemyRed";
  135.  
  136.                     }
  137.                     else if((value).equals("enemyBlue")){
  138.                     imageIcon = new ImageIcon(getClass().getResource("img/" + "BlueRight.png"));
  139.                     setIcon(imageIcon);
  140.                     nameEnemy = "enemyRed";
  141.  
  142.                     }
  143.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  144.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  145.                     setBackground(new Color(13,1,35));
  146.                     setText(nameEnemy);
  147.                 }
  148.                 else if(value instanceof String && value.equals("")){
  149.                     setText("");
  150.                     setIcon(null);
  151.                     setBackground(new Color(13,1,35));
  152.                 }
  153.                 else if(value instanceof String && value.equals("*")){
  154.                     setText("");
  155.                     ImageIcon photoNormalPoint = new ImageIcon(getClass().getResource("img/" +"NormalPoint.png"));
  156.                     setIcon(photoNormalPoint);
  157.                     setBackground(new Color(13,1,35));
  158.                 }
  159.                 else{
  160.                     setText("");
  161.                     setIcon(null);
  162.                     setBackground(new Color(13,1,35));
  163.                 }
  164.             }
  165.         });
  166.  
  167.         mapTable.addKeyListener(new java.awt.event.KeyAdapter() {
  168.             public void keyPressed(java.awt.event.KeyEvent event) {
  169.                 movePacman(event);
  170.             }
  171.         });
  172. // Create a JPanel with a FlowLayout set to center
  173.         JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
  174.         JScrollPane scrollPane = new JScrollPane(mapTable);
  175.         scrollPane.setPreferredSize(new Dimension(CELL_SIZE * COLUMNS, CELL_SIZE * ROWS));
  176.  
  177. // Create and add the counter label to the top panel
  178.         counterLabel = new JLabel("Очки: " + counter);
  179.         topPanel.add(counterLabel);
  180.  
  181. // Create and add the lives label to the top panel
  182.         livesLabel = new JLabel("Життя: " + lives);
  183.         topPanel.add(livesLabel);
  184.  
  185. // Add the top panel to the NORTH position of the main JFrame
  186.         add(topPanel, BorderLayout.NORTH);
  187.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  188.         this.add(scrollPane);
  189.         this.pack();
  190.         this.setSize(800, 865);
  191.         this.setLocationRelativeTo(null);
  192.         this.setVisible(true);
  193.     }
  194.  
  195.     int lives=3;
  196.     private Thread movementThread;
  197.     private int lastKeyCode = -1;
  198.  
  199.     private void movePacman(KeyEvent event) {
  200.         int keyCode = event.getKeyCode();
  201.         if (event.isControlDown() && event.isShiftDown() && event.getKeyCode() == KeyEvent.VK_Q) {
  202.             // Закриття гри і повернення до головного меню
  203.             dispose();  // Закриття вікна гри
  204.             MainMenu mainMenu = new MainMenu();  // Створення нового екземпляра головного меню
  205.             mainMenu.setVisible(true);  // Показати головне меню
  206.         }
  207.         if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT) {
  208.             lastKeyCode = keyCode;
  209.  
  210.             if (movementThread != null && movementThread.isAlive()) {
  211.                 // Якщо потік вже запущено, не виконувати новий рух
  212.                 return;
  213.             }
  214.             movementThread = new Thread(() -> {
  215.                 while (true) {
  216.                     try {
  217.                         Thread.sleep(300);  // Затримка 0.5 секунди
  218.                     } catch (InterruptedException e) {
  219.                         e.printStackTrace();
  220.                     }
  221.                     ImageIcon photo = null; //= new ImageIcon(getClass().getResource("img/" +"PacmanRight.png"));
  222.                     int newPacmanRow = pacmanRow;
  223.                     int newPacmanColumn = pacmanColumn;
  224.  
  225.                     switch (lastKeyCode) {
  226.                         case KeyEvent.VK_UP:
  227.                             newPacmanRow = pacmanRow - 1;
  228.                             photo = new ImageIcon(getClass().getResource("img/" +"PacmanUp.png"));
  229.  
  230.                             break;
  231.                         case KeyEvent.VK_DOWN:
  232.                             newPacmanRow = pacmanRow + 1;
  233.                             photo = new ImageIcon(getClass().getResource("img/" +"PacmanDown.png"));
  234.  
  235.                             break;
  236.                         case KeyEvent.VK_LEFT:
  237.                             newPacmanColumn = pacmanColumn - 1;
  238.                             photo = new ImageIcon(getClass().getResource("img/" +"PacmanLeft.png"));
  239.  
  240.                             break;
  241.                         case KeyEvent.VK_RIGHT:
  242.                             newPacmanColumn = pacmanColumn + 1;
  243.                             photo = new ImageIcon(getClass().getResource("img/" +"PacmanRight.png"));
  244.                             break;
  245.                         default:
  246.                             return;
  247.                     }
  248.  
  249.                     if (newPacmanRow < 0 || newPacmanRow >= ROWS || newPacmanColumn < 0 || newPacmanColumn >= COLUMNS) {
  250.                         return;
  251.                     }
  252.  
  253.                     if (!obstacleMap[newPacmanRow][newPacmanColumn]) {
  254.                         return;
  255.                     }
  256.  
  257.                     // Increment the counter if Pacman moves onto a cell with a "." marker
  258.                     if (tableModel.getValueAt(newPacmanRow, newPacmanColumn).equals("*")) {
  259.                         counter++;
  260.                         counterLabel.setText("Очки: " + counter);
  261.                     }
  262.  
  263.  
  264.                     tableModel.setValueAt("", pacmanRow, pacmanColumn);
  265.                     pacmanRow = newPacmanRow;
  266.                     pacmanColumn = newPacmanColumn;
  267.  
  268.                     Image image = photo.getImage();
  269.  
  270.                     tableModel.setValueAt(image, pacmanRow, pacmanColumn);
  271.  
  272.                     enemyBlueRight.movePacman(newPacmanRow, newPacmanColumn);
  273.                     enemyOrangeRight.movePacman(newPacmanRow, newPacmanColumn);
  274.                     enemyRedLeft.movePacman(newPacmanRow, newPacmanColumn);
  275.  
  276.                     if (enemyBlueRight.Check(newPacmanRow, newPacmanColumn) || enemyOrangeRight.Check(newPacmanRow, newPacmanColumn) || enemyRedLeft.Check(newPacmanRow, newPacmanColumn)) {
  277.                         lives--;
  278.                         livesLabel.setText("Життя: " + lives);
  279.                         if (lives == 0) {
  280.                             JOptionPane.showMessageDialog(this, "Кінець гри!" + "\nВаш рахунок: " + counter, "Гра завершена", JOptionPane.PLAIN_MESSAGE);
  281.                             enemyRedLeft.stop();
  282.                             enemyOrangeRight.stop();
  283.                             enemyBlueRight.stop();
  284.                             new SaveResult(counter);
  285.                         }
  286.                     }
  287.  
  288.                     if (countPoint == counter) {
  289.                         JOptionPane.showMessageDialog(this, "Ви виграли!" + "\nВаш рахунок: " + counter, "Гра завершена", JOptionPane.PLAIN_MESSAGE);
  290.                         enemyRedLeft.stop();
  291.                         enemyOrangeRight.stop();
  292.                         enemyBlueRight.stop();
  293.                         new SaveResult(counter);
  294.  
  295.                     }
  296.                 }
  297.             });
  298.  
  299.             movementThread.start();
  300.         }
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment