人生模拟小游戏
2025-11-03 17:25:52
发布于:浙江
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
// 定义角色类
class SimCharacter {
private:
string name;
int age;
int health; // 健康值 0-100
int happiness; // 快乐值 0-100
int energy; // 能量值 0-100
int hunger; // 饥饿值 0-100 (越高越饿)
int money; // 金钱
public:
// 构造函数
SimCharacter(string n) : name(n), age(20), health(80),
happiness(70), energy(90), hunger(30), money(1000) {}
// 获取角色状态
void getStatus() {
cout << "\n===== " << name << " 的状态 =====" << endl;
cout << "年龄: " << age << "岁" << endl;
cout << "健康: " << setw(3) << health << "% | " << getBar(health) << endl;
cout << "快乐: " << setw(3) << happiness << "% | " << getBar(happiness) << endl;
cout << "能量: " << setw(3) << energy << "% | " << getBar(energy) << endl;
cout << "饥饿: " << setw(3) << hunger << "% | " << getBar(hunger) << endl;
cout << "金钱: " << money << " 元" << endl;
cout << "==========================" << endl;
}
// 生成状态条
string getBar(int value) {
string bar;
for(int i = 0; i < 10; i++) {
if(i < value / 10) {
bar += "■";
} else {
bar += "□";
}
}
return bar;
}
// 吃饭
void eat() {
cout << name << " 正在享用美食..." << endl;
hunger = max(0, hunger - 40);
energy = min(100, energy + 20);
happiness = min(100, happiness + 10);
}
// 睡觉
void sleep() {
cout << name << " 睡了个好觉..." << endl;
energy = min(100, energy + 80);
health = min(100, health + 10);
hunger = min(100, hunger + 20);
}
// 工作
void work() {
cout << name << " 努力工作中..." << endl;
money += rand() % 100 + 50; // 50-150元收入
energy = max(0, energy - 40);
happiness = max(0, happiness - 10);
hunger = min(100, hunger + 30);
}
// 娱乐
void play() {
cout << name << " 尽情玩乐中..." << endl;
happiness = min(100, happiness + 50);
energy = max(0, energy - 30);
hunger = min(100, hunger + 20);
money = max(0, money - (rand() % 50 + 20)); // 花费20-70元
}
// 锻炼
void exercise() {
cout << name << " 正在锻炼身体..." << endl;
health = min(100, health + 30);
energy = max(0, energy - 30);
happiness = min(100, happiness + 10);
hunger = min(100, hunger + 30);
}
// 时间流逝带来的自然变化
void timePasses() {
energy = max(0, energy - 5);
hunger = min(100, hunger + 10);
// 随机小事件
if(rand() % 10 < 2) {
int event = rand() % 3;
cout << "\n--- 发生了一件小事! ---" << endl;
if(event == 0) {
cout << name << " 捡到了一些钱!" << endl;
money += rand() % 50 + 10;
} else if(event == 1) {
cout << name << " 感觉有点不舒服..." << endl;
health = max(0, health - 15);
} else {
cout << name << " 收到了朋友的问候,很开心!" << endl;
happiness = min(100, happiness + 15);
}
}
}
// 检查是否游戏结束
bool isGameOver() {
return (health <= 0 || happiness <= 0);
}
// 获取名字
string getName() {
return name;
}
};
// 显示菜单
void showMenu() {
cout << "\n请选择要进行的活动:" << endl;
cout << "1. 吃饭" << endl;
cout << "2. 睡觉" << endl;
cout << "3. 工作" << endl;
cout << "4. 娱乐" << endl;
cout << "5. 锻炼" << endl;
cout << "6. 查看状态" << endl;
cout << "0. 退出游戏" << endl;
cout << "请输入选择: ";
}
int main() {
srand(time(0)); // 初始化随机数生成器
cout << "===== 模拟人生小游戏 =====" << endl;
cout << "请输入你的角色名字: ";
string name;
cin >> name;
SimCharacter sim(name);
int day = 1;
int choice;
cout << "\n欢迎 " << sim.getName() << " 来到模拟人生世界!" << endl;
do {
cout << "\n===== 第 " << day << " 天 =====" << endl;
showMenu();
cin >> choice;
switch(choice) {
case 1:
sim.eat();
sim.timePasses();
day++;
break;
case 2:
sim.sleep();
sim.timePasses();
day++;
break;
case 3:
sim.work();
sim.timePasses();
day++;
break;
case 4:
sim.play();
sim.timePasses();
day++;
break;
case 5:
sim.exercise();
sim.timePasses();
day++;
break;
case 6:
sim.getStatus();
break;
case 0:
cout << "谢谢游玩! 再见!" << endl;
break;
default:
cout << "无效的选择,请重试。" << endl;
}
// 检查游戏是否结束
if(sim.isGameOver()) {
cout << "\n--- 游戏结束 ---" << endl;
cout << sim.getName() << " 无法继续生活了..." << endl;
cout << "你一共生存了 " << day - 1 << " 天" << endl;
choice = 0; // 退出游戏
}
} while(choice != 0);
return 0;
}
这里空空如也












有帮助,赞一个