【问题标题】:Java Maze Game Player Movement Not WorkingJava 迷宫游戏玩家移动不起作用
【发布时间】:2020-01-28 20:54:04
【问题描述】:

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

非常感谢您的帮助,谢谢。

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow!与其发布迷宫的所有代码,不如发布你用来尝试允许玩家移动的代码的 sn-p 会更好。
  • PlayerMovement 类是您创建的自定义类吗?
  • 您用来移动播放器的代码在哪里?

标签: java swing jframe maze


【解决方案1】:

给你的建议:

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

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

  • 移动应该很简单:只需在检查新位置没有墙后更改玩家的位置变量

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    相关资源
    最近更新 更多