这次代码完全使用c++的特点写的游戏,什么封装,多态,继承等等。遇到过问题,也遇到过快乐。一开始写的时候挺辛苦的。完全没有头绪。而且封装的权限问题把人的头都弄大了。不过当完成后心里无比激动,也是蛮拼了(PS:感谢与我合作的小伙伴,给了我非常多的灵感)
感觉做的最成功的就是地图类了,因为它完美的将所有东西都联系在一起,变成一个整体。
源文件:
#include <windows.h> #include <conio.h> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <fstream> #include <ctime> using namespace std; void color(int a)//颜色函数 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a); } void gotoxy(int x, int y) { CONSOLE_SCREEN_BUFFER_INFO cs; HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsoleOut, &cs); cs.dwCursorPosition.X = y; cs.dwCursorPosition.Y = x; SetConsoleCursorPosition(hConsoleOut, cs.dwCursorPosition); } #include "mon.h" #include "怪物.h" #include "wuqi.h" #include "Role.h" #include "Map.h" class Game { protected: char name[20]; int visitCount; public: Game(char a[]) { visitCount = 0; strcpy(name,a); } char* GetName() { return name; } int GetVisitCount() { return visitCount; } void addvisitCount(); void ShowName(); virtual void start() = 0; /*friend bool cmp();*/ }; void Game::ShowName() { cout << name << endl; } class GGame1:public Game { private: Map mmaapp; public: void beigan(); void start(); GGame1(); }; GGame1::GGame1():Game("RPG游戏") {} void GGame1::beigan() { color(8); char a[1][239]={ {" 公元xx年,位于拉布斯大陆的两个大国因争夺资源引发冲突,战争一触即发! 而你 ---------- 作为一名精英的雇佣兵,当然不会放过这个发财的机会, 于是,为荣誉和欲望,你来到了战争中心,走进了那战争之潮当中......." } }; for(int i=0;i<239;i++) { cout<<a[0][i]; Sleep(50); } cout<<endl<<endl; color(12); char s[1][33]={{"战争 卐 之潮 "}}; cout<<" "; for(int i=0;i<33;i++) { cout<<s[0][i]; Sleep(90); } color(9); cout<<endl<<endl<<endl<<endl; cout<<" 作者: Dennis.锋"<<endl<<" Alex.杰"; color(15); cout<<endl<<endl<<endl<<endl<<" START --请按回车键继续"; char k; k=getch(); if(k==13) { system("cls"); gotoxy(0,0); } } void GGame1::start() { visitCount++; beigan();//开始界面 mmaapp.getpeople()->renwujianli(); int End=1;//判断是否结束游戏且切换地图 int srand((unsigned)time(NULL)); int f=rand()%5; while(End) { switch(End) { case 1: End=mmaapp.map1();break; case 2: End=mmaapp.map2();break; case 3: End=mmaapp.map3();break; case 4: End=mmaapp.map4();break; case 5: End=mmaapp.map5();break; } } } class GGame2:public Game { public: virtual void start(); GGame2():Game("游戏2") { } }; void GGame2::start() { visitCount++; cout << "\t\t欢迎进入游戏2" << endl; } class GGame3:public Game { public: virtual void start(); GGame3():Game("游戏3") { } }; void GGame3::start() { visitCount++; cout << "\t\t欢迎进入游戏3" << endl; } void menu(Game** gaems) // 显示游戏列表 { for (int i = 0; i < 3; i++) { printf("%d: ",i+1);gaems[i]->ShowName(); } } void Sort(Game** p) { Game* temp; for(int i=0;i<2;++i) { for(int j=i+1;j<3;++j) { if(p[i]->GetVisitCount()<p[j]->GetVisitCount()) { temp = p[i]; p[i] = p[j]; p[j] = temp; } } } } int main() { Game* games[3];//一开始忘记了指针数组就是二维数组,一直没思路 games[0] = new GGame1;//为什么可以用基类指针数组来new一个派生类?? games[1] = new GGame2; games[2] = new GGame3; do { cout << "请输入你想玩的游戏?(1-3)" << endl; Sort(games); // 将游戏列表按访问量排序 //如何实现类指针数组的排序?? menu(games); /*for(int i=0;i<3;++i) cout << games[i]->GetVisitCount() << endl;*/ int choice; cin >> choice; // 用户输入选择 if (choice >= 1 && choice <= 3) { int i = choice; system("cls"); (*games[i-1]).start(); } else { break; // 退出系统 } } while (true); }
一系列头文件:
Rloe角色:
class Role:public Monster { protected: int Money;//金钱 int Ex;//经验 int Lvup;//升级所需经验 int Life;//生命总量 char name[10]; //玩家名字 int attckup;//攻击成长值 int denfenup;//防守成长值 int lifeup;//生命成长值 int workrole;//职业代表数1-战士 2-刺客 3-盾卫 int numA;//武器攻击代号 int numB;//防具代号 int numL;//饰品代号(增加生命值) char work[3][10];//职业 char a;//职业代号 int gwnamenum;//怪兽名编号,一开始用了Gwu的类新建个as变量,结果根本构造时都没赋值,所以WA了 int x,y;//视图坐标 int gwAT,gwFY;//储存怪物攻击力&防御力 Gwu* gw; int flag; int k,kk;//类里的Hp储存起来变量 int wuqicount[10]; int wuqiA,wuqiB; int num; Equip eq[11]; public: void up(int t);//升级系统 Role();//构造 void deleteGwu();//删除动态申请空间 char* GetName();//角色名调用 char* Getwork();//角色职业调用 int GetEX(); int GetMoney();//第一个获取钱,后面还有个获取钱的就不改了 int getmoney1();//令外一个获取钱 void addmoney(int a); void usemoney(int a); int getusemoney(int a); void showRole(); void getnum(); void setnum(int a); void attckadd(int a); int getnumA(); int getnumB(); void Wuqicount(int a,int b); int _wuqicount(int a); void daoju();//道具 void CombatC();//战斗地图(一开始是放在地图类里的,但是写着就发现它更适合和战斗函数一起) void ul();//人物数据写入,且新建怪物 int chiose();//打斗界面选择 void oneattck();//单次战斗 void Comjiemian();//自动战斗 int Combat(int t);//战斗函数,且控制结束条件 int COMBAT();//整合上面功能的函数 void renwujianli();//人物创建 }; void Role::renwujianli() { printf("请输入玩家姓名\n"); scanf("%s",name); printf("请输入你选择的职业\n"); printf("1-战士 2-刺客 3-盾卫\n"); while(a=_getch()) { if(a=='1') { attckup=5; denfenup=4; lifeup=5; workrole=0; } if(a=='2') { attckup=7; denfenup=3; lifeup=3; workrole=1; } if(a=='3') { attckup=3; denfenup=7; lifeup=7; workrole=2; } if(a=='1'||a=='2'||a=='3')break; } system("cls"); } inline void Role::deleteGwu() { if(gw!=NULL) { delete gw; gw = NULL; } } inline void Role::CombatC() { system("cls"); char a[5][10]={{"攻击"},{"魔法"},{"道具"},{"自动"},{"逃跑"}}; for(int i=0;i<5;++i) { printf("██████\n"); printf("█ %s █\n",a[i]); } printf("██████\n"); ifstream inf("map.txt"); gotoxy(17,0); char c; while(inf.get(c)) { cout.put(c); } } inline void Role::daoju()//道具 { int x=4,y=2; system("cls"); gotoxy(0,5); cout<<"------------------------------"<<endl; cout<<"物品栏"<<endl; cout<<" <>名字"<<'\t'<<"数量"<<endl; for(int i=7;i<10;i++) { cout<<" "; eq[i].getname(i); cout<<" "<<wuqicount[i]<<endl; } cout<<" <>退出"; char k=' '; gotoxy(x,y); printf("→"); do { k=_getch(); if(k=='s'&&x<6) { gotoxy(x,y); printf(" "); x++; gotoxy(x,y); printf("→"); } if(k=='w'&&x>3) { gotoxy(x,y); printf(" "); x--; gotoxy(x,y); printf("→"); } }while(k!=13); if(x==3&&wuqicount[7]>0) { wuqicount[7]--; if(life+10>Life) { life=Life; } else { life+=10; } } if(x==4&&wuqicount[8]>0) { wuqicount[8]--; if(life+20>Life) { life=Life; } else { life+=20; } } if(x==5&&wuqicount[9]>0) { wuqicount[9]--; if(life+30>Life) { life=Life; } else { life+=30; } } gotoxy(0,0); CombatC(); gotoxy(18,2);printf("姓名:%s Lv:%d 职业:%s",name,Lv,work); gotoxy(19,2);printf("HP:%d/%d 攻击力:%d 防御力:%d ",life,Life,attck,denfen); gotoxy(18,40);gw->show(gwnamenum); gotoxy(19,40);printf("HP:%d/%d 攻击力:%d 防御力:%d ",k,kk,gwAT,gwFY); if(gwAT>denfen) { life=life-gwAT+denfen; gotoxy(x,y); printf("怪兽对你伤害%d\n",gwAT-denfen); x+=2; } else { gotoxy(x,y); printf("怪兽对你伤害0\n"); x+=2; } gotoxy(18,2);printf("姓名:%s Lv:%d 职业:%s",name,Lv,work); gotoxy(19,2);printf("HP:%d/%d 攻击力:%d 防御力:%d ",life,Life,attck,denfen); gotoxy(18,40);gw->show(gwnamenum); gotoxy(19,40);printf("HP:%d/%d 攻击力:%d 防御力:%d ",k,kk,gwAT,gwFY); } inline void Role::ul()//选择列表 { gwnamenum = rand()%10 ; gw = new Gwu(gwnamenum); k = gw->getlife(); kk = gw->getllife(); gwAT=gw->getattck(); gwFY=gw->getdenfen(); int x=0,y=0; gotoxy(18,2);printf("姓名:%s Lv:%d 职业:%s",name,Lv,work[workrole]); gotoxy(19,2);printf("HP:%d/%d 攻击力:%d 防御力:%d",life,Life,attck,denfen); gotoxy(18, 40); gw->show(gwnamenum); gotoxy(19, 40); printf("HP:%d/%d 攻击力:%d 防御力:%d ", k, kk, gwAT, gwFY); } inline int Role::chiose()//选择界面 { int x=1,y=2,f=1,t; char k=' '; gotoxy(x,y); printf("→"); while(k!=13)//回车为13 { k=_getch(); f=0;t=0; if(k=='w'||k=='W') { if(x>1) { x+=-2; t=-2; f=1; } } else if(k=='s'||k=='S') { if(x<=7) { f=1; x+=2; t=2; } } if(f) { gotoxy(x-t,y); printf(" "); } if(f) { gotoxy(x,y); printf("→"); f=1; } } return x; } inline void Role::oneattck()//一次战斗 { gotoxy(18,40);gw->show(gwnamenum); gotoxy(19,40);printf("HP:%d/%d 攻击力:%d 防御力:%d ",k,kk,gwAT,gwFY); if(life<=0) { gotoxy(x,y); printf("You low!"); system("cls"); } if(x==16) { for(int i=0;i<=16;++i) { gotoxy(i,13); printf(" "); } x=0,y=13; } if(k>0) { if(attck>gwFY) { k=k-attck+gwFY; gotoxy(x,y); printf("你对怪兽伤害%d\n",attck-gwFY); x+=2; } else { gotoxy(x,y); printf("你对怪兽伤害0\n"); x+=2; } if(gwAT>denfen) { life=life-gwAT+denfen; gotoxy(x,y); printf("怪兽对你伤害%d\n",gwAT-denfen); x+=2; } else { gotoxy(x,y); printf("怪兽对你伤害0\n"); x+=2; } } else { for (int i = 0; i < 16; ++i) { printf(" "); } x=0,y=13; system("cls"); } gotoxy(18,2);printf("姓名:%s Lv:%d 职业:%s",name,Lv,work[workrole]); gotoxy(19,2);printf("HP:%d/%d 攻击力:%d 防御力:%d ",life,Life,attck,denfen); gotoxy(18,40);gw->show(gwnamenum); gotoxy(19,40);printf("HP:%d/%d 攻击力:%d 防御力:%d ",k,kk,gwAT,gwFY); ::Sleep(500); } inline void Role::Comjiemian()//自动战斗界面 { gotoxy(18,40);gw->show(gwnamenum); gotoxy(19,40);printf("HP:%d/%d 攻击力:%d 防御力:%d ",k,kk,gwAT,gwFY); while(1) { if(life<=0) { gotoxy(x,y); printf("You low!"); ::Sleep(2000); system("cls"); break; } if(x==16) { for(int i=0;i<=16;++i) { gotoxy(i,13); printf(" "); } x=0,y=13; } if(k>0) { if(attck>gwFY) { k=k-attck+gwFY; gotoxy(x,y); printf("你对怪兽伤害%d\n",attck-gwFY); x+=2; } else { gotoxy(x,y); printf("你对怪兽伤害0\n"); x+=2; } if(gwAT>denfen) { life=life-gwAT+denfen; gotoxy(x,y); printf("怪兽对你伤害%d\n",gwAT-denfen); x+=2; } else { gotoxy(x,y); printf("怪兽对你伤害0\n"); x+=2; } ::Sleep(1000); } else { gotoxy(0,0); for (int i = 1; i < 16;++i) { printf(" "); } system("cls"); break; } gotoxy(18,2);printf("姓名:%s Lv:%d 职业:%s",name,Lv,work); gotoxy(19,2);printf("HP:%d/%d 攻击力:%d 防御力:%d ",life,Life,attck,denfen); gotoxy(18,40);gw->show(gwnamenum); gotoxy(19,40);printf("HP:%d/%d 攻击力:%d 防御力:%d ",k,kk,gwAT,gwFY); } } inline int Role::Combat(int t)//进入战斗函数 { switch(t) { case 1:oneattck();break; case 3:break; case 5:daoju(); break; case 7:Comjiemian();break; case 9:break; } if(k<=0){ system("cls"); gotoxy(0, 13); printf("Win!"); Ex+=getEX;Money+=getmoney; printf("获得%d经验,获得%d金钱.",getEX,getmoney); ::Sleep(2500); if(Ex>=Lvup) { system("cls"); up(Lv); ::Sleep(2000); system("cls"); } return 1;} if(life<=0) { system("cls"); gotoxy(0, 13); printf("You lose!"); return 0; } return 0; } inline int Role::COMBAT()//战斗函数 { int t; ul(); while(t=chiose()) { if(Combat(t)) { deleteGwu(); break; } if(t==7||getlife()<=0){deleteGwu(); break;} if(t==9) { for(int i=0;i<=16;++i) { gotoxy(i,13); printf(" "); } if(rand()%50>20) { deleteGwu(); gotoxy(0,13); printf("逃跑成功!"); x=0; ::Sleep(1000); system("cls"); gotoxy(9,2);printf(" "); return 0; } else { gotoxy(9,2);printf(" "); gotoxy(0,13); printf("逃跑失败!"); if (gwAT>denfen) { life = life - gwAT + denfen; gotoxy(x, y); printf("怪兽对你伤害%d\n", gwAT - denfen); x += 2; } else { gotoxy(x, y); printf("怪兽对你伤害0\n"); x += 2; } ::Sleep(1000); } } } return 0; } Role::Role()//构造 { gw = NULL; for (int i=0;i<10;++i) { wuqicount[i]=0; } wuqiA=-1;wuqiB=-1; x=0,y=13; Life = life; strcpy(work[0],"战士"); strcpy(work[1],"刺客"); strcpy(work[2],"盾卫"); Lvup=10*Lv; Money=10000; Ex=0; numA=-1; numB=-1; num=-1; wuqiA=-1; wuqiB=-1; }; inline void Role::up(int t)//升级系统 { Lv++; Lvup=10*Lv; attck+=t; denfen+=t; life+=t*10; Life = life; for (int i = 0; i <= 16; ++i) { gotoxy(i, 13); printf(" "); } gotoxy(0,13); printf("升级!\n"); printf("Lv->%d attck->%d denfen->%d life->%d\n", Lv, attck, denfen, life); } inline int Role::GetEX()//角色经验 { return Ex; } inline int Role::GetMoney()//角色金钱 { return Money; } inline void Role::showRole() { if(numA!=-1) { attck=attck+eq[numA].getaddA(numA); } if(numB!=-1) { denfen=denfen+eq[numB].getaddB(numB); } if(wuqiA!=-1) { attck=attck-eq[wuqiA].getaddA(wuqiA); } if(wuqiB!=-1) { denfen=denfen-eq[wuqiB].getaddB(wuqiB); } cout<<"------------------------------"<<endl; cout<<" <>角色名:"<<name<<" <>等级: " << Lv << endl; cout<<" <>职业:"<<work[workrole]<<endl; cout<<" <>生命:"<<life<<endl; cout<<" <>攻击力:"<<attck<<endl; cout<<" <>防守力:"<<denfen<<endl; cout<<" <>经验:"<<Ex<<" / "<<Lvup<<endl; cout<<" <>武器:"; if(numA!=-1) { eq[numA].getname(numA); wuqiA=numA; } cout<<endl; cout<<" <>防具:"; if(numB!=-1) { eq[numB].getname(numB); wuqiB=numB; } cout<<endl; cout<<"------------------------------"<<endl; cout<<"物品栏"<<endl; cout<<" <>名字"<<'\t'<<"数量"<<endl; for(int i=0;i<10;i++) { cout<<" "; eq[i].getname(i); cout<<" "<<wuqicount[i]<<endl; } int x=22,y=2; int x1,y1; cout<<" <>退出"; x=12,y=2; gotoxy(x,y); cout<<"→"; char k=' '; char h; while(k!='\n') { k=_getch(); if(k=='s'||k=='S') { if(x<22) { gotoxy(x,y); cout<<' '; x++; gotoxy(x,y); cout<<"→"; } } if(k=='w'||k=='W') { if(x>12) { gotoxy(x,y); cout<<' '; x--; gotoxy(x,y); cout<<"→"; } } if(k==13) { if(x<22) { h=' '; x1=23; y1=5; gotoxy(x1,y1); cout<<"装备/使用"; y1=20; gotoxy(x1,y1); cout<<"放弃"; y1=2; gotoxy(x1,y1); cout<<"→"; while(h!=13) { h=_getch(); if(h=='a'||h=='A'||h=='d'||h=='D') { if(y1==2) { gotoxy(x1,2); cout<<' '; y1=17; } else if(y1==17) { gotoxy(x1,17); cout<<' '; y1=2; } } gotoxy(x1,y1); cout<<"→"; } if(y1==2)//选择装备num=x-12 { gotoxy(x1,2); cout<<" 已装备 "; if(x-12<=3)//选择装备武器 { if(wuqicount[x-12]>0) { attck=attck-eq[wuqiA].getaddA(wuqiA); wuqiA=x-12; attck=attck+eq[wuqiA].getaddA(wuqiA); gotoxy(7,11); eq[wuqiA].getname(wuqiA); gotoxy(4,13); cout<<attck; gotoxy(x1,2); cout<<" 已装备 "; } else { gotoxy(x1,2); cout<<" 数量不足,无法装备 "; } } if(x-12<=6&&x-12>3)//选择装备防具 { if(wuqicount[x-12]>0) { denfen=denfen-eq[wuqiB].getaddB(wuqiB); wuqiB=x-12; denfen=denfen+eq[wuqiB].getaddB(wuqiB); gotoxy(8,11); eq[wuqiB].getname(wuqiB); gotoxy(5,13); cout<<denfen; gotoxy(x1,2); cout<<" 已装备 "; } else { gotoxy(x1,2); cout<<" 数量不足,无法装备 "; } } if(x-12>6&&x-12<=9)//选择使用药品 { if(wuqicount[x-12]>0) { life=life+eq[x-12].getaddL(x-12); gotoxy(3,11); cout<<life; gotoxy(x1,2); cout<<" 已使用 "; eq[x-12].countsale(x-12); wuqicount[x-12]--; gotoxy(x,18); cout<<wuqicount[x-12]; } else { gotoxy(x1,2); cout<<" 数量不足,无法使用 "; } } Sleep(800); gotoxy(x1,2); cout<<" "; gotoxy(x,y); cout<<"→"; } if(y1==17)//选择放弃 { gotoxy(x1,2); cout<<" "; gotoxy(x,y); cout<<"→"; } } if(x==22) { system("cls"); break; } } } } inline void Role::addmoney(int a) { Money=Money+a; } inline void Role::usemoney(int a) { Money=Money-a; } inline int Role::getusemoney(int a) { int k; k=Money-a; return k; } inline int Role::getmoney1() { return Money; } inline void Role::getnum() { if(num<=3) { numA=num; } else if(num<=6) { numB=num; } } inline void Role::setnum(int a) { num=a; } inline void Role::Wuqicount(int a,int b) { wuqicount[a]+=b; } inline int Role::_wuqicount(int a) { return wuqicount[a]; }
人物继承的基类:
class Monster { protected: int Lv;//等级 int attck;//攻击 int denfen;//防守 int life;//生命 int llife;//最大生命 int getEX;//杀怪所得经验 int getmoney;//杀怪所得金钱 public: Monster();//构造 int getattck(); int getdenfen(); int getlife(); int getllife(); int getLv(); void setlife(int t); }; Monster::Monster()//构造 { getEX = 5; getmoney = 10; Lv=1; attck=5; denfen=5; life=10; llife = life; }; inline int Monster::getllife() { return llife; } inline void Monster::setlife(int t) { life-=t; } inline int Monster::getattck() { return attck; } inline int Monster::getdenfen() { return denfen; } inline int Monster::getlife() { return life; } inline int Monster::getLv() { return Lv; }
武器类:
struct Equipment { int addA;//武器攻击属性增加 int addB;//防守属性++ int addL;//生命++ int count;//用有数量 char name[10];//武器名 int num;//武器代号 int money;//武器买进价格 int sale;//武器卖出价格 Equipment(); }; Equipment::Equipment() { addA=0; addB=0; addL=0; count=0; } class Equip { protected: Equipment eq[10]; public: Equip(); void show(int a); int getmoney1(int i); void getname(int i); int getnum(int i); void countbuy(int i);//买一,count++ void countsale(int i);//卖一,count-- void showcount(int i); int getcount(int i); int getaddA(int i); int getaddB(int i); int getaddL(int i); }; Equip::Equip() { char eq0[10]={"小刀 "}; char eq1[10]={"手枪 "}; char eq2[10]={"步枪 "}; char eq3[10]={"狙击 "}; char eq4[10]={"步衣 "}; char eq5[10]={"铁衣 "}; char eq6[10]={"装甲 "}; char eq7[10]={"活石 "}; char eq8[10]={"活石+"}; char eq9[10]={"生石 "}; strcpy(eq[0].name,eq0); strcpy(eq[1].name,eq1); strcpy(eq[2].name,eq2); strcpy(eq[3].name,eq3); strcpy(eq[4].name,eq4); strcpy(eq[5].name,eq5); strcpy(eq[6].name,eq6); strcpy(eq[7].name,eq7); strcpy(eq[8].name,eq8); strcpy(eq[9].name,eq9); eq[0].addA=10; eq[1].addA=20; eq[2].addA=30; eq[3].addA=40; eq[4].addB=10; eq[5].addB=20; eq[6].addB=30; eq[7].addL=10; eq[8].addL=20; eq[9].addL=30; eq[0].money=100; eq[1].money=150; eq[2].money=250; eq[3].money=400; eq[4].money=100; eq[5].money=200; eq[6].money=400; eq[7].money=100; eq[8].money=200; eq[9].money=300; for(int i=0;i<10;i++) { eq[i].num=i; } } inline void Equip::show(int a) { cout<<eq[a].name<<" "<<eq[a].addA<<" "<<eq[a].addB<<" "<<eq[a].addL<<" "<<eq[a].money<<" "<<eq[a].count; } inline int Equip::getmoney1(int i) { return eq[i].money; } inline int Equip::getnum(int i) { return eq[i].num; } inline void Equip::getname(int i) { cout<<eq[i].name; } inline void Equip::countbuy(int i) { eq[i].count++; } inline void Equip::countsale(int i) { eq[i].count--; } inline void Equip::showcount(int i) { cout<<eq[i].count; } inline int Equip::getcount(int i) { return eq[i].count; } inline int Equip::getaddA(int i) { return eq[i].addA; } inline int Equip::getaddB(int i) { return eq[i].addB; } inline int Equip::getaddL(int i) { return eq[i].addL; }
怪物类:
class Gwu:public Monster { private: char num[10][15]; public: int Getnumlife(); int Getnumattck(); int Getnumdenfen(); void show(int as); Gwu(int a) { strcpy(num[0],"步兵"); strcpy(num[1],"剑兵"); strcpy(num[2],"弓兵"); strcpy(num[3],"枪兵"); strcpy(num[4],"轻骑兵"); strcpy(num[5],"重步兵"); strcpy(num[6],"长弓兵"); strcpy(num[7],"重骑兵"); strcpy(num[8],"分队长"); strcpy(num[9],"嗜血铁卫"); life = 10+a; attck = 2+a%5; denfen = 2+a%5; llife = life; } }; void Gwu::show(int as) { printf("%s",num[as]); } inline int Gwu::Getnumlife() { return life; } inline int Gwu::Getnumattck() { return attck; } inline int Gwu::Getnumdenfen() { return denfen; }
地图类:
class Map { private: char str[50][5]; int x,y,xx,yy;//第一步,旧的一步 int zx,zy;//数组内的坐标 Role* people; char strr; public: Map(); ~Map(); int map1(); void yidong(int& a,int& b); int map2(); int map3(); int map4(); int map5(); void ztjm(int (*st)[20]);//人物状态界面 void shua(int (*st)[20]);//刷地图 void Tableabout(int a,int b);//交谈 void shop(); Role* getpeople(); void Jieshao(); }; void Map::Jieshao() { gotoxy(5,40); cout<<"按esc键查看状态"; gotoxy(6,40); cout<<"由于时间关系,暂无剧情,请待更新。"<<endl; } inline Role* Map::getpeople() { return people; } inline Map::~Map() { delete people; } void Map::shop() { Equip eq[11]; printf(" 买什么?\n"); printf("----------------------------------------------------------\n"); printf(" |武器名 攻+ 防+ 命+ 价格 拥有|\n"); for(int i=0;i<10;i++) { printf(" | "); eq[i].show(i); printf(" |\n"); } printf(" < "); cout<<"退出"; printf(" >\n"); printf("----------------------------------------------------------\n"); int x=3,y=20; int x1,y1; x1=15; y1=20; gotoxy(x1,y1); printf("拥有金钱:%d ",people->getmoney1()); gotoxy(x,y); printf("→"); char k=' '; char h; while(k!='\n') { k=_getch(); if(k=='s'||k=='S') { if(x<13) { gotoxy(x,y); printf(" "); x++; gotoxy(x,y); printf("→"); } } if(k=='w'||k=='W') { if(x>3) { gotoxy(x,y); printf(" "); x--; gotoxy(x,y); printf("→"); } } if(k==13) { if(x<13) { h=' '; x1=15; y1=20; gotoxy(x1,y1); printf("拥有金钱:%d ",people->getmoney1()); x1=16; y1=20; gotoxy(x1,y1); printf("是否购买\n"); x1=17; y1=20; gotoxy(x1,y1); printf("购买"); x1=17; y1=30; gotoxy(x1,y1); printf("放弃"); x1=17; y1=41; gotoxy(x1,y1); printf("卖出"); x1=17; y1=17; gotoxy(x1,y1); printf("→"); while(h!=13) { h=_getch(); if(h=='a'||h=='A') { if(y1>20) { gotoxy(x1,y1); printf(" "); y1=y1-11; } } if(h=='d'||h=='D') { if(y1<=33) { gotoxy(x1,y1); printf(" "); y1=y1+11; } } gotoxy(x1,y1); printf("→"); } if(y1==17)//选择yes { if(people->getusemoney(eq[x-3].getmoney1(x-3))>=0) { people->usemoney(eq[x-3].getmoney1(x-3));//减钱 people->setnum(eq[x-3].getnum(x-3));//将人物num=武器num people->getnum();//讲人物num赋值给人物武器numA或人物防具numB eq[x-3].countbuy(x-3); gotoxy(x,54); eq[x-3].showcount(x-3); people->Wuqicount(x-3,eq[x-3].getcount(x-3)); gotoxy(16,20); printf(" "); gotoxy(17,17); printf("购买成功,已自动装备 "); Sleep(800); x1=15; y1=20; gotoxy(x1,y1); printf("拥有金钱:%d ",people->getmoney1()); gotoxy(17,17); printf(" "); gotoxy(x,y); } else { gotoxy(16,20); printf("金钱不足,无法购买 "); gotoxy(17,17); printf(" "); Sleep(800); gotoxy(16,20); printf(" "); gotoxy(x,y); } } if(y1==28)//选择no { gotoxy(16,20); printf(" "); gotoxy(17,20); printf(" "); gotoxy(x,y); } if(y1==39) { if(people->_wuqicount(x-3)>0) { people->setnum(eq[x-3].getnum(x-3));//将人物num=武器num people->getnum(); people->Wuqicount(x-3,-1); eq[x-3].countsale(x-3); gotoxy(x,54); eq[x-3].showcount(x-3); gotoxy(16,20); printf(" 已卖出 "); gotoxy(17,17); printf(" "); Sleep(800); gotoxy(16,20); printf(" "); gotoxy(x,y); people->addmoney(eq[x-3].getmoney1(x-3)); x1=15; y1=20; gotoxy(x1,y1); printf("拥有金钱:%d ",people->getmoney1()); } else { gotoxy(16,20); printf("数量不足,无法卖出 "); gotoxy(17,17); printf(" "); Sleep(800); gotoxy(16,20); printf(" "); gotoxy(x,y); } } } if(x==13) { system("cls"); break; } } } } inline void Map::Tableabout(int a,int b)//交谈 { if(a==1&&b==18) { printf("村长:\n"); printf(" 勇士你好!"); } else if (a==10&&b==18) { printf("村民:\n"); printf(" 勇士你好!"); } } inline void Map::shua(int (*st)[20])//刷地图 { system("cls"); Jieshao(); gotoxy(0,0); for(int i=0;i<22;++i) { for(int j=0;j<20;++j) { printf("%s",str[st[i][j]]); } printf("\n"); } gotoxy(x,y); printf("%s",str[12]); } inline void Map::ztjm(int (*st)[20])//人物状态界面 { system("cls"); people->showRole(); system("cls"); gotoxy(0,0); for(int i=0;i<22;++i) { for(int j=0;j<20;++j) { printf("%s",str[st[i][j]]); } printf("\n"); } gotoxy(x,y); printf("%s",str[12]); } Map::Map() { people = new Role; zx=3,zy=5; x=3,y=10; xx=x,yy=y; char a[50][5]={{" "}/*0*/,{"█"}/*1*/,{"⊙"}/*2*/,{"★"}/*3*/,{"◆"}/*4*/ ,{"▓"}/*5*/,{"●"}/*6*/,{"①"}/*7*/,{"②"}/*8*/,{"③"}/*9*/,{"④"}/*10*/ ,{"¤"}/*11*/,{"∏"}/*12*/,{"主"}/*13*/,{"角"}/*14*/,{"家"}/*15*/,{"房"}/*16*/ ,{"武"}/*17*/,{"器"}/*18*/,{"店"}/*19*/,{"道"}/*20*/,{"具"}/*21*/,{"村"}/*22*/ ,{"长"}/*23*/,{"民"}/*24*/,{"城"}/*25*/,{"东"}/*26*/,{"南"}/*27*/,{"西"}/*28*/ ,{"北"}/*29*/,{"门"}/*30*/,{"山"}/*31*/,{"神"}/*32*/,{"庙"}/*33*/,{"荒"}/*34*/ ,{"郊"}/*35*/,{"野"}/*36*/,{"猪"}/*37*/,{"林"}/*38*/};//39个 for(int i=0;i<39;++i)//更改上面数组记得修改赋值数 { strcpy(str[i],a[i]); } } inline void Map::yidong(int& a,int& b) { int xy[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; int fx; do {strr = _getch();} while(strr!='s'&&strr!='d'&&strr!='w'&&strr!='a'&&strr!=27); switch(strr) { case 's':fx=0;break; case 'd':fx=1;break; case 'w':fx=2;break; case 'a':fx=3;break; } if(strr!=27) { a=xy[fx][0]; b=xy[fx][1]; } } inline int Map::map1() { int a,b; int st[22][20]={ 1,1,1,1,1,1,1,1,1,1,29,30,1,1,1,1,1,1,1,1, 1,13,25,0,0,0,0,0,0,0,0,0,0,0,1,22,0,0,3,1, 1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,23,0,0,0,1, 1,13,14,15,0,0,1,0,0,0,0,0,0,0,1,15,0,0,0,1, 1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1, 1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,24,16,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1, 28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26, 30,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30, 1,1,1,1,0,0,1,1,0,0,0,1,1,1,1,1,1,0,0,1, 1,17,0,0,0,0,0,1,0,0,0,1,20,0,0,0,0,0,0,1, 1,18,0,0,0,0,0,1,0,0,0,1,21,0,0,0,0,0,0,1, 1,19,0,0,0,11,0,1,0,0,0,1,19,0,0,11,0,0,0,1, 1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,27,30,1,1,1,1,1,1,1,1,1,1 };//22行20列 shua(st); while(1) { yidong(a,b); if(strr==27) { ztjm(st); continue; } if(st[zx+a][zy+b]==3) { system("cls"); Tableabout(zx+a,zy+b); ::Sleep(2500); shua(st); continue; } if (zx+a==17&&(zy+b==5||zy+b==15)) { system("cls"); shop(); shua(st); } if((zy+b==10||zy+b==11)&&(zx+a==0)) { zx=19;x=zx; return 2;//北门 } else if((zy+b==8||zy+b==9)&&(zx+a==20)) { zx=1;x=zx; return 3;//南门 } else if((zx+a==12||zx+a==13)&&(zy+b==19)) { zy=1;y=2*zy; return 4;//东门 } else if((zx+a==12||zx+a==13)&&(zy+b==0)) { zy=18;y=2*zy; return 5;//西门 } if(zx+a>0&&zx+a<21&&zy+b>0&&zy+b<19&&st[zx+a][zy+b]!=1&&st[zx+a][zy+b]<10) { zx+=a,zy+=b; xx=x;yy=y; x=zx;y=zy*2; gotoxy(xx,yy); printf(" "); gotoxy(x,y); printf("%s",str[12]); } } } inline int Map::map2()//北门 { int a,b; int st[22][20]={ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,34,20,0,0,0,0,0,0,3,0,0,0,0,0,0,1,0,4,1, 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1, 1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1, 1,0,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1, 1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1, 1,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1,1,1,1, 1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1, 1,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,1,0,1, 1,0,0,1,1,0,0,0,1,0,0,0,1,4,0,0,0,1,0,1, 1,0,0,0,1,0,0,0,1,1,0,0,1,1,1,0,0,0,0,1, 1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1, 1,0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1, 1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,13,25,1,1,1,1,1,1,1,1 };//22行20列 shua(st); while(1) { yidong(a,b); if(strr==27) { ztjm(st); continue; } srand((unsigned)time(NULL)); if(rand()%50<15) { people->CombatC(); people->COMBAT(); shua(st); } if((zy+b==10||zy+b==11)&&(zx+a==20)) { zx=1;x=zx; return 1;//主城 } if(zx+a>0&&zx+a<21&&zy+b>0&&zy+b<19&&st[zx+a][zy+b]!=1&&st[zx+a][zy+b]<10) { zx+=a,zy+=b; xx=x;yy=y; x=zx;y=zy*2; gotoxy(xx,yy); printf(" "); gotoxy(x,y); printf("%s",str[12]); } } } inline int Map::map3()//南门 { int a,b; int st[22][20]={ 1,1,1,1,1,1,1,1,13,25,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1, 1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1, 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1, 1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,31,32,33,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };//22行20列 shua(st); while(1) { yidong(a,b); if(strr==27) { ztjm(st); continue; } srand((unsigned)time(NULL)); if(rand()%50<15) { people->CombatC(); people->COMBAT(); shua(st); } if((zy+b==8||zy+b==9)&&(zx+a==0)) { zx=19;x=zx; return 1;//主城 } if(zx+a>0&&zx+a<21&&zy+b>0&&zy+b<19&&st[zx+a][zy+b]!=1&&st[zx+a][zy+b]<12) { zx+=a,zy+=b; xx=x;yy=y; x=zx;y=zy*2; gotoxy(xx,yy); printf(" "); gotoxy(x,y); printf("%s",str[12]); } } } inline int Map::map4()//东门 { int a,b; int st[22][20]={ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,34,35,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1, 1,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1, 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 13,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,1,1,0,1,1,0,0,0,1,1,0,0,0,1, 1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1, 1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1, 1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };//22行20列 shua(st); while(1) { yidong(a,b); if(strr==27) { ztjm(st); continue; } srand((unsigned)time(NULL)); if(rand()%50<15) { people->CombatC(); people->COMBAT(); shua(st); } if((zx+a==12||zx+a==13)&&(zy+b==0)) { zy=18;y=2*zy; return 1;//主城 } if(zx+a>0&&zx+a<21&&zy+b>0&&zy+b<19&&st[zx+a][zy+b]!=1&&st[zx+a][zy+b]<12) { zx+=a,zy+=b; xx=x;yy=y; x=zx;y=zy*2; gotoxy(xx,yy); printf(" "); gotoxy(x,y); printf("%s",str[12]); } } } inline int Map::map5()//西门 { int a,b; int st[22][20]={ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,36,37,38,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,0,1,1,1,1,0,1,1,0,0,0,0,0,0,1, 1,0,0,0,1,0,1,0,0,1,0,1,1,0,0,0,0,0,0,1, 1,0,0,0,1,0,1,0,0,1,0,1,1,0,0,0,0,0,0,1, 1,0,0,0,1,1,1,0,0,1,1,1,1,0,0,0,1,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13, 1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,25, 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };//22行20列 shua(st); while(1) { yidong(a,b); if(strr==27) { ztjm(st); continue; } srand((unsigned)time(NULL)); if(rand()%50<15) { people->CombatC(); people->COMBAT(); shua(st); } if((zx+a==12||zx+a==13)&&(zy+b==19)) { zy=1;y=2*zy; return 1;//主城 } if(zx+a>0&&zx+a<21&&zy+b>0&&zy+b<19&&st[zx+a][zy+b]!=1&&st[zx+a][zy+b]<12) { zx+=a,zy+=b; xx=x;yy=y; x=zx;y=zy*2; gotoxy(xx,yy); printf(" "); gotoxy(x,y); printf("%s",str[12]); } } }
文件读取:
███████████████████████████████████████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███████████████████████████████████████
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-17 11:36:27