Snake - pomoc w poprawieniu kodu

0

witam, piszę gę snake, w tej chwili mam dwie klasy: pierwsza która tworzy okno i druga która rysuje plansze wraz z dwoma przyciskami(przycisk który rozpoczyna grę oraz przycisk który kończy grę), oraz labelem który wyświetla wynik. proszę o opinie o kodzie.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingSnake extends JFrame {

    /**
     * @param args the command line arguments
     */
    private int widthWindow = 640;
    private int heightWindow = 480;

    public JavaSwingSnake() {
        setSize(new Dimension(widthWindow, heightWindow));
        setTitle("snake !!!!!!!");
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JavaSwingSnake javaSwingSnake = new JavaSwingSnake();
                ClassDrawBoard classDrawBoard = new ClassDrawBoard();
                javaSwingSnake.add(classDrawBoard);
            }
        });
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class ClassDrawBoard extends JPanel {

    JLabel scoreLabel;
    JButton startGameButton;
    JButton endGameButton;
    BorderLayout borderLayout;
    BoxLayout boxLayout;
    BoxLayout boxLayoutButtons;
    JPanel controlPanel;
    JPanel drawPanel;
    private int sizeSquare = 20;//wielkosc kwadratu
    private int startDrawPositionX = 20;//poczatek rysowania
    private int startDrawPositionY = 20;//poczatek rysowania
    //oznaczenia cyfr w tablicy
    //0 - mur
    //1 - plansza po ktorej porusza sie waz
    static int[][] board = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//2
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//3
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//4
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//5
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//6
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//7
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//8
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//9
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//10
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//11
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//12
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//13
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//14
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//15
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//16
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//17
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//18
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//19
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//20
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}//21
    };

    public ClassDrawBoard() {
        scoreLabel = new JLabel("score: 0");
        startGameButton = new JButton("start");
        endGameButton = new JButton("end");
        controlPanel = new JPanel();
        drawPanel = new JPanel();
        borderLayout = new BorderLayout();
        boxLayoutButtons = new BoxLayout(controlPanel, BoxLayout.Y_AXIS);
        setLayout(borderLayout);
        add(drawPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.EAST);
        controlPanel.setLayout(boxLayoutButtons);
        controlPanel.setPreferredSize(new Dimension(100, 480));
        controlPanel.add(scoreLabel);
        controlPanel.add(startGameButton);
        controlPanel.add(endGameButton);
        drawPanel.setBackground(Color.BLACK);
        drawPanel.setPreferredSize(new Dimension(540, 480));
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.setColor(Color.red);
        graphics.drawRect(18, 18, 504, 424);
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (board[i][j] == 0) {
                    graphics.setColor(Color.blue);
                    graphics.fillRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare);
                }
            }
        }
        for (int i = 2; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.green);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare
                        + startDrawPositionX, (i * sizeSquare) + startDrawPositionY);
            }

        }
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 2; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.green);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare,
                        (i * sizeSquare) + startDrawPositionY + sizeSquare);
            }
        }
    }
}
0
static int[][] board = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//2
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//3
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//4
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//5
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//6
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//7
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//8
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//9
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//10
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//11
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//12
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//13
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//14
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//15
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//16
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//17
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//18
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//19
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//20
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}//21
    };

A co jeżeli zapragniesz stworzyć większą plansze? Napiszesz drugą, albo trzecią? Użyj pętli.

0

chcę stworzyć klasy odpowiedzialne za następujące rzeczy: klasa odpowiedzialna za poruszanie się, klasa która losuje miejsce w którym będzie pojawiać się owoc. w klasie metoda będzie metoda która sprawdza czy wąż może poruszać się w danym kierunku, metoda która zwiększa zmienną wynik, metoda informująca o przegranej grze. proszę o inne propozycje ;) pozdrawiam

0

