VS2012坦克大战的部分源码

偷笑
才发现这表情都是QQ的?能不能有点创意?
#include "stdafx.h"    
#include <string.h>      
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include "MyAStar.h"

#pragma   comment(lib, "Winmm.lib ")
#define KEYDOWN(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?1:0)
int QWE = 0;

int Map[30][60]={
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
	{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
	{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
	{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,2,2,1,1,1,2,2,0},
	{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,2,2,1,1,1,2,2,0},
	{0,2,2,2,2,2,2,2,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2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sBullet;
}mytank ={0,2,2,1,0},jtank={3,2,27,0,0},Qtank={2,47,25,0,0},Ktank={3,23,11,0,0},
	OldmyTank = mytank,oldjtank= jtank,oldqtank= Qtank,oldktank=Ktank,
	shoot[300]={0},shoot1[300]={0},shoot2[300]={0},shoot3[300]={0},
	Bullet = {0};

//************************************
// 方法:    DITU  设置游戏边框
// 函数名:  DITU
// 继承关系:public 
// 返回值:  void
//************************************
void DITU(){

	void WriteChar(int Wide, int High, char*m_pszChar, int m_wArr);
	for (row=0;row<30;row++)
	{
		for (col=0;col<60;col++)
		{
			if (Map[row][col]==0) //游戏边框
				WriteChar(col,row,"¤",F_RED);
			else if (Map[row][col]==2) //小土堆儿
				WriteChar(col,row,"§",F_H_BLUE);
			else if (Map[row][col]==3)
				WriteChar(col,row,"▲",F_H_CYAN); //钢筋混凝土
			else 
				WriteChar(col,row,"  ",0x0);
		}
	}
}

//************************************
// 方法:    WriteChar    
// 函数名:  WriteChar
// 继承关系:public 
// 返回值:  void
// 参数:    int Wide
// 参数:    int High
// 参数:    char * m_pszChar
//************************************
void WriteChar(int col, int row, char*m_pszChar, int m_wArr)
{
	CONSOLE_CURSOR_INFO m_cci;
	m_cci.dwSize   =2;
	m_cci.bVisible = FALSE;											    // 将该标志设为false 表示不显示光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &m_cci);      // 设置某个控制台屏幕缓冲区的光标属性,如大小,是否可见
	COORD loc ={0};
	loc.X = col * 2;												    // Y坐标的缓冲区是X的2倍,在控制台属性里能体现出来
	loc.Y = row;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), m_wArr); // 设置某个控制台屏幕缓冲区中的字符的颜色和背景色
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);     // 设置光标位置
	printf("%s", m_pszChar);
}

class MyClass
{
public:
	void makejtank();
	void makeQtank();
	void makeKtank();
	void drawshoot(tankInfo Bullet);
	void DrawTank(tankInfo mytank);
	void DrawjTank(tankInfo jtank);   //画出 一般坦克
	void DrawQTank(tankInfo Qtank);   //画出 自动寻路坦克
	void DrawKTank(tankInfo Ktank);   //画出 自动寻路坦克
	void ClsTank(tankInfo mytank);
	void MS(tankInfo pldBullet,tankInfo* pNewBullet,char * bb);    //子弹碰撞 与移动                     
	void moveShoot (tankInfo* pBullet);
	bool IsMoveTank(tankInfo mytank);

};

