《DEV小游戏》
2026-05-19 14:42:46
发布于:江苏
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
struct Item
{
string name;
string desc;
int type;
int value;
int price;
int count;
};
struct DropGoods
{
string name;
int sellGold;
int count;
};
struct Charm
{
string name;
string skill;
string effect;
int level;
int cd;
int maxCd;
bool have;
};
struct Monster
{
string name;
int hp;
int atk;
int dropGoldMin;
int dropGoldMax;
};
class Player
{
public:
int hp, maxHp, atk, luck, gold;
vector<Charm> charmBag;
vector<Item> itemBag;
vector<DropGoods> dropBag;
Player()
{
hp = 120;
maxHp = 120;
atk = 12;
luck = 5;
gold = 200;
addItem("小型生命药水", "恢复30点生命值", 1, 30, 50, 2);
addItem("冷却精华", "清空单个符咒冷却", 2, 1, 80, 1);
}
void addItem(string n, string d, int t, int v, int p, int c)
{
for (int i = 0; i < itemBag.size(); i++)
{
if (itemBag[i].name == n)
{
itemBag[i].count += c;
return;
}
}
Item temp;
temp.name = n;
temp.desc = d;
temp.type = t;
temp.value = v;
temp.price = p;
temp.count = c;
itemBag.push_back(temp);
}
void addDrop(string n, int g, int c)
{
for (int i = 0; i < dropBag.size(); i++)
{
if (dropBag[i].name == n)
{
dropBag[i].count += c;
return;
}
}
DropGoods temp;
temp.name = n;
temp.sellGold = g;
temp.count = c;
dropBag.push_back(temp);
}
int showItem()
{
cout << "\n========玩家道具背包========" << endl;
if (itemBag.empty()) cout << "暂无任何道具" << endl;
else for (int i = 0; i < itemBag.size(); i++)
cout << i + 1 << "." << itemBag[i].name << " 数量:" << itemBag[i].count << " 效果:" << itemBag[i].desc << endl;
cout << "============================" << endl;
cout << "输入13关闭页面" << endl;
int x; cin >> x;
return x;
}
int showDrop()
{
cout << "\n=======战利品仓库=======" << endl;
if (dropBag.empty()) cout << "暂无战利品" << endl;
else for (int i = 0; i < dropBag.size(); i++)
cout << i + 1 << "." << dropBag[i].name << " 数量:" << dropBag[i].count << " 可卖:" << dropBag[i].sellGold << "金币" << endl;
cout << "========================" << endl;
cout << "输入13关闭页面" << endl;
int x; cin >> x;
return x;
}
int showCharm()
{
cout << "\n=======拥有符咒=======" << endl;
if (charmBag.empty()) cout << "暂未拥有符咒" << endl;
else for (int i = 0; i < charmBag.size(); i++)
cout << i + 1 << "." << charmBag[i].name << " Lv." << charmBag[i].level << " 冷却:" << charmBag[i].cd << "/" << charmBag[i].maxCd << " 技能:" << charmBag[i].skill << endl;
cout << "======================" << endl;
cout << "输入13关闭页面" << endl;
int x; cin >> x;
return x;
}
bool useFightItem(int idx, Monster &m)
{
if (idx < 1 || idx > itemBag.size()) return false;
if (itemBag[idx - 1].count <= 0) { cout << "该道具已用完!" << endl; return false; }
Item &it = itemBag[idx - 1];
it.count--;
cout << "\n使用道具:" << it.name << endl;
if (it.type == 1)
{
hp = hp + it.value > maxHp ? maxHp : hp + it.value;
cout << "成功恢复" << it.value << "点生命值" << endl;
}
else if (it.type == 2)
{
if (!charmBag.empty())
{
int r = rand() % charmBag.size();
charmBag[r].cd = 0;
cout << "已清空" << charmBag[r].name << "冷却" << endl;
}
}
else if (it.type == 3)
{
atk += it.value;
cout << "临时提升" << it.value << "点攻击力" << endl;
}
if (it.count <= 0) itemBag.erase(itemBag.begin() + idx - 1);
return true;
}
void cutAllCd()
{
for (int i = 0; i < charmBag.size(); i++)
if (charmBag[i].cd > 0) charmBag[i].cd--;
}
void getNewCharm(Charm c)
{
bool flag = false;
for (int i = 0; i < charmBag.size(); i++)
{
if (charmBag[i].name == c.name)
{
charmBag[i].level++;
cout << "\n" << c.name << "升级至Lv." << charmBag[i].level << endl;
flag = true; break;
}
}
if (!flag)
{
c.have = true; c.level = 1; c.cd = 0;
charmBag.push_back(c);
cout << "\n获得新符咒:" << c.name << endl;
}
applyCharmBuff();
Sleep(1000);
}
int useCharmSkill(int idx, Monster &m)
{
if (idx < 1 || idx > charmBag.size()) return 0;
int pos = idx - 1;
if (charmBag[pos].cd > 0)
{
cout << "技能冷却中!还需" << charmBag[pos].cd << "回合" << endl;
return -1;
}
charmBag[pos].cd = charmBag[pos].maxCd;
Charm ch = charmBag[pos];
cout << "\n释放技能:" << ch.name << " Lv." << ch.level << endl;
int damage = 0;
if (ch.name == "鼠符咒")damage = (atk + 15) * ch.level;
if (ch.name == "牛符咒")damage = (atk * 2) * ch.level;
if (ch.name == "虎符咒")damage = (atk + 25) * ch.level;
if (ch.name == "兔符咒")damage = (atk + 10) * ch.level;
if (ch.name == "龙符咒")damage = (atk + 50) * ch.level;
if (ch.name == "蛇符咒") { cout << "隐身,本回合无敌" << endl; return 0; }
if (ch.name == "马符咒") { hp = hp + 40 * ch.level > maxHp ? maxHp : hp + 40 * ch.level; cout << "生命恢复" << endl; return 0; }
if (ch.name == "羊符咒")damage = (atk + 35) * ch.level;
if (ch.name == "猴符咒") { m.atk = m.atk - 10 * ch.level < 5 ? 5 : m.atk - 10 * ch.level; damage = (atk + 10) * ch.level; }
if (ch.name == "鸡符咒")damage = (atk + 20) * ch.level;
if (ch.name == "狗符咒") { hp = hp + 30 * ch.level > maxHp ? maxHp : hp + 30 * ch.level; cout << "不死守护" << endl; return 0; }
if (ch.name == "猪符咒")damage = (atk + 45) * ch.level;
cout << "造成" << damage << "点伤害" << endl;
return damage;
}
void applyCharmBuff()
{
for (int i = 0; i < charmBag.size(); i++)
{
if (charmBag[i].name == "牛符咒")atk += 3 * charmBag[i].level;
if (charmBag[i].name == "狗符咒")maxHp += 20 * charmBag[i].level;
if (charmBag[i].name == "鼠符咒")luck += 2 * charmBag[i].level;
}
hp = maxHp;
}
void battleEvent()
{
int r = rand() % 100;
if (r < 15)addItem("大型生命药水", "恢复80生命值", 1, 80, 120, 1);
else if (r < 30)gold += 50;
else if (r < 40)addDrop("魔物碎片", 30, 1);
}
};
vector<Charm> initAllCharm()
{
vector<Charm> c;
Charm ch1;
ch1.name = "鼠符咒";
ch1.skill = "召唤石像";
ch1.effect = "召唤魔物助战";
ch1.level = 1;
ch1.cd = 0;
ch1.maxCd = 3;
ch1.have = false;
c.push_back(ch1);
Charm ch2;
ch2.name = "牛符咒";
ch2.skill = "力量狂击";
ch2.effect = "高额物理伤害";
ch2.level = 1;
ch2.cd = 0;
ch2.maxCd = 2;
ch2.have = false;
c.push_back(ch2);
Charm ch3;
ch3.name = "虎符咒";
ch3.skill = "阴阳之力";
ch3.effect = "均衡高额输出";
ch3.level = 1;
ch3.cd = 0;
ch3.maxCd = 3;
ch3.have = false;
c.push_back(ch3);
Charm ch4;
ch4.name = "兔符咒";
ch4.skill = "极速突袭";
ch4.effect = "高速先手攻击";
ch4.level = 1;
ch4.cd = 0;
ch4.maxCd = 2;
ch4.have = false;
c.push_back(ch4);
Charm ch5;
ch5.name = "龙符咒";
ch5.skill = "烈焰轰炸";
ch5.effect = "大范围火焰伤害";
ch5.level = 1;
ch5.cd = 0;
ch5.maxCd = 4;
ch5.have = false;
c.push_back(ch5);
Charm ch6;
ch6.name = "蛇符咒";
ch6.skill = "隐遁无形";
ch6.effect = "本回合完全无敌";
ch6.level = 1;
ch6.cd = 0;
ch6.maxCd = 4;
ch6.have = false;
c.push_back(ch6);
Charm ch7;
ch7.name = "马符咒";
ch7.skill = "圣光治愈";
ch7.effect = "巨额生命回复";
ch7.level = 1;
ch7.cd = 0;
ch7.maxCd = 5;
ch7.have = false;
c.push_back(ch7);
Charm ch8;
ch8.name = "羊符咒";
ch8.skill = "灵魂穿刺";
ch8.effect = "无视敌方防御";
ch8.level = 1;
ch8.cd = 0;
ch8.maxCd = 3;
ch8.have = false;
c.push_back(ch8);
Charm ch9;
ch9.name = "猴符咒";
ch9.skill = "万物异变";
ch9.effect = "大幅度削弱敌人";
ch9.level = 1;
ch9.cd = 0;
ch9.maxCd = 3;
ch9.have = false;
c.push_back(ch9);
Charm ch10;
ch10.name = "鸡符咒";
ch10.skill = "凌空飞袭";
ch10.effect = "空中强力突袭";
ch10.level = 1;
ch10.cd = 0;
ch10.maxCd = 2;
ch10.have = false;
c.push_back(ch10);
Charm ch11;
ch11.name = "狗符咒";
ch11.skill = "永生不灭";
ch11.effect = "残血保命回血";
ch11.level = 1;
ch11.cd = 0;
ch11.maxCd = 5;
ch11.have = false;
c.push_back(ch11);
Charm ch12;
ch12.name = "猪符咒";
ch12.skill = "雷霆电眼";
ch12.effect = "超强雷电暴击";
ch12.level = 1;
ch12.cd = 0;
ch12.maxCd = 4;
ch12.have = false;
c.push_back(ch12);
return c;
}
vector<Monster> initAllMonster()
{
vector<Monster>m;
Monster m1; m1.name = "黑影小兵"; m1.hp = 120; m1.atk = 12; m1.dropGoldMin = 30; m1.dropGoldMax = 60; m.push_back(m1);
Monster m2; m2.name = "秘境守卫"; m2.hp = 180; m2.atk = 16; m2.dropGoldMin = 50; m2.dropGoldMax = 90; m.push_back(m2);
Monster m3; m3.name = "恶魔使徒"; m3.hp = 260; m3.atk = 22; m3.dropGoldMin = 80; m3.dropGoldMax = 130; m.push_back(m3);
Monster m4; m4.name = "暗影魔王"; m4.hp = 900; m4.atk = 35; m4.dropGoldMin = 200; m4.dropGoldMax = 350; m.push_back(m4);
Monster m5; m5.name = "幽影猎手"; m5.hp = 320; m5.atk = 28; m5.dropGoldMin = 100; m5.dropGoldMax = 150; m.push_back(m5);
Monster m6; m6.name = "火焰巨魔"; m6.hp = 450; m6.atk = 38; m6.dropGoldMin = 140; m6.dropGoldMax = 200; m.push_back(m6);
Monster m7; m7.name = "冰霜女巫"; m7.hp = 550; m7.atk = 45; m7.dropGoldMin = 180; m7.dropGoldMax = 250; m.push_back(m7);
Monster m8; m8.name = "深渊魔神"; m8.hp = 1800; m8.atk = 70; m8.dropGoldMin = 400; m8.dropGoldMax = 600; m.push_back(m8);
return m;
}
void shopSystem(Player &p)
{
while (1)
{
system("cls");
cout << "========商店========" << endl;
cout << "金币:" << p.gold << endl;
cout << "1.小血瓶 50" << endl;
cout << "2.大血瓶 120" << endl;
cout << "3.冷却精华 80" << endl;
cout << "4.力量药剂 150(永久+5攻)" << endl;
cout << "5.兑换战利品" << endl;
cout << "6.返回主界面" << endl;
int op; cin >> op;
if (op == 1) { if (p.gold >= 50) { p.gold -= 50; p.addItem("小型生命药水", "恢复30HP", 1, 30, 50, 1); cout << "购买成功!"; } }
else if (op == 2) { if (p.gold >= 120) { p.gold -= 120; p.addItem("大型生命药水", "恢复80HP", 1, 80, 120, 1); cout << "购买成功!"; } }
else if (op == 3) { if (p.gold >= 80) { p.gold -= 80; p.addItem("冷却精华", "清空CD", 2, 1, 80, 1); cout << "购买成功!"; } }
else if (op == 4) { if (p.gold >= 150) { p.gold -= 150; p.atk += 5; cout << "攻击力提升!"; } }
else if (op == 5)
{
int r = p.showDrop();
if (r == 13)continue;
}
else if (op == 6)break;
Sleep(800);
}
}
bool fightBattle(Player &p, Monster m)
{
while (1)
{
system("cls");
cout << "战斗:" << m.name << endl;
cout << "你的HP:" << p.hp << "/" << p.maxHp << " 敌人HP:" << m.hp << endl;
cout << "1.攻击 2.符咒 3.道具 4.查看状态 5.逃跑" << endl;
int op; cin >> op;
int hurt = 0;
bool hide = false;
if (op == 1)
{
hurt = p.atk + rand() % 8;
m.hp -= hurt;
cout << "你造成" << hurt << "点伤害!" << endl;
}
else if (op == 2)
{
int s = p.showCharm();
if (s == 13)continue;
hurt = p.useCharmSkill(s, m);
if (hurt == -1)continue;
m.hp -= hurt;
if (s <= p.charmBag.size() && p.charmBag[s - 1].name == "蛇符咒") hide = true;
}
else if (op == 3)
{
int s = p.showItem();
if (s == 13)continue;
p.useFightItem(s, m);
continue;
}
else if (op == 4)
{
cout << "攻击:" << p.atk << " 生命:" << p.hp << "/" << p.maxHp << " 金币:" << p.gold << endl;
cout << "输入13关闭" << endl;
int x; cin >> x;
continue;
}
else if (op == 5)
{
cout << "逃跑成功!" << endl;
Sleep(800);
return false;
}
if (m.hp <= 0)
{
cout << "胜利!" << endl;
int g = rand() % (m.dropGoldMax - m.dropGoldMin + 1) + m.dropGoldMin;
p.gold += g;
cout << "获得金币:" << g << endl;
p.battleEvent();
Sleep(1500);
return true;
}
if (!hide)
{
int eh = m.atk + rand() % 6;
p.hp -= eh;
cout << "受到" << eh << "点伤害!" << endl;
}
else cout << "闪避攻击!" << endl;
p.cutAllCd();
if (p.hp <= 0)
{
bool dog = false;
for (int i = 0; i < p.charmBag.size(); i++) {
if (p.charmBag[i].name == "狗符咒") {
dog = true;
}
}
if (dog)
{
p.hp = 50;
cout << "狗符咒生效,保命!" << endl;
}
else
{
cout << "你被击败了!" << endl;
Sleep(1500);
return false;
}
}
Sleep(1200);
}
}
void mainMenu(Player &p, vector<Monster> &mon, vector<Charm> &charm)
{
while (1)
{
system("cls");
cout << "====== 主界面 ======" << endl;
cout << "1.进入关卡挑战" << endl;
cout << "2.进入商店" << endl;
cout << "3.查看背包" << endl;
cout << "4.查看符咒" << endl;
cout << "5.退出游戏" << endl;
int op; cin >> op;
if (op == 1)
{
system("cls");
cout << "选择关卡:" << endl;
cout << "1.新手关卡" << endl;
cout << "2.进阶关卡" << endl;
cout << "3.精英关卡" << endl;
cout << "4.BOSS挑战" << endl;
int o; cin >> o;
int idx = 0;
if (o == 1) idx = rand() % 3;
else if (o == 2) idx = 3 + rand() % 2;
else if (o == 3) idx = 5 + rand() % 2;
else idx = 7;
fightBattle(p, mon[idx]);
p.hp = p.maxHp;
}
else if (op == 2) shopSystem(p);
else if (op == 3) p.showItem();
else if (op == 4) p.showCharm();
else if (op == 5) exit(0);
}
}
int main()
{
srand(time(0));
Player player;
vector<Charm> charms = initAllCharm();
vector<Monster> mons = initAllMonster();
// ========== 开局送一张随机符咒 ==========
int randomIdx = rand() % charms.size();
Charm startCharm = charms[randomIdx];
player.getNewCharm(startCharm);
cout << "开局赠送随机符咒成功!" << endl;
Sleep(1500);
// =======================================
cout << "欢迎来到符咒RPG!" << endl;
Sleep(1000);
mainMenu(player, mons, charms);
return 0;
}
全部评论 4

2天前 来自 浙江
0666
2天前 来自 浙江
0+66
2天前 来自 浙江
01
2天前 来自 浙江
0

























有帮助,赞一个