uzupełniłem kod o klasę losowania odpowiedniego miejsca dla owocu, owoc jest jest teraz rysowany po kliknieciu na przycisk start, chcę zrobić żeby owoc się rysował co 10 sekund lub co każde 10 sekund po zjedzeniu owoca.

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingSnake extends JFrame {

    /**
     * @param args the command line arguments
     */
    private int widthWindow = 640;
    private int heightWindow = 480;

    public JavaSwingSnake() {
        setSize(new Dimension(widthWindow, heightWindow));
        setTitle("snake !!!!!!!");
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JavaSwingSnake javaSwingSnake = new JavaSwingSnake();
                ClassDrawBoard classDrawBoard = new ClassDrawBoard();
                javaSwingSnake.add(classDrawBoard);
            }
        });
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class ClassDrawBoard extends JPanel implements ActionListener {

    ClassFruit classFruit = new ClassFruit();
    JLabel scoreLabel;
    JButton startGameButton;
    JButton endGameButton;
    BorderLayout borderLayout;
    BoxLayout boxLayout;
    BoxLayout boxLayoutButtons;
    JPanel controlPanel;
    JPanel drawPanel;
    private int sizeSquare = 20;//wielkosc kwadratu
    private int startDrawPositionX = 20;//poczatek rysowania
    private int startDrawPositionY = 20;//poczatek rysowania
    //oznaczenia cyfr w tablicy
    //0 - mur
    //1 - plansza po ktorej porusza sie waz
    //2 - owoc
    static int[][] board = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//2
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//3
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//4
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//5
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//6
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//7
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//8
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//9
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//10
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//11
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//12
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//13
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//14
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//15
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//16
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//17
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//18
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//19
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//20
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}//21
    };

    public ClassDrawBoard() {
        scoreLabel = new JLabel("score: 0");
        startGameButton = new JButton("start");
        endGameButton = new JButton("end");
        controlPanel = new JPanel();
        drawPanel = new JPanel();
        borderLayout = new BorderLayout();
        boxLayoutButtons = new BoxLayout(controlPanel, BoxLayout.Y_AXIS);
        setLayout(borderLayout);
        add(drawPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.EAST);
        controlPanel.setLayout(boxLayoutButtons);
        controlPanel.setPreferredSize(new Dimension(100, 480));
        controlPanel.add(scoreLabel);
        controlPanel.add(startGameButton);
        controlPanel.add(endGameButton);
        drawPanel.setBackground(Color.BLACK);
        drawPanel.setPreferredSize(new Dimension(540, 480));

        endGameButton.setEnabled(false);
        startGameButton.addActionListener(this);
        endGameButton.addActionListener(this);
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.setColor(Color.red);
        graphics.drawRect(18, 18, 504, 424);
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (board[i][j] == 0) {
                    graphics.setColor(Color.blue);
                    graphics.fillRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare);
                }
                if (board[i][j] == 2) {
                    graphics.setColor(Color.green);
                    graphics.fillRoundRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare, 360, 360);
                }
            }
        }
        for (int i = 2; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare
                        + startDrawPositionX, (i * sizeSquare) + startDrawPositionY);
            }

        }
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 2; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare,
                        (i * sizeSquare) + startDrawPositionY + sizeSquare);
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object object = e.getSource();
        if (object == startGameButton) {
            startGameButton.setEnabled(false);
            endGameButton.setEnabled(true);
            classFruit.randomPositionFruit();
            classFruit.drawFruit();
            repaint();
        }
        if (object == endGameButton) {
            startGameButton.setEnabled(true);
            endGameButton.setEnabled(false);
        }
    }
    
    void resetGame(){}
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.util.Random;

/**
 *
 * @author marcin
 * w klasie jest losowana pozycja na ktorej ma byc wstawiony owoc,
 * owoc jest oznaczony cyfra 2 i taka cyfra jest wstawiana w odpowiednie miejsce tabeli
 */
public class ClassFruit{
    
    Random random;
    int xPositionFruit;
    int yPositionFruit;

    public ClassFruit() {
        random = new Random();
    }
    
    void randomPositionFruit(){
        xPositionFruit = random.nextInt((ClassDrawBoard.board.length - 2)) + 1;
        yPositionFruit = random.nextInt((ClassDrawBoard.board[0].length - 2)) + 1;
        //System.out.println("xPositionFruit = " + xPositionFruit);
        //System.out.println("yPositionFruit = " + yPositionFruit);
    }
    
