c++恐怖游戏

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <chrono>
#include <conio.h>
#include <windows.h>

using namespace std;

// 游戏设置
const int WIDTH = 80;
const int HEIGHT = 20;
const int MAX_ROOMS = 5;
const int MAX_ITEMS = 3;

// 颜色定义
enum Color {
    BLACK = 0,
    BLUE = 1,
    GREEN = 2,
    CYAN = 3,
    RED = 4,
    MAGENTA = 5,
    BROWN = 6,
    LIGHTGRAY = 7,
    DARKGRAY = 8,
    LIGHTBLUE = 9,
    LIGHTGREEN = 10,
    LIGHTCYAN = 11,
    LIGHTRED = 12,
    LIGHTMAGENTA = 13,
    YELLOW = 14,
    WHITE = 15
};

// 设置控制台文本颜色
void setTextColor(Color color) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, color);
}

// 位置类
class Position {
public:
    int x, y;
    Position(int x = 0, int y = 0) : x(x), y(y) {}
};

// 物品类
class Item {
public:
    string name;
    string description;
    bool usable;

    Item(string name, string description, bool usable = false)
        : name(name), description(description), usable(usable) {}
};

// 房间类
class Room {
public:
    string name;
    string description;
    vector<Item> items;
    vector<Room*> exits;
    bool visited;
    bool hasMonster;
    int scareFactor;

    Room(string name, string description, int scareFactor = 0)
        : name(name), description(description), visited(false), hasMonster(false), scareFactor(scareFactor) {}

    void addItem(const Item& item) {
        items.push_back(item);
    }

    void addExit(Room* room) {
        exits.push_back(room);
    }

    void describe() {
        system("cls");
        setTextColor(LIGHTGRAY);
        cout << "你在" << name << endl;
        cout << description << endl;

        if (!visited) {
            visited = true;
            if (scareFactor > 0) {
                scarePlayer(scareFactor);
            }
        }

        if (hasMonster) {
            setTextColor(LIGHTRED);
            cout << "你感觉到有什么东西在附近... 你不是一个人!" << endl;
        }

        setTextColor(LIGHTGRAY);
        cout << "\n这里有: ";
        if (items.empty()) {
            cout << "什么也没有。" << endl;
        } else {
            for (const auto& item : items) {
                cout << item.name << " ";
            }
            cout << endl;
        }

        cout << "\n出口有: ";
        for (const auto& exit : exits) {
            cout << exit->name << " ";
        }
        cout << endl;
    }

    void scarePlayer(int intensity) {
        // 根据恐怖程度进行不同的惊吓
        switch (intensity) {
            case 1:
                // 轻微惊吓
                Sleep(1000);
                setTextColor(LIGHTRED);
                cout << "\n突然,你听到远处传来一声微弱的哭声..." << endl;
                Sleep(2000);
                break;
            case 2:
                // 中等惊吓
                system("cls");
                setTextColor(LIGHTRED);
                cout << "\n\n\n\n\n\t\t\t一双眼睛在黑暗中注视着你!" << endl;
                Sleep(1500);
                system("cls");
                break;
            case 3:
                // 强烈惊吓
                system("cls");
                setTextColor(RED);
                cout << "\n\n\n\n\n\t\t\t一个身影突然从你面前闪过!" << endl;
                Beep(800, 500);
                Sleep(1000);
                system("cls");
                break;
        }
        setTextColor(LIGHTGRAY);
    }
};

// 玩家类
class Player {
public:
    string name;
    Position position;
    Room* currentRoom;
    vector<Item> inventory;
    int sanity;
    bool alive;

    Player(string name, Room* startingRoom)
        : name(name), currentRoom(startingRoom), sanity(100), alive(true) {}

    void moveTo(Room* room) {
        currentRoom = room;
        currentRoom->describe();

        // 移动会降低理智
        decreaseSanity(rand() % 5 + 1);
    }

    void takeItem(const string& itemName) {
        for (auto it = currentRoom->items.begin(); it != currentRoom->items.end(); ++it) {
            if (it->name == itemName) {
                inventory.push_back(*it);
                currentRoom->items.erase(it);
                cout << "你捡起了" << itemName << endl;
                return;
            }
        }
        cout << "这里没有" << itemName << endl;
    }

