Java swing snake pomoc w poprawieniu kodu

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);

            }

        }

    }

}

[/code]

Według Wujka Boba miło by było, gdybyś sporą i złożoną metodę paint(Graphics graphics) podzielił na kilka mniejszych. Metoda powinna wykonywać jedną, konkretną i ściśle związaną z nazwą czynność. Idealna ma jakieś trzy linijki, dobra mieści się na ekranie (a kiedyś na ekranie mieściło się jakieś 8 wierszy :p).

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 :wink: pozdrawiam

google.com -> snake game class diagram

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;

    }

}[/code]




-- [b]Dodane 01.04.2013 (Pn) 21:13[/b] --



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

[code] /* * 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 !!