//************************************
// 方法:    DrawTank  画坦克
// 函数名:  DrawTank
// 继承关系:public 
// 返回值:  void
// 参数:    tankInfo mytank  
//************************************
void MyClass::DrawTank(tankInfo mytank)
{  
	for (int Y= mytank.nPosY-1;Y<mytank.nPosY+2;Y++)
	{
		for (int X = mytank.nPosX-1;X<mytank.nPosX+2;X++)
		{
			if (g_Tank[mytank.a][Y-mytank.nPosY+1][X-mytank.nPosX+1])
			{
				Map[Y][X] = 0x10;
				WriteChar(X,Y,"●",F_H_PURPLE);
			}	
		}
	}

}
void MyClass::DrawjTank(tankInfo jtank)
{  
	for (int Y= jtank.nPosY-1;Y<jtank.nPosY+2;Y++)
	{
		for (int X = jtank.nPosX-1;X<jtank.nPosX+2;X++)
		{
			if (g_Tank[jtank.a][Y-jtank.nPosY+1][X-jtank.nPosX+1])
			{
				Map[Y][X] = 0x18;
				WriteChar(X,Y,"□",F_YELLOW);
			}	
		}
	}
}
void MyClass::makejtank()  {
	ClsTank(jtank);
	int i=jtank.a;
	switch (i)
{
case 0:
	if (oldjtank.a==0)
	{
		jtank.nPosY--;
	}else jtank.a=0;break;
case 1:
	if (oldjtank.a==1)
	{
		jtank.nPosY++;
	}else jtank.a=1;break;
case 2:
	if (oldjtank.a==2)
	{
		jtank.nPosX--;
	}else jtank.a=2;break;
case 3:
	if (oldjtank.a==3)
	{
		jtank.nPosX++;
	}else jtank.a=3;
}
	if (IsMoveTank(jtank))
	{
		ClsTank(oldjtank);
		DrawjTank(jtank);
		oldjtank = jtank;
		
	}else{
		i = rand()%4;
		jtank = oldjtank;
		jtank.a = i;
	}

	int t=rand()%3;
	if (t==0)
	{
		for (int i=0;i<300;i++){
		if (!shoot1[i].g_IsBullet)     //判断数组当前位置是否为空
			{
			tankInfo tempBullet = jtank;		  //把当前中心点坐标给中间值
			switch (tempBullet.a)
			{
			case 0:
				tempBullet.nPosY-=1;break;
			case 1:
				tempBullet.nPosY+=1;break;
			case 2:				
				tempBullet.nPosX-=1;break;
			case 3:				
				tempBullet.nPosX+=1;break;
			}
			shoot1[i]=tempBullet;		 //如果为空 装填子弹		
			shoot1[i].g_IsBullet = true;
			mciSendString(L"close 2.wav",0,0,0);
			drawshoot(tempBullet);       //画出第一个子弹
			mciSendString(L"play 2.wav",0,0,0);
			//PlaySound(TEXT("1"),NULL,SND_FILENAME | SND_ASYNC);
			break;
			}	
		}
	}
}
void MyClass::DrawQTank(tankInfo Qtank)
{
	for (int Y= Qtank.nPosY-1;Y<Qtank.nPosY+2;Y++)
	{
		for (int X = Qtank.nPosX-1;X<Qtank.nPosX+2;X++)
		{
			if (g_Tank[Qtank.a][Y-Qtank.nPosY+1][X-Qtank.nPosX+1])
			{
				Map[Y][X] = 0x20;
				WriteChar(X,Y,"○",F_GREEN);
			}	
		}
	}
}
void MyClass::makeQtank()
{

	CMyAStar Obj;
	Obj.SetMap(60,30);
	POSITION Begin = {Qtank.nPosX,Qtank.nPosY};
	POSITION End   = {mytank.nPosX,mytank.nPosY};
	Obj.SetInfo(Begin,End);
	Obj.InitMap(Map);
	Obj.GetPath();
	if (Obj.m_vecPath.size()>1)
	{
		switch (Qtank.nPosX-Obj.m_vecPath[1].pos_x)
		{
		case 1:	//向左走
			if(oldqtank.a==2)
				Qtank.nPosX--;
			else
				Qtank.a=2;break;
			break;
		case -1: //向右走
			if (oldqtank.a==3)
				Qtank.nPosX++;
			else
				Qtank.a=3;
			break;
		}
		switch (Qtank.nPosY-Obj.m_vecPath[1].pos_y)
		{
		case 1://向上走
			if (oldqtank.a==0)
				Qtank.nPosY--;
			else
				Qtank.a=0;
			break;
		case -1://向下走
			if (oldqtank.a==1)
				Qtank.nPosY++;
			else
				Qtank.a=1;
			break;
		}
	}
	if (IsMoveTank(Qtank))
	{
		ClsTank(oldqtank);
		DrawQTank(Qtank);
		oldqtank = Qtank;
		Obj.m_vecPath.clear();		
	}
	int t=rand()%5;
	if (t==1)
	{
		for (int i=0;i<300;i++){
			if (!shoot2[i].g_IsBullet)     //判断数组当前位置是否为空
			{
				tankInfo tempBullet = Qtank;		  //把当前中心点坐标给中间值
				switch (tempBullet.a)
				{
				case 0:
					tempBullet.nPosY-=1;break;
				case 1:
					tempBullet.nPosY+=1;break;
				case 2:				
					tempBullet.nPosX-=1;break;
				case 3:				
					tempBullet.nPosX+=1;break;
				}
				shoot2[i]=tempBullet;		 //如果为空 装填子弹		
				shoot2[i].g_IsBullet = true;
				mciSendString(L"close 2.wav",0,0,0);
				drawshoot(tempBullet);       //画出第一个子弹
				mciSendString(L"play 2.wav",0,0,0);
				
				//PlaySound(TEXT("1"),NULL,SND_FILENAME | SND_ASYNC);
				break;
			}	
		}
	}
}
void MyClass::DrawKTank(tankInfo Qtank)
{
	for (int Y= Qtank.nPosY-1;Y<Qtank.nPosY+2;Y++)
	{
		for (int X = Qtank.nPosX-1;X<Qtank.nPosX+2;X++)
		{
			if (g_Tank[Qtank.a][Y-Qtank.nPosY+1][X-Qtank.nPosX+1])
			{
				Map[Y][X] = 0x22;
				WriteChar(X,Y,"※",F_GREEN);
			}	
		}
	}
}
void MyClass::makeKtank()
{

	CMyAStar Obj;
	Obj.SetMap(60,30);
	POSITION Begin = {Ktank.nPosX,Ktank.nPosY};
	POSITION End   = {mytank.nPosX,mytank.nPosY};
	Obj.SetInfo(Begin,End);
	Obj.InitMap(Map);
	Obj.GetPath();
	if (Obj.m_vecPath.size()>1)
	{
		switch (Ktank.nPosX-Obj.m_vecPath[1].pos_x)
		{
		case 1:	//向左走
			if(oldktank.a==2)
				Ktank.nPosX--;
			else
				Ktank.a=2;break;
			break;
		case -1: //向右走
			if (oldktank.a==3)
				Ktank.nPosX++;
			else
				Ktank.a=3;
			break;
		}
		switch (Ktank.nPosY-Obj.m_vecPath[1].pos_y)
		{
		case 1://向上走
			if (oldktank.a==0)
				Ktank.nPosY--;
			else
				Ktank.a=0;
			break;
		case -1://向下走
			if (oldktank.a==1)
				Ktank.nPosY++;
			else
				Ktank.a=1;
			break;
		}
	}
	if (IsMoveTank(Ktank))
	{
		ClsTank(oldktank);
		DrawQTank(Ktank);
		oldktank = Ktank;
		Obj.m_vecPath.clear();	
	}
	int t=rand()%6;
	if (t==1)
	{
		for (int i=0;i<300;i++){
			if (!shoot3[i].g_IsBullet)     //判断数组当前位置是否为空
			{
				tankInfo tempBullet = Ktank;		  //把当前中心点坐标给中间值
				switch (tempBullet.a)
				{
				case 0:
					tempBullet.nPosY-=1;break;
				case 1:
					tempBullet.nPosY+=1;break;
				case 2:				
					tempBullet.nPosX-=1;break;
				case 3:				
					tempBullet.nPosX+=1;break;
				}
				shoot3[i]=tempBullet;		 //如果为空 装填子弹		
				shoot3[i].g_IsBullet = true;
				mciSendString(L"close 2.wav",0,0,0);
				drawshoot(tempBullet);       //画出第一个子弹
				mciSendString(L"play 2.wav",0,0,0);
				//PlaySound(TEXT("1"),NULL,SND_FILENAME | SND_ASYNC);
				break;
			}	
		}
	}
}

