用c++编写一个人生重开模拟器

好的,那我们就来编写一个简单的文字版人生重开模拟器小游戏,步骤如下:

定义玩家数据结构,包含以下字段:姓名、性别、年龄、职业、财富、健康、情感等。例如:

struct Player {
    string name;   // 姓名
    char gender;   // 性别
    int age;       // 年龄
    string career; // 职业
    double wealth; // 财富
    double health; // 健康
    double emotion;// 情感
};


编写玩家角色创建模块,可以让玩家输入姓名、性别、年龄,并随机生成职业和初始属性值。例如:

void createPlayer(Player& player) {
    cout << "请输入你的姓名:";
    getline(cin, player.name);
    cout << "请输入你的性别(M/F):";
    cin >> player.gender;
    cout << "请输入你的年龄:";
    cin >> player.age;

    // 随机生成职业和属性值
    string careers[] = {"程序员", "医生", "教师", "律师", "演员"};
    int idx = rand() % 5;
    player.career = careers[idx];
    player.wealth = rand() % 1000000 + 100000;
    player.health = rand() % 100 + 50;
    player.emotion = rand() % 100;
}


编写游戏流程控制模块,例如:

int main() {
    // 初始化随机种子
    srand(time(nullptr));

    // 创建玩家角色
    Player player;
    createPlayer(player);

    // 进入游戏循环
    gameLoop(player);

    return 0;
}

这样,一个简单的文字版人生重开模拟器小游戏就完成了。当然,这只是一个简化版。
我还有一个增加了随机事件的:
 

#include
using namespace std;

struct Player {
string name;   // 姓名
char gender;   // 性别
int age;       // 年龄
string career; // 职业
double wealth; // 财富
double health; // 健康
double emotion;// 情感
};

// 职业结构体
struct Career {
string name;        // 名称
double income;      // 收入
double health_plus; // 健康加成
double emotion_plus;// 情感加成
};

// 职业列表
vector careers = {
{"程序员", 10000, -10, -5},
{"医生", 20000, 20, -10},
{"教师", 8000, 10, 10},
{"律师", 15000, -10, 15},
{"演员", 5000, -20, 20},
{"企业家", 30000, -5, 5},
{"警察", 8000, 30, -10},
{"军人", 20000, 50, -10}
};

// 随机生成职业
void generateCareer(Player& player) {
int idx = rand() % careers.size();
player.career = careers[idx].name;
player.wealth = rand() % 100000 + 50000;
player.health = rand() % 50 + 50 + careers[idx].health_plus;
player.emotion = rand() % 50 + 50 + careers[idx].emotion_plus;
player.wealth += careers[idx].income;
}

// 全局随机事件列表
vector events = {
"参加比赛获得奖金",
"遭遇偷盗,损失财物",
"突然得到一笔意外之财",
"被人骗钱了"
};

void createPlayer(Player& player) {
cout << "请输入你的姓名:";
getline(cin, player.name);
cout << "请输入你的性别(M/F):";
cin >> player.gender;
cout << "请输入你的年龄:";
cin >> player.age;

// 随机生成职业和属性值
generateCareer(player);
}

void gameLoop(Player& player) {
while (true) {
// 显示菜单选项
cout << "你打算做什么?" << endl;
cout << "1. 工作" << endl;
cout << "2. 看病" << endl;
cout << "3. 恋爱" << endl;
cout << "4. 购物" << endl;
cout << "5. 结婚" << endl;
cout << "6. 旅游" << endl;
cout << "7. 投资" << endl;
cout << "8. 退出游戏" << endl;

    // 读取用户选择
    int choice;
    cin >> choice;

    // 根据选择更新玩家属性值
    switch (choice) {
        case 1:
            player.wealth += rand() % 10000 + 5000;
            player.health -= rand() % 10 + 5;
            player.emotion -= rand() % 10 + 5;
            break;
        case 2:
            player.wealth -= rand() % 20000 + 5000;
            player.health += rand() % 20 + 10;
            player.emotion -= rand() % 10 + 5;
            break;
        case 3:
            player.wealth -= rand() % 5000 + 1000;
            player.health -= rand() % 5 + 5;
            player.emotion += rand() % 20 + 10;
            break;
        case 4:
            player.wealth -= rand() % 10000 + 5000;
            player.emotion += rand() % 20 + 10;
            break;
        case 5:
            if (player.age >= 25 && player.emotion >= 60) {
                cout << "恭喜你,你结婚了!" << endl;
                return;
            }
            cout << "条件不足,无法结婚。" << endl;
            break;
        case 6:
            player.wealth -= rand() % 30000 + 10000;
            player.health += rand() % 20 + 10;
            player.emotion += rand() % 30 + 10;
            break;
        case 7:
            player.wealth += rand() % 50000 + 10000;
            player.emotion += rand() % 10 + 5;
            break;
        case 8:
            cout << "退出游戏。" << endl;
            return;
        default:
            cout << "无效选择,请重新输入。" << endl;
    }

    // 显示随机事件
    int event_idx = rand() % events.size();
    cout << "发生了一件意外事件:" << events[event_idx] << endl;

    // 显示玩家属性值
    cout << "当前状态:";
    cout << "财富:" << player.wealth << ",";
    cout << "健康:" << player.health << ",";
    cout << "情感:" << player.emotion << endl;

    // 判断游戏是否结束
    if (player.wealth <= 0 || player.health <= 0) {
        cout << "你已经倒下了,游戏结束。" << endl;
        return;
    }
}
}

int main() {
// 初始化随机种子
srand(time(nullptr));

// 创建玩家角色
Player player;
createPlayer(player);

// 进入游戏循环
gameLoop(player);

return 0;
}

你可能感兴趣的:(开发语言,c++,人生重开模拟器,编程教程,c++如何制作游戏)