Vla_DOS

Untitled

May 12th, 2023
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.21 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.KeyEvent;
  5. import java.io.IOException;
  6. import javax.imageio.ImageIO;
  7. import javax.swing.*;
  8. import javax.swing.table.DefaultTableCellRenderer;
  9. import javax.swing.table.DefaultTableModel;
  10. import javax.swing.Timer;
  11.  
  12. public class PacmanMap extends JFrame {
  13.     private static final int CELL_SIZE = 20;
  14.     private static final int ROWS = 31;
  15.     private static final int COLUMNS = 28;
  16.  
  17.     private JTable mapTable;
  18.     private DefaultTableModel tableModel;
  19.     private int pacmanRow = (ROWS / 2) + 2;
  20.     private int pacmanColumn = COLUMNS / 2;
  21.     private int ghostRow = (ROWS / 2) + 2;
  22.     private int ghostColumn = (COLUMNS / 2) + 1;
  23.     final int W=1; // Wall.
  24.     final int F=2; // Crossroads with food
  25.     final int E=3; // Empty crossroads
  26.     private int obstacleMap[][] = {
  27.             {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},
  28.             {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},
  29.             {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},
  30.             {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},
  31.             {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},
  32.             {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},
  33.             {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},
  34.             {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},
  35.             {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},
  36.             {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},
  37.             {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},
  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,E,E,E,E,E,E,W,F,W,W,F,W,W,W,W,W,W},
  41.             {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},
  42.             {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},
  43.             {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},
  44.             {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},
  45.             {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},
  46.             {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},
  47.             {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},
  48.             {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},
  49.             {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},
  50.             {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},
  51.             {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},
  52.             {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},
  53.             {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},
  54.             {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},
  55.             {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},
  56.             {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},
  57.             {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}
  58.     };
  59.     BlueGhost blueGhost;
  60.     public PacmanMap()  {
  61.         CreateStatusBar();
  62.         add(p);
  63.  
  64.         initUI();
  65.     }
  66.     private void initUI(){
  67.         tableModel = new DefaultTableModel(ROWS, COLUMNS);
  68.         ImageIcon photo = new ImageIcon(getClass().getResource("img/" +"PacmanRight.png"));
  69.         Image image = photo.getImage();
  70.         for (int row = 0; row < ROWS; row++) {
  71.             for (int column = 0; column < COLUMNS; column++) {
  72.                 tableModel.setValueAt("X", row, column);
  73.                 if (obstacleMap[row][column]==2) {
  74.                     tableModel.setValueAt(".", row, column);
  75.                 }
  76.                 else if (obstacleMap[row][column]==3) {
  77.                     tableModel.setValueAt("0", row, column);
  78.                 }
  79.             }
  80.         }
  81.  
  82.         tableModel.setValueAt(image, pacmanRow, pacmanColumn);
  83.         blueGhost = new BlueGhost(pacmanRow, pacmanColumn);
  84.  
  85.         tableModel.setValueAt(blueGhost.toString(), ghostRow, ghostColumn);
  86.  
  87.         mapTable = new JTable(tableModel);
  88.         mapTable.setGridColor(Color.black);
  89.         mapTable.setRowHeight(CELL_SIZE);
  90.         for (int column = 0; column < COLUMNS; column++) {
  91.             mapTable.getColumnModel().getColumn(column).setPreferredWidth(CELL_SIZE);
  92.         }
  93.         mapTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
  94.             @Override
  95.             public void setValue(Object value) {
  96.                 if(value instanceof Image){
  97.                     ImageIcon icon = new ImageIcon((Image)value);
  98.                     setIcon(icon);
  99.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  100.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  101.                     setText(null);
  102.                 }
  103.                 else if(value == "X"){
  104.                     setBackground(new Color(80,111,214));
  105.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  106.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  107.  
  108.                     setText(value.toString());
  109.                     setIcon(null);
  110.                 }
  111.                 else if(value == "."){
  112.                     setBackground(new Color(13,1,35));
  113.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  114.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  115.  
  116.                     setText(value.toString());
  117.                     setIcon(null);
  118.                 }
  119.                 else if(value == "0"){
  120.                     setBackground(new Color(13,1,35));
  121.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  122.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  123.  
  124.                     setText(value.toString());
  125.                     setIcon(null);
  126.                 }
  127.                 else if(value == "E"){
  128.                     setBackground(new Color(100,0,0));
  129.                     setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
  130.                     setVerticalAlignment(DefaultTableCellRenderer.CENTER);
  131.  
  132.                     setText(value.toString());
  133.                     setIcon(null);
  134.                 }
  135.                 else{
  136.                     setIcon(null);
  137.                     setText("");
  138.                     setBackground(new Color(13,1,35));
  139.  
  140.                 }
  141.             }
  142.         });
  143.  
  144.         mapTable.addKeyListener(new java.awt.event.KeyAdapter() {
  145.             public void keyPressed(java.awt.event.KeyEvent event) {
  146.                 movePacman(event);
  147.             }
  148.         });
  149.  
  150.         JScrollPane scrollPane = new JScrollPane(mapTable);
  151.         scrollPane.setPreferredSize(new Dimension(CELL_SIZE * COLUMNS, CELL_SIZE * ROWS));
  152. //        enemy = new Enemy(2, 2, 1,mapTable);
  153. //        enemy.run();
  154.  
  155.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  156.         this.add(scrollPane);
  157.         this.pack();
  158.         this.setLocationRelativeTo(null);
  159.         this.setVisible(true);
  160.     }
  161.     JLabel lab1;
  162.     JPanel p ;
  163.     int lives;
  164.     private void CreateStatusBar() {
  165.  
  166.         try {
  167.         lab1 = new JLabel("User Name", JLabel.LEFT);
  168.         lab1.setBackground(Color.BLUE);
  169.         lab1.setText(" ");
  170.         Font f = new Font("Broadway", Font.BOLD, 18);
  171.  
  172.         lab1.setFont(f);
  173.  
  174.         p = new JPanel(){
  175.             private static final long serialVersionUID = 1L;
  176.             private Image pac = ImageIO.read(getClass().getClassLoader().getResource("img/"  +"Heart.png"));
  177.             public void paint (Graphics g){
  178.                 super.paint(g);
  179.                 for(int i = 0;i<lives;i++)
  180.                     g.drawImage(pac, 28*i, 1, null);
  181.             }
  182.         };
  183.  
  184.         p.setLayout(new FlowLayout());
  185.         p.add(lab1);
  186.         } catch (IOException e) {
  187.             e.printStackTrace();
  188.         }
  189.     }
  190.     private void movePacman(KeyEvent event) {
  191.         int newPacmanRow = pacmanRow;
  192.         int newPacmanColumn = pacmanColumn;
  193.  
  194.         switch (event.getKeyCode()) {
  195.             case KeyEvent.VK_UP:
  196.                 newPacmanRow = pacmanRow - 1;
  197.                 break;
  198.             case KeyEvent.VK_DOWN:
  199.                 newPacmanRow = pacmanRow + 1;
  200.                 break;
  201.             case KeyEvent.VK_LEFT:
  202.                 newPacmanColumn = pacmanColumn - 1;
  203.                 break;
  204.             case KeyEvent.VK_RIGHT:
  205.                 newPacmanColumn = pacmanColumn + 1;
  206.                 break;
  207.             default:
  208.                 return;
  209.         }
  210.  
  211.         if (newPacmanRow < 0 || newPacmanRow >= ROWS || newPacmanColumn < 0 || newPacmanColumn >= COLUMNS) {
  212.             return;
  213.         }
  214.  
  215.         if (obstacleMap[newPacmanRow][newPacmanColumn] == 1) {
  216.             return;
  217.         }
  218.  
  219.         ImageIcon photo = new ImageIcon(getClass().getResource("img/" +"PacmanRight.png"));
  220.         Image image = photo.getImage();
  221.         tableModel.setValueAt("", pacmanRow, pacmanColumn);
  222.         pacmanRow = newPacmanRow;
  223.         pacmanColumn = newPacmanColumn;
  224.         tableModel.setValueAt(image, pacmanRow, pacmanColumn);
  225.         c = pacmanColumn;
  226.         r = pacmanRow;
  227.         T();
  228.  
  229.     }
  230.     public int c = 1;
  231.     public int r = 1;
  232.     int delay = 1000; // milliseconds
  233.     public void T(){
  234.  
  235.         ActionListener taskPerformer = new ActionListener() {
  236.             int direction = 1;   // 1: рух вправо, 2: рух вліво, 3: рух вниз, 4: рух вгору
  237.             public void actionPerformed(ActionEvent evt) {
  238.                 int newGhostRow = ghostRow;
  239.                 int newGhostColumn = ghostColumn;
  240.  
  241.                 //newPacmanColumn = ghostColumn + 1;
  242.  
  243.                 if (obstacleMap[newGhostRow][newGhostColumn] == 1) {
  244.                     if (direction == 1 || direction == 2) {
  245.                         direction = 3;  // рух вниз
  246.                     } else if (direction == 3 || direction == 4) {
  247.                         direction = 1;  // рух вправо
  248.                     }
  249.                 }
  250.                 if (direction == 1) {  // рух вправо
  251.                     newGhostColumn = ghostColumn + 1;
  252.                 } else if (direction == 2) {  // рух вліво
  253.                     newGhostColumn = ghostColumn - 1;
  254.                 } else if (direction == 3) {  // рух вниз
  255.                     newGhostRow = ghostRow + 1;
  256.                 } else if (direction == 4) {  // рух вгору
  257.                     newGhostRow = ghostRow - 1;
  258.                 }
  259.  
  260.                 if (newGhostRow < 0 || newGhostRow >= ROWS || newGhostColumn < 0 || newGhostColumn >= COLUMNS) {
  261.                     return;
  262.                 }
  263.  
  264.                 if (obstacleMap[newGhostRow][newGhostColumn] == 1) {
  265.                     if (direction == 1) {
  266.                         direction = 2;  // рух вниз
  267.                     }
  268.                     else if (direction == 2) {
  269.                         direction = 3;  // рух вправо
  270.                     }
  271.                     else if (direction == 3) {
  272.                         direction = 4;  // рух вправо
  273.                     }
  274.                     else if (direction == 4) {
  275.                         direction = 1;  // рух вправо
  276.                     }
  277.                     newGhostRow = ghostRow;
  278.                     newGhostColumn = ghostColumn;
  279.                 }
  280.  
  281.                 ImageIcon photo = new ImageIcon(getClass().getResource("img/" +"RedLeft.png"));
  282.                 Image image = photo.getImage();
  283.                 tableModel.setValueAt("", ghostRow, ghostColumn);
  284.                 ghostRow = newGhostRow;
  285.                 ghostColumn = newGhostColumn;
  286.                 tableModel.setValueAt(image, ghostRow, ghostColumn);
  287.  
  288.  
  289.             }
  290.         };
  291.         Timer timer = new Timer(delay, taskPerformer);
  292.         timer.start();
  293.         if(ghostRow == r && ghostColumn == c){
  294.             JOptionPane.showMessageDialog(this, "Hello World!", "Message",
  295.                     JOptionPane.PLAIN_MESSAGE);
  296.             timer.stop();
  297.         }
  298.  
  299.     }
  300.     public static void main(String[] args) throws IOException {
  301.          EventQueue.invokeLater(() -> {
  302.             new PacmanMap();
  303.         });
  304.  
  305.     }
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment