C++写养成类游戏
小公举养成记
游戏界面如下图
星座是函数内部计算得来的
这个游戏和我上次写游戏《笑傲江湖之鸿蒙》相似,不过代码量更少,而且是用C++语言写的,
当然凭我现在的水平也只能写这种人物动作全靠文字描述的游戏,虽然不难,但也挺经典
代码如下:
main.cpp文件
#include <iostream> #include <windows.h> #include <iomanip> #include <ctime> #include <cstring> #include <mmsystem.h> //导入声音头文件 #pragma comment(lib,"Winmm.lib")
include "XGG.h"
define left 20
using namespace std;
const string heng="--------------------------------------------------"; //宏定义
int * my_attribute; //定义指向女儿属性指针
//界面设计
void show()
{set_color(2,0); set_position(left,0); cout <<heng; set_position(left,1); cout <<"|"; set_position(left+18,1); set_color(12,0); cout <<"小公举养成记"; set_color(2,0); set_position(left+49,1); cout <<"|"; set_position(left,2); cout <<heng; for(int i=0;i<8;i++) { set_position(left,3+i); cout <<"|"; set_position(left+49,3+i); cout <<"|"; } set_position(left,11); cout <<heng; for(int i=0;i<8;i++) { set_position(left,11+i); cout <<"|"; set_position(left+49,11+i); cout <<"|"; } set_position(left,19); cout <<heng;
}
//星座函数(通过月份与日期查找当前星座)
string constell[12][2]={
{"山羊座","水瓶座"},
{"水瓶座","双鱼座"},
{"双鱼座","白羊座"},
{"白羊座","金牛座"},
{"金牛座","双子座"},
{"双子座","巨蟹座"},
{"巨蟹座","狮子座"},
{"狮子座","处女座"},
{"处女座","天秤座"},
{"天秤座","天蝎座"},
{"天蝎座","射手座"},
{"射手座","山羊座"},};
//十二星座的天数
int constell_day[]={20,19,21,20,21,22,23,23,23,24,23,22};
//创建属性数组,属性包括(体力、智力、武力、魅力、自尊、道德、气质、体贴、魔法)
string attribute_name[9]={"体力","智力","武力","魅力","自尊","道德","气质","体贴","魔法"};
int attribute[12][2][9]=
{
{{43,43,20,27,23,27,32,11,17},{41,20,29,25,23,32,35,20,19}},
{{41,20,29,25,23,32,35,20,19},{80,15,15,25,11,21,21,10,40}},
{{80,15,15,25,11,21,21,10,40},{46,30,28,35,20,29,20,23,20}},
{{46,30,28,35,20,29,20,23,20},{50,35,23,26,18,20,42,12,25}},
{{50,35,23,26,18,20,42,12,25},{40,31,33,23,17,33,37,14,16}},
{{40,31,33,23,17,33,37,14,16},{85,19,11,28,20,37,10,13,37}},
{{85,19,11,28,20,37,10,13,37},{35,28,36,19,18,40,30,31,10}},
{{35,28,36,19,18,40,30,31,10},{42,33,25,24,32,28,29,18,20}},
{{42,33,25,24,32,28,29,18,20},{50,25,40,20,18,17,33,10,18}},
{{50,25,40,20,18,17,33,10,18},{27,31,15,26,19,20,20,23,26}},
{{27,31,15,26,19,20,20,23,26},{56,21,16,22,25,23,27,40,20}},
{{56,21,16,22,25,23,27,40,20},{43,43,20,27,23,27,32,11,17}}};
//血型属性,各种血型有各种附加能力
void bloods(string _blood)
{
if(_blood"A"||_blood"a")
{
my_attribute[5]+=10;
my_attribute[6]+=10;
}
else if(_blood"B"||_blood"b")
{
my_attribute[2]+=10;
my_attribute[4]+=10;
}
else if(_blood"AB"||_blood"ab")
{
my_attribute[1]+=10;
my_attribute[8]+=10;
}
else if(_blood"O"||_blood"o")
{
my_attribute[3]+=10;
my_attribute[7]+=10;
}
else
{
set_position(left+2,8);
cout <<"血型输入不合法!";
}}
//主函数
int main(){
PlaySound("wen.wav",NULL, SND_FILENAME | SND_ASYNC|SND_LOOP); //背景音乐循环播放//修改窗口名称,使用windows.h自带函数 SetConsoleTitle("小公举养成记"); //显示界面 show(); //用户输入数据,并初始化数据 int money=50; //游戏内金钱初始化 string fathername; string daughtername; int birth_month; int birth_day; string blood; set_color(9,0); set_position(left+2,3); cout <<"请输入父亲姓名:"; cin >>fathername; set_position(left+2,4); cout <<"请输入女儿姓名:"; cin >>daughtername; set_position(left+2,5); cout <<"请输入女儿出生日期(几月几日):"; set_position(left+34,5); cout <<"月"; set_position(left+38,5); cout <<"日"; set_position(left+32,5); cin >>birth_month; set_position(left+36,5); cin >>birth_day; //当获取到出生日期后,打印星座 set_position(left+2,6); string my_constell; my_constell=constell[birth_month-1][birth_day/constell_day[birth_month-1]]; cout <<"星座:" <<my_constell; //提取出该星座属性 my_attribute=attribute[birth_month-1][birth_day/constell_day[birth_month-1]]; set_position(left+2,7); cout <<"请输入女儿血型:"; cin >>blood; bloods(blood); //清空界面一 cclear(left+1,3,8); //开场描述 set_position(left+5,3); cout <<"1852年的"<<birth_month<<"月,勇士"<<fathername<<"从邪恶的巨龙手中"; set_position(left+1,4); cout <<"救下了一个3岁的小女孩"<<daughtername<<",而这一天也就成了"; set_position(left+1,5); cout <<"小女孩的生日。她的命运也将从此发生改变"; //操作界面显示 int m; //定义一个变量,代表随机产生的数 int month=birth_month; for(int year=1852;year<1868;year++) for(month=(month==birth_month)?birth_month:1;month<=12;month++) { bloods(blood); //当满足该血型时,每月获得特定属性加成 //当女儿生日时,同样随机获得属性加成 if(month==birth_month) { srand((unsigned)time(NULL)); m=rand()%8; my_attribute[m]+=m; my_attribute[m-1]+=m-1; money+=10*m; set_position(left+2,8); cout <<"本月是女儿生日,父亲精心给女儿准备了礼物!"; set_position(left+2,9); cout <<attribute_name[m]<<"+"<<m; cout <<attribute_name[m-1]<<"+"<<m-1; cout <<"金钱"<<"+"<<m*10; set_position(left+10,10); system("pause"); } //界面1显示并选择操作 int operation; cclear(left+1,3,8); set_position(left+10,3); cout <<"1.查看状态"; set_position(left+10,4); cout <<"2.安排行程"; set_position(left+10,5); cout <<"3.亲子谈话"; set_position(left+10,6); cout <<"4.退出游戏"; //显示人物金钱 set_position(left+30,3); cout <<"金钱:"<<money<<"¥"; //显示游戏内时间(界面1的右下角) set_position(left+30,10); cout <<"时间:"<<year<<"年"<<month<<"月"; set_position(left+10,7); cout <<"请选择操作:"; cin >>operation; switch(operation) { case 1: { //显示女儿各种信息 cclear(left+1,12,7); set_position(left+2,12); cout <<"姓名:"<<daughtername<<"\t"<<"年龄:"<<3+(year-1852)<<"\t\t"<<"父亲:"<<fathername; set_position(left+2,13); cout <<"生日:"<<birth_month<<"月"<<birth_day<<"日"<<"\t"<<"星座:"<<my_constell<<"\t"<<"血型:"<<blood; set_position(left+2,14); cout <<attribute_name[0]<<":"<<my_attribute[0]<<"\t\t"<<attribute_name[1]<<":"<<my_attribute[1]<<"\t"<<attribute_name[2]<<":"<<my_attribute[2]; set_position(left+2,15); cout <<attribute_name[3]<<":"<<my_attribute[3]<<"\t\t"<<attribute_name[4]<<":"<<my_attribute[4]<<"\t"<<attribute_name[5]<<":"<<my_attribute[5]; set_position(left+2,16); cout <<attribute_name[6]<<":"<<my_attribute[6]<<"\t\t"<<attribute_name[7]<<":"<<my_attribute[7]<<"\t"<<attribute_name[8]<<":"<<my_attribute[8]; } break; case 2: { //界面2显示并选择操作 int operation2; cclear(left+1,12,7); set_position(left+5,12); cout <<"1.学堂上课"; set_position(left+5,13); cout <<"2.出城踏青"; set_position(left+5,14); cout <<"3.集市闲逛"; set_position(left+5,15); cout <<"4.兼职赚钱"; set_position(left+5,16); cout <<"请选择操作:"; cin >>operation2; if(operation2==1) { set_position(left+2,17); cout <<"上课什么的最无聊了,毕竟我是个学霸,不用学也会!"; srand((unsigned)time(NULL)); m=rand()%8; my_attribute[m]+=m; my_attribute[m-1]+=m-1; my_attribute[m+1]+=m+1; set_position(left+2,18); cout <<attribute_name[m]<<"+"<<m; cout <<attribute_name[m-1]<<"+"<<m-1; cout <<attribute_name[m+1]<<"+"<<m+1; } else if(operation2==2) { set_position(left+2,17); cout <<"城外阳光明媚,风景迷人,真舍不得这么早回家啊!"; set_position(left+2,18); srand((unsigned)time(NULL)); m=rand()%8; my_attribute[m]+=m; my_attribute[m-1]+=m-1; my_attribute[m+1]+=m+1; cout <<attribute_name[m]<<"+"<<m; cout <<attribute_name[m-1]<<"+"<<m-1; cout <<attribute_name[m+1]<<"+"<<m+1; } else if(operation2==3) { if(money<=0) { set_position(left+2,17); cout <<"口袋空空,连个钢镚都没有!逛个屁! "; } set_position(left+2,17); cout <<"出来逛街就是爽!让我买买买!这个要!这个也要!"; set_position(left+2,18); srand((unsigned)time(NULL)); m=rand()%8; my_attribute[m]+=m; my_attribute[m-1]+=m-1; money-=m*10; cout <<attribute_name[m]<<"+"<<m; cout <<attribute_name[m-1]<<"+"<<m-1; cout <<"金钱"<<"-"<<m*10; } else if(operation2==4) { set_position(left+2,17); cout <<"又是元气满满的一天,搞钱才是人生大事!╯^╰"; set_position(left+2,18); srand((unsigned)time(NULL)); m=rand()%5+10; money+=m*10; cout <<"金钱"<<"+"<<m*10; } } break; case 3: { srand((unsigned)time(NULL)); m=rand()%10+5; if(m%2==0) { cclear(left+1,12,7); set_position(left+2,12); cout <<"为了教育女儿,父亲今天说话非常严厉!"; set_position(left+2,13); my_attribute[1]+=m+1; my_attribute[5]+=m+2; my_attribute[7]+=m+3; cout <<attribute_name[1]<<"+"<<m+1; cout <<attribute_name[5]<<"+"<<m+2; cout <<attribute_name[7]<<"+"<<m+3; } else { cclear(left+1,12,7); set_position(left+2,12); cout <<"父亲细心的教导女儿,语气温柔!"; set_position(left+2,13); my_attribute[0]+=m+1; my_attribute[3]+=m+2; my_attribute[4]+=m+3; cout <<attribute_name[0]<<"+"<<m+1; cout <<attribute_name[3]<<"+"<<m+2; cout <<attribute_name[4]<<"+"<<m+3; } } break; case 4: system("cls"); exit(0); break; } } //游戏后续 cclear(left+1,3,8); cclear(left+1,12,7); set_position(left+2,4); cout <<"一眨眼,已经过去这么多年了!女儿已经18岁了!"; set_position(left+2,6); int sum; //定义人物属性总和 //根据人物属性,得到不同的结局 sum=my_attribute[0]+my_attribute[1]+my_attribute[2]+my_attribute[3]+my_attribute[4]+my_attribute[5]+my_attribute[6]+my_attribute[7]+my_attribute[8]; if(sum>=10000) { cout <<"在不断努力下,小女孩成为了让所有人敬佩的女王!"; } if(sum>=8000&&sum<10000) { cout <<"小女孩嫁给了王子,收获了爱情,成为了王妃!"; } if(sum>=5000&&sum<8000) { cout <<"小女孩凭借武艺,当上了皇室的女将军,风光无限!"; } if(sum>=3000&&sum<5000) { srand((unsigned)time(NULL)); m=rand()%4; if(m==0) { cout <<"小女孩成为了皇家学院的院长!教书育人!"; } if(m==1) { cout <<"小女孩深的皇室喜爱,被封为异性公主!"; } if(m==2) { cout <<"近卫团长官聘请小女孩担任团长!"; } if(m==3) { cout <<"小女孩智慧过人,经商有成,成为了皇城有名的富豪!"; } if(m==4) { cout <<"小女孩被决定当个悬壶济世的医生!救人无数!"; } } if(sum<3000) { srand((unsigned)time(NULL)); m=rand()%4; if(m==0) { cout <<"小女孩资质平平,最后还是个平民!"; } if(m==1) { cout <<"小女孩发挥自己的长处,加入了狩猎小队!入山打猎!"; } if(m==2) { cout <<"小女孩文笔不错,最后选择了成为一名作家!"; } if(m==3) { cout <<"小女孩苦于经济,只好在酒吧当了个女郎!"; } if(m==4) { cout <<"小女孩信仰上帝,选择去了修女院进修!"; } } set_position(left,20); cout <<"游戏结束!"<<endl; exit(0); return 0;
}
GXX.h文件
#ifndef XGG_H_INCLUDED #define XGG_H_INCLUDED
include <windows.h>
//改变控制台颜色(前景色、背景色)
//0-黑色,1-蓝色,2-绿色,3-浅绿色,4-红色,5-紫色,6-黄色,7-白色,8-灰色,9-淡蓝色,
//10-淡绿色,11-淡浅绿色,12-淡红色,13-淡紫色,14-淡黄色,15-亮白色
void set_color(int upcolor,int backcolor);
//设置光标位置
void set_position(int x,int y);
//清屏函数
void cclear(int x,int y,int hang);void set_color(int upcolor,int backcolor)
{
HANDLE winHandle; //句柄
winHandle=GetStdHandle(STD_OUTPUT_HANDLE); //将控制台窗口赋值给定义的句柄
//设置文字颜色
SetConsoleTextAttribute(winHandle,upcolor+backcolor*0x10);//设置控制台文本属性
}void set_position(int x,int y)
{
HANDLE winHandle; //句柄
COORD pos={x,y}; //定义一个位置
winHandle=GetStdHandle(STD_OUTPUT_HANDLE); //将控制台窗口赋值给定义的句柄
SetConsoleCursorPosition(winHandle,pos); //设置光标位置
}
void cclear(int x,int y,int row)
{
int i,j;for(i=0;i<row;i++) { set_position(x,y+i); for(j=0;j<48;j++) printf(" "); }
}
endif // XGG_H_INCLUDED
在这个项目中没有像前一个游戏一样大量是用自定义函数,而是将很多功能都在main函数里写,总的来说层次还是很清晰的
一些数据我都是参考这张图片,当然很多内容也有自家的想法
** 背景音乐实现**
头文件
#include <mmsystem.h> //导入声音头文件
#pragma comment(lib,"Winmm.lib")
main函数里
PlaySound("wen.wav",NULL, SND_FILENAME | SND_ASYNC|SND_LOOP); //背景音乐循环播放
这些源代码里都有,这里只是挑出来细讲
注意:
如果使用code:block软件写的话,还需要自己链接库
文件具体位置如下:
C:\Program Files (x86)\CodeBlocks\MinGW\lib\libwinmm.a
谢谢大家
如果哪里有错误和不足,可以告诉我,一起学习,一起进步!