迷宫发电机和播放器请帮助我

时间:2015-06-01 14:43:52

标签: java swing jframe jpanel maze

我已经创建了一个随机迷宫生成器游戏,并希望在迷宫中有一个小块移动。目前我遇到的问题是,当我将两个对象添加到JFrame时,只显示输入的最后一个,另一个被覆盖。我曾经尝试过使用JPanels,但我似乎无法让它工作,有人可以解决我的问题吗?

以下是我对游戏的看法:

这会创建并控制播放器:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class keyer extends JPanel implements ActionListener, KeyListener 
{
//This Creates and controls the player
Timer times = new Timer(5, this);
double x = 0;
double y = 0;
double movx = 0;
double movy = 0;
// flag for if left key is down
public boolean left_key_down = false;
// flag for if right key is down
public boolean right_key_down = false;
// flag for if up key is down
public boolean up_key_down = false;
// flag for if down key is down
public boolean down_key_down = false;

public keyer() 
{
    times.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(true);
}

// Draws/Paints
public void paintComponent(Graphics g) 
{

    super.paintComponent(g);
    Graphics2D gg = (Graphics2D) g;
    Image img = Toolkit.getDefaultToolkit().createImage("clear.jpg");
    gg.drawImage(img, 0, 0, null);
    gg.fill(new Rectangle.Double(x, y, 20, 20));
}

public void actionPerformed(ActionEvent input) 
{
    if (x > 1259) 
    {
        movx = 0;
        x = 1259;
    }
    if (y < 0) 
    {
        movy = 0;
        y = 0;
    }
    if (y > 940) 
    {
        movy = 0;
        y = 940;
    }
    if (x < 0) {
        movx = 0;
        x = 0;
    }

    x = x + movx;
    y = y + movy;
    repaint();
}

// the keys (up is negative down is positive)
public void up() 
{
    movy = -3;
    movx = 0;
}

public void down() 
{
    movy = 3;
    movx = 0;
}

public void left() 
{
    movx = -3;
    movy = 0;
}

public void right() 
{
    movx = 3;
    movy = 0;
}

public void upleft() 
{
    movx = -3;
    movy = -3;
}

public void downleft() 
{
    movx = -3;
    movy = 3;
}

public void upRight() 
{
    movx = 3;
    movy = -3;
}

public void downRight() 
{
    movx = 3;
    movy = 3;
}

// this sets up how you know which key is used
public void keyPressed(KeyEvent input) 
{
    int code = input.getKeyCode();

    if (code == KeyEvent.VK_RIGHT) 
    {
        right_key_down = true;
    } 
    else if (code == KeyEvent.VK_LEFT) 
    {
        left_key_down = true;
    } 
    else if (code == KeyEvent.VK_UP) 
    {
        up_key_down = true;
    } 
    else if (code == KeyEvent.VK_DOWN) 
    {
        down_key_down = true;
    }

    if (up_key_down == true && left_key_down == true) 
    {
        upleft();
    } 
    else if (down_key_down == true && left_key_down == true)
    {
        downleft();
    } 
    else if (up_key_down == true && right_key_down == true)
    {
        upRight();
    } 
    else if (down_key_down == true && right_key_down == true)
    {
        downRight();
    } 
    else 
    {
        if (code == KeyEvent.VK_UP) 
        {
            up();
        }
        if (code == KeyEvent.VK_DOWN) 
        {
            down();
        }
        if (code == KeyEvent.VK_RIGHT) 
        {
            right();
        }
        if (code == KeyEvent.VK_LEFT) 
        {
            left();
        }
    }
}

public void keyTyped(KeyEvent key) 
{

}

public void keyReleased(KeyEvent key) 
{
    int keys = key.getKeyCode();

    if (keys == KeyEvent.VK_RIGHT) 
    {
        right_key_down = false;
    } else if (keys == KeyEvent.VK_LEFT) 
    {
        left_key_down = false;
    } else if (keys == KeyEvent.VK_UP) 
    {
        up_key_down = false;
    } else if (keys == KeyEvent.VK_DOWN) 
    {
        down_key_down = false;
    }

    movx = 0;
    movy = 0;
}

}

以下类生成迷宫:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.*;