    void drawFruit(){
        ClassDrawBoard.board[xPositionFruit][yPositionFruit] = 2;
    }
}
0

klasa Timer.

0

witam, wstawiam dalszą część kodu mojego snake, teraz owoc pojawia się co 10 sekund i kilka innych drobnych rzeczy zostało zmienionych. proszę o opinie

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingSnake extends JFrame {

    /**
     * @param args the command line arguments
     */
    private int widthWindow = 640;
    private int heightWindow = 480;

    public JavaSwingSnake() {
        setSize(new Dimension(widthWindow, heightWindow));
        setTitle("snake !!!!!!!");
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JavaSwingSnake javaSwingSnake = new JavaSwingSnake();
                ClassDrawBoard classDrawBoard = new ClassDrawBoard();
                javaSwingSnake.add(classDrawBoard);
            }
        });
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author marcin
 */
public class ClassDrawBoard extends JPanel implements ActionListener {

    ClassFruit classFruit = new ClassFruit();
    JLabel scoreLabel;
    JButton startGameButton;
    JButton endGameButton;
    JButton jButtonExit;
    BorderLayout borderLayout;
    BoxLayout boxLayout;
    BoxLayout boxLayoutButtons;
    JPanel controlPanel;
    JPanel drawPanel;
    Timer timer;
    private int sizeSquare = 20;//wielkosc kwadratu
    private int startDrawPositionX = 20;//poczatek rysowania
    private int startDrawPositionY = 20;//poczatek rysowania
    //oznaczenia cyfr w tablicy
    //0 - mur
    //1 - plansza po ktorej porusza sie waz
    //2 - owoc
    static int[][] board = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//2
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//3
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//4
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//5
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//6
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//7
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//8
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//9
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//10
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//11
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//12
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//13
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//14
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//15
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//16
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//17
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//18
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//19
        {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//20
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}//21
    };

    public ClassDrawBoard() {
        scoreLabel = new JLabel("score: 0");
        startGameButton = new JButton("start");
        endGameButton = new JButton("end");
        jButtonExit = new JButton("exit");
        controlPanel = new JPanel();
        drawPanel = new JPanel();
        borderLayout = new BorderLayout();
        boxLayoutButtons = new BoxLayout(controlPanel, BoxLayout.Y_AXIS);
        setLayout(borderLayout);
        add(drawPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.EAST);
        controlPanel.setLayout(boxLayoutButtons);
        controlPanel.setPreferredSize(new Dimension(100, 480));
        controlPanel.add(scoreLabel);
        controlPanel.add(startGameButton);
        controlPanel.add(endGameButton);
        controlPanel.add(jButtonExit);
        drawPanel.setBackground(Color.BLACK);
        drawPanel.setPreferredSize(new Dimension(540, 480));

        endGameButton.setEnabled(false);
        startGameButton.addActionListener(this);
        endGameButton.addActionListener(this);
        jButtonExit.addActionListener(this);
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        drawBoard(graphics);
        drawSchedule(graphics);
    }

    private void drawBoard(Graphics graphics) {
        graphics.setColor(Color.red);
        graphics.drawRect(18, 18, 504, 424);
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (board[i][j] == 0) {
                    graphics.setColor(Color.blue);
                    graphics.fillRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare);
                }
                if (board[i][j] == 2) {
                    graphics.setColor(Color.green);
                    graphics.fillRoundRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare, 360, 360);
                }
            }
        }
    }

    private void drawSchedule(Graphics graphics) {
        for (int i = 2; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare
                        + startDrawPositionX, (i * sizeSquare) + startDrawPositionY);
            }

        }
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 2; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare,
                        (i * sizeSquare) + startDrawPositionY + sizeSquare);
            }
        }
    }
    ActionListener actionListenerDrawFruit = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            resetGame();
            classFruit.randomPositionFruit();
            classFruit.drawFruit();
            repaint();
        }
    };

    @Override
    public void actionPerformed(ActionEvent e) {
        Object object = e.getSource();
        if (object == startGameButton) {
            startGameButton.setEnabled(false);
            endGameButton.setEnabled(true);
            classFruit.randomPositionFruit();
            classFruit.drawFruit();
            repaint();
            timer = new Timer(10000, actionListenerDrawFruit);
            timer.start();
        }
        if (object == endGameButton) {
            startGameButton.setEnabled(true);
            endGameButton.setEnabled(false);
            resetGame();
            repaint();
            timer.stop();
        }
        if (object == jButtonExit) {
            System.exit(0);
        }
    }

    private void resetGame() {
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                board[i][j] = 1;
            }
        }
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.util.Random;

