המחלקה SnakeGrid

למי שמעוניין לעשות שינויים במחלקה SnakeGrid מצורף קוד המקור

 

import javax.swing.JFrame;

import javax.swing.JLabel;

 

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.ComponentOrientation;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.util.function.IntConsumer;

 

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

 

public class SnakeGrid extends JFrame implements KeyListener{

 

        public static final int UP_KEY = 0;

        public static final int DOWN_KEY = 1;

        public static final int LEFT_KEY = 2;

        public static final int RIGHT_KEY = 3;

        public static final int P_KEY = 4;

        public static final int E_KEY = 5;

        private int SCREEN_WIDTH = 800;

        private int SCREEN_HEIGHT = 800;

        private int DIFF = 30;

        private int block_sizeX,block_sizeY;

        private int [][] boardArr;

        private JPanel[][] panelArr;

        private JPanel topPanel,gridPanel;

        private int rows,cols;

        private JLabel scoreLBL;

        private int scoreIdx;

        private IntConsumer upClick,leftClick,rightClick,downClick,pClick,eClick;

        public SnakeGrid(int [][] boardArr) {

                this.boardArr = boardArr;

                init();

        }

        public SnakeGrid(int [][] boardArr,int width,int height) {

                this.boardArr = boardArr;

                this.SCREEN_WIDTH = width;

                this.SCREEN_HEIGHT = height;

                init();

        }

        private void init() {

                rows = boardArr.length;

                cols = boardArr[0].length;

                panelArr = new JPanel[rows][cols];

                gridPanel = new JPanel();               

                gridPanel.setLayout(new GridLayout(rows,cols,1,1));

                gridPanel.setBounds(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);

                gridPanel.setBackground(Color.black);

                topPanel = new JPanel();

                topPanel.setBackground(Color.black);

                scoreLBL = new JLabel(" ",SwingConstants.CENTER);

                scoreLBL.setOpaque(true);

                scoreLBL.setForeground(Color.CYAN);

                scoreLBL.setBackground(Color.black);

                topPanel.add(scoreLBL);

                scoreLBL.setText("Score : 0");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT+30));

                this.addKeyListener(this);

                this.getContentPane().setBackground(Color.black);

                this.setTitle("Snake Game");

                GridBagLayout gbl = new GridBagLayout();

                this.setLayout(gbl);

                GridBagConstraints gcon = new GridBagConstraints();

                gcon.weightx = 1;

                gcon.weighty = 1;

                gcon.fill = GridBagConstraints.BOTH;

                gcon.gridx = 0;

                gcon.gridy = 0;

                gcon.weightx = cols;

                gcon.weighty = 1;

                gbl.setConstraints(topPanel, gcon);

                gcon.gridx = 0;

                gcon.gridy = 1;

                gcon.weightx = cols;

                gcon.weighty = rows;

                gbl.setConstraints(gridPanel, gcon);

                boolean flag = true;

                for (int row=0;row<rows;row++) {

                        for (int col=0;col<cols;col++) {

                                panelArr[row][col] = new JPanel();

                                gridPanel.add(panelArr[row][col]);

                        }

                }

                this.add(topPanel);

                this.add(gridPanel);

                updateBoard(boardArr);

                this.pack();

                this.setFocusable(true);

                this.setResizable(false);

                this.setVisible(true);

                this.setLocationRelativeTo(null);

        }

        public void showMessage(String str) {

                JOptionPane.showMessageDialog(this, str);

        }

        public void updateScore(int score) {

                scoreLBL.setText("Score : "+score);

                repaint();

        }

        public void paint(Graphics g) {

                super.paint(g);

                paintComponents(g);

               

        }

        public void paintComponents(Graphics g) {

                super.paintComponents(g);

                draw(g);

        }

       

        public void draw(Graphics g) {

                g.setColor(Color.white);

                //clearBoard();

               

        }

       

        public void updateGrid(int [][] arr) {

                boardArr = arr;

                updateBoard(arr);;

                repaint();

        }

        private void clearBoard() {

                boolean flag = true;

                for (int row=0;row<rows;row++) {

                        for (int col=0;col<cols;col++) {

                                if (flag) {

                                        flag = !flag;

                                        panelArr[row][col].setBackground(new Color(171,214,83));

                                }

                                else {

                                        flag = !flag;

                                        panelArr[row][col].setBackground(new Color(161,207,72));

                                }                              

                        }

                        flag = !flag;

                }

        }

        private void updateBoard(int [][] arr) {

                boardArr = arr;

                clearBoard();

                for (int row=0;row<rows;row++) {

                        for (int col=0;col<cols;col++) {

                                if (boardArr[row][col] == 0) {

                                       

                                }

                                else if (boardArr[row][col] == 1) {

                                        panelArr[row][col].setBackground(new Color(58,104,213));

                                }

                                else if (boardArr[row][col] == 2) {

                                        panelArr[row][col].setBackground(new Color(230,71,29));

                                }

                        }

                }

        }

        public void dansMethod(int x, IntConsumer aMethod) {

        switch (x) {

        case UP_KEY:

        upClick = aMethod;

        break;

                case DOWN_KEY:

                        downClick = aMethod;

                      break;

                case LEFT_KEY:

                        leftClick = aMethod;

                        break;

                case RIGHT_KEY:

                        rightClick = aMethod;

                        break;

                case P_KEY:

                        pClick = aMethod;

                        break;

                case E_KEY:

                        eClick = aMethod;

                        break;

        }

       

    }

        @Override

        public void keyTyped(KeyEvent e) {

                // TODO Auto-generated method stub

               

        }

 

        @Override

        public void keyPressed(KeyEvent e) {

 

                switch (e.getKeyCode()) {

                case KeyEvent.VK_LEFT:

                        System.out.println("Left pressed");

                        leftClick.accept(0);

                        break;

                case KeyEvent.VK_RIGHT:

                        System.out.println("Right pressed");

                        rightClick.accept(0);

                        break;

                case KeyEvent.VK_DOWN:

                        System.out.println("Down pressed");

                        downClick.accept(0);

                        break;

                case KeyEvent.VK_UP:

                        System.out.println("Up pressed");

                        upClick.accept(0);

                        break;

                case KeyEvent.VK_P:

                        System.out.println("p pressed");

                        pClick.accept(0);

                        break;

                case KeyEvent.VK_E:

                        System.out.println("e pressed");

                        eClick.accept(0);

                        break;

                }

               

        }

 

        @Override

        public void keyReleased(KeyEvent e) {

                // TODO Auto-generated method stub

               

        }

       

}