public class Maze extends JPanel 
{
private Room[][] mazerooms;
private ArrayList<Wall> walls;
private Random randomnum;
private int height;
private int width;
private int num;
private JoinRoom join;
private int AreaX;
private int AreaY;
private int roomSize;
private int random;

public Maze(int height, int width) 
{
    this.height = height;
    this.width = width;
    mazerooms = new Room[height][width];
    walls = new ArrayList<Wall>((height) * (width));
    generateRandomMaze();
    setPreferredSize(new Dimension(800, 700));
}

private void generateRandomMaze() 
{
    generateInitialRooms();


    join = new JoinRoom(width * height);

    randomnum = new Random();

    num = width * height;

    while (num > 1) 
    {
        random = randomnum.nextInt(walls.size());

        Wall temp = walls.get(random);

        int roomone = temp.currentRoom.y + temp.currentRoom.x * width;
        int roomtwo = temp.nextRoom.y + temp.nextRoom.x * width;

        //This checks to see if roomone and roomtwo are taken already
        if (join.find(roomone) != join.find(roomtwo)) 
        {
            walls.remove(random);

            join.unionRooms(join.find(roomone), join.find(roomtwo));

            temp.confirmGone = true;

            temp.currentRoom.nextTo.add(temp.nextRoom);

            temp.nextRoom.nextTo.add(temp.currentRoom);
            num--;
        }
    }
}

private int roomNumber = 0;

private void generateInitialRooms() 
{
    for (int a = 0; a < height; a++) 
    {
        for (int b = 0; b < width; b++) 
        {
            // create up walls
            mazerooms[a][b] = new Room(a, b);

            if (a == 0) {
                mazerooms[a][b].up = new Wall(mazerooms[a][b]);
            } 
            else 
            {
                mazerooms[a][b].up = new Wall(mazerooms[a - 1][b], mazerooms[a][b]);
                walls.add(mazerooms[a][b].up);
            }
            if (a == height - 1) 
            {
                mazerooms[a][b].down = new Wall(mazerooms[a][b]);
            }
            if (b == 0) 
            {
                mazerooms[a][b].left = new Wall(mazerooms[a][b]);
            } 
            else 
            {
                mazerooms[a][b].left = new Wall(mazerooms[a][b - 1], mazerooms[a][b]);
                walls.add(mazerooms[a][b].left);
            }
            if (b == width - 1) 
            {
                mazerooms[a][b].right = new Wall(mazerooms[a][b]);
            }
            mazerooms[a][b].roomName = roomNumber++;
        }
    }

    mazerooms[0][0].left.confirmGone = true;

    mazerooms[0][0].roomName = 0;

    mazerooms[height - 1][width - 1].down.confirmGone = true;

    mazerooms[height - 1][width - 1].roomName = (height * width);
}

public void paintComponent(Graphics g) 
{
    AreaX = 5;
    AreaY = 5;
    roomSize = (width - AreaX) / width + 50;

    // temp variables used for painting
    int x = AreaX;
    int y = AreaY;
    for (int a = 0; a <= height - 1; a++) 
    {
        for (int b = 0; b <= width - 1; b++) 
        {
            if (!(mazerooms[a][b].up.confirmGone)) 
            {
                g.drawLine(x, y, x + roomSize, y);
            }
            if (mazerooms[a][b].left.confirmGone == false) 
            {
                g.drawLine(x, y, x, y + roomSize);
            }
            if ((a == height - 1) && mazerooms[a][b].down.confirmGone == false) 
            {
                g.drawLine(x, y + roomSize, x + roomSize, y + roomSize);
            }
            if ((b == width - 1) && mazerooms[a][b].right.confirmGone == false) 
            {
                g.drawLine(x + roomSize, y, x + roomSize, y + roomSize);
            }
            x += roomSize;
        }
        x = AreaX;
        y += roomSize;
    }
}

}

public class Wall 
{
public Room currentRoom, nextRoom;
public boolean confirmGone = false;

public Wall(Room a, Room b) 
{
    currentRoom = a;
    nextRoom = b;
}

public Wall(Room r) 
{
    currentRoom = r;
    nextRoom = null;
}

}

import java.util.*;

public class Room 
{
public Wall up, right, down, left;
public int x, y;
public int roomName;
public Room prev;
public List<Room> nextTo;

public Room(int x, int y) 
{
    this.x = x;
    this.y = y;
    nextTo = new LinkedList<Room>();
    prev = null;
    roomName = 0;
}

public int getRoomName() 
{
    return roomName++;
}

}

public class JoinRoom 
{
private int[] set;

public JoinRoom(int num) 
{
    set = new int[num];

    for (int i = 0; i < set.length; i++) 
    {
        set[i] = -1;
    }
}
public void unionRooms(int roomnum1, int roomnum2) 
{
    if (set[roomnum1] > set[roomnum2]) 
    {
        set[roomnum1] = roomnum2;
    }
    else
    {
        if (set[roomnum2] == set[roomnum1]) 
        {
            set[roomnum1]--;
        }
        set[roomnum2] = roomnum1;
    }
}   

public int find(int rec) 
{
    if (set[rec] < 0) 
    {
        return rec;
    } 
    else 
    {
        return set[rec] = find(set[rec]);
    }
}

}

最后,这是主类:

import javax.swing.*;

public class Main 
{
public static void main(String[] args) 
{
    JFrame frame = new JFrame();
    Maze one = new Maze(17, 25);
    keyer Key = new keyer();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setTitle("2D Maze Game");
    frame.add(one);
    frame.add(Key);
    frame.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:1)

  

当我将两个对象添加到JFrame时,只显示输入的最后一个,另一个被覆盖

JFrame的内容窗格使用BorderLayout - 添加组件而不指定默认位置添加到CENTER,它只能包含一个组件。一些选择:

  1. 使用其他LayoutManager
  2. 将它们添加到JPanel(使用相应的布局),然后将其添加到JFrame
  3. 将它们添加到不同的位置。
  4. 例如,使用(3):

    frame.add(one);//adds to CENTER
    frame.add(Key, BorderLayout.SOUTH);