/**
 *
 * @author marcin
 * w klasie jest losowana pozycja na ktorej ma byc wstawiony owoc,
 * owoc jest oznaczony cyfra 2 i taka cyfra jest wstawiana w odpowiednie miejsce tabeli
 */
public class ClassFruit{
    
    Random random;
    int xPositionFruit;
    int yPositionFruit;

    public ClassFruit() {
        random = new Random();
    }
    
    void randomPositionFruit(){
        xPositionFruit = random.nextInt((ClassDrawBoard.board.length - 2)) + 1;
        yPositionFruit = random.nextInt((ClassDrawBoard.board[0].length - 2)) + 1;
        System.out.println("xPositionFruit = " + xPositionFruit);
        System.out.println("yPositionFruit = " + yPositionFruit);
    }
    
    void drawFruit(){
        ClassDrawBoard.board[xPositionFruit][yPositionFruit] = 2;
    }
}
0

Moim zdaniem tragedia. Po kolei:

  1. WYWAL tą tablicę board i zrób ją jak człowiek
  2. Zasada jednej odpowiedzialności. Jeśli klasa jest panelem to NIE JEST jednocześnie actionListenerem! Zrób OSOBNE action listenery i podepnij je pod guziki a nie baw się w jakieś ify z d**y w listenerze
  3. Przeczytaj co to MVC i się zastosuj. U ciebie wszystko jest pomieszane.
0