    void useItem(const string& itemName) {
        for (auto it = inventory.begin(); it != inventory.end(); ++it) {
            if (it->name == itemName) {
                if (it->usable) {
                    cout << "你使用了" << itemName << endl;
                    
                    // 使用手电筒可以增加理智
                    if (itemName == "手电筒") {
                        increaseSanity(10);
                        cout << "手电筒的光让你感到安心一些..." << endl;
                    }
                    
                    // 使用药可以恢复理智
                    if (itemName == "镇静剂") {
                        increaseSanity(25);
                        cout << "你感到平静了许多..." << endl;
                    }
                    
                    inventory.erase(it);
                    return;
                } else {
                    cout << itemName << "不能使用" << endl;
                    return;
                }
            }
        }
        cout << "你没有" << itemName << endl;
    }

    void checkInventory() {
        cout << "\n你的物品: ";
        if (inventory.empty()) {
            cout << "你没有任何物品。" << endl;
        } else {
            for (const auto& item : inventory) {
                cout << item.name << " ";
            }
            cout << endl;
        }
    }

    void decreaseSanity(int amount) {
        sanity -= amount;
        if (sanity <= 0) {
            sanity = 0;
            goInsane();
        }
        cout << "你的理智: " << sanity << "/100" << endl;
    }

    void increaseSanity(int amount) {
        sanity += amount;
        if (sanity > 100) {
            sanity = 100;
        }
        cout << "你的理智: " << sanity << "/100" << endl;
    }

    void goInsane() {
        system("cls");
        setTextColor(RED);
        cout << "\n\n\n\n\n\t\t\t你失去了理智,无法继续生存下去..." << endl;
        cout << "\t\t\t游戏结束!" << endl;
        alive = false;
    }
};

// 游戏类
class Game {
private:
    vector<Room*> rooms;
    Player* player;
    bool gameOver;
    int timeElapsed;

public:
    Game() : gameOver(false), timeElapsed(0) {
        srand(time(0));
        createWorld();
        player = new Player("探索者", rooms[0]);
        rooms[0]->visited = true;
    }

    ~Game() {
        for (auto room : rooms) {
            delete room;
        }
        delete player;
    }

    void createWorld() {
        // 创建房间
        Room* hallway = new Room("走廊", "一条长长的、昏暗的走廊,墙壁上的墙纸已经剥落。", 1);
        Room* livingRoom = new Room("客厅", "一个宽敞的客厅,家具上覆盖着灰尘。", 1);
        Room* kitchen = new Room("厨房", "一个老旧的厨房,水槽里堆满了脏盘子。", 2);
        Room* bedroom = new Room("卧室", "一间凌乱的卧室,床脚有一些奇怪的污渍。", 2);
        Room* basement = new Room("地下室", "一个潮湿、阴暗的地下室,弥漫着一股霉味。", 3);
        Room* attic = new Room("阁楼", "一个狭窄的阁楼,天花板很低,充满了蜘蛛网。", 3);

        // 添加房间到游戏中
        rooms.push_back(hallway);
        rooms.push_back(livingRoom);
        rooms.push_back(kitchen);
        rooms.push_back(bedroom);
        rooms.push_back(basement);
        rooms.push_back(attic);

        // 设置出口
        hallway->addExit(livingRoom);
        hallway->addExit(kitchen);
        hallway->addExit(bedroom);
        hallway->addExit(basement);
        hallway->addExit(attic);

        livingRoom->addExit(hallway);
        kitchen->addExit(hallway);
        bedroom->addExit(hallway);
        basement->addExit(hallway);
        attic->addExit(hallway);

        // 添加物品
        livingRoom->addItem(Item("照片", "一张褪色的全家福照片,上面的人脸有些模糊。"));
        kitchen->addItem(Item("手电筒", "一个旧手电筒,看起来还能用。", true));
        bedroom->addItem(Item("日记", "一本破旧的日记,页面已经泛黄。"));
        basement->addItem(Item("钥匙", "一把生锈的钥匙,不知道能打开什么。"));
        attic->addItem(Item("镇静剂", "一支装满液体的注射器,标签上写着'镇静剂'。", true));

        // 设置怪物位置
        int monsterRoom = rand() % rooms.size();
        rooms[monsterRoom]->hasMonster = true;
    }

