Ruo Tao Zhang
Ruo Tao Zhang

Reputation: 1

Java Snake Game Body doesn't grow

Currently I'm studying Computer Science and my teacher want me to make a snake game with array.

I have this code that is exactly same as my friend's but it only grow one body length and won't grow longer and after it eats the food it starts to slow down the speed of moving snake. I'm not sure where I went wrong please help. Thank you.

Here's the code:

public class Main extends JPanel implements KeyListener, ActionListener {
    private static final long serialVersionUID = 1L;
    static int dir;
    static int i;
    static int x[] = new int[200]; // Decleare Array of snake on x coordinate
    static int y[] = new int[200]; // Decleare Array of snake on y coordinate
    static int taillength = 1;
    static int sxinc = 20, syinc = 20; // Speed of moving snake
    static int fx = 100, fy = 100; // Declare the position of where food at
    static int f2x = 300, f2y = 300; // Declare the position of where food2 at
    static int fmx = 300, fmy = 100; // Declare the position of where food3 at
    static int score = 0; // Create Score Counter
    static int width = 745, height = 489; // Declare the size of JPanel
    static int nsx, nsy; // The new value of the snake movement
    static int csx = 20, csy = 20; // The value to add/minus on the number to
    static BufferedImage background = null;
    static JFrame f;
    static JFrame g;

    public Main() {
        addKeyListener(this);
    }

    public void addNotify() {
        super.addNotify();
        requestFocus();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, width, height, this);
        g.setColor(Color.GREEN);
        g.fillRect(fx, fy, 20, 20);
        g.setFont(new Font("Times New Roman", Font.BOLD, 15));
        g.setColor(Color.GREEN);
        g.drawString("GREEN - Add 1 body length, add 1 score", 0, 429);
        g.setColor(Color.BLUE);
        g.fillRect(f2x, f2y, 20, 20);
        g.setFont(new Font("Times New Roman", Font.BOLD, 15));
        g.setColor(Color.BLUE);
        g.drawString("BLUE - Add 2 body length, add 2 score", 0, 444);
        g.setColor(Color.CYAN);
        g.fillRect(fmx, fmy, 20, 20);
        g.setFont(new Font("Times New Roman", Font.BOLD, 15));
        g.setColor(Color.CYAN);
        g.drawString("CYAN - Minus 1 body length, add 1 score", 0, 459);
        g.setColor(Color.RED);
        for (int j = 0; j < x.length && j < taillength; j++) {
            g.fillRect(x[j], y[j], 20, 20);
            g.setColor(Color.ORANGE);
        }
        g.fillRect(x[0], y[0], 20, 20);
        g.setColor(Color.RED);
        g.setFont(new Font("Times New Roman", Font.BOLD, 25));
        g.setColor(Color.WHITE);
        g.drawString("Score : " + score, 305, 459);

    }

    public void snakenew() {
        for (int i = 0; i < x.length; i++) {
            x[i] = 0;
            y[i] = 0;
        }
    }

    public static void main(String a[]) {
        x[0] = 300;
        y[0] = 220;
        try { // Import Background
            background = ImageIO.read(new File("H:/shutterstock_12730534.jpg"));
        } catch (IOException e) {
        }
        Main p = new Main();
        g = new JFrame();
        g.add(p);
        g.setSize(200, 300);
        g.setVisible(true);
        g.setResizable(false);
        f = new JFrame();
        f.add(p);
        f.setSize(width, height);
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Timer t = new Timer(60, p);
        t.start();
    }

    public void actionPerformed(ActionEvent e) {
        if ((x[0] + 20 > width) || (x[0] < 0) || (y[0] + 40 > height)
                || (y[0] < 0)) { // Game over when hit the wall
            JOptionPane.showMessageDialog(null, "You hit the wall!", "Game",
                    JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }

        if ((taillength > 1) && (x[i] != x[0]) && (y[i] != y[0])) { // Game over
                                                                    // when
                                                                    // touch you
                                                                    // snake
                                                                    // body
            if ((x[0] == x[i]) & (y[0] == y[i])) {
                JOptionPane.showMessageDialog(null, "You ran into yourself!",
                        "Game", JOptionPane.INFORMATION_MESSAGE);
            }
        }

        if (dir == KeyEvent.VK_UP) {
            if (y[0] == y[taillength]) {
                y[0] = y[0] - syinc;
            }
        }

        else if (dir == KeyEvent.VK_DOWN) {
            if (y[0] == y[taillength]) {
                y[0] = y[0] + syinc;
            }
        }

        else if (dir == KeyEvent.VK_LEFT) {
            if (x[0] == x[taillength]) {
                x[0] = x[0] - sxinc;
            }
        }

        else if (dir == KeyEvent.VK_RIGHT) {
            if (x[0] == x[taillength]) {
                x[0] = x[0] + sxinc;
            }
        }
        if (dir == KeyEvent.VK_K) {
            if ((score > 6) && (taillength > 5)) {
                taillength = taillength - 5;
                score = score - 7;
            }
        }

        if ((x[0] == fx) && (y[0] == fy)) { // Food Score and random food
            fx = (int) (Math.random() * 37) * 20;
            fy = (int) (Math.random() * 25) * 20;
            taillength++;
            score++;
        }

        if ((x[0] == f2x) && (y[0] == f2y)) {
            f2x = (int) (Math.random() * 37) * 20;
            f2y = (int) (Math.random() * 25) * 20;
            taillength = taillength + 2;
            score = score + 2;
        }

        if ((x[0] == fmx) && (y[0] == fmy)) {
            if (taillength > 0) {
                fmx = (int) (Math.random() * 37) * 20;
                fy = (int) (Math.random() * 25) * 20;
                taillength--;
                score++;
            }
        }

        for (i = taillength; i > 0; i--) {
            x[i] = x[(i - 1)];
            y[i] = y[(i - 1)];
        }

        f.repaint();
    }

    public void keyPressed(KeyEvent ke) {
        dir = ke.getKeyCode();
    }

    public void keyReleased(KeyEvent arg0) {
    }

    public void keyTyped(KeyEvent arg0) {
    }
}

Upvotes: 0

Views: 465

Answers (1)

Zesty Memes
Zesty Memes

Reputation: 442

A few things:

  • You are using a timer, but the item you are putting in the timer has no run() method... That's not how a timer works (please reference the last point as to why).
  • You are redrawing the whole screen every single time you tick. Not only is that ridiculous, it is most likely the cause of your aforementioned lag when you grow the body. Fixing this will ensure that you experience little to now lag between growth (although, you will still need to compensate at later growths by changing the speed of the snake; this, will also make the game more difficult as you go on, just like real Snake). This usage of the paint() method, can be attributed to the same reasoning as the last point.
  • You took someone elses code. Don't use code that doesn't belong to you--it might work, and you're fine, or you might have the same bug as the other guy, and now you've got a great time explaining why you have the copied code of some other student.

In conclusion: if you want to borrow code, never borrow code from someone who is in the same course as you. Finally, look up some examples of Snake games in Java. I'm sure you'll find some people who have experienced similar problems, from whom you might learn. I hope this helps you, and best of luck!

Upvotes: 2

Related Questions