我希望这对于你们来说不是太业余,但是我很难用Java使用对象创建一个小型的基于文本的游戏。到目前为止,我已经为Player,Item(这将供以后使用,现在我有更简单的目标),Room,Inventory(再次供以后使用)和Main类编写了类。我应该使用什么来跟踪自己的位置?我想像在Zork中那样来回移动(例如向北,向南等),我曾想过使用一个包含每个位置的ArrayList,但这又使我更加困惑。到目前为止,我写的是:
class Player{
//int healthPoints; for later use
private String playerName;
public void setPlayerName(String playerNameParam)
{
playerName=playerNameParam;
}
public String getPlayerName(){
return playerName;
}
}
class Item{
private String itemName;
public void setItemName(String itemNameParam)
{
itemName=itemNameParam;
}
public String getItemName()
{
return itemName;
}
}
class ExitRoom{
}
class Room{
private String roomName;
public void setRoomName(String roomNameParam){
roomName=roomNameParam;
}
public String getRoomName(){
return roomName;
}
private String roomDescription;
public void setRoomDescription(String roomDescriptionParam){
roomDescription=roomDescriptionParam;
}
public String getRoomDescription(){
return roomDescription;
}
}
class Inventory{
private ArrayList<Item> items= new ArrayList<Item>();
public boolean findItem(String itemToFind)
{
for(int i=0;i<items.size();i++){
if(items.get(i).getItemName()==itemToFind){
return true;
}
}
return false;
}
}
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Player player = new Player();
boolean gameRunning=true;
while(gameRunning){
System.out.println("Welcome to TextBasedGamev1!"
+ "Before beginning, please enter your name");
String name=scanner.nextLine();
player.setPlayerName(name);
Room forestWelcome=new Room();
Room forestSouth=new Room();
Room forestNorth=new Room();
Room abandonedHouse=new Room();
}
任何帮助都非常感谢!
最佳答案
通常,方法是使用每个单元格和一个点的矩阵来标记玩家的位置,例如
Class Map {
private Room[][] matrix;
private Player player;
...
}
class Player{
private Point position;
....
}