Witam, po dłuższej przerwie postanowiłem się wziąć za siebie i kontynuować pisanie snake-a. Wstawiam moje kolejne wypociny i proszę o sugestie odnośnie kodu. Mam też kilka pytań odnośnie rysowania węża,teraz owoc rysuje się co 5 sekund, a w wersji finalnej chcę aby rysował się co 5 sekund lub po każdym zjedzeniu. Moje pytanie brzmi jak zsynchronizować rysowanie się owocu z poruszaniem węża który będzie się np poruszać z prędkością jedną kratkę na sekundę czy te wątki nie będą się gryzły?
Jeżeli chodzi o węża to będzie go tworzył interfejs ArrayList: głowę i ogon osobno a jako argument jest klasa Coordinate z polami: x, y, numberPartBody: głowa to 3 a ogon 4.
Nie mogę wyobrazić sobie rysowania węża. Wąż ma być rysowany pętli for i w skrócie pętla przelatując przez całą tablice jeżeli trafi na cyfrę oznaczającą część ciała węża to wtedy będzie rysowała go. Ale jak jeszcze to połączyć z ArrayList?? ArrayList i tablica dwu wymiarowa z planszą maja być podczas iteracji porównywane?? Proszę o pomoc bo nie mogę sobie tego wyobrazić
Pozdrawiam

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingSnake extends JFrame {

    /**
     * @param args the command line arguments
     */
    private int widthWindow = 640;
    private int heightWindow = 480;

    public JavaSwingSnake() {
        setSize(new Dimension(widthWindow, heightWindow));
        setTitle("snake !!!!!!!");
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JavaSwingSnake javaSwingSnake = new JavaSwingSnake();
                ClassDrawBoard classDrawBoard = new ClassDrawBoard();
                javaSwingSnake.add(classDrawBoard);
            }
        });
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class ClassDrawBoard extends JPanel {

    SnakeListeners snakeListeners = new SnakeListeners();
    JLabel scoreLabel;
    static JButton startGameButton;
    static JButton endGameButton;
    static JButton jButtonExit;
    BorderLayout borderLayout;
    BoxLayout boxLayout;
    BoxLayout boxLayoutButtons;
    JPanel controlPanel;
    JPanel drawPanel;
    private int sizeSquare = 20;//wielkosc kwadratu
    private int startDrawPositionX = 20;//poczatek rysowania
    private int startDrawPositionY = 20;//poczatek rysowania
    int defaultXPositionHeadSnake = 10;
    int defaultYPositionHeadSnake = 10;
    //oznaczenia cyfr w tablicy
    //0 - mur
    //1 - plansza po ktorej porusza sie waz
    //2 - owoc
    //3 - glowa weza
    //4 - ogon weza
    static final int numberOfGridCellsHeight = 21;
    static final int numberOfGridCellsWidth = 25;
    static int[][] board = new int[numberOfGridCellsHeight][numberOfGridCellsWidth];

    public ClassDrawBoard() {
        scoreLabel = new JLabel("score: 0");
        startGameButton = new JButton("start");
        endGameButton = new JButton("end");
        jButtonExit = new JButton("exit");
        controlPanel = new JPanel();
        drawPanel = new JPanel();
        borderLayout = new BorderLayout();
        boxLayoutButtons = new BoxLayout(controlPanel, BoxLayout.Y_AXIS);
        setLayout(borderLayout);
        add(drawPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.EAST);
        controlPanel.setPreferredSize(new Dimension(100, 480));
        controlPanel.add(scoreLabel);
        controlPanel.add(startGameButton);
        controlPanel.add(endGameButton);
        controlPanel.add(jButtonExit);
        drawPanel.setBackground(Color.BLACK);
        drawPanel.setPreferredSize(new Dimension(540, 480));

        fillBoard();

        endGameButton.setEnabled(false);
        startGameButton.addActionListener(snakeListeners.actionListenerButtonStart);
        endGameButton.addActionListener(snakeListeners.actionListenerButtonEnd);
        jButtonExit.addActionListener(snakeListeners.actionListenerButtonExit);
    }

    private void fillBoard() {
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (i == 0) {
                    board[i][j] = 0;
                } else if (i == board.length - 1) {
                    board[i][j] = 0;
                } else if (j == 0) {
                    board[i][j] = 0;
                } else if (j == board[0].length - 1) {
                    board[i][j] = 0;
                } else {
                    board[i][j] = 1;
                }
            }
        }
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        drawBoard(graphics);
        drawSchedule(graphics);
    }

    private void drawBoard(Graphics graphics) {
        graphics.setColor(Color.red);
        graphics.drawRect(18, 18, 504, 424);
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (board[i][j] == 0) {
                    graphics.setColor(Color.blue);
                    graphics.fillRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare);
                }
                if (board[i][j] == 2) {
                    graphics.setColor(Color.green);
                    graphics.fillRoundRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare, 360, 360);
                }
            }
        }
    }

    private void drawSchedule(Graphics graphics) {
        for (int i = 2; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare
                        + startDrawPositionX, (i * sizeSquare) + startDrawPositionY);
            }

        }
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 2; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare,
                        (i * sizeSquare) + startDrawPositionY + sizeSquare);
            }
        }
        repaint();
    }

    static void resetGame() {
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                board[i][j] = 1;
            }
        }
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.util.Random;

/**
 *
 * @author marcin w klasie jest losowana pozycja na ktorej ma byc wstawiony
 * owoc, owoc jest oznaczony cyfra 2 i taka cyfra jest wstawiana w odpowiednie
 * miejsce tabeli
 */
public class ClassFruit {

    Random random;
    int xPositionFruit;
    int yPositionFruit;

    public ClassFruit() {
        random = new Random();
    }

    void randomPositionFruit() {
        xPositionFruit = random.nextInt((ClassDrawBoard.board.length - 2)) + 1;
        yPositionFruit = random.nextInt((ClassDrawBoard.board[0].length - 2)) + 1;
        System.out.println("xPositionFruit = " + xPositionFruit);
        System.out.println("yPositionFruit = " + yPositionFruit);
    }

