2015-12-15 67 views
-1

我目前正在尝试进行文本冒险,但我在房间导航方面遇到困难。总共4 x 4 - 16间客房。就游戏的界面和整体故事而言,它仍处于重大建构之中,我只是为了获得功能而编写代码。我正在计划使用门和锁定任务,但我必须确保我可以先正确导航。JavaScript文本冒险室导航

我的问题是,游戏在第12个房间开始,导航西部工作得很好,就像导航东部或西部在第12个房间西部的房间中工作,等等。但是,如果玩家向北行进,则导航到与地图上完全不同的房间。

This is a map of the game just for reference.

这是地图的游戏只为参考的。从第12个房间,玩家应该能够输入“北部”并直接进入第8个房间。相反,它似乎进入了第九个房间。往东走就能成功进入第13个房间。

我想要北方和南方的工作。似乎只有我的东西方导航正在工作。

下面是完整的代码:

<!doctype html> 
<title>House</title> 

<img src="" width="300" height="267"> 
<p id="output"></p> 
<input id="input" type="text" placeholder="Enter your action…"> 
<button>enter</button> 

<script> 

//Create the map 
var map = []; 

map[0] = "Graveyard"; 
map[1] = "Coffin Room"; 
map[2] = "Monster Party"; 
map[3] = "Spacious Room"; 
map[4] = "Burned Room"; 
map[5] = "Ghost Room"; 
map[6] = "Empty Room"; 
map[7] = "Catacombs"; 
map[8] = "Cluttered Room"; 
map[9] = "Spider Room"; 
map[10] = "Dressing Room"; 
map[11] = "Front Door and Entrance Hall"; 
map[12] = "Closet"; 
map[13] = "Very Dark Room"; 
map[14] = "Kitchen"; 
map[15] = "Gate"; 

// create a two dimensional array to store path movement for each room 
// [ NORTH, SOUTH, EAST, WEST ] 
var mapPath = []; 
mapPath[0] = [false, true, true, false]; 
mapPath[1] = [false, true, true, true]; 
mapPath[2] = [false, true, true, true]; 
mapPath[3] = [false, true, false, true]; 
mapPath[4] = [true, true, true, false]; 
mapPath[5] = [true, true, true, true]; 
mapPath[6] = [true, true, true, true]; 
mapPath[7] = [true, true, false, true]; 
mapPath[8] = [true, true, true, false]; 
mapPath[9] = [true, true, true, true]; 
mapPath[10] = [true, true, true, true]; 
mapPath[11] = [true, true, false, true]; 
mapPath[12] = [true, false, true, false]; 
mapPath[13] = [true, false, true, true]; 
mapPath[14] = [true, false, true, true]; 
mapPath[15] = [true, false, false, true]; 

//Set the player's start location 
var mapLocation = 12; 

//Set the images 
var images = []; 

images[0] = ""; 
images[1] = ""; 
images[2] = ""; 
images[3] = ""; 
images[4] = ""; 
images[5] = ""; 
images[6] = ""; 
images[7] = ""; 
images[8] = ""; 

//Set the blocked path messages 
var blockedPathMessages = []; 

blockedPathMessages[0] = "It's too dangerous to move that way."; 
blockedPathMessages[1] = "A mysterious force holds you back."; 
blockedPathMessages[2] = "A tangle of thorns blocks your way."; 
blockedPathMessages[3] = "You can't step over the dragon."; 
blockedPathMessages[4] = "The terrain is too rocky that way."; 
blockedPathMessages[5] = "The gate locks shut."; 
blockedPathMessages[6] = "The river is too deep to cross."; 
blockedPathMessages[7] = "The trees are too thick to pass."; 
blockedPathMessages[8] = "You're too scared to go that way."; 

//Create the objects and set their locations 
var items = ["stone"]; 
var itemLocations = [6]; 

//An array to store what the player is carrying 
var backpack = []; 

//Initialize the player's input 
var playersInput = ""; 

//Initialize the gameMessage 
var gameMessage = ""; 

//Create an array of actions the game understands 
//and a variable to store the current action 
var actionsIKnow 
    = ["north", "east", "south", 
    "west", "take", "use", "drop"]; 
