跳到正文

休闲·娱乐

植物大战僵尸(c++游戏)

Coder_MartinQiu约 17 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月21日
篇幅
约 17 分钟

版本

  1. v1当前

这个似乎有 Bug !!!(僵尸血那么低吗?救我!)

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<stdio.h>
#include<conio.h>
using namespace std;

struct PLAYER{
	int money=150;
	int Garden_len=0;
	struct Garden{
		string name;
		char photo;
		int fight_string,healthy,sun;
	}garden[100];
}player;

struct FIGHT_PLAYER{
	int sun=100,scores=0;
}fight_player;

int plants_num=4;

struct PLANTS{
	string name;
	char photo;
	int fight_string,healthy,sun;
}plants[5];

int zombles_num=3;

struct ZOMBLES{
	string name;
	char photo;
	int fight_string,healthy;
}zombles[5];

int mall_num=4;

struct PLANTS_MALL{
	string name;
	char photo;
	int fight_string,healthy,maney;
}mall[5];

int n=10,m=51;

string mp[10]={"___________________________________________________",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
			   "'-------------------------------------------------'",};

int get_rand(int l,int r){
	return rand()%r+l;
}


void init(){
	plants[0]={"向日葵",'F',0,100,50};
	plants[1]={"豌豆射手",'P',10,100,100};
	plants[2]={"坚果",'O',0,500,50};
	plants[3]={"菜问",'G',25,100,125};
	zombles[0]={"普通僵尸",'Z',5,80};
	zombles[1]={"铁桶僵尸",'T',12,100};
	zombles[2]={"路障僵尸",'A',15,120};
	mall[0]={"向日葵",'F',0,100,50};
	mall[1]={"豌豆射手",'P',10,100,100};
	mall[2]={"坚果",'O',0,500,50};
	mall[3]={"菜问",'G',25,100,125};
	return;
}

void Beginner_Tutorial(){
	cout<<"是否进入新手教程?\n1.是; 0.否\n";
	bool TF;
	cin>>TF;
	if(TF){
		cout<<" F 表示向日葵\n";
		Sleep(500);
		cout<<" P 表示豌豆射手\n";
		Sleep(500);
		cout<<" O 表示坚果\n";
		Sleep(500);
		cout<<" G 表示菜问\n";
		Sleep(500);
		cout<<" Z 表示普通僵尸\n";
		Sleep(500);
		cout<<" T 表示铁桶僵尸\n";
		Sleep(500);
		cout<<" A 表示路障僵尸\n";
		Sleep(500);
		cout<<"按 c 键种植\n";
		Sleep(500);
		cout<<"按任意键退出\n";
		char Cc;
		Cc=getch();
	}
	system("cls");
	return;
}

void welcome(){
	cout<<"  § Plants VS. Zombles §\n press any key to continue...\n\n\n"<<endl;
	srand(time(0)),init();
	cout<<" made by @player_qiumingxuan"<<endl;
	char Cc;
	Cc=getch();
	system("cls");
	Beginner_Tutorial();
	Sleep(800);
	system("cls");
	return;
}

void Mall(){
	cout<<"商品:\n";
	for(int i=0;i<mall_num;i++){
		cout<<i<<" : "<<mall[i].name<<"(金币:"<<mall[i].maney<<" 枚);\n";
	}
	int xz;
	cin>>xz;
	if(xz<0 || xz>=mall_num){
		cout<<"欢迎下次光临!\n";
		return;
	}
	if(player.money<mall[xz].maney){
		cout<<"钱不够!!!\n";
		return;
	}
	for(int i=1;i<=player.Garden_len;i++){
		if(player.garden[i].name==mall[xz].name){
			cout<<"你有了!!!\n";
			return;
		}
	}
	cout<<"购买了"<<mall[xz].name<<"!\n";
	player.money-=mall[xz].maney;
	player.garden[++player.Garden_len]={mall[xz].name,mall[xz].photo,mall[xz].fight_string,mall[xz].healthy,mall[xz].maney};
	return;
}

