二維坦克大戰遊戲代碼開發

這是我實際面試中,遇到的一個題目,編寫一個坦克大戰遊戲。一開始感覺懵,後來代碼寫寫就好了。

#include<iostream>
#include<stdlib.h>
#include<graphics.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#pragma comment(lib,"winmm.lib") 
using namespace std;
int xx[50][50];
void refresh()
{
	int a,b;
	for(a=0;a<=49;a++)
		for(b=0;b<=49;b++)
			xx[a][b]=0;
}
void graph()
{
	int a,b;
	for(a=0;a<=49;a++)
		for(b=0;b<=49;b++)
		{
			if(xx[a][b]!=0)
			{
				setfillcolor(WHITE);
				setlinecolor(WHITE);
				fillrectangle(a*10,b*10,a*10+8,b*10+8);
			}
			if(xx[a][b]==0)
			{
				setfillcolor(BLACK);
				setlinecolor(BLACK);
				fillrectangle(a*10,b*10,a*10+8,b*10+8);
			}
		}
}
int kb(void)
{
	char a[2],shang[3],xia[3],zuo[3],you[3];
	while(1)
	{
		strcpy(shang,"郒");
		strcpy(xia,"郟");
		strcpy(zuo,"郖");
		strcpy(you,"郙");
		a[0]=getch();
		if(a[0]==shang[0]||a[0]==zuo[0]||a[0]==xia[0]||a[0]==you[0])
		{
			a[1]=getch();
			if(a[1]==shang[1])
				return 1;
			if(a[1]==zuo[1])
				return 3;
			if(a[1]==xia[1])
				return 2;
			if(a[1]==you[1])
				return 4;
		}
		if(a[0]==13)
			return 5;
	}
}
class tank;
class bullet;
class tank
{
private:
	int x,y,v,direction,t,body[4][9];
public:
	tank(int T);
	void move(bullet *&p,tank *b[],int tt);
	void fire(bullet *&p);
	void die(tank *&p);
	void tank1();
	void viewin();
	void viewcl();
	void me(int x,int y,tank *&p);
	void smart(tank *p,bullet *b,bullet *&p1,tank *p2[]);
	int gett();
	int getx();
	int gety();
};
class bullet
{
private:
	int x,y,v,direction,t;
public:
	bullet(int X,int Y,int T,int Direction,int V);
	void move(bullet *&p,tank *p1[]);
	void die(bullet *&p,tank *p1[]);
	void viewin();
	void viewcl();
	int getx();
	int gety();
};
int bullet::getx()
{
	return x;
}
int bullet::gety()
{
	return y;
}
void tank::fire(bullet *&p)
{
	if(p==NULL)
	{
		PlaySound ("子彈發射聲.wav",NULL,SND_FILENAME | SND_ASYNC);
		p=new bullet(x,y,t+3,direction,1);
	}
}
int tank::gett()
{
	return t;
}
void bullet::viewin()
{
	if(xx[x][y]==0)
		xx[x][y]=t;
}
void bullet::viewcl()
{
	if(!(xx[x][y]!=0&&xx[x][y]!=t))
		xx[x][y]=0;
}
bullet::bullet(int X,int Y,int T,int Direction,int V)
{
	t=T;
	x=X;
	y=Y;
	direction=Direction;
	v=V;
	viewin();
}
void bullet::move(bullet *&p,tank *p1[])
{
	switch(direction)
	{
	case 1:viewcl();y-=v;break;
	case 2:viewcl();y+=v;break;
	case 3:viewcl();x-=v;break;
	case 4:viewcl();x+=v;break;
	}
	die(p,p1);
}
void bullet::die(bullet *&p,tank *p1[])
{
	int a;
	if(x<=0||y<=0||x>=49||y>=49)
	{
		bullet *q=p;
		p=NULL;
		viewcl();
		delete q;
		return ;
	}
	int key=0,tx,ty,tt;
	for(a=0;a<2;a++)
	{
		if(t==4)
			if(a==0)
				continue;
			if(t!=4)
				if(a!=0)
					continue;
				if(p1[a]!=NULL)
				{
					tx=p1[a]->getx();
					ty=p1[a]->gety();
					tt=p1[a]->gett();
					if(abs(tx-x)<=1&&abs(ty-y)<=1)
					{	
						key=1;
						break;
					}
				}
	}
	if(key==1)
	{
		p1[a]->die(p1[a]);
		bullet *q=p;
		p=NULL;
		delete q;
		return ;
	}
	viewin();
}
void tank::tank1()
{
	if(t==0)
		direction=2;
	if(t==1)
		direction=1;
	x=1;
	y=48;
	v=1;
}
int tank::getx()
{
	return this->x;
}
int tank::gety()
{
	return this->y;
}
void tank::me(int x,int y,tank *&p)
{
	if(x-(this->x)<=1&&x-(this->x)>=-1)
		if(y-(this->y)<=1&&y-(this->y)>=-1)
			die(p);
}
void tank::die(tank *&p)
{
	p->viewcl();
	tank *q=p;
	p=NULL;
	delete q;
}
tank::tank(int T)
{
	t=T;
	int a;
	if(t==0)
	{
		direction=2;
		x=5;
		y=1;
	}
	if(t==2)
	{
		t=0;
		direction=2;
		x=43;
		y=1;
	}
	if(t==1)
	{
		direction=1;
		x=1;
		y=48;
	}
	v=1;
	for(a=0;a<=8;a++)
	{
		body[0][a]=t+1;
		body[1][a]=t+1;
		body[2][a]=t+1;
		body[3][a]=t+1;
		if(a==0||a==2||a==7)
		{
			body[0][a]=0;
		}
		if(a==1||a==6||a==8)
		{
			body[1][a]=0;
		}
		if(a==0||a==5||a==6)
		{
			body[2][a]=0;
		}
		if(a==2||a==3||a==8)
		{
			body[3][a]=0;
		}
	}
	viewin();
}
void tank::smart(tank *p,bullet *b,bullet *&p1,tank *p2[])
{
	static int kk;
	int dx,dy,bx,by;
	if(p==NULL)
		return ;
	dx=p->getx()-x;
	dy=p->gety()-y;
	if(b==NULL)
	{
		bx=100;
		by=100;
	}
	if(b!=NULL)
	{
		bx=b->getx()-x;
		by=b->gety()-y;
	}
	if(abs(bx)>1&&abs(by)>1)
	{
		if(b==NULL)
		{
			if(dx<-1&&dy<-1&&abs(dy)<=abs(dx))
			{
				move(p1,p2,1);
				return ;
			}
			if(dx<-1&&dy<-1&&abs(dy)>abs(dx))
			{
				move(p1,p2,3);return ;
			}
			if(dx<-1&&dy>1&&abs(dy)<=abs(dx))
			{
				move(p1,p2,2);return ;
			}
			if(dx<-1&&dy>1&&abs(dy)>abs(dx))
			{
				move(p1,p2,3);return ;
			}
			if(dx>1&&dy<-1&&abs(dy)<=abs(dx))
			{
				move(p1,p2,1);return ;
			}
			if(dx>1&&dy<-1&&abs(dy)>abs(dx))
			{
				move(p1,p2,4);return ;
			}
			if(dx>1&&dy>1&&abs(dy)<=abs(dx))
			{
				move(p1,p2,2);return ;
			}
			if(dx>1&&dy>1&&abs(dy)>abs(dx))
			{
				move(p1,p2,4);return ;
			}
		}
		if(abs(dx)<=5&&abs(dy)<=5)
		{
			kk=1+rand()%4;
			move(p1,p2,kk);return ;
		}
		if(abs(dx)<=1)
		{
			if(dy>0)
			{
				if(direction==2)
				{
					move(p1,p2,5);return ;
				}
				else
				{
					move(p1,p2,2);return ;
				}
			}
			if(dy<0)
			{
				if(direction==1)
				{
					move(p1,p2,5);return ;
				}
				else
				{
					move(p1,p2,1);return ;
				}
			}
		}
		if(abs(dy)<=1)
		{
			if(dx<0)
			{
				if(direction==3)
				{
					move(p1,p2,5);return ;
				}
				else
				{
					move(p1,p2,3);return ;
				}
			}
			if(dx>0)
			{
				if(direction==4)
				{
					move(p1,p2,5);return ;
				}
				else
				{
					move(p1,p2,4);return ;
				}
			}
		}
	}
	else
	{
		if(abs(bx)<=1)
		{
			if(dx>0)
			{
				move(p1,p2,3);return ;
			}
			if(dx<0)
			{
				move(p1,p2,4);return ;
			}
			if(dx==0)
			{
				move(p1,p2,3);return ;
			}
		}
		if(abs(by)<=1)
		{
			if(dy>0)
			{
				move(p1,p2,1);return ;
			}
			if(dy<0)
			{
				move(p1,p2,2);return ;
			}
			if(dy==0)
			{
				move(p1,p2,1);return ;
			}
		}
	}
}
void tank::move(bullet *&p,tank *b[],int tt=0)
{
	int k,a,key=0;
	if(tt==0)
		if(kbhit())
		{
			k=kb();
			switch(k)
			{
			case 1:viewcl();y-=v;direction=k;break;
			case 2:viewcl();y+=v;direction=k;break;
			case 3:viewcl();x-=v;direction=k;break;
			case 4:viewcl();x+=v;direction=k;break;
			case 5:fire(p);break;
			}
			for(a=0;a<=1;a++)
			{
				if(abs(b[a]->getx()-x)<=2&&abs(abs(b[a]->gety()-y)<=2)&&abs(b[a]->getx()-x)!=0&&abs(abs(b[a]->gety()-y)!=0))
				{
					key=1;
				}
			}
			if(key||x<1||x>48||y<1||y>48)
				switch(k)
			{
			case 1:y+=v;direction=k;break;
			case 2:y-=v;direction=k;break;
			case 3:x+=v;direction=k;break;
			case 4:x-=v;direction=k;break;
			case 5:fire(p);break;
			}
			viewin();
		}
		if(tt!=0)
		{
			k=tt;
			switch(k)
			{
			case 1:viewcl();y-=v;direction=k;break;
			case 2:viewcl();y+=v;direction=k;break;
			case 3:viewcl();x-=v;direction=k;break;
			case 4:viewcl();x+=v;direction=k;break;
			case 5:fire(p);break;
			}
			for(a=0;a<=1;a++)
			{
				if(abs(b[a]->getx()-x)<=2&&abs(abs(b[a]->gety()-y)<=2)&&abs(b[a]->getx()-x)!=0&&abs(abs(b[a]->gety()-y)!=0))
				{
					key=1;
				}
			}
			if(key||x<1||x>48||y<1||y>48)
				switch(k)
			{
			case 1:y+=v;direction=k;break;
			case 2:y-=v;direction=k;break;
			case 3:x+=v;direction=k;break;
			case 4:x-=v;direction=k;break;
			case 5:fire(p);break;
			}
			viewin();
		}
}
void tank::viewin()
{
	int a,b,c=0;
	for(b=y-1;b<=y+1;b++)
	{
		for(a=x-1;a<=x+1;a++)
		{
			xx[a][b]=body[direction-1][c];
			c++;
		}
	}
}
void tank::viewcl()
{
	int a,b,c=0;
	for(a=x-1;a<=x+1;a++)
	{
		for(b=y-1;b<=y+1;b++)
		{
			xx[a][b]=0;
		}
	}
}
int main()
{
	int order;
	cout<<"讓我去還是你自己去\n1:玩家\n2:電腦"<<endl;
	cin>>order;
	initgraph(500,500);
	refresh();
	tank *t[3];
	t[0]=new tank(1);
	t[1]=new tank(0);
	//t[2]=new tank(2);
	bullet *bu[3];
	int a;
	for(a=0;a<=2;a++)
		bu[a]=NULL;
	graph();
	while(1)
	{
		for(a=0;a<=2;a++)
		{
			if(bu[a]!=NULL)
			{
				bu[a]->move(bu[a],t);
			}
		}
		for(a=1;a<=1;a++)
		{
			if(t[a]!=NULL)
				t[a]->smart(t[0],bu[0],bu[a],t);
		}
		if(order==1)
		{
			if(t[0]!=NULL)
				t[0]->move(bu[0],t);
		}
		if(order!=1)
		{
			if(t[0]!=NULL)
				t[0]->smart(t[1],bu[1],bu[0],t);
		}
		if(t[0]==NULL)
		{
			closegraph();
			system("cls");
			cout<<"你輸了,我不是說你渣,而是在座的各位都是渣渣"<<endl;
			Sleep(5000);
			break;
		}
		if(t[1]==NULL)
		{
			closegraph();
			system("cls");
			cout<<"你居然贏了"<<endl;
			Sleep(5000);
			break;
		}
		graph();
		/*for(int k=0;k<=49;k++)
		{
		for(int l=0;l<=49;l++)
		{
		cout<<xx[l][k];
		}
		cout<<endl;
	}*/
		Sleep(10);
		//system("cls");
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章