一、设计背景
第一次学校课程设计作业,初出茅庐之作,4000多行代码,不过没有分文件编写导致看起来繁杂,并且经验不足存在诸多bug,但是功能丰富性已经尽量做到最大程度。仅记录留念,如果有需要思路的可以看看(小声:其实这个代码就是一坨灾难),日后编程经验丰富了想再回来修改修改
二、程序简介
是一款文字游戏,没有做到可视化管理(当时还没有接触到EasyX之类的图形库),而且很多东西都是查了一堆资料我也没参透用法就直接照搬的,导致后来有一些代码可能并没有实际作用但最后也删除不了成了*山(lll¬ω¬)。包含玩家创建、宝可梦获取、宝可梦培育、道具使用、商店系统、地域选择、对战系统等功能,不过有些知识不完善比如数据储存,我是不知道对于游戏数据该怎么保存并读取,以及一些隐性bug,而且其实UI设计也不够到位,有些错误判断应该还未完善。不过自己独自一人奋战那么久做出来的第一个成果还是很有成就感的!以及求求大佬轻喷QAQ
三、代码部分展示
// 宝可梦基类
class Pokemon
{
public:
string Name; // 宝可梦名称
string Type; // 宝可梦属性
int LV; // 宝可梦等级
int EX; // 宝可梦经验
int MaxHP; // 宝可梦最大血量
int CurrentHP; // 宝可梦当前血量
int MaxSP; // 宝可梦最大体力
int CurrentSP; // 宝可梦当前体力
int ATK; // 宝可梦攻击力
int DEF; // 宝可梦防御力
int SPD; // 宝可梦速度
pair<int, int> HP_Growth; // 血量成长数值
pair<int, int> SP_Growth; // 体力成长数值
pair<int, int> ATK_Growth; // 攻击力成长数值
pair<int, int> DEF_Growth; // 防御力成长数值
pair<int, int> SPD_Growth; // 速度成长数值
int EvolveStage; // 进化阶段,默认为1
int EvolveLevel1 = 25; // 第一阶段进化等级门槛
int EvolveLevel2 = 55; // 第二阶段进化等级门槛
vector<Skill> Skills;
vector<Skill> SkillStorage;
vector<BuffEffect> BuffEffects;
SkillManager<Pokemon> skillManager; // 技能管理器
Pokemon(string n, string t, int l, int e, int h, int s, int a, int d, int p, pair<int, int> hg, pair<int, int> sg, pair<int, int> ag, pair<int, int> dg, pair<int, int> pg)
: Name(n), Type(t), LV(l), EX(e), MaxHP(h), MaxSP(s), ATK(a), DEF(d), SPD(p), HP_Growth(hg), SP_Growth(sg), ATK_Growth(ag), DEF_Growth(dg), SPD_Growth(pg) {}
void AddSkill(Skill newSkill)
{
if (Skills.size() < 4)
{
// 技能列表还没满,直接加入新技能
Skills.push_back(newSkill);
cout << Name << "学会了新技能:" << newSkill.Name << " !" << endl;
}
else
{
// 技能列表已满,将新技能放入技能仓库
SkillStorage.push_back(newSkill);
cout << "宝可梦:" << Name <<"的携带技能列表已满,新技能:" << newSkill.Name << " 已放入技能仓库。" << endl;
}
}
void ReplaceSkill(int index)
{
if (index < 0 || index >= static_cast<int>(Skills.size()))
{
cout << "无效的技能编号!" << endl;
return;
}
if (SkillStorage.empty())
{
return;
}
// 显示技能仓库中的可替换技能
cout << "可供选择替换的技能有:" << endl;
for (int i = 0; i < static_cast<int>(SkillStorage.size()); i++)
cout << i + 1 << ". " << SkillStorage[i].Name << endl;
int storageIndex;
cout << "请输入要替换成的技能编号:";
cin >> storageIndex;
if (storageIndex <= 0 || storageIndex > static_cast<int>(SkillStorage.size()))
{
cout << "无效的技能编号!" << endl;
return;
}
// 将被替换的技能放入技能仓库之前先移除相同的技能
SkillStorage.erase(SkillStorage.begin() + storageIndex - 1);
// 替换技能
Skill newSkill = Skills[index]; // 保存新技能
Skills[index] = SkillStorage[storageIndex - 1];
cout << "的技能 " << index + 1 << " 已成功替换为 " << Skills[index].Name << "!" << endl;
// 将更新后的技能列表存入技能管理器中
skillManager.Skills = Skills;
// 将被替换的技能放入技能仓库
SkillStorage.push_back(newSkill);
}
// 升级门槛
int GetLevelThreshold(int level) const
{
static constexpr int MaxLevel = 100; // 满级100
if (level <= 0 || level > MaxLevel)
return -1; // 返回无效的等级
int threshold = 0;
if (level >= 1 && level <= 25)
{
// 1级到25级每级增加100经验
threshold = level * 100;
}
else if (level > 25 && level <= 55)
{
// 25级到55级每级增加500经验
threshold = 2500 + (level - 25) * 500;
}
else if (level > 55 && level <= 100)
{
// 55级到100级每级的经验是前一级的1.25倍
int prevLevelThreshold = GetLevelThreshold(level - 1);
threshold = prevLevelThreshold * 1.25;
}
return threshold;
}
int LevelUp()
{
int flag;
if (LV >= 100)
{
cout << "宝可梦" << Name << "已经达到满级!" << endl;
flag = 0;
}
else if(LV >= 1 && LV < 100 ) // 升级
{
if(EX >= GetLevelThreshold(LV))
{
LV++;
cout << "宝可梦" << Name << "升级到了等级 " << LV << "!" << endl;
// 生成随机数
std::uniform_int_distribution<int> dist1(HP_Growth.first, HP_Growth.second);
std::uniform_int_distribution<int> dist2(SP_Growth.first, SP_Growth.second);
std::uniform_int_distribution<int> dist3(ATK_Growth.first, ATK_Growth.second);
std::uniform_int_distribution<int> dist4(DEF_Growth.first, DEF_Growth.second);
std::uniform_int_distribution<int> dist5(SPD_Growth.first, SPD_Growth.second);
// 接收随机数
int HPIncrease = dist1(gen);
int SPIncrease = dist2(gen);
int ATKIncrease = dist3(gen);
int DEFIncrease = dist4(gen);
int SPDIncrease = dist5(gen);
// 计算属性增长
MaxHP += HPIncrease;
MaxSP += SPIncrease;
ATK += ATKIncrease;
DEF += DEFIncrease;
SPD += SPDIncrease;
cout << "血量增加了 " << HPIncrease << " 点。" << endl;
cout << "体力增加了 " << SPIncrease << " 点。" << endl;
cout << "攻击力增加了 " << ATKIncrease << " 点。" << endl;
cout << "防御力增加了 " << DEFIncrease << " 点。" << endl;
cout << "速度增加了 " << SPDIncrease << " 点。" << endl;
flag = 1;
}
}
return flag;
}
// 属性相克判断
double Revision(const Skill& skillType, const Pokemon& pokemonType)
{
if(skillType.Type == "火" && (pokemonType.Type == "火" || pokemonType.Type == "无"))
return 1.0;
else if(skillType.Type == "火" && pokemonType.Type == "水")
return 0.5;
else if(skillType.Type == "火" && pokemonType.Type == "草")
return 2.0;
else if(skillType.Type == "火" && pokemonType.Type == "电")
return 0.9;
else if(skillType.Type == "水" && (pokemonType.Type == "水" || pokemonType.Type == "无"))
return 1.0;
else if(skillType.Type == "水" && pokemonType.Type == "火")
return 2.0;
else if(skillType.Type == "水" && pokemonType.Type == "草")
return 0.5;
else if(skillType.Type == "水" && pokemonType.Type == "电")
return 0.9;
else if(skillType.Type == "草" && (pokemonType.Type == "草" || pokemonType.Type == "无"))
return 1.0;
else if(skillType.Type == "草" && pokemonType.Type == "火")
return 0.5;
else if(skillType.Type == "草" && pokemonType.Type == "水")
return 2.0;
else if(skillType.Type == "草" && pokemonType.Type == "电")
return 0.9;
else if(skillType.Type == "电" && (pokemonType.Type == "电" || pokemonType.Type == "无"))
return 1.0;
else if(skillType.Type == "电" && pokemonType.Type == "火")
return 0.5;
else if(skillType.Type == "电" && pokemonType.Type == "水")
return 2.0;
else if(skillType.Type == "电" && pokemonType.Type == "草")
return 0.9;
else return 1.0;
}
int FinalDamage(Skill& skill, Pokemon& pokemon)
{
// 生成随机值
std::uniform_int_distribution<int> dist(skill.ATK.first, skill.ATK.second);
int random = dist(gen);
// 计算最终伤害
int damage = (pokemon.ATK + random) * Revision(skill, pokemon) - pokemon.DEF;
if(damage >= 0)
return damage;
else return 0;
}
virtual void shuffleSkills() {}; // AI技能随机的范围 虚函数
// 应用BUFF
void ApplyBuff(Skill skill)
{
if (skill.DUR > 0)
{
bool found = false;
for (auto& buff : BuffEffects)
{
if (buff.name == skill.Name)
{
buff.hpBonus += skill.HPP;
buff.atkBonus += skill.ATKP;
buff.defBonus += skill.DEFP;
buff.spdBonus += skill.SPDP;
buff.duration = max(buff.duration, skill.DUR); // 取较大值作为新的回合数
found = true;
break;
}
}
if (!found)
{
BuffEffects.emplace_back(skill.Name, skill.HPP, skill.ATKP, skill.DEFP, skill.SPDP, skill.DUR);
}
cout << endl;
cout << "该宝可梦使用了技能:" << skill.Name << ",恢复生命:" << skill.HPP << ",增益攻击:" << skill.ATKP << ",增益防御:" << skill.DEFP << ",增益速度:" << skill.SPDP << ",持续回合数:" << skill.DUR << endl;
// 记录初始属性值
for (auto& buff : BuffEffects)
{
if (buff.name == skill.Name)
{
buff.initialHPP = max(buff.initialHPP, buff.hpBonus);
buff.initialATKP = max(buff.initialATKP, buff.atkBonus);
buff.initialDEFP = max(buff.initialDEFP, buff.defBonus);
buff.initialSPDP = max(buff.initialSPDP, buff.spdBonus);
break;
}
}
}
}
// 更新BUFF
void UpdateBuff()
{
for (auto it = BuffEffects.begin(); it != BuffEffects.end();)
{
it->duration--;
if (it->duration == 0)
{
cout << Name << "的技能增益效果:" << it->name << ",消失了!" << endl;
// 计算需要减少的属性值
int decreaseATKP = it->atkBonus - it->initialATKP;
int decreaseDEFP = it->defBonus - it->initialDEFP;
int decreaseSPDP = it->spdBonus - it->initialSPDP;
// 进行属性值的消去
ATK -= max(0, decreaseATKP);
DEF -= max(0, decreaseDEFP);
SPD -= max(0, decreaseSPDP);
it = BuffEffects.erase(it);
}
else ++it;
}
}
virtual ~Pokemon() = default; // 添加虚析构函数
};