    void start() {
        system("cls");
        setTextColor(YELLOW);
        cout << R"(
=========================================================================
                      恐怖游戏:废弃的宅邸
=========================================================================
你是一名灵异调查员,来到这座废弃的宅邸寻找超自然现象的证据。
但是,当你进入宅邸的那一刻,你感觉到有些不对劲...
你的理智开始下降,你必须找到离开的方法,同时保持你的理智!

=========================================================================
                              游戏指令
-------------------------------------------------------------------------
移动: go [房间名]         拾取: take [物品名]
使用: use [物品名]        查看物品: inventory
查看帮助: help            退出: quit
=========================================================================
)" << endl;

        player->currentRoom->describe();
        gameLoop();
    }

    void gameLoop() {
        while (!gameOver && player->alive) {
            setTextColor(LIGHTGRAY);
            cout << "\n> ";
            
            string command;
            getline(cin, command);
            
            processCommand(command);
            
            // 随机事件
            if (rand() % 10 == 0) {
                randomEvent();
            }
            
            timeElapsed++;
        }
    }

    void processCommand(const string& command) {
        string cmd, arg;
        size_t spacePos = command.find(' ');
        
        if (spacePos != string::npos) {
            cmd = command.substr(0, spacePos);
            arg = command.substr(spacePos + 1);
        } else {
            cmd = command;
        }
        
        if (cmd == "go") {
            for (auto exit : player->currentRoom->exits) {
                if (exit->name == arg) {
                    player->moveTo(exit);
                    checkMonster();
                    return;
                }
            }
            cout << "你不能去那里!" << endl;
        } else if (cmd == "take") {
            player->takeItem(arg);
        } else if (cmd == "use") {
            player->useItem(arg);
        } else if (cmd == "inventory") {
            player->checkInventory();
        } else if (cmd == "help") {
            printHelp();
        } else if (cmd == "quit") {
            gameOver = true;
            cout << "你决定离开这个恐怖的地方..." << endl;
        } else {
            cout << "未知命令!输入 help 查看可用命令。" << endl;
        }
    }

    void printHelp() {
        setTextColor(YELLOW);
        cout << R"(
=========================================================================
                              游戏指令
-------------------------------------------------------------------------
移动: go [房间名]         拾取: take [物品名]
使用: use [物品名]        查看物品: inventory
查看帮助: help            退出: quit
=========================================================================
)" << endl;
    }

    void randomEvent() {
        // 随机事件,增加恐怖感
        int event = rand() % 5;
        
        switch (event) {
            case 0:
                setTextColor(LIGHTRED);
                cout << "\n你听到楼上有脚步声..." << endl;
                player->decreaseSanity(2);
                break;
            case 1:
                setTextColor(LIGHTRED);
                cout << "\n一阵冷风吹过,让你脊背发凉..." << endl;
                player->decreaseSanity(3);
                break;
            case 2:
                setTextColor(LIGHTRED);
                cout << "\n你听到远处传来诡异的笑声..." << endl;
                player->decreaseSanity(2);
                break;
            case 3:
                system("cls");
                setTextColor(LIGHTRED);
                cout << "\n\n\n\n\n\t\t\t你看到一个人影闪过!" << endl;
                Sleep(1000);
                system("cls");
                player->currentRoom->describe();
                player->decreaseSanity(5);
                break;
            case 4:
                setTextColor(LIGHTRED);
                cout << "\n你感觉到有什么东西在注视着你..." << endl;
                player->decreaseSanity(3);
                break;
        }
    }

    void checkMonster() {
        if (player->currentRoom->hasMonster) {
            // 遭遇怪物
            system("cls");
            setTextColor(RED);
            cout << "\n\n\n\n\n\t\t\t你遇到了一个可怕的怪物!" << endl;
            Sleep(2000);
            
            // 取决于理智值决定是否存活
            if (player->sanity < 30) {
                cout << "\n\t\t\t你的理智太低,无法面对这个恐怖..." << endl;
                Sleep(2000);
                player->goInsane();
            } else {
                cout << "\n\t\t\t你勉强保持镇定,怪物消失了..." << endl;
                player->decreaseSanity(20);
                Sleep(2000);
                player->currentRoom->hasMonster = false;
                player->currentRoom->describe();
            }
        }
    }
};

int main() {
    Game game;
    game.start();
    
    setTextColor(WHITE);
    return 0;
}    

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值