//RMC2-起源 第二版重写,开始时间2025/1/1
//主题,异世界的奇幻冒险故事
//...
//加了一点注释,方便自己和他人阅读
//注:本游戏基于小熊猫DEVC++ 3.1.2871开发
#include<bits/stdc++.h>
#include<windows.h>
#include<thread>
#include<conio.h>
using namespace std;
bool xsjc = 0;
char ch;
//地图
string jcdt[30] = {
" ",
" ",
" ",
" ",
" ",
" ",
" 墙墙墙墙墙墙墙墙墙墙 ",
" 墙 墙 ",
" 墙墙墙墙墙墙墙墙墙墙墙墙墙 墙 ",
" 墙 墙 ",
" 墙 墙墙墙墙 墙 ",
" 墙墙 墙 墙 墙 墙墙 ",
" 墙 墙 墙 墙 ",
" 墙 墙 墙 墙 ",
" 墙 墙 ",
" 墙 墙墙墙墙墙墙墙墙 墙 墙 墙 墙 ",
" 墙墙墙 墙墙墙墙 墙 墙 墙 墙 墙 ",
" 墙墙墙墙 墙 墙 墙 ",
" 墙 墙墙墙墙墙墙墙墙墙墙墙墙墙墙墙 墙 ",
" 墙 ",
" 墙墙墙墙墙墙墙墙墙墙 墙 墙 ",
" 墙墙 墙墙 ",
" 墙 墙 ",
" 墙 墙 ",
" 墙墙墙 墙墙 ",
" ",
" ",
" ",
" ",
" "
};
//我吸取了第一版的教训,每个武器是不可能有一个通用的模版的,我们应该给每一个武器都写一个派生类,统一归类到基类item上。
//物品这一段还没写好,后面再写
// 基类 Item
class Item {
public:
virtual ~Item() = default;
virtual void use() const = 0; // 纯虚函数,表示每个派生类都需要实现这个函数
virtual string getName() const = 0; // 纯虚函数,返回物品名称
};
// 派生类 Bronze_Sword
class Bronze_Sword : public Item {
public:
void use() const override {
cout << "Using Sword: Deal 2 damage!" << endl;
}
string getName() const override {
return "Sword";
}
};
// 背包类 Inventory
class Inventory {
private:
vector<unique_ptr<Item>> items;
public:
void addItem(unique_ptr<Item> item) {
items.push_back(move(item));
}
void useItem(size_t index) const {
if (index < items.size()) {
items[index]->use();
} else {
cout << "Error:Invalid item index!" << endl;
}
}
void listItems() const {
for (size_t i = 0; i < items.size(); ++i) {
cout << "Item " << i << ": " << items[i]->getName() << endl;
}
}
void removeItem(size_t index) {
if (index < items.size()) {
items.erase(items.begin() + index); // 移除指定索引处的物品
} else {
cout << "Error:Invalid item index!" << endl;
}
}
};
// 玩家类 Player
class Player {
public:
int x, y; //坐标
int hp, hpmax; //生命值及其上限
int mp, mpmax; //魔法值及其上限
int kx;//抗性(伤害减免)
int hf;//每回合的生命恢复值
int sh;//基础伤害
double Exp;//经验值
int lv;//等级
Inventory inventory;//背包
void attack(Player &x) { //攻击
x.hp -= sh - x.kx;
}
Player() : hp(20), mp(20), kx(0), hf(0), sh(1), Exp(0), lv(0), hpmax(20), mpmax(20) {}//初始化列表
void addExp(double t) {//添加经验并检查是否可以升级
Exp += t;
while (1) {
if ((double)((pow(lv + 1, 1.5) / 2) * 1.4 + 100) <= Exp) {
lv++;
Exp -= (double)((pow(lv, 1.5) / 2) * 1.4 + 100);
//cout<<"level"<<lv<<"="<<(double)((pow(lv,1.5)/2)*1.4+100)<<endl;
} else {
break;
}
}
}
} play;
int ti(float a) {//Setpos辅助函数
return ((int)(a * 10 + 5)) / 10;
}
void Setpos(float x, float y) { //移动光标位置
COORD pos;
pos.X = ti(y * 4) / 2;
pos.Y = ti(x);
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void Color(int a) {//调整颜色的函数
if (a == 0 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (a == 1 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (a == 2 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
if (a == 3 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
if (a == 4 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
if (a == 5 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
if (a == 6 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
if (a == 7 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (a == 8 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
if (a == 9 ) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE);
if (a == 10) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE);
if (a == 11) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_BLUE);
if (a == 12) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN);
if (a == 13) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
if (a == 14) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE);
}
void say(string str) {//输出1(带括号)
cout << "「";
Sleep(40);
for (int i = 0; i < str.size(); i++) {
cout << str[i];
Sleep(40);
}
cout << "」" << endl;
Sleep(250);
}
void Slowsay(string a) {//输出2(不带括号)
int l = a.size();
for (int i = 0; i < l; i++) {
cout << a[i];
Sleep(20);
}
printf("\n");
Sleep(250);
}
void print() {//打印地图
for (int i = 0; i < 30; i++) {
Setpos(i + 1, 0.5);
cout << jcdt[i];
}
}
void fu_ping_mu() { //副屏幕函数
Color(8);
Setpos(1, 65.5);
cout << " RMC ";
Color(5);
Setpos(3, 65.5);
cout << "地 点:战争古迹";
Setpos(4, 65.5);
cout << "剧 情:新手教程";
Setpos(8, 65.5);
cout << "血 量:" << play.hp;
Setpos(9, 65.5);
cout << "等 级:" << play.lv;
Setpos(10, 65.5);
cout << "经 验 值:" << play.Exp;
/*play.Exp=123456;
play.levelup();
Setpos(10, 65.5);
cout<<play.lv<<" "<<play.Exp;*/
}
//int x,y;//坐标
//int hp;//生命值
//int mp;//魔法值
//int kx;//抗性(伤害减免)
//int hf;//每回合的生命恢复值
//int sh;//基础伤害
void bag() {
Color(0);
system("cls");
cout << "┌─────────────────────────────────────────────────────────────────────────────────────────────┬──────┬──────┬──────┬──────┬──────┬────────────────────┐" << endl;
cout << "│ │ │ │ │ │ │ │" << endl;
cout << "│ ┌────────────────────┐ ┌────────────────────┐ ┌────────────────────┐ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ └────────────────────┘ └────────────────────┘ └────────────────────┘ │ │ │ │ │ │ │" << endl;
cout << "│ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ ┌────────────────────┐ ┌────────────────────┐ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ │ │ │ │ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │ │ │ │ │" << endl;
cout << "│ └────────────────────┘ └────────────────────┘ ├──────┼──────┼──────┼──────┼──────┤ │" << endl;
cout << "│ │ │ │ │ │ │ │" << endl;
cout << "│ └──────┴──────┴──────┴──────┴──────┤ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────────────────┘" << endl;
fu_ping_mu();
Color(1);
Setpos(2, 39);
cout << "x坐标:" << play.x;
Setpos(4, 39);
cout << "y坐标:" << play.y;
Color(2);
Setpos(7, 39);
cout << "生命值:" << play.hp;
Setpos(9, 39);
cout << "生命值上限:" << play.hpmax;
Setpos(11, 39);
cout << "魔法值:" << play.mp;
Setpos(13, 39);
cout << "魔法值上限:" << play.mpmax;
Color(5);
Setpos(16, 39);
cout << "伤害减免:" << play.kx;
Setpos(18, 39);
cout << "生命回复:" << play.hf;
Setpos(20, 39);
cout << "基础伤害:" << play.sh;
Color(12);
Setpos(23, 39);
cout << "等级:" << play.lv;
Setpos(25, 39);
cout << "经验值:" << play.Exp;
Color(4);
Setpos(1, 5);
cout << "主手武器 头部装备 胸部装备";
Setpos(14, 5);
cout << " 腿部装备 脚部装备";
Setpos(33, 0);
system("pause");//物品还没做好,这一段先省略了
Color(0);
system("cls");
cout << "┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────────────────┐" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────────────────┘" << endl;
print();
}
void jiao_cheng() {
Color(0);
system("cls");
cout << "┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────────────────┐" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "│ │ │" << endl;
cout << "└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────────────────┘" << endl;
print();//更新地图,在角色移动时,地图的更新是局部的,这样可以避免闪屏带来的卡顿和闪烁
play.x = 14, play.y = 66; //角色初始位置
int jd = 0;
Setpos(33, 1);
cout << "按下wasd移动,按下空格推进剧情";
while (1) {
fu_ping_mu();//更新副屏幕
Color(5);
Setpos(play.x + 1, play.y / 2 + 0.5);
cout << "我"; //显示玩家的位置
while (1)if (_kbhit())break;
ch = _getch();
Color(0);
if (ch == 'w' || ch == 'a' || ch == 's' || ch == 'd') {
Setpos(play.x + 1, play.y / 2 + 0.5);
cout << jcdt[play.x][play.y] << jcdt[play.x][play.y + 1]; //覆盖玩家现在的位置
}
if (ch == 'w' && play.x > 0)play.x--;
else if (ch == 'a' && play.y > 1)play.y -= 2;
else if (ch == 's' && play.x < 29)play.x++;
else if (ch == 'd' && play.y < 126)play.y += 2;
else if (ch == ' ') {
jd++;
Setpos(33, 1);
if (jd == 1) {
cout << "现在,看到你的侧边栏,这里依次显示了你的各种信息,如血量等等。";
} else if (jd == 2) {
cout << "按下e键可以查看你的背包,再按一次e可以返回,试试看吧! ";
}
} else if (ch == 'e') {
bag();
Setpos(33, 1);
if(jd==0){
cout << "按下wasd移动,按下空格推进剧情";
}else if (jd == 1) {
cout << "现在,看到你的侧边栏,这里依次显示了你的各种信息,如血量等等。";
} else if (jd == 2) {
cout << "按下e键可以查看你的背包,再按一次e可以返回,试试看吧! ";
}
}
}
}
void game() {//(pow(x,1.5)/150)*1.25+100
;
}
void start() { //游戏主界面
Color(0);
system("cls");
cout << "┌────────────────────────────────────────────────────────────┐" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "└────────────────────────────────────────────────────────────┘" << endl;
Color(8);
Setpos(1, 8.5);
cout << "RRRRRR M M CCCCCC ";
Setpos(2, 8.5);
cout << "R R MM MM CC C";
Setpos(3, 8.5);
cout << "R R M M M M CC ";
Setpos(4, 8.5);
cout << "RRRRRR M MM M C ";
Setpos(5, 8.5);
cout << "RRR M M CC ";
Setpos(6, 8.5);
cout << "R RR M M CC C";
Setpos(7, 8.5);
cout << "R RR M M CCCCCC ";
Color(13);
Setpos(8, 1);
cout << "····-----──────────────────<()>──────────────────-----····";
Color(2);
Setpos(10, 13.5);
cout << "开始游戏";
Setpos(11, 13.5);
cout << "新手教程";
Setpos(12, 13.5);
cout << "退出游戏";
Setpos(33, 1);
Color(13);
cout << "按w,s选取功能,空格确定" << endl;
Color(5);
int xs = 1;
while (1) {
Setpos(9 + xs, 13);//更新选取符号
cout << ">";
Setpos(9 + xs, 17.5);
cout << "<";
while (1)if (kbhit())break;//这两行是用来检测按键是否按下的,没按下就继续等待
ch = getch();
Setpos(9 + xs, 13);//清除之前的选取符号
cout << " ";
Setpos(9 + xs, 17.5);
cout << " ";
if (ch == 'w' && xs > 1)xs--;
else if (ch == 's' && xs < 3)xs++;
else if (ch == ' ') {
if (xs == 1) {
if (!xsjc) {
Color(0);
system("cls");
cout << "┌────────────────────────────────────────────────────────────┐" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "│ │" << endl;
cout << "└────────────────────────────────────────────────────────────┘" << endl;
Color(4);
Setpos(1, 1);
Slowsay("您还没有通过新手教程!");
Setpos(2, 1);
Slowsay("我们建议您先通过新手教程,在进行游戏,不过您也可以直接开始");
Setpos(3, 1);
Slowsay("游戏,但我们不建议您这样做。");
Sleep(200);
Color(5);
Setpos(5, 1);
cout << "按1:回到开始界面";
Setpos(6, 1);
cout << "按2:直接开始游戏";
ch = ' ';//避免不必要的bug
while (ch != '1' && ch != '2') {
while (1)if (kbhit())break;
ch = getch();
}
if (ch == '1')start();
}
game();
} else if (xs == 2) {
jiao_cheng();
} else if (xs == 3) {
Color(0);
Setpos(34, 0);
exit(0);
}
}
}
}
int main() {
//隐藏光标,避免闪烁
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
//随机种子
srand((unsigned)time(NULL));
start();
}
dalao们帮忙想想代码能怎么优化。
还有,请洛谷的文豪们帮忙想想剧情吧...自己实在是想不出来了。