    void drawFruit() {
        ClassDrawBoard.board[xPositionFruit][yPositionFruit] = 2;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.util.ArrayList;

/**
 *
 * @author marcin
 */
public class Snake {
   
    ArrayList<Coordinate> arrayListHead;
    ArrayList<Coordinate> arrayListTail;
   
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author marcin
 */
public class SnakeListeners  {

    ClassFruit classFruit = new ClassFruit();
    Timer timer;
   
    ActionListener actionListenerDrawFruit = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ClassDrawBoard.resetGame();
            classFruit.randomPositionFruit();
            classFruit.drawFruit();
        }
    };
   
    ActionListener actionListenerButtonStart = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object object = e.getSource();
           
            if (object == ClassDrawBoard.startGameButton) {
                ClassDrawBoard.startGameButton.setEnabled(false);
                ClassDrawBoard.endGameButton.setEnabled(true);
                classFruit.randomPositionFruit();
                classFruit.drawFruit();
                timer = new Timer(5000, actionListenerDrawFruit);
                timer.start();
            }
        }
    };
   
    ActionListener actionListenerButtonEnd = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object object = e.getSource();

            if (object == ClassDrawBoard.endGameButton) {
                ClassDrawBoard.startGameButton.setEnabled(true);
                ClassDrawBoard.endGameButton.setEnabled(false);
                ClassDrawBoard.resetGame();
                timer.stop();
            }
        }
    };
    ActionListener actionListenerButtonExit = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object object = e.getSource();

            if (object == ClassDrawBoard.jButtonExit) {
                System.exit(0);
            }
        }
    };
} 
0

witam, napisałem Klasę Snake aby zobaczyć jak będzie wyglądało poruszanie węża z równoczesnym rysowaniem się owocu. Wąż porusza się co sekundę o jedno miejsce w tablicy a owoc rysuje się co 2 sekundy. namiastka głowy węza w tej chwili porusza się tylko w jednym kierunku, ale działa dobrze.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author marcin
 */
public class Snake {

    int defaultXPositionHeadSnake = 10;
    int defaultYPositionHeadSnake = 10;
    Timer timer;

    public Snake() {
        ClassDrawBoard.board[defaultXPositionHeadSnake][defaultYPositionHeadSnake] = 3;
        timer = new Timer(1000, actionListener);
        timer.start();
    }

        ActionListener actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(ClassDrawBoard.board[defaultXPositionHeadSnake - 1][defaultYPositionHeadSnake] == 1){
                    ClassDrawBoard.board[defaultXPositionHeadSnake - 1][defaultYPositionHeadSnake] = 3;
                    ClassDrawBoard.board[defaultXPositionHeadSnake][defaultYPositionHeadSnake] = 1;
                    defaultXPositionHeadSnake--;
                }
            }
        };
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author marcin
 */
public class SnakeListeners {

    ClassFruit classFruit = new ClassFruit();
    Timer timer;
    
