Для моего проекта кодирования я должен написать игру-лабиринт. Однако в настоящее время у меня есть лабиринт, отображаемый в JFrame, где игрок представлен красным квадратом, маркер конца - синим квадратом, а стены - черными квадратами. Но независимо от того, что я пытаюсь сделать, я не могу заставить игрока передвигаться по лабиринту.
package practiceMazeGame;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MazeBoard extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
// Creates the maze board using a 2D array.
public static int[][] maze =
{{3,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,0,1,1,1,1},
{1,0,1,1,0,0,0,1,1,1,0,1,1,1,1},
{1,0,1,1,0,1,0,1,0,1,0,0,0,0,0},
{0,0,0,1,0,1,0,1,0,1,1,1,1,1,1},
{0,1,1,0,0,1,0,1,0,1,0,0,0,0,1},
{0,1,1,0,1,1,0,1,0,1,0,1,1,0,1},
{0,0,0,0,1,1,0,1,0,1,0,1,0,0,1},
{0,1,1,1,1,1,0,1,0,1,0,1,0,1,1},
{0,0,0,1,1,1,0,1,0,1,0,1,0,0,0},
{0,1,0,1,0,0,0,1,0,1,0,1,1,1,0},
{0,1,1,1,0,1,1,1,0,1,0,0,0,1,0},
{0,0,0,1,0,0,0,0,0,1,0,1,1,1,0},
{0,1,1,1,1,1,1,1,0,1,0,1,1,1,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,1,2},
};
//Sets the output screen information
public MazeBoard() {
// Displays 'Maze Game' in the title bar of the window.
setTitle("Maze Game");
// Sets the size of the output screen.
setSize(640, 640);
// Sets the location of where the window will appear.
setLocationRelativeTo(null);
// Stops the program when the window is closed.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PlayerMovement mySquare = new PlayerMovement(640, 640);
add(mySquare);
}
// The function in charge of the graphics of the maze.
public void paint(Graphics g) {
super.paint(g);
g.translate(50, 50);
// They go through every section of the 2D array to make sure all the blocks are covered.
for (int row =0; row <maze.length; row++) {
for (int col = 0; col < maze[0].length; col++) {
Color color;
switch(maze[row][col]){
// Sets the colour of the blocks of the array with a one to be black.
case 1 : color = Color.black; break;
// Sets the colour villain to be red.
case 2 : color = Color.red; break;
// Sets the colour of the player to be blue.
case 3 : color = Color.blue; break;
// Sets the colour of the rest of the maze to be white.
default : color = Color.white; break;
}
// Sets the screen the for behind the maze.
g.setColor(color);
// Fills the rectangle needed for the maze with the colours previously decided.
g.fillRect(30 * col, 30 * row, 30, 30);
// Sets the colour of the gird lines for the maze.
g.setColor(Color.black);
// Draws the rectangle needed for the maze.
g.drawRect(30 * col, 30 * row, 30, 30);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Outputs the maze.
MazeBoard view = new MazeBoard();
view.setVisible(true);
}
});
}
}
Я был бы очень признателен за помощь, спасибо.