int fpl_num=0;

struct FIGHT_PLANTS{
	string name;
	char photo;
	int x,y,fight_string,healthy;
}fpl[110];

int fzo_num=0;

struct FIGHT_ZOMBLES{
	string name;
	char photo;
	int x,y,fight_string,healthy;
}fzo[110];

int idx_fzo(int x,int y,char op){
	for(int i=1;i<=fzo_num;i++){
		if(fzo[i].x==x && fzo[i].y==y && fzo[i].photo==op)  return i;
	}
	return 0;
}

int idx_fpl(int x,int y,char op){
	for(int i=1;i<=fpl_num;i++){
		if(fpl[i].x==x && fpl[i].y==y && fpl[i].photo==op)  return i;
	}
	return 0;
}

int Tm=0,win=0,Sun=0;

void PLants_fight(){

	return;
}
void ZOmbles_fight(){

	return;
}

bool check_win;

void ZOmbles_go(){

	return;
}

int sun_num;
int Tm_sun[110];

void fight(){
	memset(Tm_sun,0,sizeof Tm_sun);
	fpl_num=0,fzo_num=0,Tm=0,check_win=0,win=0,Sun=50,sun_num=0;
	string mp[10]={"___________________________________________________",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "| | | | | | | | | | | | | | | | | | | | | | | | | |",
				   "'-------------------------------------------------'",};
	while(true){
		Tm++,sun_num=0;
		if(win==20){
			cout<<"恭喜通关!!!\n";
			player.money+=200;
			return;
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mp[i][j]=='F'){
					sun_num++;
					Tm_sun[sun_num]++;
				}
			}
		}
		for(int i=1;i<=sun_num;i++){
			if(Tm_sun[i]%5==0) Sun+=50;
		}
		cout<<"阳光:"<<Sun<<"\n";
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cout<<mp[i][j];
			}
			cout<<"\n";
		}
		cout<<"按 c 键种植,按 r 跳过;\n";
		char a;
		cin>>a;
		if(a=='c'){
	        cout<<"你要种植什么?\n";
	        for(int i=1;i<=player.Garden_len;i++)	cout<<i<<" : "<<player.garden[i].name<<"\n";
			int zz;
			cin>>zz;
			if(zz<1 || zz>player.Garden_len){
				cout<<"种植失败!\n";
				system("cls");
				continue;

			}
			cout<<"种植在哪?(坐标[x,y])\n";
			int x,y;
			cin>>x>>y;
			int nx=x,ny=y;
			nx++;
			ny=ny*2+1;
			if(nx<0 || nx>=n-1 || ny<0 || ny>=m-1){
				cout<<"种植失败!\n";
				system("cls");
				continue;
			}
			if(Sun<player.garden[zz].sun){
				cout<<"种植失败!\n";
				system("cls");
				continue;
			}
			Sun-=player.garden[zz].sun;
			mp[nx][ny]=player.garden[zz].photo;
			fpl[++fpl_num]={player.garden[zz].name,player.garden[zz].photo,nx,ny,player.garden[zz].fight_string,player.garden[zz].healthy};
			cout<<"种植了"<<player.garden[zz].name<<"!\n";
			Sleep(500);
			system("cls");
		}
		if(Tm%5==0){
			int zx=get_rand(1,n-2);
			while(mp[zx][m-2]!=' ' && zx>=2) zx--;
			int zzl=get_rand(0,2);
			mp[zx][m-2]=zombles[zzl].photo;
			fzo[++fzo_num]={zombles[zzl].name,zombles[zzl].photo,zombles[zzl].fight_string,zombles[zzl].healthy};
		}
		PLants_fight();
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mp[i][j]=='|')   continue;
				if(mp[i][j]=='P'){
					for(int k=j+1;k<m;k++){
						if((mp[i][k]=='Z' || mp[i][k]=='T' || mp[i][k]=='A') && mp[i][k]!='|'){
							int zt=idx_fzo(i,k,mp[i][k]);
							int pt=idx_fpl(i,j,'P');
							fzo[zt].healthy-=fpl[pt].fight_string;
							cout<<"[ "<<i<<" , "<<k<<" ]的僵尸遭到 "<<fpl[pt].fight_string<<" 点攻击!\n";
							Sleep(800);
							if(fzo[zt].healthy<=0)	mp[i][k]=' ',win++;
							break;
						}
					}
				}
				if(mp[i][j]=='G'){
					for(int k=j+1;k<=j+2;k++){
						if((mp[i][k]=='Z' || mp[i][k]=='T' || mp[i][k]=='A') && mp[i][k]!='|'){
							int zt=idx_fzo(i,k,mp[i][k]);
							int pt=idx_fpl(i,j,'G');
							cout<<"[ "<<i<<" , "<<k<<" ]的僵尸遭到 "<<fpl[pt].fight_string<<" 点攻击!\n";
							Sleep(800);
							fzo[zt].healthy-=fpl[pt].fight_string;
							if(fzo[zt].healthy<=0)	mp[i][k]=' ',win++;
							break;
						}
					}
				}
			}
		}
		ZOmbles_fight();
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mp[i][j]=='|')   continue;
				if(mp[i][j]=='Z'){
					int k=j-2;
					if(mp[i][k]!='Z' && mp[i][k]!='T' && mp[i][k]!='A' && mp[i][k]!=' ' && mp[i][k]!='|'){
						int zt=idx_fzo(i,k,'Z');
						int pt=idx_fpl(i,j,mp[i][k]);
						fpl[pt].healthy-=fzo[zt].fight_string;
						if(fpl[pt].healthy<=0)	mp[i][k]=' ';
					}
				}
				if(mp[i][j]=='T'){
					int k=j-2;
					if(mp[i][k]!='Z' && mp[i][k]!='T' && mp[i][k]!='A' && mp[i][k]!=' ' && mp[i][k]!='|'){
						int zt=idx_fzo(i,k,'T');
						int pt=idx_fpl(i,j,mp[i][k]);
						fpl[pt].healthy-=fzo[zt].fight_string;
						if(fpl[pt].healthy<=0)	mp[i][k]=' ';
					}
				}
				if(mp[i][j]=='A'){
					int k=j-2;
					if(mp[i][k]!='Z' && mp[i][k]!='T' && mp[i][k]!='A' && mp[i][k]!=' ' && mp[i][k]!='|'){
						int zt=idx_fzo(i,k,'A');
						int pt=idx_fpl(i,j,mp[i][k]);
						fpl[pt].healthy-=fzo[zt].fight_string;
						if(fpl[pt].healthy<=0)	mp[i][k]=' ';
					}
				}
			}
		}
		ZOmbles_go();
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mp[i][j]=='|')   continue;
				if(mp[i][j]=='Z' || mp[i][j]=='T' || mp[i][j]=='A'){
					int k=j-2;
					int zt=idx_fzo(i,j,mp[i][j]);
					if(k<0){
						check_win=1;
						cout<<"僵尸吃掉了你的脑子!!!\n";
						return;
					}
					if(mp[i][k]==' '){
						mp[i][k]=mp[i][j],mp[i][j]=' ';
						fzo[zt].y=k;
					}
				}
			}
		}
		system("cls");
	}
	return;
}

void interfaces(){
	cout<<"欢迎来到戴夫的花园。\n";
	cout<<"你有 "<<player.money<<" 枚金币。\n";
	cout<<"你现在要做:\n";
	cout<<"    0. 去商城;\n";
	cout<<"    1. 保卫花园;\n";
	bool there;
	cin>>there;
	if(!there)  Mall();
	else    fight();
	return;
}

int main(){
	welcome();
	while(true){
		interfaces();
		Sleep(800);
		system("cls");
	}
	return 0;
}
原文