Create an interactive text-based adventure game where the user navigates through a story by making choices. Each decision will lead to different outcomes, branching paths, or even the end of the game!
For example:
1
→ “You encounter a bear! Do you run (1) or fight (2)?”2
→ “The bear chases you down. Game over!”This challenge introduces you to:
The program should:
if-else
statements (or a dictionary/map) to determine the next step based on the user’s choices.def adventure_game():
print("Welcome to the Text-Based Adventure Game!")
print("Your goal is to survive and find the treasure!")
print("Let the adventure begin!\n")
# Starting point
while True:
print("You are standing in front of a dark cave.")
print("Do you want to:")
print("1. Enter the cave.")
print("2. Walk away.")
choice = input("Enter 1 or 2: ")
if choice == "1":
print("\nYou step into the cave and see two tunnels.")
print("Do you:")
print("1. Take the left tunnel.")
print("2. Take the right tunnel.")
choice = input("Enter 1 or 2: ")
if choice == "1":
print("\nYou encounter a sleeping dragon!")
print("Do you:")
print("1. Try to sneak past it.")
print("2. Attack the dragon.")
choice = input("Enter 1 or 2: ")
if choice == "1":
print("\nYou sneak past the dragon and find a treasure chest! You win!")
else:
print("\nThe dragon wakes up and breathes fire! Game over!")
else:
print("\nThe tunnel collapses behind you, trapping you. Game over!")
elif choice == "2":
print("\nYou walk away safely. Maybe next time you'll find the courage to enter. Game over!")
else:
print("\nInvalid choice. Please enter 1 or 2.")
continue
# Ask if the user wants to play again
replay = input("\nWould you like to play again? (yes/no): ").lower()
if replay != "yes":
print("\nThanks for playing! Goodbye!")
break
adventure_game()
import java.util.Scanner;
public class AdventureGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Text-Based Adventure Game!");
System.out.println("Your goal is to survive and find the treasure!");
System.out.println("Let the adventure begin!\n");
while (true) {
System.out.println("You are standing in front of a dark cave.");
System.out.println("Do you want to:");
System.out.println("1. Enter the cave.");
System.out.println("2. Walk away.");
System.out.print("Enter 1 or 2: ");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.println("\nYou step into the cave and see two tunnels.");
System.out.println("Do you:");
System.out.println("1. Take the left tunnel.");
System.out.println("2. Take the right tunnel.");
System.out.print("Enter 1 or 2: ");
choice = scanner.nextInt();
if (choice == 1) {
System.out.println("\nYou encounter a sleeping dragon!");
System.out.println("Do you:");
System.out.println("1. Try to sneak past it.");
System.out.println("2. Attack the dragon.");
System.out.print("Enter 1 or 2: ");
choice = scanner.nextInt();
if (choice == 1) {
System.out.println("\nYou sneak past the dragon and find a treasure chest! You win!");
} else {
System.out.println("\nThe dragon wakes up and breathes fire! Game over!");
}
} else {
System.out.println("\nThe tunnel collapses behind you, trapping you. Game over!");
}
} else if (choice == 2) {
System.out.println("\nYou walk away safely. Maybe next time you'll find the courage to enter. Game over!");
} else {
System.out.println("\nInvalid choice. Please enter 1 or 2.");
continue;
}
System.out.print("\nWould you like to play again? (yes/no): ");
String replay = scanner.next();
if (!replay.equalsIgnoreCase("yes")) {
System.out.println("\nThanks for playing! Goodbye!");
break;
}
}
}
}
function adventureGame() {
alert("Welcome to the Text-Based Adventure Game!");
alert("Your goal is to survive and find the treasure!");
while (true) {
const choice1 = prompt("You are standing in front of a dark cave.\n1. Enter the cave.\n2. Walk away.\nEnter 1 or 2:");
if (choice1 === "1") {
const choice2 = prompt("You step into the cave and see two tunnels.\n1. Take the left tunnel.\n2. Take the right tunnel.\nEnter 1 or 2:");
if (choice2 === "1") {
const choice3 = prompt("You encounter a sleeping dragon!\n1. Try to sneak past it.\n2. Attack the dragon.\nEnter 1 or 2:");
if (choice3 === "1") {
alert("You sneak past the dragon and find a treasure chest! You win!");
} else {
alert("The dragon wakes up and breathes fire! Game over!");
}
} else {
alert("The tunnel collapses behind you, trapping you. Game over!");
}
} else if (choice1 === "2") {
alert("You walk away safely. Maybe next time you'll find the courage to enter. Game over!");
} else {
alert("Invalid choice. Please enter 1 or 2.");
continue;
}
const replay = prompt("Would you like to play again? (yes/no):");
if (replay.toLowerCase() !== "yes") {
alert("Thanks for playing! Goodbye!");
break;
}
}
}
adventureGame();
In Day 28: File Reader and Writer, you’ll dive into file operations, learning to read and write data, which is crucial for handling more complex projects!