Back to Game

Text Adventure - Source Code

Play Game
JavaScript
let gameData = {
  start: {
    text: "You wake up in a mysterious forest. What do you do?",
    choices: [
      { text: "Explore the forest", next: "forest" },
      { text: "Look for a way out", next: "path" },
      { text: "Rest and think", next: "rest" }
    ]
  },
  forest: {
    text: "You venture deeper into the forest and find an ancient tree.",
    choices: [
      { text: "Climb the tree", next: "tree" },
      { text: "Examine the roots", next: "roots" },
      { text: "Go back", next: "start" }
    ]
  },
  path: {
    text: "You find a winding path leading to a village.",
    choices: [
      { text: "Enter the village", next: "village" },
      { text: "Follow the path further", next: "mountain" },
      { text: "Return to forest", next: "start" }
    ]
  },
  rest: {
    text: "While resting, you notice strange symbols carved into nearby rocks.",
    choices: [
      { text: "Study the symbols", next: "symbols" },
      { text: "Ignore them and move on", next: "start" }
    ]
  }
};

let currentScenario = 'start';

function showScenario(scenarioKey) {
  const scenario = gameData[scenarioKey];
  const textContainer = document.getElementById('scenario-text');
  const choicesContainer = document.getElementById('choices');
  
  textContainer.innerText = scenario.text;
  choicesContainer.innerHTML = '';
  
  scenario.choices.forEach(choice => {
    const choiceButton = document.createElement('button');
    choiceButton.innerText = choice.text;
    choiceButton.onclick = () => showScenario(choice.next);
    choicesContainer.appendChild(choiceButton);
  });
}

function restartGame() {
  currentScenario = 'start';
  showScenario(currentScenario);
}

showScenario(currentScenario);

This code demonstrates the core game logic. The actual implementation may include additional features and optimizations.