当前版本:V0.1.6
运行要求:C++11
可通过在编译器选项中加入-std=c++11强制修改编译器。
主题代码:
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstdio> // 用于清屏操作
using namespace std;
// 清屏函数,跨平台支持
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 输出分隔线
void printSeparator() {
cout << "\n----------------------------------------\n" << endl;
}
// 成就结构体
struct Achievement {
string name;
string description;
bool unlocked;
Achievement(string n, string d) : name(n), description(d), unlocked(false) {}
};
// 统计数据类
class Statistics {
private:
int feedCount;
int playCount;
int cleanCount;
int healCount;
int daysSurvived;
int maxHappiness;
int maxHealth;
int maxCleanliness;
public:
Statistics() {
feedCount = 0;
playCount = 0;
cleanCount = 0;
healCount = 0;
daysSurvived = 0;
maxHappiness = 0;
maxHealth = 0;
maxCleanliness = 0;
}
// 更新统计数据
void updateFeed() { feedCount++; }
void updatePlay() { playCount++; }
void updateClean() { cleanCount++; }
void updateHeal() { healCount++; }
void updateDaysSurvived() { daysSurvived++; }
void updateMaxHappiness(int current) { maxHappiness = max(maxHappiness, current); }
void updateMaxHealth(int current) { maxHealth = max(maxHealth, current); }
void updateMaxCleanliness(int current) { maxCleanliness = max(maxCleanliness, current); }
// 获取统计数据
int getFeedCount() { return feedCount; }
int getPlayCount() { return playCount; }
int getCleanCount() { return cleanCount; }
int getHealCount() { return healCount; }
// 显示统计数据
void showStats() {
cout << "\n===== 游戏统计数据 =====" << endl;
cout << "喂食次数: " << feedCount << endl;
cout << "玩耍次数: " << playCount << endl;
cout << "清洁次数: " << cleanCount << endl;
cout << "治疗次数: " << healCount << endl;
cout << "存活天数: " << daysSurvived << endl;
cout << "最高快乐值: " << maxHappiness << endl;
cout << "最高健康值: " << maxHealth << endl;
cout << "最高清洁度: " << maxCleanliness << endl;
cout << "========================" << endl;
}
};
// 小猫类
class Kitten {
private:
string name;
string type; // 固定为"小猫"
int age;
int hunger; // 饥饿值 0-100,越高越饿
int happiness; // 快乐值 0-100,越高越快乐
int health; // 健康值 0-100,越高越健康
int cleanliness;// 清洁度 0-100,越低越脏
int energy; // 精力值 0-100,越低越疲倦
int friendliness;// 友好度 0-100,越高越亲近
// 记录健康值保持100的天数(用于成就)
int daysWithMaxHealth;
public:
// 构造函数,固定类型为小猫
Kitten(string n) : name(n), type("小猫") {
age = 0;
hunger = 30;
happiness = 70;
health = 100;
cleanliness = 80;
energy = 80; // 初始精力值
friendliness = 50; // 初始友好度
daysWithMaxHealth = 0;
}
// 获取小猫状态
void getStatus() {
cout << "\n[" << name << " 的状态]" << endl;
cout << "类型: " << type << endl;
cout << "年龄: " << age << " 天" << endl;
cout << "饥饿值: " << hunger << "/100" << (hunger > 70 ? " [非常饿]" : (hunger > 40 ? " [有点饿]" : " [很饱]")) << endl;
cout << "快乐值: " << happiness << "/100" << (happiness < 30 ? " [不开心]" : (happiness > 70 ? " [超开心]" : " [一般]")) << endl;
cout << "健康值: " << health << "/100" << (health < 30 ? " [需要治疗]" : (health > 70 ? " [很健康]" : " [还行]")) << endl;
cout << "清洁度: " << cleanliness << "/100" << (cleanliness < 30 ? " [很脏]" : (cleanliness > 70 ? " [很干净]" : " [一般]")) << endl;
cout << "精力值: " << energy << "/100" << (energy < 30 ? " [很疲倦]" : (energy > 70 ? " [精力充沛]" : " [一般]")) << endl;
cout << "友好度: " << friendliness << "/100" << (friendliness < 30 ? " [很疏远]" : (friendliness > 70 ? " [超亲近]" : " [一般]")) << endl;
}
// 绘制小猫的基础ASCII艺术画面
void drawKitten() {
vector<string> baseArt = {
" /\\_/\\",
" ( o.o )",
" > ^ <"
};
// 根据状态修改形象
vector<string> moodArt = baseArt;
// 饥饿状态
if (hunger > 70) {
moodArt.insert(moodArt.begin(), "食物 我好饿啊...");
}
// 清洁状态
if (cleanliness < 30) {
moodArt.push_back("污垢 我需要洗澡...");
}
// 输出小猫形象
cout << "\n----- " << name << " 的样子 -----" << endl;
for (const string& line : moodArt) {
cout << line << endl;
}
cout << "-------------------------" << endl;
}
// 绘制喂食时的图画(无特殊字符版本)
void drawFeeding() {
clearScreen();
cout << "\n----- 喂食时间 -----" << endl;
cout << " /\\_/\\ |\\ " << endl;
cout << " ( ^.^ ) /|\\ " << endl;
cout << " >v< / \\ " << endl;
cout << "\n" << name << " 吧唧吧唧吃得好香!" << endl;
printSeparator();
this_thread::sleep_for(chrono::seconds(2));
}
// 绘制玩耍时的图画
void drawPlaying() {
clearScreen();
cout << "\n----- 玩耍时间 -----" << endl;
cout << " /\\_/\\ | " << endl;
cout << " ( ^o^ ) O " << endl;
cout << " >^< /|\\ " << endl;
cout << "\n" << name << " 玩得超开心!" << endl;
printSeparator();
this_thread::sleep_for(chrono::seconds(2));
}
// 绘制清洁时的图画
void drawCleaning() {
clearScreen();
cout << "\n----- 清洁时间 -----" << endl;
cout << " /\\_/\\ ~~ " << endl;
cout << " ( ^.^ ) /|\\ " << endl;
cout << " >^< / \\ " << endl;
cout << "\n" << name << " 变得香喷喷!" << endl;
printSeparator();
this_thread::sleep_for(chrono::seconds(2));
}
// 绘制治疗时的图画
void drawHealing() {
clearScreen();
cout << "\n----- 治疗时间 -----" << endl;
cout << " /\\_/\\ + " << endl;
cout << " ( ^.^ ) # " << endl;
cout << " >^< " << endl;
cout << "\n" << name << " 感觉好多啦!" << endl;
printSeparator();
this_thread::sleep_for(chrono::seconds(2));
}
// 绘制休息时的图画
void drawResting() {
clearScreen();
cout << "\n----- 休息时间 -----" << endl;
cout << " /\\_/\\ Z " << endl;
cout << " ( -.- ) _|_ " << endl;
cout << " >^< / \\ " << endl;
cout << "\n" << name << " 睡得好香呀~" << endl;
printSeparator();
this_thread::sleep_for(chrono::seconds(2));
}
// 喂食
void feed() {
drawFeeding(); // 显示喂食图画
hunger = max(0, hunger - 30);
happiness += 15;
energy += 10;
friendliness += 5;
if (happiness > 100) happiness = 100;
if (energy > 100) energy = 100;
if (friendliness > 100) friendliness = 100;
cout << name << " 吃得心满意足!" << endl;
}
// 玩耍
void play() {
drawPlaying(); // 显示玩耍图画
happiness = min(100, happiness + 35);
hunger += 10;
energy -= 15;
friendliness += 8;
cleanliness -= 10;
if (hunger > 100) hunger = 100;
if (energy < 0) energy = 0;
if (friendliness > 100) friendliness = 100;
if (cleanliness < 0) cleanliness = 0;
cout << name << " 玩得不亦乐乎!" << endl;
}
// 清洁
void clean() {
drawCleaning(); // 显示清洁图画
cleanliness = min(100, cleanliness + 40);
happiness += 10;
friendliness += 5;
if (happiness > 100) happiness = 100;
if (friendliness > 100) friendliness = 100;
cout << name << " 变得干干净净,好舒服!" << endl;
}
// 治疗
void heal() {
drawHealing(); // 显示治疗图画
health = min(100, health + 50);
energy += 20;
happiness -= 5;
if (happiness < 0) happiness = 0;
if (energy > 100) energy = 100;
cout << name << " 感觉好多啦!" << endl;
}
// 休息(恢复精力)
void rest() {
drawResting(); // 显示休息图画
energy = min(100, energy + 40);
hunger += 5;
if (hunger > 100) hunger = 100;
cout << name << " 好好休息了一下,精力恢复了!" << endl;
}
// 随机事件
void triggerRandomEvent(vector<Achievement>& achievements) {
int eventChance = rand() % 8; // 约12.5%的概率触发随机事件
if (eventChance == 0) {
int eventType = rand() % 6; // 多种随机事件
switch(eventType) {
case 0:
cout << "\n【随机事件】" << name << " 找到了一个毛线球!快乐值增加了!" << endl;
happiness = min(100, happiness + 20);
friendliness += 10;
if (friendliness > 100) friendliness = 100;
// 检查成就
for (auto& ach : achievements) {
if (ach.name == "幸运儿" && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
break;
case 1:
cout << "\n【随机事件】" << name << " 不小心弄脏了自己!" << endl;
cleanliness = max(0, cleanliness - 30);
break;
case 2:
cout << "\n【随机事件】" << name << " 精神很好,健康值提升了!" << endl;
health = min(100, health + 15);
energy += 15;
if (energy > 100) energy = 100;
break;
case 3:
cout << "\n【随机事件】" << name << " 好像有点闷闷不乐..." << endl;
happiness = max(0, happiness - 25);
break;
case 4:
cout << "\n【随机事件】" << name << " 追着蝴蝶玩得很开心!" << endl;
happiness = min(100, happiness + 30);
energy -= 10;
if (energy < 0) energy = 0;
break;
case 5:
cout << "\n【随机事件】" << name << " 发现了美味的零食!" << endl;
hunger = max(0, hunger - 20);
happiness += 10;
if (happiness > 100) happiness = 100;
break;
}
}
}
// 时间流逝对小猫状态的影响
void timePasses(vector<Achievement>& achievements, Statistics& stats) {
// 触发随机事件
triggerRandomEvent(achievements);
// 每过一段时间,年龄增加
static int days = 0;
days++;
if (days % 7 == 0) { // 每7个周期增加1天年龄
age++;
cout << "\n恭喜!" << name << " 已经 " << age << " 天了!" << endl;
// 检查成就
for (auto& ach : achievements) {
if ((ach.name == "一周年快乐" && age >= 7) && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
if ((ach.name == "长久陪伴" && age >= 30) && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
stats.updateDaysSurvived();
}
// 状态自然变化
hunger += rand() % 5 + 3; // 3-7点饥饿值增加
if (hunger > 100) hunger = 100;
happiness -= rand() % 4 + 2; // 2-5点快乐值减少
if (happiness < 0) happiness = 0;
cleanliness -= rand() % 3 + 1; // 1-3点清洁度减少
if (cleanliness < 0) cleanliness = 0;
energy -= rand() % 3 + 2; // 2-4点精力值减少
if (energy < 0) energy = 0;
// 健康值受其他状态影响
if (hunger > 80 || cleanliness < 20) {
health -= rand() % 3 + 2; // 饥饿或肮脏会导致健康下降
daysWithMaxHealth = 0; // 健康值下降,重置计数器
} else if (happiness > 70) {
health += rand() % 2 + 1; // 快乐会略微提升健康
}
// 友好度自然变化
if (happiness > 50) {
friendliness += rand() % 2; // 快乐时友好度缓慢增加
} else {
friendliness -= rand() % 2; // 不快乐时友好度缓慢减少
}
if (friendliness < 0) friendliness = 0;
else if (friendliness > 100) friendliness = 100;
if (health < 0) health = 0;
else if (health > 100) {
health = 100;
daysWithMaxHealth++; // 健康值保持100,增加计数器
}
// 更新统计数据
stats.updateMaxHappiness(happiness);
stats.updateMaxHealth(health);
stats.updateMaxCleanliness(cleanliness);
// 状态警告
if (hunger > 90) {
cout << name << " 快要饿死了!" << endl;
} else if (hunger > 70) {
cout << name << " 非常饿!" << endl;
}
if (health < 20) {
cout << name << " 病得很重!需要治疗!" << endl;
} else if (health < 40) {
cout << name << " 不太舒服..." << endl;
}
if (happiness < 20) {
cout << name << " 非常不开心,需要陪伴!" << endl;
}
if (cleanliness < 20) {
cout << name << " 太脏了,需要清洗!" << endl;
}
if (energy < 20) {
cout << name << " 快要累垮了,需要休息!" << endl;
}
}
// 检查小猫是否还活着
bool isAlive() {
return health > 0;
}
// 获取小猫名字
string getName() {
return name;
}
// 获取当前属性值
int getHunger() { return hunger; }
int getHappiness() { return happiness; }
int getHealth() { return health; }
int getCleanliness() { return cleanliness; }
int getEnergy() { return energy; }
int getFriendliness() { return friendliness; }
int getDaysWithMaxHealth() { return daysWithMaxHealth; }
};
// 显示更新日志
void showChangelog() {
cout << "\n===== 宠物养成模拟器 - 更新日志 =====" << endl;
cout << "当前版本: V0.1.6" << endl << endl;
cout << "V0.1.6 更新内容:" << endl;
cout << "- 移除了所有特殊字符和表情符号,确保在Dev-C++中正常显示" << endl;
cout << "- 优化了ASCII艺术图画,使用基础字符表现小猫的各种状态" << endl;
cout << "- 调整了文字描述,保持可爱风格的同时确保兼容性" << endl << endl;
cout << "V0.1.5 更新内容:" << endl;
cout << "- 宠物类型固定为小猫,专注于小猫的养成体验" << endl;
cout << "- 为喂食、玩耍、清洁、治疗和休息等互动添加了专属图画" << endl;
cout << "- 优化了小猫的ASCII艺术形象,使其更加生动" << endl << endl;
cout << "V0.1.4 更新内容:" << endl;
cout << "- 修复了'class Pet' has no member named 'getHunger'的错误" << endl;
cout << "- 优化了游戏画面显示逻辑" << endl << endl;
cout << "======================================" << endl;
}
// 显示成就列表(未解锁的显示为???)
void showAchievements(vector<Achievement>& achievements) {
cout << "\n===== 成就列表 =====" << endl;
int unlockedCount = 0;
for (auto& ach : achievements) {
if (ach.unlocked) {
cout << "✓ " << ach.name << endl;
cout << " " << ach.description << endl << endl;
unlockedCount++;
} else {
cout << "✗ ???" << endl;
cout << " ???" << endl << endl;
}
}
cout << "已解锁: " << unlockedCount << "/" << achievements.size() << endl;
cout << "====================" << endl;
}
// 初始化成就列表
void initAchievements(vector<Achievement>& achievements) {
// 原有成就
achievements.push_back(Achievement("初次喂食", "第一次给小猫喂食"));
achievements.push_back(Achievement("玩耍伙伴", "第一次陪小猫玩耍"));
achievements.push_back(Achievement("清洁能手", "第一次给小猫清洁"));
achievements.push_back(Achievement("健康卫士", "第一次给小猫治疗"));
achievements.push_back(Achievement("快乐时光", "让小猫的快乐值达到100"));
achievements.push_back(Achievement("健康生活", "让小猫的健康值保持在100达3天"));
achievements.push_back(Achievement("一周年快乐", "成功养育小猫满7天"));
achievements.push_back(Achievement("幸运儿", "触发一次随机幸运事件"));
// 新增成就
achievements.push_back(Achievement("好好休息", "第一次让小猫休息"));
achievements.push_back(Achievement("亲密无间", "让小猫的友好度达到100"));
achievements.push_back(Achievement("洁癖达人", "让小猫的清洁度保持在100达2天"));
achievements.push_back(Achievement("精力充沛", "让小猫的精力值达到100"));
achievements.push_back(Achievement("喂食专家", "累计喂食50次"));
achievements.push_back(Achievement("玩耍大师", "累计玩耍30次"));
achievements.push_back(Achievement("长久陪伴", "成功养育小猫满30天"));
achievements.push_back(Achievement("全能主人", "解锁所有其他成就"));
achievements.push_back(Achievement("猫咪之友", "见过小猫的所有不同状态形象"));
}
// 检查成就解锁条件
void checkAchievements(int action, Kitten& kitten, vector<Achievement>& achievements, Statistics& stats) {
switch(action) {
case 2: // 喂食
for (auto& ach : achievements) {
if (ach.name == "初次喂食" && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
if (ach.name == "喂食专家" && stats.getFeedCount() >= 50 && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
break;
case 3: // 玩耍
for (auto& ach : achievements) {
if (ach.name == "玩耍伙伴" && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
if (ach.name == "快乐时光" && kitten.getHappiness() == 100 && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
if (ach.name == "玩耍大师" && stats.getPlayCount() >= 30 && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
break;
case 4: // 清洁
for (auto& ach : achievements) {
if (ach.name == "清洁能手" && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
break;
case 5: // 治疗
for (auto& ach : achievements) {
if (ach.name == "健康卫士" && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
break;
case 10: // 休息
for (auto& ach : achievements) {
if (ach.name == "好好休息" && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
if (ach.name == "精力充沛" && kitten.getEnergy() == 100 && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
break;
}
// 检查健康生活成就
for (auto& ach : achievements) {
if (ach.name == "健康生活" && kitten.getDaysWithMaxHealth() >= 3 && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
// 检查友好度成就
for (auto& ach : achievements) {
if (ach.name == "亲密无间" && kitten.getFriendliness() == 100 && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
// 检查猫咪之友成就(见过所有状态)
static bool sawHappy = false, sawSad = false, sawSick = false, sawHealthy = false, sawHungry = false, sawDirty = false;
if (kitten.getHappiness() > 70) sawHappy = true;
if (kitten.getHappiness() < 30) sawSad = true;
if (kitten.getHealth() < 30) sawSick = true;
if (kitten.getHealth() > 70) sawHealthy = true;
if (kitten.getHunger() > 70) sawHungry = true;
if (kitten.getCleanliness() < 30) sawDirty = true;
for (auto& ach : achievements) {
if (ach.name == "猫咪之友" && sawHappy && sawSad && sawSick && sawHealthy && sawHungry && sawDirty && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
// 检查全能主人成就(所有其他成就都已解锁)
bool allUnlocked = true;
for (auto& ach : achievements) {
if (ach.name != "全能主人" && !ach.unlocked) {
allUnlocked = false;
break;
}
}
for (auto& ach : achievements) {
if (ach.name == "全能主人" && allUnlocked && !ach.unlocked) {
ach.unlocked = true;
cout << "恭喜 解锁成就: " << ach.name << " - " << ach.description << endl;
}
}
}
// 显示游戏菜单
void showMenu() {
cout << "\n===== 宠物养成模拟器 (V0.1.6) =====" << 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;
cout << "9. 查看统计数据" << endl;
cout << "10. 让小猫休息" << endl;
cout << "0. 退出游戏" << endl;
cout << "====================================" << endl;
cout << "请选择操作: ";
}
int main() {
srand(time(0)); // 初始化随机数生成器
cout << "欢迎来到宠物养成模拟器 (V0.1.6)!" << endl;
cout << "在这个版本中,你将照顾一只可爱的小猫!" << endl;
string petName;
cout << "请给你的小猫起个名字: ";
cin >> petName;
Kitten myKitten(petName);
vector<Achievement> achievements;
Statistics stats;
initAchievements(achievements);
cout << "太好了!你获得了一只可爱的小猫,名字叫" << petName << "!" << endl;
cout << "尝试照顾好你的小猫,解锁各种成就吧!" << endl;
this_thread::sleep_for(chrono::seconds(2)); // 暂停2秒让玩家看清信息
int choice;
bool running = true;
while (running && myKitten.isAlive()) {
clearScreen(); // 清屏,为绘制新画面做准备
// 绘制小猫画面
myKitten.drawKitten();
// 显示菜单并处理用户选择
showMenu();
cin >> choice;
switch (choice) {
case 1:
myKitten.getStatus();
break;
case 2:
myKitten.feed();
stats.updateFeed();
checkAchievements(choice, myKitten, achievements, stats);
myKitten.timePasses(achievements, stats);
break;
case 3:
myKitten.play();
stats.updatePlay();
checkAchievements(choice, myKitten, achievements, stats);
myKitten.timePasses(achievements, stats);
break;
case 4:
myKitten.clean();
stats.updateClean();
checkAchievements(choice, myKitten, achievements, stats);
myKitten.timePasses(achievements, stats);
break;
case 5:
myKitten.heal();
stats.updateHeal();
checkAchievements(choice, myKitten, achievements, stats);
myKitten.timePasses(achievements, stats);
break;
case 6:
cout << "等待中..." << endl;
// 模拟时间流逝,连续更新几次状态
for (int i = 0; i < 5; i++) {
this_thread::sleep_for(chrono::seconds(1));
clearScreen();
myKitten.drawKitten();
myKitten.timePasses(achievements, stats);
}
break;
case 7:
showChangelog();
break;
case 8:
showAchievements(achievements);
break;
case 9:
stats.showStats();
break;
case 10:
myKitten.rest();
checkAchievements(choice, myKitten, achievements, stats);
myKitten.timePasses(achievements, stats);
break;
case 0:
running = false;
cout << "谢谢游玩!再见!" << endl;
break;
default:
cout << "无效的选择,请重试。" << endl;
}
// 等待用户确认,然后继续
if (choice != 0 && choice != 6 && myKitten.isAlive()) {
cout << "\n按回车键继续...";
cin.ignore();
cin.get();
}
// 检查小猫状态是否危急
if (!myKitten.isAlive()) {
clearScreen();
cout << "\n很遗憾," << myKitten.getName() << " 离开了我们..." << endl;
cout << "最终统计数据:" << endl;
stats.showStats();
cout << "游戏结束。" << endl;
}
}
return 0;
}