0

noob here trying to make a simple snake game. followed along with tutorial on youtube and my code mayches the working code as best i can tell...snake not responding to commands, seems KeyListener not working. printed out requestFocusInWindow in jpanel constructor to make sure it was in focus and got back false, even though i entered setFocusable(true) asfirst arg in panel constructor.

please help me figure out why snake not responding to movement commands

package otherSnake;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;

public class Panel extends JPanel implements Runnable {

    public static final int width = 800, height = 800;
    private boolean running = false;
    private Thread thread;

    private Bodypart b;
    private ArrayList<Bodypart> snake;

    private Apple apple;
    private ArrayList<Apple> apples;

    private Random r;

    private int size = 5;
    private int x = 10, y = 10;

    private boolean right = true, left = false, up = false, down = false;

    private int ticks = 0;
    private Key key;

    public Panel() {
        setFocusable(true);

        key = new Key();
        addKeyListener(key);
        setPreferredSize(new Dimension(width, height));

        r = new Random();
        snake = new ArrayList<Bodypart>();
        apples = new ArrayList<Apple>();
        start();

    }

    public void tick() {
        if (snake.size() == 0) {
            b = new Bodypart(x, y, 10);
            snake.add(b);
        }

        if (apples.size() == 0) {
            int xCord = r.nextInt(79);
            int yCord = r.nextInt(79);

            apple = new Apple(xCord, yCord, 10);
            apples.add(apple);
        }


        ticks++;

        if (ticks > 250000) {
            if (right)
                x++;
            if (left)
                x--;
            if (up)
                y--;
            if (down)
                y++;

            ticks = 0;

            b = new Bodypart(x, y, 10);
            snake.add(b);

            if (snake.size() > size) {
                snake.remove(0);
            }
        }
    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, width, height);
        g.setColor(Color.BLACK);
        for (int i = 0; i < width / 10; i++) {
            g.drawLine(i * 10, 0, i * 10, height);
        }
        for (int i = 0; i < height / 10; i++) {
            g.drawLine(0, i * 10, width, i * 10);
        }
        for (int i = 0; i < snake.size(); i++) {
            snake.get(i).draw(g);

        }
        for (int i = 0; i < apples.size(); i++) {
            apples.get(i).draw(g);

        }
    }

    public void start() {
        running = true;
        thread = new Thread(this, "Game Loop");
        thread.start();

    }

    public void stop() {
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (running) {
            tick();
            repaint();

        }

    }
    private class Key implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_RIGHT ) {
                up = false;
                down = false;
                right = true;
                //left=false;
            }
            if (key == KeyEvent.VK_LEFT  ) {
                up = false;
                down = false;
                left = true;
                //right=false;
            }
            if (key == KeyEvent.VK_UP ) {
                up = true;
                down = false;
                left = false;
                right = false;
            }
            if (key == KeyEvent.VK_DOWN  ) {
                up = false;
                down = true;
                left = false;
                right = false;
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

    }
}
TongChen
  • 1,414
  • 1
  • 11
  • 21
  • You might want to press a key other than one of those 4 and 'println' its own keyCode, as that might help you narrow down the problem (on my Mac I am currently encountering the issue with those 4 keys, but not with others, such as, for example, the letter 't'). – Albert Turri May 06 '20 at 21:35
  • I am also wondering if this other question may be of help: https://stackoverflow.com/questions/53095491/java-snake-game-apple-showing-while-snake-is-invisible – Albert Turri May 06 '20 at 21:45
  • Its better to work with the ActionMap/InputMap. I think you can find plenty of examples. Here is one https://stackoverflow.com/a/60882049/2067492 – matt May 07 '20 at 05:45
  • https://stackoverflow.com/questions/8482268/java-keylistener-not-called – matt May 07 '20 at 05:48
  • Also you don't show a call to `requestFocus` or `requestFocusInWindow()` either of which should be called after the display has been shown. And you would check it by using `isFocusOwner()` which would show if your JPanel has focus. – matt May 07 '20 at 05:53

0 Answers0