    ActionListener actionListenerDrawFruit = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ClassDrawBoard.resetGame();
            classFruit.randomPositionFruit();
            classFruit.drawFruit();
            
        }
    };
    
    ActionListener actionListenerButtonStart = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object object = e.getSource();
            
            if (object == ClassDrawBoard.startGameButton) {
                ClassDrawBoard.startGameButton.setEnabled(false);
                ClassDrawBoard.endGameButton.setEnabled(true);
                classFruit.randomPositionFruit();
                classFruit.drawFruit();
                new Snake();
                timer = new Timer(2000, actionListenerDrawFruit);
                timer.start();
            }
        }
    };
    
    ActionListener actionListenerButtonEnd = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object object = e.getSource();

            if (object == ClassDrawBoard.endGameButton) {
                ClassDrawBoard.startGameButton.setEnabled(true);
                ClassDrawBoard.endGameButton.setEnabled(false);
                ClassDrawBoard.resetGame();
                timer.stop();
            }
        }
    };
    ActionListener actionListenerButtonExit = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object object = e.getSource();

            if (object == ClassDrawBoard.jButtonExit) {
                System.exit(0);
            }
        }
    };
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsnake;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class ClassDrawBoard extends JPanel {

    SnakeListeners snakeListeners = new SnakeListeners();
    JLabel scoreLabel;
    static JButton startGameButton;
    static JButton endGameButton;
    static JButton jButtonExit;
    BorderLayout borderLayout;
    BoxLayout boxLayout;
    BoxLayout boxLayoutButtons;
    JPanel controlPanel;
    JPanel drawPanel;
    private int sizeSquare = 20;//size square
    private int startDrawPositionX = 20;//begin position drawing
    private int startDrawPositionY = 20;//begin position drawing
    //legends:
    //0 - wall
    //1 - board on witch the snake moves
    //2 - fruit
    //3 - head snake
    //4 - tail snake
    static final int numberOfGridCellsHeight = 21;
    static final int numberOfGridCellsWidth = 25;
    static int[][] board = new int[numberOfGridCellsHeight][numberOfGridCellsWidth];

    public ClassDrawBoard() {
        scoreLabel = new JLabel("score: 0");
        startGameButton = new JButton("start");
        endGameButton = new JButton("end");
        jButtonExit = new JButton("exit");
        controlPanel = new JPanel();
        drawPanel = new JPanel();
        borderLayout = new BorderLayout();
        boxLayoutButtons = new BoxLayout(controlPanel, BoxLayout.Y_AXIS);
        setLayout(borderLayout);
        add(drawPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.EAST);
        controlPanel.setPreferredSize(new Dimension(100, 480));
        controlPanel.add(scoreLabel);
        controlPanel.add(startGameButton);
        controlPanel.add(endGameButton);
        controlPanel.add(jButtonExit);
        drawPanel.setBackground(Color.BLACK);
        drawPanel.setPreferredSize(new Dimension(540, 480));

        fillBoard();

        endGameButton.setEnabled(false);
        startGameButton.addActionListener(snakeListeners.actionListenerButtonStart);
        endGameButton.addActionListener(snakeListeners.actionListenerButtonEnd);
        jButtonExit.addActionListener(snakeListeners.actionListenerButtonExit);
    }

    private void fillBoard() {
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (i == 0) {
                    board[i][j] = 0;
                } else if (i == board.length - 1) {
                    board[i][j] = 0;
                } else if (j == 0) {
                    board[i][j] = 0;
                } else if (j == board[0].length - 1) {
                    board[i][j] = 0;
                } else {
                    board[i][j] = 1;
                }
            }
        }
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        drawBoard(graphics);
        drawSchedule(graphics);
    }

    private void drawBoard(Graphics graphics) {
        graphics.setColor(Color.red);
        graphics.drawRect(18, 18, 504, 424);
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (board[i][j] == 0) {
                    graphics.setColor(Color.blue);
                    graphics.fillRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare);
                }
                if (board[i][j] == 2) {
                    graphics.setColor(Color.green);
                    graphics.fillRoundRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare, 360, 360);
                }
                if (board[i][j] == 3) {
                    graphics.setColor(Color.red);
                    graphics.fillRoundRect((sizeSquare * j) + startDrawPositionX,
                            (sizeSquare * i) + startDrawPositionY, sizeSquare,
                            sizeSquare, 360, 360);
                }
            }
        }
        repaint();
    }

    private void drawSchedule(Graphics graphics) {
        for (int i = 2; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare
                        + startDrawPositionX, (i * sizeSquare) + startDrawPositionY);
            }

        }
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 2; j < board[0].length - 1; ++j) {
                graphics.setColor(Color.darkGray);
                graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
                        + sizeSquare, (j * sizeSquare) + sizeSquare,
                        (i * sizeSquare) + startDrawPositionY + sizeSquare);
            }
        }
        repaint();
    }

    static void resetGame() {
        for (int i = 1; i < board.length - 1; ++i) {
            for (int j = 1; j < board[0].length - 1; ++j) {
                board[i][j] = 1;
            }
        }
    }
}
0

Hmmm, straszna dyamika gry, możesz zastosować wątki by płyniej się poruszał wąż. Ogólnie kod dla mnie wygląda troche strasznie - możesz zrobić mały diagram klas, i na podstawie diagramu, przeformatowac aktualne klasy.

0

ja bym rysował wszystko na buffered image a później zrzucał wszystko na ekran czy tam panel w zależności na jakiej powierzchni rysujesz.

0

a ja chcę się skupić bardziej na tym wężu ponieważ rysowanie działa.

1 użytkowników online, w tym zalogowanych: 0, gości: 1