这是刚完成的成品,很是激动,先急急发下!
getch.h #ifndef GETCH_H #define GETCH_H #include <termios.h> /* for tcxxxattr, ECHO, etc */ #include <unistd.h> /* for STDIN_FILENO *//*simulate windows' getch(), it works!!*/ int getch (void) { int ch; struct termios oldt, newt;// get terminal input's attribute tcgetattr(STDIN_FILENO, &oldt); newt = oldt; //set termios' local mode newt.c_lflag &= ~(ECHO|ICANON); tcsetattr(STDIN_FILENO, TCSANOW, &newt); //read character from terminal input ch = getchar(); //recover terminal's attribute tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return ch; } #endif main.cpp #include "structs.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "getch.h" #define weichengshu 0 #define chengshu 1 #define kuwei 2 Plant plant[4]; void flush(User* user); //刷新 void print(char c,int number) //连续输出number个c字符 { int i=0; for(;i<number;i++) { printf("%c",c); } } void printTitle() //打印“开心农场”头部内容 { system("clear"); print('/n',1); print(' ',32); printf("开 心 农 场/n"); print('-',80); printf("/n"); } void printMenu() //打印登陆注册主菜单 { printTitle(); print('/n',7); print(' ',32); printf("|1. 登陆游戏|/n"); print(' ',32); printf("|2. 注册游戏|/n"); print(' ',32); printf("|3. 退出游戏|/n"); print('/n',2); print(' ',32); printf("请选择操作:"); } void initPlant(Plant* plant) //初始化农作物 { strcpy(plant[0].name,"萝卜"); //初始化萝卜 plant[0].id = 0; plant[0].gainAcount = 10; plant[0].grassState = 0; plant[0].wormState = 0; plant[0].plantState = weichengshu; plant[0].plantedTime = (unsigned long)time(NULL); plant[0].groupTime = 60; strcpy(plant[1].name,"白菜"); //初始化白菜 plant[1].id = 1; plant[1].gainAcount = 10; plant[1].grassState = 0; plant[1].wormState = 0; plant[1].plantState = weichengshu; plant[1].plantedTime = (unsigned long)time(NULL); plant[1].groupTime = 120; strcpy(plant[2].name,"玉米"); //初始化玉米 plant[2].id = 2; plant[2].gainAcount = 10; plant[2].grassState = 0; plant[2].plantState = weichengshu; plant[2].wormState = 0; plant[2].plantedTime = (unsigned long)time(NULL); plant[2].groupTime = 180; strcpy(plant[3].name,"黄豆"); //初始化黄豆 plant[3].id = 3; plant[3].gainAcount = 10; plant[3].grassState = 0; plant[3].plantState = weichengshu; plant[3].wormState = 0; plant[3].plantedTime = (unsigned long)time(NULL); plant[3].groupTime = 240; } void initLand(Land* land) //初始化新用户12块土地信息 { int i=0; for(;i<6;i++) { land[i].isPlanted = 1; strcpy(land[i].plant.name,"空地"); land[i].plant.id = 4; land[i].plant.gainAcount = 10; land[i].plant.grassState = 0; land[i].plant.plantState = weichengshu; land[i].plant.wormState = 0; land[i].plant.plantedTime = (unsigned long)time(NULL); land[i].plant.groupTime = 0; land[i].dryState = 0; } i=6; for(;i<12;i++) { land[i].isPlanted = 0; strcpy(land[i].plant.name,"空地"); land[i].plant.id = 4; land[i].plant.gainAcount = 10; land[i].plant.grassState = 0; land[i].plant.plantState = weichengshu; land[i].plant.wormState = 0; land[i].plant.plantedTime = (unsigned long)time(NULL); land[i].plant.groupTime = 0; land[i].dryState = 0; } } void initSeed(Seed* seed) //初始化仓库中种子 { strcpy(seed[0].name,"萝卜"); seed[0].id = 0; seed[0].seedPrice = 50; seed[0].seedNumber = 5; strcpy(seed[1].name,"白菜"); seed[1].id = 1; seed[1].seedPrice = 70; seed[1].seedNumber = 5; strcpy(seed[2].name,"玉米"); seed[2].id = 2; seed[2].seedPrice = 80; seed[2].seedNumber = 5; strcpy(seed[3].name,"黄豆"); seed[3].id = 3; seed[3].seedPrice = 100; seed[3].seedNumber = 5; } void initGain(Gain* gain) //初始化仓库中果实 { strcpy(gain[0].name,"萝卜"); gain[0].id = 0; gain[0].gainPrice = 10; gain[0].gainNumber = 0; strcpy(gain[1].name,"白菜"); gain[1].id = 1; gain[1].gainPrice = 15; gain[1].gainNumber = 0; strcpy(gain[2].name,"玉米"); gain[2].id = 2; gain[2].gainPrice = 20; gain[2].gainNumber = 0; strcpy(gain[3].name,"黄豆"); gain[3].id = 3; gain[3].gainPrice = 25; gain[3].gainNumber = 0; } void initStorage(Storage* storage) //初始化新用户仓库 { storage->experience = 0; storage->grade = 1; storage->money = 500; initSeed(storage->seed); initGain(storage->gain); } //初始化新用户信息 void initUser(User* user,char* registName,char* registPwd) { strcpy(user->name,registName); strcpy(user->password,registPwd); initPlant(plant); initLand(user->land); initStorage(&(user->storage)); } void saveUserInfo(User* user) //保存用户信息到文件 { FILE* saveStream; if((saveStream = fopen("data.dat","ab"))==NULL) { printf("保存用户信息的文件不存在!/n"); getch(); exit(0); } fwrite(user,sizeof(User),1,saveStream); fclose(saveStream); } //判断注册帐号是否已存在 int checkUserRegist(User* user,char* registName) { int userIndex = 0; int userNumber; FILE *registStream; registStream = fopen("data.dat","ab+"); fseek(registStream,0L,SEEK_END); userNumber=ftell(registStream)/sizeof(User); for(;userIndex<userNumber;userIndex++) { fseek(registStream,sizeof(User)*userIndex,SEEK_SET); fread(user,sizeof(User),1,registStream); if(0==strcmp(user->name,registName)) { return 0; break; } } fclose(registStream); return 1; } void printRegistSucceed() //打印注册成功 { printTitle(); print('/n',2); print(' ',32); printf("注 册 游 戏/n"); print('/n',7); print(' ',28); printf("注 册 成 功! /n"); print(' ',28); printf("按任意键返回主菜单!/n"); getchar(); getch(); /////////////////////// } void printRegistFaild() //打印注册失败 { printTitle(); print('/n',2); print(' ',32); printf("注 册 游 戏/n"); print('/n',7); print(' ',26); printf("已存在帐号,请重新注册/n"); print(' ',28); printf("按任意键返回主菜单!/n"); getchar(); getch(); /////////////////////// } void regist() //注册新用户 { char registName[20]; char registPwd[20]; printTitle(); print('/n',2); print(' ',32); printf("注 册 游 戏/n"); print('/n',7); print(' ',28); printf(" 请输入帐号: "); scanf("%s",registName); printTitle(); print('/n',2); print(' ',32); printf("注 册 游 戏/n"); print('/n',7); print(' ',28); printf(" 请输入密码: "); scanf("%s",registPwd); User user; if(checkUserRegist(&user,registName)) { initUser(&user,registName,registPwd); saveUserInfo(&user); printRegistSucceed(); } else { printRegistFaild(); } } //判断用户是否能登陆 int checkUserLoad(User* user,char* loadName,char* loadPwd) { int userIndex = 0; int userNumber; FILE *loadStream; if((loadStream = fopen("data.dat","rb"))==NULL) { printf("保存用户信息的文件不存在!/n"); getch(); exit(0); } fseek(loadStream,0L,SEEK_END); userNumber = ftell(loadStream)/sizeof(User); printf("loaduserNo:%d/n",userNumber);/////////////////////// for(;userIndex<userNumber;userIndex++) { fseek(loadStream,sizeof(User)*userIndex,SEEK_SET); fread(user,sizeof(User),1,loadStream); if((0==strcmp(user->name,loadName))&&(0==strcmp(user->password,loadPwd))) { return 1; break; } } fclose(loadStream); return 0; } void printLand(User* user) //输出土地信息 { flush(user); printf("土地状态in printLand%d/n",user->land[0].plant.plantState); int landIndex = 0; printTitle(); printf("玩家:%s/t/t等级:%d/t/t经验:%d/t/t金钱:%.2f/n",user->name,user->storage.grade,user->storage.experience,user->storage.money); print('-',80); printf("/n"); for(;landIndex<3;landIndex++) { printf("/t%d.土地/t/t%d.土地/t/t%d.土地/t/t%d.土地/t/n",4*landIndex+1,4*landIndex+2,4*landIndex+3,4*landIndex+4); int i = 0; for(;i<4;i++) { if(user->land[4*landIndex+i].isPlanted) { printf("/t%s/t",user->land[4*landIndex+i].plant.name); } else { printf("/t荒 地/t"); } } printf("/n"); i = 0; for(;i<4;i++) { if((user->land[4*landIndex+i].plant.id!=4)&&user->land[4*landIndex+i].isPlanted &&(user->land[4*landIndex+i].plant.plantState==weichengshu)) { printf("/t未成熟/t"); } else if((user->land[4*landIndex+i].plant.id!=4)&&user->land[4*landIndex+i].isPlanted &&(user->land[4*landIndex+i].plant.plantState==chengshu)) { printf("/t已成熟/t"); } else if((user->land[4*landIndex+i].plant.id!=4)&&user->land[4*landIndex+i].isPlanted &&(user->land[4*landIndex+i].plant.plantState==kuwei)) { printf("/t枯 萎/t"); } else { printf("/t /t"); } } printf("/n"); i = 0; for(;i<4;i++) { if((user->land[4*landIndex+i].plant.id!=4)&&user->land[4*landIndex+i].plant.wormState) { printf("/t虫 害/t"); } else { printf("/t /t"); } } printf("/n"); i = 0; for(;i<4;i++) { if((user->land[4*landIndex+i].plant.id!=4)&&user->land[4*landIndex+i].plant.grassState) { printf("/t杂 草/t"); } else { printf("/t /t"); } } printf("/n"); i = 0; for(;i<4;i++) { if((user->land[4*landIndex+i].plant.id!=4)&&user->land[4*landIndex+i].dryState) { printf("/t干 旱/t"); } else { printf("/t /t"); } } printf("/n"); print('-',80); printf("/n"); } } void reSaveUser(User* user) //重新保存用户信息 { int userNumber; int index; FILE* reSaveStream; User userTemp; if((reSaveStream = fopen("data.dat","rb+"))==NULL) { printf("保存用户信息的文件不存在!/n"); getch(); exit(0); } fseek(reSaveStream,0L,SEEK_END); userNumber = ftell(reSaveStream)/sizeof(User); index = 0; for(;index<userNumber;index++) { fseek(reSaveStream,sizeof(User)*index,SEEK_SET); fread(&userTemp,sizeof(User),1,reSaveStream); if(strcmp(userTemp.name,user->name)==0) { fseek(reSaveStream,sizeof(User)*index,SEEK_SET); fwrite(user,sizeof(User),1,reSaveStream); break; } } fclose(reSaveStream); } void newLand(User* user) //开垦土地 { int select; while(1) { printLand(user); print(' ',28); printf("开 垦 土 地/n"); print(' ',24); printf("请选择土地(0键返回):"); scanf("%d",&select); if(select==0) { getch(); break; } else if(select>12||select<0) { print(' ',24); printf("请选择正确的土地!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].isPlanted) { print(' ',24); printf("所选土地已开垦!(任意键返回)"); getchar(); getch(); } else if(user->storage.money<1000) { print(' ',24); printf("你的金钱不够!(任意键返回)"); getchar(); getch(); } else { user->land[select-1].isPlanted = 1; user->storage.experience+=50; user->storage.money-=1000; } } } void newPlant(User* user) //种植新作物 { int select; int index; while(1) { printLand(user); print(' ',28); printf("种 植 作 物/n"); print(' ',24); printf("请选择土地(0键返回):"); scanf("%d",&select); if(select==0) { getch(); break; } else if(!user->land[select-1].isPlanted) { print(' ',24); printf("所选土地未开垦!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].plant.id!=4) { print(' ',24); printf("所选土地已有作物!(任意键返回)"); getchar(); getch(); } else { while(1) { printLand(user); print(' ',28); printf("仓 库 种 子/n"); printf("/t/t1.萝卜:%d粒/t/t2.白菜:%d粒/t/n",user->storage.seed[0].seedNumber, user->storage.seed[1].seedNumber); printf("/t/t3.玉米:%d粒/t/t4.黄豆:%d粒/t/n",user->storage.seed[2].seedNumber, user->storage.seed[3].seedNumber); print(' ',24); printf("请选择种子(0键返回):"); scanf("%d",&index); if(index==0) { break; } else if(user->storage.seed[index-1].seedNumber<=0) { print(' ',24); printf("种子没库存!(任意键返回)"); getchar(); getch(); break; } else { strcpy(user->land[select-1].plant.name,plant[index-1].name); user->land[select-1].plant.id = plant[index-1].id; user->land[select-1].plant.groupTime = plant[index-1].groupTime; user->land[select-1].plant.plantedTime = (unsigned long)time(NULL); user->land[select-1].plant.wormState = plant[index-1].wormState; user->land[select-1].plant.grassState = plant[index-1].grassState; user->land[select-1].plant.plantState = plant[index-1].plantState; user->storage.seed[index-1].seedNumber-=1; user->storage.experience+=50; break; } } } } } void clearPlant(User* user) //铲除农作物 { int select; while(1) { printLand(user); print(' ',28); printf("铲 除 作 物/n"); print(' ',24); printf("请选择土地(0键返回):"); scanf("%d",&select); if(0==select) { getch(); break; } else if(!user->land[select-1].isPlanted) { print(' ',24); printf("所选土地未开垦!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].plant.id==4) { print(' ',24); printf("所选土地无作物!(任意键返回)"); getchar(); getch(); } else { strcpy(user->land[select-1].plant.name,"空地"); user->land[select-1].plant.id = 4; user->land[select-1].plant.gainAcount = 10; user->land[select-1].plant.grassState = 0; user->land[select-1].plant.plantState = 0; user->land[select-1].plant.wormState = 0; user->land[select-1].plant.plantedTime = (unsigned long)time(NULL); user->land[select-1].plant.groupTime = 0; } } } void getGain(User* user) //收割果实 { int index = 0; for(;index<12;index++) { if(user->land[index].isPlanted) { if(user->land[index].plant.id!=4) { if(user->land[index].plant.plantState==chengshu) { user->storage.gain[user->land[index].plant.id].gainNumber+= user->land[index].plant.gainAcount; user->land[index].plant.plantState = kuwei; } } } } printLand(user); print(' ',28); printf("收 割 作 物/n"); print(' ',24); printf("收割完毕(任意键返回)"); user->storage.experience+=50; getchar(); getch(); } void clearWorm(User* user) //除虫 { int select; while(1) { printLand(user); print(' ',28); printf(" 除 虫 /n"); print(' ',24); printf("请选择土地(0键返回):"); scanf("%d",&select); if(0==select) { getch(); break; } else if(!user->land[select-1].isPlanted) { print(' ',24); printf("所选土地未开垦!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].plant.id==4) { print(' ',24); printf("所选土地无作物!(任意键返回)"); getchar(); getch(); } else if(!user->land[select-1].plant.wormState) { print(' ',24); printf("所选土地无虫害!(任意键返回)"); getchar(); getch(); } else { user->land[select-1].plant.wormState = 0; print(' ',24); printf("除害虫成功!(任意键返回)"); user->storage.experience+=50; getchar(); getch(); } } } void clearGrass(User* user) //除草 { int select; while(1) { printLand(user); print(' ',28); printf(" 除 草 /n"); print(' ',24); printf("请选择土地(0键返回):"); scanf("%d",&select); if(0==select) { getch(); break; } else if(!user->land[select-1].isPlanted) { print(' ',24); printf("所选土地未开垦!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].plant.id==4) { print(' ',24); printf("所选土地无作物!(任意键返回)"); getchar(); getch(); } else if(!user->land[select-1].plant.grassState) { print(' ',24); printf("所选土地无杂草!(任意键返回)"); getchar(); getch(); } else { user->land[select-1].plant.grassState = 0; print(' ',24); printf("除杂草成功!(任意键返回)"); user->storage.experience+=50; getchar(); getch(); } } } void doFertilize(User* user) //施肥 { int select; while(1) { printLand(user); print(' ',28); printf(" 施 肥 /n"); print(' ',24); printf("请选择土地(0键返回):"); scanf("%d",&select); if(0==select) { getch(); break; } else if(select>12||select<0) { print(' ',24); printf("请选择正确的土地!(任意键返回)"); getchar(); getch(); } else if(!user->land[select-1].isPlanted) { print(' ',24); printf("所选土地未开垦!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].plant.id==4) { print(' ',24); printf("所选土地无作物!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].plant.groupTime<=40) { print(' ',24); printf("肥够多了!(任意键返回)"); getchar(); getch(); } else if(user->storage.money<200) { print(' ',24); printf("你的金币不够!(任意键返回)"); getchar(); getch(); } else { user->land[select-1].plant.groupTime-=20; print(' ',24); printf("施肥成功!(任意键返回)"); user->storage.experience+=50; user->storage.money-=200; getchar(); getch(); } } } void watering(User* user) //浇水 { int select; while(1) { printLand(user); print(' ',28); printf(" 浇 水 /n"); print(' ',24); printf("请选择土地(0键返回):"); scanf("%d",&select); if(0==select) { getch(); break; } else if(!user->land[select-1].isPlanted) { print(' ',24); printf("所选土地未开垦!(任意键返回)"); getchar(); getch(); } else if(user->land[select-1].plant.id==4) { print(' ',24); printf("所选土地无作物!(任意键返回)"); getchar(); getch(); } else if(!user->land[select-1].dryState) { print(' ',24); printf("水够多了!(任意键返回)"); getchar(); getch(); } else { user->land[select-1].dryState = 0; print(' ',24); printf("浇水成功!(任意键返回)"); user->storage.experience+=50; user->land[select-1].plant.gainAcount+=2; getchar(); getch(); } } } void inStorage(User* user) //仓库 { int select; int index; int seedNo; while(1) { printLand(user); print(' ',28); printf(" 仓 库 /n"); printf("/t/t1.卖果实/t/t2.买种子/t/n"); print(' ',24); printf("请选择操作(0键返回):"); scanf("%d",&select); if(0==select) { getch(); break; } else if(1==select) { printLand(user); print(' ',28); printf(" 仓 库 果 实 /n"); printf("/t/t1.萝卜:%d/t/t2.白菜:%d/t/n",user->storage.gain[0].gainNumber, user->storage.gain[1].gainNumber); printf("/t/t3.玉米:%d/t/t4.黄豆:%d/t/n",user->storage.gain[2].gainNumber, user->storage.gain[3].gainNumber); print(' ',24); index = 0; for(;index<4;index++) { user->storage.money+=user->storage.gain[index].gainNumber* user->storage.gain[index].gainPrice; user->storage.gain[index].gainNumber = 0; } printf("以上果实卖出成功(任意键键返回)"); getchar(); getch(); } else if(2==select) { while(1) { printLand(user); print(' ',28); printf(" 仓 库 /n"); printf("/t/t1.萝卜:%.2f金币/粒/t/t2.白菜:%.2f金币/粒/t/n",user->storage.seed[0].seedPrice, user->storage.seed[1].seedPrice); printf("/t/t3.玉米:%.2f金币/粒/t/t4.黄豆:%.2f金币/粒/t/n",user->storage.seed[2].seedPrice, user->storage.seed[3].seedPrice); print(' ',23); printf("请选择买入的种子(0键返回):"); scanf("%d",&index); if(0==index) { break; } else if(index<0||index>3) { print(' ',23); printf("请选择正确的种子(任意键键返回):"); getchar(); getch(); } else { print(' ',23); printf("请选择买入的数量:"); scanf("%d",&seedNo); if(seedNo>0) { if(seedNo*user->storage.seed[index-1].seedPrice>user->storage.money) { print(' ',23); printf("你的金钱不够(任意键键返回):"); getchar(); getch(); } else { user->storage.money-=seedNo*user->storage.seed[index-1].seedPrice; user->storage.seed[index-1].seedNumber+=seedNo; print(' ',24); printf("买种子成功(任意键键返回)"); getchar(); getch(); } } else { print(' ',23); printf("请选择正确的数字(任意键键返回):"); getchar(); getch(); } } } } } } void flush(User* user) //刷新 { int index = 0; int number; long nowTime; nowTime=(unsigned long)time(NULL); srand((unsigned)time(NULL)); for(;index<12;index++) { if(kuwei!=user->land[index].plant.plantState&&user->land[index].isPlanted &&user->land[index].plant.id!=4) { if((nowTime-user->land[index].plant.plantedTime)>=user->land[index].plant.groupTime) { user->land[index].plant.plantState = chengshu; if(user->land[index].dryState) { user->land[index].plant.gainAcount-=2; } if(user->land[index].plant.wormState) { user->land[index].plant.gainAcount-=2; } if(user->land[index].plant.grassState) { user->land[index].plant.gainAcount-=2; } } } if(!user->land[index].dryState&&user->land[index].isPlanted &&(user->land[index].plant.plantState==weichengshu)) { number = rand()%400; if(number>390) { user->land[index].dryState = 1; } } if(!user->land[index].plant.grassState&&user->land[index].isPlanted &&(user->land[index].plant.plantState==weichengshu)) { number = rand()%400; if(number<300&&number>290) { user->land[index].plant.grassState = 1; } } if(!user->land[index].plant.wormState&&user->land[index].isPlanted &&(user->land[index].plant.plantState==weichengshu)) { number = rand()%400; if(number>200&&number<=190) { user->land[index].plant.wormState = 1; } } } } void quit(User* user) //退出 { reSaveUser(user); exit(0); } void mainMenu(User* user) //农场 主界面 { char select; while(1) { printLand(user); printf("/t1.开垦/t/t2.种植/t/t3.铲除/t/t4.收割/t/n"); printf("/t5.除虫/t/t6.除草/t/t7.施肥/t/t8.浇水/t/n"); printf("/t9.仓库/t/tF.刷新/t/tB.注销/t/tQ.退出/t/n"); print(' ',28); printf("请选择操作:"); scanf("%c",&select); switch(select) { case '1': newLand(user); break; case '2': newPlant(user); break; case '3': clearPlant(user); break; case '4': getGain(user); break; case '5': clearWorm(user); break; case '6':