var action = ""; 

//An array of items the game understands 
//and a variable to store the current item 
var itemsIKnow = ["flute", "stone", "sword"]; 
var item = ""; 

//The img element 
var image = document.querySelector("img"); 

//The input and output fields 
var output = document.querySelector("#output"); 
var input = document.querySelector("#input"); 

//The button 
var button = document.querySelector("button"); 
button.style.cursor = "pointer"; 
button.addEventListener("click", clickHandler, false); 

window.addEventListener("keydown", keyHandler, false); 

//Dispay the player's location 
render(); 

function keyHandler(event) { 
    if (event.keyCode === 13) { 
     clickHandler(); 
    } 
} 

function clickHandler() { 
    playGame(); 
} 

function playGame() { 
    //Get the player's input and convert it to lowercase 
    playersInput = input.value; 
    playersInput = playersInput.toLowerCase(); 

    //Reset these variables from the previous turn 
    gameMessage = ""; 
    action = ""; 

    //Figure out the player's action 
    for (i = 0; i < actionsIKnow.length; i++) { 
     if (playersInput.indexOf(actionsIKnow[i]) !== -1) { 
      action = actionsIKnow[i]; 
      console.log("player's action: " + action); 
      break; 
     } 
    } 

    //Figure out the item the player wants 
    for (i = 0; i < itemsIKnow.length; i++) { 
     if (playersInput.indexOf(itemsIKnow[i]) !== -1) { 
      item = itemsIKnow[i]; 
      console.log("player's item: " + item); 
     } 
    } 

    //Choose the correct action 
    switch(action) { 
     case "north": 
      if (mapPath[mapLocation][0]) { 
       mapLocation -= 3; 
      } else { 
       gameMessage = blockedPathMessages[mapLocation]; 
      } 
      break; 

     case "east": 
      if (mapPath[mapLocation][2]) { 
       mapLocation += 1; 
      } else { 
       gameMessage = blockedPathMessages[mapLocation]; 
      } 
      break; 

     case "south": 
      if (mapPath[mapLocation][1]) { 
       mapLocation += 3; 
      } else { 
       gameMessage = blockedPathMessages[mapLocation]; 
      } 
      break; 

     case "west": 
      if (mapPath[mapLocation][3]) { 
       mapLocation -= 1; 
      } else { 
       gameMessage = blockedPathMessages[mapLocation]; 
      } 
      break; 

     case "take": 
      takeItem() 
      break; 

     case "drop": 
      dropItem(); 
      break; 

     case "use": 
      useItem(); 
      break; 

     default: 
      gameMessage = "I don't understand that."; 
    } 
    //Render the game 
    render(); 
} 

function takeItem() { 
    //Find the index number of the item in the items array 
    var itemIndexNumber = items.indexOf(item); 

    //Does the item exist in the game world 
    //and is it at the player's current location? 
    if (itemIndexNumber !== -1 && itemLocations[itemIndexNumber] === mapLocation) { 
     gameMessage = "You take the " + item + "."; 

     //Add the item to the player's backpack 
     backpack.push(item); 

     //Remove the item from the game world 
     items.splice(itemIndexNumber, 1); 
     itemLocations.splice(itemIndexNumber, 1); 

     //Display in the console for testing 
     console.log("World items: " + items); 
     console.log("backpack items: " + backpack); 
    } else { 
     //Message if you try and take an item 
     //that isn't in the current location 
     gameMessage = "You can't do that."; 
    } 
} 

function dropItem() { 
    //Try to drop the item only if the backpack isn't empty 
    if (backpack.length !== 0) { 
     //Find the item's array index number in the backpack 
     var backpackIndexNumber = backpack.indexOf(item); 

     //The item is in the backpack if backpackIndex number isn't -1 
     if (backpackIndexNumber !== -1) { 

      //Tell the player that the item has been dropped 
      gameMessage = "You drop the " + item + "."; 

      //Add the item from the backpack to the game world 
      items.push(backpack[backpackIndexNumber]); 
      itemLocations.push(mapLocation); 

      //Remove the item from the player's backpack 
      backpack.splice(backpackIndexNumber, 1); 
     } else { 
      //Message if the player tries to drop 
      //something that's not in the backpack 
      gameMessage = "You can't do that."; 
     } 
    } else { 
     //Message if the backpack is empty 
     gameMessage = "You're not carrying anything."; 
    } 
} 