//************************************
// 方法:    ClsTank   擦除老坦克
// 函数名:  ClsTank
// 继承关系:public 
// 返回值:  void
// 参数:    tankInfo mytank
//************************************
void MyClass::ClsTank(tankInfo mytank)
{
	for (int Y= mytank.nPosY-1;Y<mytank.nPosY+2;Y++)
	{
		for (int X = mytank.nPosX-1;X<mytank.nPosX+2;X++)
		{
			if (g_Tank[mytank.a][Y-mytank.nPosY+1][X-mytank.nPosX+1])
			{
				Map[Y][X] = 1;
				WriteChar(X,Y,"  ",F_H_WHITE);
			}
		}
	}
}//************************************

// 方法:    MS   子弹移动 判断碰撞
// 函数名:  MS
// 继承关系:public 
// 返回值:  void 
// 参数:    tankInfo * pBullet ,tankInfo Bullet ,char * bb指向数组的指针
//************************************
void MyClass::MS(tankInfo Bullet,tankInfo* pBullet,char * bb)
{
	if (Map[pBullet->nPosY][pBullet->nPosX]==1)
	{
		WriteChar(Bullet.nPosX,Bullet.nPosY,"  ",F_H_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 1;
		WriteChar(pBullet->nPosX,pBullet->nPosY,bb,F_WHITE);
		Map[pBullet->nPosY][pBullet->nPosX] = 11;
	}
	else if (Map[pBullet->nPosY][pBullet->nPosX]==2)//  土墙
	{
		WriteChar(Bullet.nPosX,Bullet.nPosY,"  ",F_H_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 1;
		WriteChar(pBullet->nPosX,pBullet->nPosY,"  ",F_H_WHITE);
		Map[pBullet->nPosY][pBullet->nPosX]=1;
		memset(pBullet,0,sizeof(tankInfo));
	}

	else if (Map[pBullet->nPosY][pBullet->nPosX]==3||Map[pBullet->nPosY][pBullet->nPosX]==0||Map[pBullet->nPosY][pBullet->nPosX]==11)  //  遇到边界  钢筋混凝土 
	{
		WriteChar(Bullet.nPosX,Bullet.nPosY,"  ",F_H_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 1;
		memset(pBullet,0,sizeof(tankInfo));
	}      
	else if (Map[pBullet->nPosY][pBullet->nPosX]==0x10)  //自己坦克中弹 直接退出
	{
		
		WriteChar(Bullet.nPosX,Bullet.nPosY,"  ",F_H_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 1;
		memset(pBullet,0,sizeof(tankInfo));
		for (;;)
		{system("cls");printf("你输了");}
	}
	else if (Map[pBullet->nPosY][pBullet->nPosX]==0x18)   //敌方坦克中弹 擦去 重生
	{
		QWE++;
		WriteChar(Bullet.nPosX,Bullet.nPosY,"  ",F_H_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 1;
		memset(pBullet,0,sizeof(tankInfo));
		ClsTank(jtank);
		jtank.nPosX=2;
		jtank.nPosY=17;
		makejtank();
	}
	else if (Map[pBullet->nPosY][pBullet->nPosX]==0x20)   //牛逼坦克中弹 擦去 重生
	{
		QWE++;
		WriteChar(Bullet.nPosX,Bullet.nPosY,"  ",F_H_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 1;
		memset(pBullet,0,sizeof(tankInfo));
		ClsTank(Qtank);
		Qtank.nPosX=37;
		Qtank.nPosY=17;
		makeQtank();
	}
	else if (Map[pBullet->nPosY][pBullet->nPosX]==0x22)   //2号牛逼坦克中弹 擦去 重生
	{
		QWE++;
		WriteChar(Bullet.nPosX,Bullet.nPosY,"  ",F_H_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 1;
		memset(pBullet,0,sizeof(tankInfo));
		ClsTank(Ktank);
		Ktank.nPosX=23;
		Ktank.nPosY=11;
		makeKtank();
	}
}
void MyClass::moveShoot (tankInfo* pBullet)
{
	tankInfo Bullet =* pBullet;
	switch (pBullet->a)
	{
	case 0:
		pBullet->nPosY--;
		MS(Bullet ,pBullet,"↑");
		break;
	case 1:
		pBullet->nPosY++;
		MS(Bullet ,pBullet,"↓");		
		break;
	case 2:				 
		pBullet->nPosX--;
		MS(Bullet ,pBullet,"←");		
		break;
	case 3:				 
		pBullet->nPosX++;
		MS(Bullet ,pBullet,"→");	
		break;
	}
}

//************************************
// 方法:    drawshoot  画出第一个子弹
// 函数名:  drawshoot
// 继承关系:public 
// 返回值:  void
// 参数:    tankInfo Bullet
//************************************
void MyClass::drawshoot(tankInfo Bullet)
{
	if (Bullet.a==0)
	{
		WriteChar(Bullet.nPosX,Bullet.nPosY,"↑",F_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 11;
	}
	else if (Bullet.a==1)
	{
		WriteChar(Bullet.nPosX,Bullet.nPosY,"↓",F_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 11;
	}
	else if (Bullet.a==2)
	{
		WriteChar(Bullet.nPosX,Bullet.nPosY,"←",F_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 11;
	}
	else if (Bullet.a==3)
	{
		WriteChar(Bullet.nPosX,Bullet.nPosY,"→",F_WHITE);
		Map[Bullet.nPosY][Bullet.nPosX] = 11;
	}
}

//************************************
// 方法:    IsMoveTank  判断碰撞移动
// 函数名:  IsMoveTank
// 继承关系:public 
// 返回值:  bool
// 参数:    tankInfo mytank
//************************************
bool MyClass::IsMoveTank(tankInfo mytank)
{
	switch (mytank.a)
	{
	case 0:
		mytank.nPosY--;
		for (int X = mytank.nPosX-1;X<mytank.nPosX+2;X++)
		{
			if (Map[mytank.nPosY][X]==1)
			{
				continue;
			}
			else if (Map[mytank.nPosY][X]==2||Map[mytank.nPosY][X]==3||Map[mytank.nPosY][X]==0||Map[mytank.nPosY-1][X]==0x10||Map[mytank.nPosY-1][X]==0x18||Map[mytank.nPosY-1][X]==0x20||Map[mytank.nPosY-1][X]==0x22)
			{
				return false;
			}
		}
		break;
	case 1:
		mytank.nPosY++;
		for (int X = mytank.nPosX-1;X<mytank.nPosX+2;X++)
		{
			if (Map[mytank.nPosY][X]==1)
			{
				continue;
			}
			else if (Map[mytank.nPosY][X]==2||Map[mytank.nPosY][X]==3||Map[mytank.nPosY][X]==0||Map[mytank.nPosY+1][X]==0x10||Map[mytank.nPosY+1][X]==0x18||Map[mytank.nPosY+1][X]==0x20||Map[mytank.nPosY+1][X]==0x20)
			{
				return false;
			}
		}
		break;
	case 2:
		mytank.nPosX--;
		for (int Y = mytank.nPosY-1;Y<mytank.nPosY+2;Y++)
		{
			if (Map[Y][mytank.nPosX]==1)
			{
				continue;
			}
			else if (Map[Y][mytank.nPosX]==2||Map[Y][mytank.nPosX]==3||Map[Y][mytank.nPosX]==0||Map[Y][mytank.nPosX-1]==0x10||Map[Y][mytank.nPosX-1]==0x18||Map[Y][mytank.nPosX-1]==0x20||Map[Y][mytank.nPosX-1]==0x22)
			{
				return false;
			}
		}
		break;

	case 3:
		mytank.nPosX++;
		for (int Y = mytank.nPosY-1;Y<mytank.nPosY+2;Y++)
		{
			if (Map[Y][mytank.nPosX]==1)
			{
				continue;
			}
			else if (Map[Y][mytank.nPosX]==2||Map[Y][mytank.nPosX]==3||Map[Y][mytank.nPosX]==0||Map[Y][mytank.nPosX+1]==0x10||Map[Y][mytank.nPosX+1]==0x18||Map[Y][mytank.nPosX+1]==0x20||Map[Y][mytank.nPosX+1]==0x22)
			{
				return false;
			}
		}break;
	}
	return true;
}

//************************************
// 方法:    Gameing     键盘键入
// 函数名:  Gameing
// 继承关系:public 
// 返回值:  void
//************************************
void Gameing ()
{
	MyClass u;
	DITU();
	u.DrawTank(OldmyTank);

	int nTime = 4000;
	int nTimeShoot = 4000;
	srand( (unsigned)time( NULL ) );

	clock_t moveStart = clock();
	clock_t moveEnd ;
	clock_t moveEnemyStart = clock();
	clock_t moveEnemyEnd ;
	clock_t moveBulletStart = clock();
	clock_t moveBulletEnd ;
	while (true)
	{	
		//子弹移动
		moveBulletEnd = clock();
		if (moveBulletEnd-moveBulletStart>20)
		{
			moveBulletStart =moveBulletEnd;
			for (int i=0;i<200;i++)
			{
				if (shoot[i].g_IsBullet)
				{
					u.moveShoot(&shoot[i]);    //从坦克尖开始移动
					u.DrawTank(mytank);        //占了坦克尖端  得重新画一个坦克
				}
				if (shoot1[i].g_IsBullet)
				{
					u.moveShoot(&shoot1[i]);    //从坦克尖开始移动
					u.DrawjTank(jtank);        //占了坦克尖端  得重新画一个坦克
				}
				if (shoot2[i].g_IsBullet)
				{
					u.moveShoot(&shoot2[i]);    //从坦克尖开始移动
					u.DrawQTank(Qtank);        //占了坦克尖端  得重新画一个坦克
				}
				if (shoot3[i].g_IsBullet)
				{
					u.moveShoot(&shoot3[i]);    //从坦克尖开始移动
					u.DrawKTank(Ktank);        //占了坦克尖端  得重新画一个坦克
				}
			} 
		}
		
		//敌军移动
		moveEnemyEnd = clock();
		if (moveEnemyEnd-moveEnemyStart>300)
		{
			moveEnemyStart =moveEnemyEnd;
			u.makejtank();                       //   生成敌军坦克,并移动	
			u.makeKtank();
			u.makeQtank();                       //   生成ai敌军坦克,并移动	
		}

		//接收玩家控制
		moveEnd = clock();
		if (moveEnd-moveStart>100)
		{
			moveStart =moveEnd;

			if (KEYDOWN('w')||KEYDOWN('W'))
			{
				if (OldmyTank.a==0)
				{	
					mytank.nPosY--;
				}
				else
					mytank.a = 0;
			}

			if (KEYDOWN('s')||KEYDOWN('S'))
			{
				if (OldmyTank.a==1)
				{
					mytank.nPosY++;
				}
				else
					mytank.a=1;
			}
			if (KEYDOWN('a')||KEYDOWN('A'))
			{
				if (OldmyTank.a==2)
				{
					mytank.nPosX--;
				}
				else
					mytank.a=2;
			}
			if (KEYDOWN('d')||KEYDOWN('D'))
			{
				if (OldmyTank.a==3)
				{
					mytank.nPosX++;
				}
				else
					mytank.a=3;
			}
			if (KEYDOWN(' '))
			{
				for (int i=0;i<200;i++)
				{
					if (!shoot[i].g_IsBullet)     //判断数组当前位置是否为空
					{
						Bullet = mytank;		  //把当前中心点坐标给中间值
						switch (Bullet.a)
						{
						case 0:
							Bullet.nPosY-=1;break;
						case 1:
							Bullet.nPosY+=1;break;
						case 2:				
							Bullet.nPosX-=1;break;
						case 3:				
							Bullet.nPosX+=1;break;
						}
						shoot[i]=Bullet;		 //如果为空 装填子弹		
						shoot[i].g_IsBullet = true;
						mciSendString(L"close 2.wav",0,0,0);
						u.drawshoot(Bullet);       //画出第一个子弹
						mciSendString(L"play 2.wav",0,0,0);
						//PlaySound(TEXT("1"),NULL,SND_FILENAME | SND_ASYNC);
						break;
					}	
				} 
			}
			if (u.IsMoveTank(mytank))             //移动坦克
			{
				u.ClsTank(OldmyTank);
				u.DrawTank(mytank);
				OldmyTank = mytank;
				continue;
			}
			mytank = OldmyTank;
		}
    }
}






你可能感兴趣的:(源码,vs2012,C++学习心得)