im trying to figure out why the key input work randomly,

        random = new Random();
        this.score = score;
        this.borderHeight = borderHeight;
        this.borderWidth = borderWidth;

        setPreferredSize(new Dimension(this.borderWidth, this.borderHeight));
        setBackground(Color.black);

        snakeHead = new Tile(5 * tileSize, 5 * tileSize);
        snakeBody = new ArrayList<>();
        Food = new Tile(10, 10);

        placeFood();

        velX = 0;
        velY = 0;
        gameLoop = new Timer(124, this);

        setFocusable(true);
        addKeyListener(this);

         gameLoop.start();
        setFocusable(true);
Was this page helpful?