嗯,它还不是迷宫。我从绘制每个单元格开始
不要完全掌握你在尝试什么,但我认为一个单元格需要不同的颜色,而不是线条。因此,例如,白色单元格是玩家可以移动到的地方。黑色的细胞是一堵墙。不知道单条线怎么画迷宫?
我正在尝试创建一个 DFS 生成的迷宫。
不知道那是什么,但这里有一些旧代码可能会给你一些想法。
这是一个迷宫,您可以为每个单元格绘制图标(而不是我上面建议的颜色)。在这个基本实现中,您只有两种类型的图标:
- 一层楼
- 一堵墙
包含 0/1 的文件用于识别每个单元格,因此是单元格的图标
然后你就有了一个可以在地板之间移动的棋子,当它撞到墙上时会被挡住。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SmallWorld extends JPanel implements KeyListener, ActionListener
{
//WORLD
String[][] worldArray = new String[30][30];
//Block colors
private static Color redBlock = new Color(255,0,0,255);
private static Color whiteBlock = new Color(255,255,255,255);
private static Color blackBlock = new Color(0,0,0,150);
//World images
// JLabel wall = new JLabel( new ImageIcon("wall.gif") );
// JLabel floor = new JLabel( new ImageIcon("floor.gif") );
ImageIcon wallIcon = new ImageIcon("dukewavered.gif");
ImageIcon floorIcon = new ImageIcon("copy16.gif");
//Starting coordinates
int blockPositionX = 1;
int blockPositionY = 1;
//Typing area to capture keyEvent
JTextField typingArea;
//Viewable area
JViewport viewable;
String type = "";
JLayeredPane layeredPane;
JPanel worldBack;
JPanel panel;
JPanel player;
Dimension worldSize = new Dimension(1500, 1500);
public SmallWorld() {
super( );
createWorld();
layeredPane = new JLayeredPane();
add(layeredPane);
layeredPane.setPreferredSize( worldSize );
worldBack = new JPanel();
layeredPane.add(worldBack, JLayeredPane.DEFAULT_LAYER);
worldBack.setLayout( new GridLayout(30, 30) );
worldBack.setPreferredSize( worldSize );
worldBack.setBounds(0, 0, worldSize.width, worldSize.height);
worldBack.setBackground( whiteBlock );
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
JPanel square = new JPanel( new BorderLayout() );
worldBack.add( square );
type = worldArray[i][j];
if ( type.equals("1") )
{
square.add( new JLabel(wallIcon) );
}
else
{
square.add( new JLabel(floorIcon) );
}
}
}
//Draw the player
player = new JPanel();
player.setBounds(50, 50, 50, 50);
player.setBackground( Color.black );
player.setLocation(50, 50);
layeredPane.add(player, JLayeredPane.DRAG_LAYER);
//set where the player starts
// panel = (JPanel)worldBack.getComponent( 31 );
// panel.add( player );
//Create the typing area with keyListener, add to window
typingArea = new JTextField(20);
typingArea.addKeyListener(this);
typingArea.setEditable( false );
add(typingArea);
}
//Get the world from the DAT file and translate it into
//a 2d array to be used by paintComponent
public void createWorld() {
String getData = null;
int countRow = 0;
try {
//Get file
FileReader fr = new FileReader("world.dat");
BufferedReader br = new BufferedReader(fr);
getData = new String();
//read each line from file, store to 2d array
while ((getData = br.readLine()) != null) {
if(countRow < 30) {
for (int i = 0; i < 30; i++) {
String temp = "" + getData.charAt(i);
worldArray[countRow][i] = temp;
}
countRow++;
}
}
} catch (IOException e) {
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
}
//Move Block around the world
public void moveBlock() {
Point pt = new Point();
pt.x = blockPositionX * 50;
pt.y = blockPositionY * 50;
int x = Math.max(0, pt.x - 250);
int y = Math.max(0, pt.y - 250);
Rectangle r = new Rectangle(x, y, 550, 550);
scrollRectToVisible( r );
}
//check for collisions with blocks
public boolean checkCollision(int row, int col) {
if ( worldArray[col][row].equals("1") ) {
return true;
}
else {
return false;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
updateView( e );
e.consume();
}
public void keyReleased(KeyEvent e) {
}
public void actionPerformed(ActionEvent e) {
}
//Update the view of the window based on which button is pressed
protected void updateView( KeyEvent e ) {
//if UP
if ( e.getKeyCode() == 38 ) {
if ( checkCollision( blockPositionX, blockPositionY - 1 ) == false ) {
blockPositionY = blockPositionY - 1;
moveBlock();
player.setLocation(blockPositionX *50, blockPositionY*50);
}
}
//if DOWN
if ( e.getKeyCode() == 40 ) {
if ( checkCollision( blockPositionX, blockPositionY + 1 ) == false ) {
blockPositionY = blockPositionY + 1;
moveBlock();
player.setLocation(blockPositionX *50, blockPositionY*50);
}
}
//if LEFT
if ( e.getKeyCode() == 37 ) {
if ( checkCollision( blockPositionX - 1, blockPositionY ) == false ) {
blockPositionX = blockPositionX - 1;
moveBlock();
player.setLocation(blockPositionX *50, blockPositionY*50);
}
}
//if RIGHT
if ( e.getKeyCode() == 39 ) {
if ( checkCollision( blockPositionX + 1, blockPositionY ) == false ) {
blockPositionX = blockPositionX + 1;
moveBlock();
player.setLocation(blockPositionX *50, blockPositionY*50);
}
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SmallWorld");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new SmallWorld();
newContentPane.setPreferredSize( new Dimension(1500, 1500) );
JScrollPane scrollPane = new JScrollPane( newContentPane );
scrollPane.setPreferredSize( new Dimension(568, 568) );
frame.getContentPane().add( scrollPane );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setResizable( false );
//frame.setSize(500,520);
frame.setVisible( true );
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
您将需要几个小图标来表示地板/墙壁。
您还需要一个文件来表示迷宫。这是一个示例“world.dat”文件:
111111111111111111111111111111
100010001010001000101000100011
100010001010001000101000100011
100011101010000000001000000001
100010000010000000001000000001
100110000010000000001000000001
100000000010000000001000000001
111100011110000000001000000001
100000000110001110111000111011
100000000000001110111000111011
100000000000000000000000000001
100010001110000000000000000001
100010001110001110111000111011
100011101100000000000000000011
100010000110001110111000111011
100110000100000000000000000011
100000000110000000001000000001
111100011100000000000000000011
100000000100000000000000000011
100000000010000000000000000001
100000000010000000000000000001
100010000000000000000000000001
100010000000001110111000111011
100011101110000000011000000001
100010000110000000011000000001
100110000110000000001000000001
100000000110001110111000111011
111100011110000000011000000001
100000000110000000011000000001
111111111111111111111111111111