冒险类的基于文本的冒险游戏
在我的游戏中,我写了几个课程,包括房间,灯,胸部,Java,播放器,键和地图。 这些都经过了测试,是正确的,所以现在我正在写我的冒险课,这是该计划的驱动程序。 我需要将玩家房间的位置设置为[0] [0],我无法弄清楚如何。 这是我到目前为止在我的房间和冒险课上所拥有的。
public class Adventure {
Scanner in = new Scanner(System.in);
private Room room;
public Adventure() {
Player player = new Player();
Map map = new Map();
player.setX(0);
player.setY(0);
int x = 0;
int y = 0;
map.getRoom(x, y).getDescription();
}
}
public class Room {
private String description;
private boolean north;
private boolean south;
private boolean east;
private boolean west;
private boolean isDark;
private Lamp theLamp;
private Key theKey;
private Chest theChest;
/**
* Returns the text description of this room
*/
public String getDescription() {
return description;
}
/**
* Returns true if the player can go north from this room
*/
public boolean canGoNorth() {
return north;
}
/**
* Returns true if the player can go south from this room
*/
public boolean canGoSouth() {
return south;
}
/**
* Returns true if the player can go east from this room
*/
public boolean canGoEast() {
return east;
}
/**
* Returns true if the player can go west from this room
*/
public boolean canGoWest() {
return west;
}
/**
* Returns the lamp object in this room.
* If no lamp is present, returns null
*/
public Lamp getLamp() {
return theLamp;
}
/**
* Sets the lamp variable in this room to null
*/
public void clearLamp() {
theLamp = null;
}
/**
* Returns the key object in this room.
* If no key is present, returns null
*/
public Key getKey() {
return theKey;
}
/**
* Sets the key variable in this room to null
*/
public void clearKey() {
theKey = null;
}
/**
* Returns the chest object in this room.
* If no chest is present, returns null
*/
public Chest getChest() {
return theChest;
}
/**
* Returns true if there is no light in this room,
* veeeeeeeery dangerous!
*/
public boolean isDark() {
return isDark;
}
/**
* Hey wassup dawg? I'ma constructor. I make the objects round these parts,
* sometimes without even trying, knowwhatimssayin?
* Yall don't haveta worry 'bout me for this'ere game, but look me up in
* Chapter 6 sometime. Kay?
*
*/
public Room(String description, boolean north, boolean south, boolean east,
boolean west, boolean isDark, Lamp theLamp, Key theKey,
Chest theChest) {
super();
this.description = description;
this.north = north;
this.south = south;
this.east = east;
this.west = west;
this.isDark = isDark;
this.theLamp = theLamp;
this.theKey = theKey;
this.theChest = theChest;
}
}
我必须将房间位置设置为0,0,以便地图类中的描述将打印出来。
你已经有了Room对象,那么真的有必要存储每个房间的X / Y坐标吗? 您可以创建一个Room对象的结构,它们通过North / South / East / West Room对象相互链接(如果适用)。
然后,您可以在玩家类中添加一个变量,例如“currentRoom”或“location”。 包含一个函数来获取和设置此位置,您现在可以设置和访问角色的当前位置。
如果X / Y对你来说真的很重要,那么我猜你可以在冒险类中创建一个2维的Room对象数组,然后使用X / Y坐标来定位该结构中的房间(即“x”可能指向到行,“y”表示定位存储在数组中的位置的列)。
然而,当你走的时候它可能更容易,更少浪费,并且更容易扩展来创建Room对象,并通过exit / Room指针将它们链接到其他房间对象。
public class Player {
private Room location = null;
public void setLocation(Room newLocation) {
location = newLocation;
}
}
public class Room {
private Room NorthExit = null;
public Room getNorthExit() {
return NorthExit;
}
public setNorthExit(Room newRoom) {
NorthExit = newRoom;
}
}
// In main somewhere...
Room room1 = new Room();
Room room2 = new Room();
room1.setNorthExit(room2);
Player player1 = new Player();
player1.setLocation(room1);
您可以在上面的代码中看到我们创建并链接两个房间,如果我们位于第一个房间,则用户可以将room1设置为其位置,并从其位置对象访问关于room1的所有信息。 我们还可以通过位置对象/引用为每个出口访问我们的位置对象中的任何相邻房间。
链接地址: http://www.djcxy.com/p/5499.html