文字冒险游戏,增加时间和游戏结束

时间:2014-03-10 09:33:33

标签: java

我正在制作一个简单的文字冒险游戏,但我遇到了两个小问题..

问题1:我想制作一个结束游戏目标..一旦玩家到达某个房间,游戏结束......但我似乎无法得到if语句正确..

问题2:我想制定一个时间限制(步骤)让我们说用户将完成游戏的30个步骤..所以对于他进入的每个房间,计数器将获得+1 ..并且一旦他达到30他会松开的步骤。

我有一个创建房间及其位置的createRooms方法

private void createRooms()
{
    Room gate, graveyard, church, crypt, entrance, hall, kitchen, buttery, greathall, staircase,
        dungeon, topstaircase, throne, solar, wardrobe, privy;

    // create the rooms
    gate = new Room("outside the old gate of the castle");
    graveyard = new Room("on a wind-swept gaveyard");
    church = new Room("in a small ancient church with medieval windows");
    crypt = new Room("in the crypt of the church");
    entrance = new Room("at the big wooden entrance of the castle");
    hall = new Room("in the dark entrance hall of the castle");
    kitchen = new Room("in the kitchen with a huge table and a big stove");
    buttery = new Room("in the buttery of the castle");
    greathall = new Room("in the great hall of the castle with its magnificient huge windows");
    staircase = new Room("at the staircase");
    dungeon = new Room("in the dark dungeon of the castle");
    topstaircase = new Room("at the top of the staircase");
    throne = new Room("in the throne room with golden walls");
    solar = new Room("in the solar of the castle");
    wardrobe = new Room("in the wardroble of the Lord of the castle");
    privy = new Room("in the privy");

    // initialise room exits

    gate.setExit("north", graveyard);

    graveyard.setExit("south", gate);
    graveyard.setExit("east", church);
    graveyard.setExit("north", entrance);

    church.setExit("west", graveyard);
    church.setExit("south", crypt);

    crypt.setExit("north", church);

    entrance.setExit("south", graveyard);
    entrance.setExit("north", hall);

    hall.setExit("south", graveyard);
    hall.setExit("west", kitchen);
    hall.setExit("north", greathall);
    hall.setExit("east", staircase);

    kitchen.setExit("east", hall);
    kitchen.setExit("south", buttery);

    buttery.setExit("north", kitchen);

    greathall.setExit("south", hall);

    staircase.setExit("west", hall);
    staircase.setExit("down", dungeon);
    staircase.setExit("up", topstaircase);

    topstaircase.setExit("down", staircase);
    topstaircase.setExit("north", throne);
    topstaircase.setExit("south", solar);

    throne.setExit("south", topstaircase);

    solar.setExit("north", topstaircase);
    solar.setExit("west", wardrobe);
    solar.setExit("east", privy);

    wardrobe.setExit("east", solar);

    privy.setExit("west", solar);

    currentRoom = gate;  // start game at gate
}

这是我试过的

if(room == throne) {
    System.out.println("Congratulations you have won the game!");
    System.out.println("Press any key then enter to continue")
    System.exit(); 
}

我还不确定如何在祝贺消息之后暂停,并让用户按任意键然后输入,然后关闭游戏。

至于时代..不知道从哪里开始..我认为我需要写一些像:

For(Every goRoom)
int a +1
When(int a ==30)
System.out.println("You have Lost the game")
then the game exits or starts the game..

这是正确的吗?

1 个答案:

答案 0 :(得分:0)

对于您的问题,这不是一个完整的答案,因为您的程序运行似乎存在一些问题:首先执行此操作,然后您可以决定您为胜利和用户交互做些什么。

您的Room变量仅在createRooms方法的范围内定义,这似乎是错误的。您需要在班级中全局定义它们(尝试移动行

Room gate, graveyard, church, crypt, entrance, hall, kitchen, buttery, greathall, staircase, dungeon, topstaircase, throne, solar, wardrobe, privy;

在方法之外(并使它们成为private,除非你需要从外部访问变量,否则将变量设为私有总是好的:它会保留封装。)

此外,如果您想计算用户步数,则需要一个变量来保存您班级中的计数。

private Room gate, graveyard, church, crypt, entrance, hall, kitchen, buttery, greathall, staircase, dungeon, topstaircase, throne, solar, wardrobe, privy = null;
private int steps = 0

private void createRooms() {
  gate = new Room("outside the old gate of the castle");
  graveyard = new Room("on a wind-swept gaveyard");
  ...

然后你可以从任何一个类方法调用:

if (room.equals(throne)) {
  // any of your victory printout

这至少会让你走上正确的道路。

我还建议您不要将System.out.println语句直接放在主类中,您应该使用可插入接口(依赖注入)将游戏机制(您的主类)和用户界面的关注点分开。

例如,您可以定义类似interface UserOutput的内容,其中包含onVictoryonRoomChange等方法,然后使用class ConsoleUserOutput来实现System.out.println ConsoleUserOutput 1}}所有这些方法中的语句。并且您在创建时在主类中注入UserOutput(以便主类只知道接口{{1}})。更好的是,您可以通过这种方式插入所有用户交互:等待他按键等等。