Good day. There is a code for a small labyrinth, I try to transfer a player’s movement through the maze from the RealMaze class of the keyPressed method to the Player class, but I ran into a problem. The transmitted variable x changes, but returns unchanged and as a result of movement to the right does not occur. What needs to be changed?
import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * В этой программе осуществляется движение по лабиринту. * Передвигаться нужно с помощью стрелок. */ public class RealMaze implements KeyListener { //Начальные координаты игрока, public static int X = 0; public static int Y = 0; JButton button; MyDrawPanel myDrawPanel; JFrame frame; Player player = new Player(); public static void main(String[] args) throws Exception { RealMaze realMaze = new RealMaze(); realMaze.go(); } public void go()throws Exception { //Отрисовка окна frame = new JFrame(); myDrawPanel = new MyDrawPanel(); button = new JButton(); frame.setSize(500, 560); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(BorderLayout.CENTER, myDrawPanel); frame.getContentPane().add(BorderLayout.SOUTH, button); frame.add(new JLabel(new ImageIcon("C:/Users/IP44/Downloads/thinWall2.png"))); button.addKeyListener(this); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { try { int key = e.getKeyCode(); if (e.getKeyCode() == KeyEvent.VK_RIGHT) { player.moveRight(map, X, Y); System.out.println("RealMaze x " + X); } if (map[X][Y] == 0 && e.getKeyCode() == KeyEvent.VK_UP) { Y--; if (map[X][Y] == 2) { Y--; } } if (map[X][Y] == 0 && e.getKeyCode() == KeyEvent.VK_DOWN) { Y++; if (map[X][Y] == 2) { Y++; } } frame.repaint(); } catch (Exception ex) {ex.printStackTrace();} } @Override public void keyReleased(KeyEvent e) { } class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.red); // Размер стороны квадрата. int sideLength = getSideLength(); // Размер квадрата с учетом размера окна и размера массива. g.fillRect(X*sideLength, Y*sideLength, sideLength, sideLength); g.setColor(Color.gray); } private int getSideLength() { // Минимальная сторона компоненты. int minComponentSide = (this.getHeight() > this.getWidth()) ? this.getWidth() : this.getHeight(); // Максимальная сторона карты. int maxMapSide = (map.length > map[0].length) ? map.length : map[0].length; // Длина стороны, чтобы карта уместилась и комнаты были квадратные. return minComponentSide / maxMapSide; } } //Карта передвижения, 1 - стена, 2 - тонкая стена int[][] map = new int[][]{ {0,0,2,0,0,0,0}, //x {0,0,2,0,0,0,0}, //| {0,0,1,0,0,0,0}, //| {0,0,1,1,2,2,2}, //| {0,0,0,0,0,0,0}, //| {0,0,0,0,0,0,0}, //| {0,0,0,0,0,0,0} //| // - - - - - y }; } class Player { int moveRight(int[][] map, int x, int y) { System.out.println("Player x before " + x); if (map[x][y] == 0) { x++; if (map[x][y] == 2) { x++; } } System.out.println("Player x after " + x); return map[x][y]; } }