function useItem() { 
    //1. Find out if the item is in the backpack 

    //Find the item's array index number in the backpack 
    var backpackIndexNumber = backpack.indexOf(item); 

    //If the index number is -1, then it isn't in the backpack. 
    //Tell the player that he or she isn't carrying it. 
    if (backpackIndexNumber === -1) { 
     gameMessage = "You're not carrying it."; 
    } 

    //If there are no items in the backpack, then 
    //tell the player the backpack is empty 
    if (backpack.length === 0) { 
     gameMessage += " Your backpack is empty"; 
    } 

    //2. If the item is found in the backpack 
    //figure out what to do with it 
    if (backpackIndexNumber !== -1) { 
     switch(item) { 
      case "flute": 
       if(mapLocation === 8) { 
        gameMessage = "Beautiful music fills the air."; 
        gameMessage += "A wizend old man steps outside " 
        gameMessage += "and hands you a sword!"; 

        //Add the sword to the world 
        items.push("sword"); 
        itemLocations.push(mapLocation); 
       } else { 
        gameMessage = "You try and play the flute " 
        gameMessage += "but it makes no sound here."; 
       } 
       break; 

      case "sword": 
       if(mapLocation === 3) { 
        gameMessage = "You swing the sword and slay the dragon! "; 
        gameMessage += "You've saved the forest of Lyrica!"; 
       } else { 
        gameMessage = "You swing the sword listlessly."; 
       } 
       break; 

      case "stone": 
       if(mapLocation === 1) { 
        gameMessage = "You drop the stone in the well."; 
        gameMessage += " A magical flute appears!"; 

        //Remove the stone from the player's backpack 
        backpack.splice(backpackIndexNumber, 1); 

        //Add the flute to the world 
        items.push("flute"); 
        itemLocations.push(mapLocation); 
       } else { 
        gameMessage = "You fumble with the stone in your pocket."; 
       } 
       break;      
     } 
    } 
} 

function render() { 
    //Render the location 
    output.innerHTML = map[mapLocation] + "<br>"; 
    image.src = "images/" + images[mapLocation]; 

    // display the directions the user can travel 
    for (var i = 0; i < mapPath[mapLocation].length; i++) { 
     if (mapPath[mapLocation][i]) { 
      switch (i) { 
       case 0: 
        output.innerHTML += "A path leads north<br>" 
        break;  
       case 1: 
        output.innerHTML += "A path leads south<br>" 
        break;  
       case 2: 
        output.innerHTML += "A path leads east<br>" 
        break;  
       case 3: 
        output.innerHTML += "A path leads west<br>" 
        break;  
      } 
     } 
    } 

    //Display an item if there's one in this location 
    //1. Loop through all the game items 
    for (var i = 0; i < items.length; i++) { 
     //Find out if there's an item at this location 
     if (mapLocation === itemLocations[i]) { 
      //Display it 
      output.innerHTML += "<br>You see a <strong>" + items[i] + "</strong> here."; 
     } 
    } 

    //Display the game message 
    output.innerHTML += "<br><em>" + gameMessage + "</em>"; 

    //Display the player's backpack contents 
    if (backpack.length !== 0) { 
     output.innerHTML += "<br>You are carrying: " + backpack.join(", "); 
    } 

    input.value = ""; 
    input.focus(); 

} 

</script> 
+0

如果你使用二维数组,你可能真的简化了你的代码。 – Coderchu

+0

此外,请只包含与您的问题相关的代码,不要只转储您的整个代码。如果您在导航时遇到问题,请仅导航js。 – Coderchu

回答

0

你的问题就在这里,你真的应该由4同样地移动,而不是3

case "north": 
    if (mapPath[mapLocation][0]) { 
     mapLocation -= 3; // CHANGE THIS TO -= 4 
    } 

,移动 “南” 的时候,你” d想要增加4.想想看,12-3给你9,但是12的房间“北”是8.