用Java写一个基于文本的冒险游戏

漫步杂记 5 月前


好的,让我们来创造一个有趣的创意项目吧!这次我们将构建一个基于文本的冒险游戏。这个游戏将让玩家通过一系列的选择来探索不同的故事情节。这种类型的游戏通常被称为“选择你的冒险”游戏。

游戏概述:

  • 游戏目标:玩家将探索一个神秘的世界,并在旅途中做出选择以决定故事的发展方向。
  • 游戏机制:玩家将遇到各种事件,每个事件会有多个选项供玩家选择。不同的选择会导致不同的结果。
  • 故事情节:我们将创建一个简单的故事情节,玩家将在一个古老的森林中探险,寻找传说中的宝藏。
import java.util.Scanner;

public class TextAdventureGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Adventure of the Lost Treasure!");
        System.out.println("You find yourself at the edge of an ancient forest. What do you do?");
        System.out.println("1. Enter the forest.");
        System.out.println("2. Search for clues around the edge of the forest.");
        System.out.print("Choose (1 or 2): ");

        int choice = scanner.nextInt();

        if (choice == 1) {
            enterForest(scanner);
        } else if (choice == 2) {
            searchForClues(scanner);
        } else {
            System.out.println("Invalid choice. Game over.");
        }

        scanner.close();
    }

    private static void enterForest(Scanner scanner) {
        System.out.println("You step into the forest...");
        System.out.println("Suddenly, you hear a rustling in the bushes.");
        System.out.println("1. Investigate the noise.");
        System.out.println("2. Keep moving forward.");
        System.out.print("Choose (1 or 2): ");

        int choice = scanner.nextInt();

        if (choice == 1) {
            encounterCreature(scanner);
        } else if (choice == 2) {
            continueExploring(scanner);
        } else {
            System.out.println("Invalid choice. Game over.");
        }
    }

    private static void searchForClues(Scanner scanner) {
        System.out.println("You decide to search for clues...");
        System.out.println("After some time, you find an old map.");
        System.out.println("The map leads deep into the forest.");
        System.out.println("Do you follow the map? (yes/no)");
        String answer = scanner.next();

        if ("yes".equalsIgnoreCase(answer)) {
            enterForest(scanner);
        } else {
            System.out.println("You decide to stay put and think about your next move.");
            System.out.println("Suddenly, a mysterious figure appears...");
            System.out.println("What do you do? (run / talk)");
            answer = scanner.next();

            if ("run".equalsIgnoreCase(answer)) {
                System.out.println("You run away and escape the figure.");
                System.out.println("You feel relieved but also curious about what might have happened.");
            } else if ("talk".equalsIgnoreCase(answer)) {
                System.out.println("You approach the figure and start a conversation.");
                System.out.println("The figure turns out to be a friendly guide who helps you find the treasure.");
                System.out.println("Congratulations! You found the lost treasure!");
            } else {
                System.out.println("Invalid choice. Game over.");
            }
        }
    }

    private static void encounterCreature(Scanner scanner) {
        System.out.println("A small creature jumps out from behind a tree!");
        System.out.println("What do you do? (run / talk)");
        String answer = scanner.next();

        if ("run".equalsIgnoreCase(answer)) {
            System.out.println("You run away, but the creature follows you.");
            System.out.println("You manage to lose it after a while.");
        } else if ("talk".equalsIgnoreCase(answer)) {
            System.out.println("The creature speaks in a strange language.");
            System.out.println("You try to communicate and eventually understand each other.");
            System.out.println("The creature reveals the location of the treasure.");
            System.out.println("Congratulations! You found the lost treasure!");
        } else {
            System.out.println("Invalid choice. Game over.");
        }
    }

    private static void continueExploring(Scanner scanner) {
        System.out.println("You continue deeper into the forest...");
        System.out.println("After a while, you come across a clearing with a mysterious stone monument.");
        System.out.println("Do you investigate the monument? (yes/no)");
        String answer = scanner.next();

        if ("yes".equalsIgnoreCase(answer)) {
            System.out.println("The monument has inscriptions that hint at the location of the treasure.");
            System.out.println("You follow the clues and eventually find the treasure.");
            System.out.println("Congratulations! You found the lost treasure!");
        } else {
            System.out.println("You decide not to investigate and keep exploring.");
            System.out.println("Unfortunately, you get lost and never find the treasure.");
            System.out.println("Game over.");
        }
    }
}

运行说明:

  • 编译程序:确保你的开发环境已安装 JDK,并使用 javac TextAdventureGame.java 命令编译代码。
  • 运行程序:使用 java TextAdventureGame 命令运行程序。
  • 输入数据:根据提示输入你的选择。

这个游戏提供了一个简单的框架,你可以根据自己的想象扩展更多的场景和情节。希望你喜欢这个创意项目!

评论(0)

发布评论

相关文章