Java迷宫游戏玩家动作不正常

问题描述 投票:-1回答:1

对于我的编码项目,我必须编写一个迷宫游戏。但是,我目前在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);
        }
    });
}
}

非常感谢您的帮助。

java jframe maze
1个回答
0
投票

对您的建议:

  • 将玩家的位置存储在数组中(作为3值)是较差的设计:您应该仅将其用于迷宫设计并将玩家的位置存储在单独的变量中

  • 您的着色开关语句可以首先检查所绘制的位置是否与玩家的位置相匹配,以确定蓝色方块应在的位置。或者,您也可以在绘制迷宫之后绘制该正方形。

  • 移动应该是很简单的:检查新位置是否没有墙壁后,只需更改玩家的位置变量即可。]

© www.soinside.com 2019 - 2024. All rights reserved.