C++#include<bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#define ll long long
#define endl "\n"
using namespace std;
char c;
const ll inf=0x3f3f3f3f3f;
ll hp=5,mhp=5,atk=3,df=2,bpl=5,gr=1,eexp=0,bi=400,mxp=2*gr+3,knt=0;
ll alv=1,ent=15,aatk=2,kdf;
ll xhp=5,mxhp=5,xtk=2,xdf=2,xgr=1;
bool bj[20];
int vis;
bool cnt;
string mh=" ";
vector<string>bp(40," "),as(4," "),sk(4," ");
string ol[15]={"[剑]","[瞬伤 I]","[瞬伤 III]","[瞬伤 V]","[治疗 I]","[治疗 III]","[治疗 V]","[力量丹]","[<def>/]"};
ll pr1[15]={40,5,8,11,3,5,11,100,inf};
map<string,int>num,p;
string al[5]={"[头盔]","[胸甲]","[裤子]","[靴子]"};
ll pr2[5]={30,50,40,30};
ll fdf[5]={3,5,4,3};
map<string,int>als;
string sl[5]={"[暴怒]","[复活]","[恢复]","[荆棘]"};
ll pr3[5]={400,400,500,200};
string xname[10]={"[骷髅]","[僵尸]","[玩家]","[史莱姆]","[坤坤]","[狼王]","[骷髅王]","[尸王]","[神]"},xnam,name;
bool bossf,godf;
string cra[7]={"[木棍]","[铁锭]","[药草]","[伤害药草]","[玻璃瓶]","[码源之力]","[容器]"};
mt19937 gen(time(0));
ll ran(ll a,ll b){
if(a>b)swap(a,b);
uniform_int_distribution<ll>dist(a,b);
return dist(gen);
}
void sc(int c){
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h,c);
}
void gotoxy(int x,int y){
COORD position;
position.X=x;
position.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
}
void c_r(string s){
for(char c:s){
cout<<c;
Sleep(50);
}
cout<<endl;
}
void c_r2(string s){
for(char c:s){
cout<<c;
Sleep(50);
}
}
void give(int id);
void xx(){
cout<<"||主手||"<<endl;
if(mh==" ")cout<<"0."<<mh<<endl;
else cout<<"0."<<mh<<" 耐久:["<<ent<<"]等级:["<<alv<<']'<<endl;
cout<<"||背包||"<<endl;
for(int i=0;i<bpl;i++){if(bp[i]!=" ")cout<<i+1<<'.'<<bp[i]<<" 个数:"<<num[bp[i]]<<endl;else cout<<i+1<<'.'<<" "<<endl;}
cout<<"||盔甲||"<<endl;
for(int i=0;i<4;i++){if(as[i]!=" ")cout<<i+1<<'.'<<as[i]<<" 耐久:"<<als[as[i]]<<endl;else cout<<i+1<<'.'<<" "<<endl;}
cout<<"||技能||"<<endl;
for(int i=0;i<4;i++){if(sk[i]!=" ")cout<<i+1<<'.'<<sk[i]<<endl;else cout<<i+1<<'.'<<" "<<endl;}
}
void subi(){
int screenWidth=GetSystemMetrics(SM_CXSCREEN);
int screenHeight=GetSystemMetrics(SM_CYSCREEN);
int x=ran(0ll,inf)%screenWidth;
int y=ran(0ll,inf)%screenHeight;
SetCursorPos(x, y);
Sleep(100);
}
void xuse(string s){
if(s==ol[1]){
cout<<"它使用了"<<s<<endl;
Sleep(500);
hp-=2;
}else if(s==ol[4]){
cout<<"它使用了"<<s<<endl;
Sleep(500);
xhp+=2;
}else if(s==ol[5]){
cout<<"它使用了"<<s<<endl;
Sleep(500);
xhp+=5;
}
}
int ang(){if(hp<=mhp/3&&sk[0]==sl[0])return 2;return 1;}
void shop(){
system("cls");
system("color F6");
while(1){
int pc=0;
string pc2;
cout<<"||商店||"<<endl;
cout<<"$$金币$$:"<<bi<<endl;
cout<<"||物品列表||"<<endl;
for(int i=0;i<9;i++)cout<<i<<'.'<<ol[i]<<" 价格:"<<pr1[i]<<endl;
cout<<"||盔甲列表||"<<endl;
for(int i=0;i<4;i++)cout<<i+9<<'.'<<al[i]<<" 价格:"<<pr2[i]<<endl;
cout<<"||技能列表||"<<endl;
for(int i=0;i<4;i++)cout<<i+13<<'.'<<sl[i]<<" 价格:"<<pr3[i]<<endl;
cout<<"||升级列表||"<<endl;
cout<<"17."<<ol[0]<<" 价格:"<<alv*5-2<<endl;
cout<<"18."<<"[背包格]"<<" 价格:"<<bpl*2-3<<endl;
xx();
cout<<"选择:";
cin>>pc2;
if(pc2.size()==1)pc=pc2[0]-48;
else if(pc2.size()==2)pc=(pc2[0]-48)*10+pc2[1]-48;
else{
Sleep(500);
c_r2("无效选择。");
return;
}
if(pc==0){
if(bi>=pr1[pc]){
if(mh==" "){
mh=ol[pc];
ent=15;
aatk=2;
alv=1;
bi-=pr1[pc];
system("cls");
}else{
c_r2("已有物品。");
Sleep(500);
system("cls");
break;
}
}else{
c_r2("金币不足。");
Sleep(500);
system("cls");
}
}else if(pc>0&&pc<=8){
if(bi>=pr1[pc]){
give(pc);
bi-=pr1[pc];
}else{
c_r2("金币不足。");
Sleep(500);
system("cls");
}
}else if(pc>=9&&pc<=12){
if(bi>=pr2[pc-9]){
if(as[pc-9]==" "){
as[pc-9]=al[pc-9];
bi-=pr2[pc-9];
als[as[pc-9]]=100;
kdf+=fdf[pc-9];
}else{
c_r2("已有盔甲。");
Sleep(500);
system("cls");
}
}else{
c_r2("金币不足。");
Sleep(500);
system("cls");
}
}else if(pc>=13&&pc<=16){
if(bi>=pr3[pc-13]){
if(sk[pc-13]==" "){
sk[pc-13]=sl[pc-13];
bi-=pr3[pc-13];
}else{
c_r2("已有技能。");
Sleep(500);
system("cls");
}
}else{
c_r("金币不足。");
Sleep(500);
system("cls");
}
}else if(pc==17){
if(bi>=alv*5-2&&mh==ol[0]){
bi-=alv*5-2;
alv++;
ent=alv*30;
c_r2("剑已升级。");
Sleep(500);
system("cls");
}else if(bi>=alv*5-2&&mh!=ol[0]){
c_r2("没有剑。");
Sleep(500);
system("cls");
}else if(bi<=alv*5-2&&mh==ol[0]){
c_r2("金币不足。");
Sleep(500);
system("cls");
}else{
c_r2("你来想屁吃是吗???");
Sleep(500);
system("cls");
}
}else if(pc==18){
if(bi>=bpl*2-3&&bpl<9){
bpl++;
c_r2("背包已升级。");
Sleep(500);
system("cls");
}else{
c_r2("金币不足/容量达到上限");
Sleep(500);
system("cls");
}
}else{
c_r2("商店已退出。");
Sleep(500);
system("cls");
return;
}
Sleep(100);
system("cls");
}
}
void give(int id){
bool gf=0;
if(id==0){
if(mh==" "){
mh=ol[id];
ent=15;
aatk=2;
alv=1;
c_r2("你已获得剑。");
Sleep(500);
system("cls");
return;
}else{
c_r2("已有物品。");
Sleep(500);
system("cls");
return;
}
}
for(int i=0;i<bpl;i++){
if(bp[i]==ol[id]){
num[ol[id]]++;
cout<<"获得了 "<<ol[id]<<endl;
Sleep(500);
gf=1;
system("cls");
break;
}
}
if(!gf){
for(int i=0;i<bpl;i++){
if(bp[i]==" "){
num[ol[id]]++;
bp[i]=ol[id];
gf=1;
cout<<"你获得了"<<ol[id]<<"。"<<endl;
Sleep(500);
system("cls");
break;
}
}
}
if(!gf){
c_r("背包不足。");
Sleep(500);
system("cls");
}
}
void lot(){
system("cls");
while(1){
system("cls");
cout<<"<==========[抽奖]==========>"<<endl;
cout<<"1.[高级]:35/次"<<endl;
cout<<"2.[中级]:20/次"<<endl;
cout<<"3.[低级]:10/次"<<endl;
cout<<"$[剩余金币]$:"<<bi<<endl;
cout<<"<==========================>"<<endl;
cout<<"选择:";
string cho;
cin>>cho;
int obj;
if(cho=="1"){
if(bi<35){
cout<<"金币不足"<<endl;
Sleep(500);
system("cls");
}else{
give(ran(0,7));
bi-=35;
system("cls");
}
}else if(cho=="2"){
if(bi<20){
cout<<"金币不足"<<endl;
Sleep(500);
system("cls");
}else{
give(ran(0,6));
bi-=20;
system("cls");
}
}else if(cho=="3"){
if(bi<10){
cout<<"金币不足"<<endl;
Sleep(500);
system("cls");
}else{
give(ran(1,6));
bi-=10;
system("cls");
}
}else{
cout<<"无效输入"<<endl;
Sleep(500);
return;
}
}
}
void als_k(){
for(int i=0;i<4;i++){
if(as[i]!=" "){
als[al[i]]-=ran(1ll,2ll);
if(als[al[i]]<0){
cout<<as[i]<<"破碎了。"<<endl;
kdf-=fdf[i];
als[as[i]]=0;
as[i]=" ";
}
}
}
}
void jj(int x){
if(sk[3]==sl[3]){
cout<<sl[3]<<"造成了"<<max(1ll,x/3ll)<<"点伤害"<<endl;
Sleep(500);
}
}
void p_h(){
bj[vis]=1;
vis++;
int hurt=atk+ran(-2ll,1ll);
if(bj[19]==1){
cout<<endl<<"暴击!";
xhp-=hurt*2;
for(int i=0;i<20;i++)bj[i]=0;
vis=0;
Sleep(500);
system("cls");
return;
}
if(mh==" "){
ll ra=max((hurt-ceil(xdf/3)),double(0));
xhp-=ra;
cout<<endl<<"你造成了"<<ra<<"点伤害"<<endl;
Sleep(500);
}else{
aatk=max(alv*2-1,2ll);
ent--;
ll ra=max((aatk+atk-ceil(xdf/3)),double(0));
xhp-=ra;
cout<<endl<<"你使用了"<<mh<<"造成了"<<ra<<"点伤害。"<<endl;
Sleep(500);
if(ent<0)mh=" ";
}
int xhurt=xtk+ran(-2ll,1ll);
int xra=max((xhurt-ceil(df/3))-kdf/4,double(0));
hp-=xra;
if(godf){
cout<<endl<<xname[8]<<"降下了暴怒的惩罚!"<<endl;
for(int i=1;i<=6;i++)subi();
}else cout<<xnam<<"造成了"<<xra<<"点伤害"<<endl;
if(xhp<=mxhp/3)xuse(ol[ran(1ll,5ll)]);
jj(xhurt);
als_k();
Sleep(500);
system("cls");
return;
}
void use(string s){
cout<<endl<<"你使用了"<<s<<endl;
Sleep(500);
if(s==ol[1])xhp-=2;
else if(s==ol[2])xhp-=4;
else if(s==ol[3])xhp-=8;
else if(s==ol[4])hp+=2;
else if(s==ol[5])hp+=5;
else if(s==ol[6])hp+=7;
else if(s==ol[7])atk+=15;
else if(s==ol[8])xhp-=xhp;
}
void bin(){
system("cls");
while(1){
int cln,money=0;
cout<<"你要回收几个格子:";
cin>>cln;
if(cln==0)return;
c_r2("哪几个格子(序号):");
for(int i=0;i<cln&&i<10;i++){
int slot=0;
cout<<i+1<<'.';
cin>>slot;
if(slot>=10)return;
if(slot==0){
mh=" ";
bi+=alv*3+40;
money+=alv*3+40;
continue;
}
int idx=-1;
for(int j=0;j<15;j++)if(ol[j]==bp[slot-1])idx=j;
if(idx!=-1){
bi+=pr1[idx]*num[bp[slot-1]]*3/4;
money+=pr1[idx]*num[bp[slot-1]]*3/4;
}
num[bp[slot-1]]=0;
bp[slot-1]=" ";
}
cout<<"已回收"<<money<<"金币"<<endl;
Sleep(500);
system("cls");
}
}
void uhp(){
if(hp<mhp/2&&cnt){
hp+=2;
system("cls");
system("color F2");
c_r2("恢复");
Sleep(500);
system("color FB");
system("cls");
return;
}
}
void bos(){
if(knt>0&&knt%50==0){
xgr=gr*3;
bossf=1;
mxhp=xgr*3/2+ran(-1ll,1ll);
xdf=xgr/3+ran(-1ll,1ll);
xtk=max(xgr/2,10ll);
xnam=xname[ran(5ll,7ll)];
xhp=mxhp;
}else if(knt>0&&knt%200==0){
xgr=gr*10;
godf=1;
mxhp=xgr*10+ran(-1ll,1ll);
xdf=xgr/2+ran(-1ll,1ll);
xtk=max(xgr*4/3-3,10ll);
xnam=xname[8];
xhp=mxhp;
}
}
int dead(){
if(hp<=0){
if(sk[1]==sl[1]){
system("cls");
c_r("[复活]技能触发了!");
hp=mhp;
sk[1]=" ";
Sleep(1000);
system("cls");
return 1;
}else{
system("cls");
c_r("菜.....");
Sleep(700);
system("pause");
return 0;
}
}
}
void update(){
system("cls");
c_r("怪物死了。");
godf=0;
bossf=0;
bi+=xgr*3;
xgr=ran(1,gr);
mxhp=xgr*4/5+ran(1ll,3ll);
xhp=mxhp;
xdf=((df+xdf)*ran(1ll,2ll))/3;
xtk=max(xgr*2-3,1ll)*2/10+3;
xnam=xname[ran(0ll,4ll)];
knt++;
Sleep(500);
eexp+=max(xgr-1,1ll);
give(ran(1ll,6ll));
bos();
}
void lvup(){
gr+=eexp/mxp;
mxp=2*gr+3;
eexp%=mxp;
system("cls");
c_r("升级了!");
c_r("1.血量加一。");
c_r("2.防御加一。");
c_r("3.攻击加一。");
c_r2("选择:");
int su;
cin>>su;
if(su==1)mhp++;
else if(su==2)df++;
else atk++;
system("cls");
}
void anger(){
if(hp<=mhp/3&&sk[0]==sl[0]){
system("cls");
system("color F4");
c_r("暴怒");
Sleep(500);
system("color FB");
system("cls");
return;
}
}
void fight(){
system("cls");
while(1){
while(xhp>0){
if(!dead())return;
if(hp>mhp)hp=mhp;
if(xhp>mxhp)xhp=mxhp;
cnt=!cnt;
bos();
if(sk[2]==sl[2])uhp();
cout<<"<=====[monster information]=====>"<<endl;
cout<<"monstername:"<<xnam<<" | lv:"<<xgr<<endl;
cout<<"hp:"<<xhp<<'/'<<mxhp<<" | df:"<<xdf<<" | atk:"<<xtk<<endl;
cout<<"<=====[player information]=====>"<<endl;
cout<<"id:["<<name<<"] | lv:"<<gr<<endl;
cout<<"hp:"<<hp<<'/'<<mhp<<" | df:"<<df<<" | atk:"<<atk<<endl;
cout<<"$money$:"<<bi<<" | EXP:"<<eexp<<endl;
cout<<"#还差["<<mxp-eexp<<"]点经验升级#"<<endl;
cout<<"击杀:"<<knt<<endl;
cout<<"暴击条:[";
for(int i=0;i<20;i++){
if(bj[i])cout<<'/';
else cout<<'\\';
}
cout<<']'<<endl;
cout<<"<=====[backpack]=====>"<<endl;
xx();
cout<<"<q攻击><0~n使用物品><e退出>"<<endl;
cout<<"操作:";
char c1=getch();
if(c1=='q'){
anger();
p_h();
}else if(c1=='e'){
return;
}else if(c1>='1'&&c1<='9'&&(c1-'1')<bp.size()&&bp[c1-'1']!=" "){
if((c1-'1')<bp.size()&&bp[c1-'1']!=" "){
if(num[bp[c1-'1']]>1){
num[bp[c1-'1']]--;
use(bp[c1-'1']);
system("cls");
continue;
}else{
num[bp[c1-'1']]--;
use(bp[c1-'1']);
bp[c1-'1']=" ";
system("cls");
continue;
}
}
}
Sleep(100);
system("cls");
}
if(!dead())return;
update();
if(eexp>=mxp)lvup();
}
}
void start(){
for(int i=1;i<=10;i++)cout<<endl;
cout<<" ========RPG游戏=======\n";
cout<<" | (发育小游戏) |\n";
cout<<" |按 下 任 意 键 开 始|\n";
cout<<" ======================";
getch();
}
void name_in(){
system("cls");
c_r2("输入你的名字:");
cin>>name;
system("cls");
}
void saveGame(){
ofstream saveFile("save.dat",ios::binary);
if(!saveFile){
c_r("保存失败!");
return;
}
saveFile.write((char*)&hp,sizeof(hp));
saveFile.write((char*)&mhp,sizeof(mhp));
saveFile.write((char*)&atk,sizeof(atk));
saveFile.write((char*)&df,sizeof(df));
saveFile.write((char*)&bpl,sizeof(bpl));
saveFile.write((char*)&gr,sizeof(gr));
saveFile.write((char*)&eexp,sizeof(eexp));
saveFile.write((char*)&bi,sizeof(bi));
saveFile.write((char*)&mxp,sizeof(mxp));
saveFile.write((char*)&knt,sizeof(knt));
saveFile.write((char*)&alv,sizeof(alv));
saveFile.write((char*)&ent,sizeof(ent));
saveFile.write((char*)&aatk,sizeof(aatk));
saveFile.write((char*)&kdf,sizeof(kdf));
saveFile.write((char*)&xgr,sizeof(xgr));
saveFile.write((char*)&xtk,sizeof(xtk));
saveFile.write((char*)&xdf,sizeof(xdf));
saveFile.write((char*)&mxhp,sizeof(mxhp));
saveFile.write((char*)&xhp,sizeof(xhp));
auto saveString=[&saveFile](const string&s){
size_t len=s.size();
saveFile.write((char*)&len,sizeof(len));
saveFile.write(s.c_str(),len);
};
saveString(xnam);
saveString(mh);
saveString(name);
for (int i=0;i<40;i++) saveString(bp[i]);
for (int i=0;i<4;i++)saveString(as[i]);
for (int i=0;i<4;i++)saveString(sk[i]);
auto saveMap=[&saveFile,&saveString](const map<string,int>&m){
size_t mSize=m.size();
saveFile.write((char*)&mSize,sizeof(mSize));
for (const auto& p:m){
saveString(p.first);
saveFile.write((char*)&p.second,sizeof(p.second));
}
};
saveMap(num);
saveMap(als);
saveFile.close();
c_r("游戏已保存!");
Sleep(1000);
}
bool loadGame(){
ifstream saveFile("save.dat",ios::binary);
if (!saveFile){
c_r("没有找到存档文件!");
Sleep(1000);
system("cls");
return false;
}
saveFile.read((char*)&hp,sizeof(hp));
saveFile.read((char*)&mhp,sizeof(mhp));
saveFile.read((char*)&atk,sizeof(atk));
saveFile.read((char*)&df,sizeof(df));
saveFile.read((char*)&bpl,sizeof(bpl));
saveFile.read((char*)&gr,sizeof(gr));
saveFile.read((char*)&eexp,sizeof(eexp));
saveFile.read((char*)&bi,sizeof(bi));
saveFile.read((char*)&mxp,sizeof(mxp));
saveFile.read((char*)&knt,sizeof(knt));
saveFile.read((char*)&alv,sizeof(alv));
saveFile.read((char*)&ent,sizeof(ent));
saveFile.read((char*)&aatk,sizeof(aatk));
saveFile.read((char*)&kdf,sizeof(kdf));
saveFile.read((char*)&xgr,sizeof(xgr));
saveFile.read((char*)&xtk,sizeof(xtk));
saveFile.read((char*)&xdf,sizeof(xdf));
saveFile.read((char*)&mxhp,sizeof(mxhp));
saveFile.read((char*)&xhp,sizeof(xhp));
auto loadString=[&saveFile]()->string {
size_t len;
saveFile.read((char*)&len,sizeof(len));
string s(len,' ');
saveFile.read(&s[0],len);
return s;
};
xnam=loadString();
mh=loadString();
name=loadString();
for (int i=0;i<40;i++)bp[i]=loadString();
for (int i=0;i<4;i++)as[i]=loadString();
for (int i=0;i<4;i++)sk[i]=loadString();
auto loadMap=[&saveFile,&loadString](map<string,int>& m){
m.clear();
size_t mSize;
saveFile.read((char*)&mSize, sizeof(mSize));
for(size_t i=0;i<mSize;i++){
string key=loadString();
int val;
saveFile.read((char*)&val,sizeof(val));
m[key]=val;
}
};
loadMap(num);
loadMap(als);
saveFile.close();
c_r("游戏读取成功!");
Sleep(1000);
system("cls");
return true;
}
void yh(){
CONSOLE_FONT_INFOEX fontInfo={sizeof(fontInfo)};
fontInfo.FontWeight=FW_BOLD;
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE),FALSE,&fontInfo);
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
system("color FB");
}
int main(){
yh();
start();
name_in();
if(loadGame());
xnam=xname[ran(0ll,4ll)];
while(1){
if(!dead())return 0;
if(sk[2]==sl[2])uhp();
cout<<"||主菜单||"<<endl;
cout<<"<=====[player information]=====>"<<endl;
cout<<"id:["<<name<<"] | lv:"<<gr<<endl;
cout<<"hp:"<<hp<<'/'<<mhp<<" | df:"<<df<<" | atk:"<<atk<<endl;
cout<<"$money$:"<<bi<<" | EXP:"<<eexp<<endl;
cout<<"#还差["<<mxp-eexp<<"]点经验升级#"<<endl;
cout<<"击杀:"<<knt<<endl;
cout<<"<=====[choices]=====>"<<endl;
cout<<"1.战斗"<<endl;
cout<<"2.查看背包"<<endl;
cout<<"3.商店"<<endl;
cout<<"4.回收"<<endl;
cout<<"5.抽奖"<<endl;
cout<<"6.保存"<<endl;
cout<<"choice:";
cin>>c;
int cho=c-'0';
if(cho>=1&&cho<=6){
if(cho==1){
fight();
}else if(cho==2){
system("cls");
xx();
system("pause");
}else if(cho==3){
shop();
system("color FB");
}else if(cho==4){
bin();
}else if(cho==5){
lot();
}else if(cho==6){
saveGame();
}
}else{
cout<<"无效选择"<<endl;
Sleep(400);
}
if(!dead())return 0;
Sleep(100);
system("cls");
}
return 0;
}