Java KeyPressed Event not recognised by seperate logic class

31 views Asked by At

Firstly, I apologize if this has been asked a lot. I have found a lot of solution possibilities, but I don't want to copy paste a whole approach, as I'm trying to understand what I'm doing.

I'm rather new to programming and trying out some things by creating a very basic snake game. So far, I have defined a couple of classes, of which "Board" creates the necessary graphical interface and handles refreshing, and "Game" that handles all the game logic and catches keyboard inputs.

My current classes:

package src.lecture._10_snake;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Game implements KeyListener {
    private static Snake snake;
    private Board board;
    public Game(Board board){
        snake = new Snake();
        this.board = board;
    }
    public void run(){
//        while(true){
//
//        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("keypressed event");
        int keyCode = e.getKeyCode();
        switch (keyCode) {
            case KeyEvent.VK_UP:
                // Move the snake up
                snake.moveUp();
                break;
            case KeyEvent.VK_DOWN:
                // Move the snake down
                snake.moveDown();
                break;
            case KeyEvent.VK_LEFT:
                // Move the snake left
                snake.moveLeft();
                break;
            case KeyEvent.VK_RIGHT:
                // Move the snake right
                snake.moveRight();
                break;
        }
        board.refresh();

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    public static void main(String[] args) {
        Board board = new Board(snake);
        Game game = new Game(board);
        board.addKeyListener(game);
        game.run();
    }
}

package src.lecture._10_snake;

import javax.swing.*;
import java.awt.*;


public class Board extends JPanel{
    private JFrame frame = new JFrame("Snake game");
    private JPanel panel = new JPanel();
    private final int height = 35;
    private final int width = 35;
    private Snake snake;

    public Board(Snake snake){
        this.snake = snake;
        panel.setLayout(new GridLayout(height, width));
        for (int i = 0; i < height*width; i++) {
            JLabel cell = new JLabel("x");
            panel.add(cell);
        }
        frame.add(panel);
        frame.setSize(800, 800); // Set the desired size
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close the application when the frame is closed
        frame.setVisible(true);

    }
    public void refresh(){
        int[] snakePosition = snake.getPosition();
        for (int i = 0; i < snakePosition.length; i++) {
            JLabel currentCell = (JLabel) panel.getComponent(snakePosition[i] * width + snakePosition[i+1]);
            currentCell.setText("o");
        }

        repaint();
    }
}

For now, I simply want to be able to trigger the "KeyPressed" method in "Game". I can't figure out what I'm doing wrong. I tried a bunch of things with window selection, but nothing seemed to work.

Can someone help me figure this out? Appreciate any help.

0

There are 0 answers