C++小游戏

本人写的一个小游戏.
实际上还没有完成...
先放出来康康吧.

#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;


inline int read(){
	int sum=0,f=1;char c=getchar();
	while((c<'0'||c>'9')&&c!=EOF){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9'&&c!=EOF){sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}

const int N=105;
struct creature{
	int a[N],n;
	string s[N];
	inline void init(){
		n=11;
		s[1]="level";
		s[2]="damage";
		s[3]="spelldamage";
		s[4]="defense";
		s[5]="spellresistance";
		s[6]="HP";
		s[7]="speed";
		s[9]="money";
		s[10]="exp";
		s[11]="next level exp";
	}
	inline void set(int *p){
		for(int i=0;i<=101;i++) a[i]=p[i];
	}
	
};
struct Shop{
	int a[N],f[N],n;
	string s[N],ss[N];
	inline void init(){
		n=8;
		s[1]="Small life potion";ss[1]="Restore 100 health";a[1]=1000;f[1]=100;
		s[2]="Middle life potion";ss[2]="Restore 500 health";a[2]=3000;f[2]=500;
		s[3]="Large life potion";ss[3]="Restore 1000 health";a[3]=7000;f[3]=1000;
		s[4]="Huge life potion";ss[4]="Restore 2000 health";a[4]=16000;f[4]=2000;
		s[5]="Small combat record";ss[5]="Gain 10 exp";a[5]=1000;f[5]=100;
		s[6]="Middle combat record";ss[6]="Gain 50 exp";a[6]=3000;f[6]=500;		
		s[7]="Large combat record";ss[7]="Gain 100 exp";a[7]=7000;f[7]=1000;
		s[8]="Huge combat record";ss[8]="Gain 200 exp";a[8]=16000;f[8]=2000;
	}
}S;
struct Ware{
	int a[N],n;
}W;
creature you,Hastin,puppy;
int progress=1;
inline void fight(creature x,creature y);
inline void save();

inline void check_panel(){
	cout<<"name: "<<you.s[0]<<endl;
	for(int i=1;i<=you.n;i++)
		cout<<you.s[i],printf(": %d\n",you.a[i]);
}

inline void check_exp(){
	while(you.a[10]>=you.a[11]){
		printf("You are level %d!\n",++you.a[1]);
		puts("damage+=15!\ndefense+=10!\nHp+=80!");
		you.a[2]+=15;
		you.a[4]+=10;
		you.a[6]+=80;
		you.a[10]-=you.a[11];you.a[11]+=5;
		if(you.a[1]>40&&you.a[5]<70){
			puts("spellresistance+=1!");				
			you.a[5]++;
		}
		if(you.a[1]>45){
			puts("spelldamage+=1!");
			you.a[3]++;
		}
		if(you.a[1]>60&&you.a[7]<50){
			puts("speed+=1!");
			you.a[7]++;
		}
	}
}

inline void hint(){
	puts("You meet an old man!");
	puts("If you give he 99999 yuan,he will take you to a place.");
	puts("1.yes 2.no");
	int op=read();
	if(op!=1) return ;
	else{
		if(you.a[9]>99999){
			you.a[9]-=99999;
			int tem=rand()%100;
			if(!tem){
				you.a[8]=1;
				puts("Your HP will never fall below 1.");
			}
			else puts("He disappeared.");
		}
		else puts("Amount is less than");
	}
}

inline void layer(int x){
	if(x==1){
		puts("You meet the prairie puppy.");
		int o[103]={0,1,200,0,50,0,1000,5,0,50,100,10,20};
		puppy.set(o);
		fight(you,puppy);
		progress=max(progress,2);
		int tem=rand()%100;
		if(!tem) hint();
	}
	else if(x==2){
		puts("You meet the mountain puppy.");
		int o[103]={0,10,400,10,150,5,2000,8,0,200,300,30,50};
		puppy.set(o);
		fight(you,puppy);
		progress=max(progress,3);
	}
	else if(x==3){
		puts("You meet the sea puppy.");
		int o[103]={0,30,1000,100,500,10,4000,15,0,500,800,100,150};
		puppy.set(o);
		fight(you,puppy);
		progress=max(progress,4);
	}
	else if(x==4){
		puts("You meet the prairie dog.");
		int o[103]={0,50,2100,400,1000,20,9000,24,0,500,800,300,500};
		puppy.set(o);
		fight(you,puppy);
		progress=max(progress,5);

	}
	else if(x==5){
		puts("You will fight Hastin!");
		fight(you,Hastin);
		puts("You killed Hastin!");
		puts("You are the inly god in the world!");
	}
}

inline void world(){
	while(1){
		puts("Which layer do you want to go?");		
		int op=read();
		if(op>progress){
			puts("You can't go there until you're stronger.");
			continue;
		}
		layer(op);break;
	}
}

inline void shop(){
	puts("welcome to the shop!");
	puts("can I help you?");
	puts("1.return 2.buy something");
	int op=read();
	if(op!=2) return ;
	for(int i=1;i<=S.n;i++){
		printf("%d.%s(%s): %dyuan\n",i,S.s[i].c_str(),S.ss[i].c_str(),S.a[i]);
	}
	puts("Which prop do you want to buy?");
	op=read();
	puts("How many props do you want to buy?");
	int sum=read();
	if(you.a[9]>=S.a[op]*sum){
		you.a[9]-=S.a[op]*sum;
		W.a[op]+=sum;
		puts("Purchase succeeds!");
	}
	else puts("Not sufficient funds!");
}

inline void use_props(creature &x){
	for(int i=1;i<=S.n;i++){
		printf("%d.%s: %d\n",i,S.s[i].c_str(),W.a[i]);
	}
	puts("Which prop do you want to use?");
	int op=read();
	if(W.a[op]){
		puts("How many props do you want to use?");
		int sum=read();
		W.a[op]-=sum;
		if(op<=4){
			int tem=min(you.a[6],x.a[6]+S.f[op]*sum);
			printf("You've recovered %d HP!(HP cap limit)\n",tem-x.a[6]);
			printf("Your HP: %d\n",tem);
			x.a[6]=tem;
		}
		else{
			you.a[10]+=S.f[op]*sum;
			printf("You get %d exp!\n",S.f[op]*sum);
			check_exp();
		}
	}
	else puts("You don't have the prop!");	
}

inline void warehouse(){	
	for(int i=1;i<=S.n;i++){
		printf("%d.%s: %d\n",i,S.s[i].c_str(),W.a[i]);
	}
	while(1){
		puts("1.return 2.use props");
		int op=read();
		if(op!=2) return ;
		if(op==2) use_props(you);
	}
}

inline int you_attack(creature &x,creature &y){
	int tem=rand()%100;
	if(tem<y.a[7]){
		cout<<(y.a[0]? "You":"He")<<" ";
		puts("evaded the attack!");
		return 0;
	}
	int ret=0;
	if(x.a[2]) ret+=max(5,x.a[2]-y.a[4]);
	if(x.a[3]) ret+=max(2,x.a[3]*(100-y.a[5])/100);
	y.a[6]=max(y.a[8],y.a[6]-ret);
	return ret;	
}

inline void get_money(int x,int y){
	int tem=rand()%(y-x+1)+x;
	you.a[9]+=tem;
	printf("You get %d yuan!\n",tem);
}

inline void get_exp(int x,int y){
	int tem=rand()%(y-x+1)+x;
	you.a[10]+=tem;
	printf("You get %d exp!\n",tem);
	check_exp();
}

inline void fight(creature x,creature y){
	int tem=0;
	while(1){		
		if(!tem){
			puts("1.attack 2.use props 3.run away");
			int op=read();
			if(op==1){
				int ret=you_attack(x,y);
				printf("You caused %d harm.\n",ret);
				if(!y.a[6]){
					puts("You won!");
					get_money(y.a[9],y.a[10]);get_exp(y.a[11],y.a[12]);
					break;
				}
			}
			else if(op==2) use_props(x);
			else if(op==3) return ;
		}
		else{
			int ret=you_attack(y,x);
			printf("He caused %d harm.\n",ret);
			printf("Your HP:%d\n",x.a[6]);
			if(!x.a[6]){puts("You are dead!");save();exit(0);}
		}
		tem^=1;
	}
}

inline void go_out(){
	puts("where to go?");
	puts("1.The World 2.shop 3.warehouse");
	int op=read();
	if(op==1) world();
	else if(op==2) shop();
	else if(op==3) warehouse();
}

inline void wait(int x){
	while(x--);
}

inline void put_words(){
	puts("welcome to the game!");
	wait(500000000);
	puts("you are in a another world.");
	wait(400000000);
	puts("Hastin is the god of the world");
	wait(400000000);
	puts("And he says:");
	wait(400000000);
	puts("The world have many layers.");
	wait(400000000);
	puts("Go to 100 layer ans you will be the only god in the world!");
	wait(400000000);
	puts("But first,please tell me your name");
	cin>>you.s[0];you.a[0]=1;

}

inline void down(){
	int o[101]={0,1,249,0,154,0,1229,1,0,0,0,5};
	you.set(o);
	save();
}

inline void first_init(){
	FILE *fp=fopen("down.in","rt");
	you.init();
	Hastin.init();
	S.init();
	if(fp==NULL){
		put_words();
		down();
		fp=fopen("down.in","rt");
	}
	else{
		int o[101];
		char ss[N];		
		fscanf(fp,"%s",ss);
		you.s[0]=ss;
		for(int i=0;i<=11;i++) fscanf(fp,"%d",&o[i]);
		for(int i=1;i<=S.n;i++) fscanf(fp,"%d",&W.a[i]);
		you.set(o);
	}
	int oo[101]={0,100,1999999999,1999999999,1999999999,100,1999999999,98};
	Hastin.set(oo);
	fclose(fp);
}

inline void save(){
	FILE *fp=fopen("down.in","wt");
	fprintf(fp,"%s\n",you.s[0].c_str());
	for(int i=0;i<=11;i++) fprintf(fp,"%d ",you.a[i]);
	fprintf(fp,"\n");
	for(int i=1;i<=S.n;i++) fprintf(fp,"%d ",W.a[i]);
	fclose(fp);
}

int main(){
	srand((int)time(0));
	puts("The World of Hastin.");
	puts("Made by zsq259");
	puts("Translation is provided by youdao translation");
	puts("Thanks for M_sea's help.");
	wait(400000000);
	puts("Load into the archive...");
	wait(500000000);	
	first_init();
	you.a[0]=1;
//	system("cls");
	while(1){
		puts("home");
		puts("1.check the panel 2.go out 3.save 4.exit 5.clear");
		int op=read();
		if(op==1) check_panel();
		else if(op==2) go_out();
		else if(op==3) save();
		else if(op==4){save();break;}
		else if(op==5){down();break;}
	}
	return 0;
}


posted @ 2019-12-17 21:30  permzf  阅读(206)  评论(0)    收藏  举报