程序生成的房间(与艾萨克的绑定一样)

时间:2018-04-09 18:57:18

标签: java eclipse procedural

我正在制作一个简单的程序生成的地牢,你的角色会在随机方向随机打开或关闭的房间里产生。这已经完成了。

当角色离开房间时,它将为新房间生成新的随机数。存储旧房间的编号,如果角色向后移动,则生成带有这些编号的房间。这部分还没有完成,但很容易做到,但我还不想这样做,因为它会导致下面的问题。

无论玩家在哪里旅行并知道何时使用变量,我怎样才能创建可以存储的内容?我怎么能自动创建这样的变量?

当前代码:

public class Executable extends JFrame implements KeyListener {
Container contentPane=this.getContentPane();
Graphics bufferGraphics;

int xAxis; //Universal Variables:
int yAxis;
int characterX=463;
int characterY=486;
int oldCharacterX=463;
int oldCharacterY=486;
Image characterNorth = CustomImages.createImageIcon("Images/characterNorth.jpg").getImage();
Image characterEast = CustomImages.createImageIcon("Images/characterEast.jpg").getImage();
Image characterSouth = CustomImages.createImageIcon("Images/characterSouth.jpg").getImage();
Image characterWest = CustomImages.createImageIcon("Images/characterWest.jpg").getImage();
Image brickWall = CustomImages.createImageIcon("Images/brickWall.jpg").getImage();
Image brickFloor = CustomImages.createImageIcon("Images/brickFloor.jpg").getImage();
Image character=characterNorth;
boolean pressed=false;
static boolean northDoor;
static boolean eastDoor;
static boolean southDoor;
static boolean westDoor;
static boolean Randomizer=true;
static int north;
static int east;
static int south;
static int west;
static Random r=new Random();
boolean doorOpen=false;

public static void main(String[] args) { //Main
    north=r.nextInt(3)+1;
    east=r.nextInt(3)+1;
    south=r.nextInt(3)+1;
    west=r.nextInt(3)+1;
    if(north==1) {
        northDoor=true;
    }else {
        northDoor=false;
    }
    if(east==1) {
        eastDoor=true;
    }else {
        eastDoor=false;
    }
    if(south==1) {
        southDoor=true;
    }else {
        southDoor=false;
    }
    if(west==1) {
        westDoor=true;
    }else {
        westDoor=false;
    }
    Executable e=new Executable();
}

public Executable() { //Canvas
    this.setBounds(0, 0, 1680, 1050);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(this);
    setFocusable(true);
    requestFocusInWindow();
}

public void paint(Graphics g) { //Paint 

    for(xAxis=58;xAxis<=858;xAxis=xAxis+50) {
        for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
            g.drawImage(brickFloor,xAxis,yAxis,null);
        }
        yAxis=31;
    }
    for(xAxis=8;xAxis<958;xAxis=xAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    yAxis=931;
    for(xAxis=8;xAxis<=908;xAxis=xAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    xAxis=8;
    for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    xAxis=908;
    for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }

    while(!doorOpen) {
        if(northDoor==false && eastDoor==false && southDoor==false && westDoor==false) {
            north=r.nextInt(3)+1;
            east=r.nextInt(3)+1;
            south=r.nextInt(3)+1;
            west=r.nextInt(3)+1;
            if(north==1) {
                northDoor=true;
            }else {
                northDoor=false;
            }
            if(east==1) {
                eastDoor=true;
            }else {
                eastDoor=false;
            }
            if(south==1) {
                southDoor=true;
            }else {
                southDoor=false;
            }
            if(west==1) {
                westDoor=true;
            }else {
                westDoor=false;
            }
        }else {
            doorOpen=true;
        }
    }

    if(northDoor) {
        g.drawImage(brickFloor,458,31,null);
    }
    if(eastDoor) {
        g.drawImage(brickFloor,908,481,null);
    }
    if(southDoor) {
        g.drawImage(brickFloor,458,931,null);
    }
    if(westDoor) {
        g.drawImage(brickFloor,8,481,null);
    }

    g.drawImage(character,characterX,characterY,null);
}

@Override
public void keyPressed(KeyEvent arg0) { //Character rotation and movement.
    if(pressed==false) {
        pressed=true;
        if(arg0.getKeyCode() == KeyEvent.VK_W){
            if(character==characterNorth) {
                if(characterY>86 && characterX>13 && characterX<913) {
                    characterY=characterY-50;
                }else if(northDoor && characterX==463) {
                    oldCharacterY=characterY;
                    characterY=characterY-50;
                }
            }else {
                character=characterNorth;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_A){
            if(character==characterWest && characterY>36 && characterY<926) {
                if(characterX>63) {  //and y is greater than and less than
                    oldCharacterX=characterX;
                    characterX=characterX-50;
                }else if(westDoor && characterY==486) {
                    oldCharacterX=characterX;
                    characterX=characterX-50;
                }
            }else {
                character=characterWest;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_S){
            if(character==characterSouth) {
                if(characterY<871 && characterX>13 && characterX<913) {
                    oldCharacterY=characterY;
                    characterY=characterY+50;
                }else if(southDoor && characterX==463) {
                    oldCharacterY=characterY;
                    characterY=characterY+50;
                }
            }else {
                character=characterSouth;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_D){
            if(character==characterEast && characterY>36 && characterY<926) {
                if(characterX<848) {  //and y is greater than and less than
                    oldCharacterX=characterX;
                    characterX=characterX+50;
                }else if(eastDoor && characterY==486) {
                    oldCharacterX=characterX;
                    characterX=characterX+50;
                }
            }else {
                 character=characterEast;
            }
        }
        repaint(oldCharacterX,oldCharacterY,40,40);
        repaint(characterX,characterY,40,40);
        }
    }

@Override
public void keyReleased(KeyEvent arg0) { //Prevents keys from being held down.
    if(arg0.getKeyCode() == KeyEvent.VK_W){
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_A){
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_S){
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_D){
        pressed=false;
    }
}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

  }
}

2 个答案:

答案 0 :(得分:0)

如果您希望存储与每个房间相关联的随机数,然后向后遍历,按顺序,通过随机数,堆栈是您要查找的数据结构。要获得对堆栈的概念性理解,请参阅Stack (abstract data type)。简而言之,堆栈就像一堆纸,其中放置在堆顶部的最后一个项目是在遍历纸张时从堆中移除的第一个元素,称为后进先出(LIFO)排序。

在Java中,Deque(双端队列,发音为&#34; deck&#34;)可以用作堆栈。要将元素放在堆栈的顶部,使用push方法并从堆栈顶部删除元素,使用pop方法(请注意pop都获得堆栈顶部的值并将其从堆栈中删除)。例如:

Deque<Integer> stack = new ArrayDeque<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop());
stack.push(30);
System.out.println(stack.pop());
System.out.println(stack.pop());

此代码将产生以下输出:

20
30
10

要封装此功能,您可以创建一个名为RoomTracker的新类(或类似名称以表示跟踪前一个房间的操作),并按如下方式实现:

public class RoomTracker {

    private final Deque<Integer> rooms = new ArrayDeque<>();

    public void addRoomNumber(Integer roomNumber) {
        rooms.push(roomNumber);
    }

    public Integer getPreviousRoomNumber() {
        return rooms.pop();
    }
}

以与以前类似的方式,您可以使用以下代码:

RoomTracker tracker = new RoomTracker();
tracker.addRoomNumber(10);
tracker.addRoomNumber(20);
System.out.println(tracker.getPreviousRoomNumber());
tracker.addRoomNumber(30);
System.out.println(tracker.getPreviousRoomNumber());
System.out.println(tracker.getPreviousRoomNumber());

这将产生以下输出:

20
30
10

如果您需要跟踪您进入房间的方向(这样您就可以找到玩家是否要前往某个房间),您可以创建一个名为Room的类并存储随机生成的数字,玩家进入的方向:

public enum Direction {
    TOP, RIGHT, BOTTOM, LEFT;
}

public class Room {

    private final int number;
    private final Direction enteredFrom;

    public Room(int number, Direction enteredFrom) {
        this.number = number;
        this.enteredFrom = enteredFrom;
    }

    public int getNumber() {
        return number;
    }

    public Direction getEnteredFrom() {
        return enteredFrom;
    }
}

然后,您可以按RoomTracker

重新定义Room
public class RoomTracker {

    private final Deque<Room> rooms = new ArrayDeque<>();

    public void addRoom(Room room) {
        rooms.push(room);
    }

    public Room getPreviousRoom() {
        return rooms.pop();
    }
}

然后,当玩家从一个房间移动到另一个房间时,您可以将Room个对象推入堆叠。例如:

RoomTracker tracker = new RoomTracker();
tracker.push(new Room(10, Direction.LEFT);   // Travel right into room 10
tracker.push(new Room(20, Direction.TOP);    // Travel down into room 20

答案 1 :(得分:0)

希望这会有所帮助。我建议你有一个游戏状态,它处理绘制,移动,关键事件,如你当前的Executable类。

以下内容的基本概述。 GameState引用了currentRoomState这是您应该认为该角色所在的内容。移动到门后,您需要检查此内容,调用currentRoomState.MoveToNextRoom()适当的参数。

这将触发当前房间状态以生成新房间,设置对新房间的引用以绑定到正确的门,将字符放在新房间的正确门上,并引用旧房间在新房间里,将当前房间设置到新房间。

当你搬到一个有前一个房间的门时,变量将一直存在。

祝你好运

-Chris

例如:

class GameState{
            RoomState startingRoomState = new RoomState();
            RoomState currentRoomState = startingRoomState;

            public void move(int x, int y)
            {
                currentRoomState.iCharacterPosX += x;
                currentRoomState.iCharacterPosY += y;

                // Check if need to move to a new room (eg move north)
                currentRoomState = currentRoomState.MoveToNextRoom(true, false, false, false);
            }

            public void draw()
            {
                //Based on the character pos and draw variables in currentRoomState
            }
}

然后你有一个房间状态,描述了一个房间

class RoomState{
                //Where the doors are
                boolean northDoor = false;
                boolean eastDoor = false;
                boolean southDoor = false;
                boolean westDoor = false;

                // Reference to the room adjacent rooms
                RoomState rs_NorthDoor = null;
                RoomState rs_EastDoor = null;
                RoomState rs_SouthDoor = null;
                RoomState rs_WestDoor = null;

                int iCharacterPosX = 0;
                int iCharacterPosY = 0;

                // Initial
                RoomState(){
                    northDoor = true; eastDoor = true; southDoor = true; westDoor = true;
                }

                RoomState(RoomState prevState, boolean north, boolean east, boolean south, boolean west){
                    // Randomise door states

                    // Put character in the position he should be
                    iCharacterPosX = 0;//right most x Coord - oldX
                    iCharacterPosX = 0;//bottom most y Coord - oldy

                    if(north)
                    {
                        rs_NorthDoor = prevState;
                    } //elseif other doors
                }

                public RoomState MoveToNextRoom(boolean north, boolean east, boolean south, boolean west){
                    if(north){
                        if(rs_NorthDoor == null)
                        {
                            rs_NorthDoor = new RoomState(this, north, east, south, west);
                        }
                        return rs_NorthDoor;
                    } //else if the others

                    return null;
                }
            }