⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weapon.cpp

📁 坦克游戏
💻 CPP
字号:
// Weapon.cpp: implementation of the CWeapon class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Weapon.h"
#include "MainGame.h"
#include "Explode.h"
#include "Bonus.h"

extern int score1,score2;
extern int staticspeed1,staticspeed2;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWeapon::CWeapon(CWeapon *weapon):CContainerObject(weapon)
{

}

CWeapon::~CWeapon()
{

}

void CWeapon::displayObject()
{
	
}

bool CWeapon::ActiveObject()
{
	int i;
	switch(direction)
	{
	case 0:	for(i=0;i<speed;i++)
			{
				if(ChargeAttack()!=0)return false;
				positionY--;
			}break;
	case 1:	for(i=0;i<speed;i++)
			{
				if(ChargeAttack()!=0)return false;
				positionX++;
			}break;	
	case 2:	for(i=0;i<speed;i++)
			{
				if(ChargeAttack()!=0)return false;
				positionY++;
			}break;	
	case 3:	for(i=0;i<speed;i++)
			{
				if(ChargeAttack()!=0)return false;
				positionX--;
			}break;	
	}
	return true;
}


int  CWeapon::ChargeAttack()
{
///////////////////////////////////////////////
	CMainGame *pMainGame=(CMainGame*)AfxGetMainWnd();
	CContainerObject *temp=CContainerObject::pFirst;
	while(temp)
	{
		if(temp!=this)
		{
			if(strcmp(name,"bullet")==0)
			{
				if(strcmp(temp->name,"bullet")==0)	//子弹对子弹
				{
					if(positionX > temp->positionX-3
						&&positionX < temp->positionX+6+3
						&&positionY > temp->positionY-3
						&&positionY < temp->positionY+6+3
						&&((temp->type<=7 && type>7)||(temp->type>7 && type<=7)))
					{
						delete temp;
						return 1;
					}
				}
				if(strcmp(temp->name,"missile")==0)	//子弹对导弹
				{
					if(positionX > temp->positionX-3
						&&positionX < temp->positionX+10+3
						&&positionY > temp->positionY-3
						&&positionY < temp->positionY+30+3
						&&((temp->type<=7 && type>7)||(temp->type>7 && type<=7)))
						{
							CExplode *bomb=new CExplode(positionX-12,positionY-12,4);
							delete temp;
							return 1;
						}
				}
				if(strcmp(temp->name,"PlayerTank")==0//子弹对坦克
					|| strcmp(temp->name,"EnemyTank")==0)
				{
					if(positionX > temp->positionX
						&&positionX < temp->positionX+32
						&&positionY > temp->positionY	
						&&positionY < temp->positionY+32)
					{
						if((temp->type<=7 && type>7)||(temp->type>7 && type<=7))
						{
						if(strcmp(temp->name,"PlayerTank")==0)
						{
							int k=0;
							if(pMainGame->gameover)
							{
								pMainGame->gameover=0;
								k=1;
							}
							if(temp->type<=3)
							{
								pMainGame->player2--;
								if(pMainGame->player2>0)
								{
									pMainGame->playerTank2=new CPlayerTank(32*11,32*14,0,0);
								}
								else pMainGame->playerTank2 = NULL;
							}
							else
							{
								pMainGame->player1--;
								if(pMainGame->player1>0)
								{
									pMainGame->playerTank1=new CPlayerTank(32*8,32*14,0,4);
								}
								else pMainGame->playerTank1 = NULL;
							}
							if(!pMainGame->gameover && k)
							{
								pMainGame->gameover=1;
								k=0;
							}
							CExplode *bomb=new CExplode(temp->positionX,temp->positionY,2);
						}//playerTank
						else
						{
							if(temp->type==12 || temp->type==13)
							{
								temp->type++;
								sndPlaySound("sound//hit.wav",SND_ASYNC|SND_NODEFAULT);
								return 1;
							}
							if(temp->type==9 || temp->type==11 || temp->type==15)
							{
								CBonus *bonus=new CBonus;
								if(temp->type==15)temp->type-=3;
								else temp->type--;
								return 1;
							}
							pMainGame->Enemy--;
							if(type<=3)
								score2+=((temp->type*2+3)/10)*100;
							else
								score1+=((temp->type*2+3)/10)*100;
							CExplode *bomb=new CExplode(temp->positionX,temp->positionY,3);
						}
						delete temp;
						return 1;
						}
						else if(temp->type<=7 && type<=7)
						{
							if(temp->type>3)staticspeed1=0;
							else staticspeed2=0;
							return 1;
						}
						
					}
				}
			}//if(strcmp(name,"bullet")==0)
			if(strcmp(name,"missile")==0)
			{
				if(strcmp(temp->name,"PlayerTank")==0
					|| strcmp(temp->name,"EnemyTank")==0)
				{
					if(positionX > temp->positionX
						&&positionX < temp->positionX+32
						&&positionY > temp->positionY	
						&&positionY < temp->positionY+32
						&&((temp->type<=7 && type>7)||(temp->type>7 && type<=7)))
					{
						if(strcmp(temp->name,"EnemyTank")==0)
						{
							CExplode *bomb=new CExplode(temp->positionX,temp->positionY,1);
							pMainGame->Enemy--;

							if(type<=3)
								pMainGame->playerTank2->score+=((temp->type*2+3)/10)*10;
							else
								pMainGame->playerTank1->score+=((temp->type*2+3)/10)*10;
						}
						else
							CExplode *bomb=new CExplode(temp->positionX,temp->positionY+32,4);
						delete temp;
						return 1;
					}
				}
			}
		}//if(temp!=this)
		temp=temp->pNext;
	}

///////////////////////////////////////////////

	if(positionX<=0||positionX>=640||
		positionY<=0||positionY>=480)
	{
		return -1;
	}

	switch(direction)
	{
	case 0:	if((positionY+6)%8)return 0; break;
	case 1:	if((positionX)%8)return 0; break;
	case 2:	if((positionY)%8)return 0; break;
	case 3:	if((positionX+6)%8)return 0; break;
	}

	int returnValue=0;
	int row=positionY/8;
	int column=positionX/8; 
	switch(direction)
	{
	case 0:	
	case 2:	if(pMainGame->theMap->GameMap[row][column]==3)
			{
				pMainGame->theMap->GameMap[row][column]=0;
				pMainGame->theMap->GameMap[row][column-1]=0;
				returnValue=3;
			}
			if(pMainGame->theMap->GameMap[row][column+1]==3)
			{
				pMainGame->theMap->GameMap[row][column+1]=0;
				pMainGame->theMap->GameMap[row][column+2]=0;
				returnValue=3;
			}
			if(pMainGame->theMap->GameMap[row][column]==1||pMainGame->theMap->GameMap[row][column+1]==1)
			{
				returnValue=1;
			}
			if(pMainGame->theMap->GameMap[row][column]==5||pMainGame->theMap->GameMap[row][column+1]==5)
			{
				pMainGame->LossGame();
			}
			break;
	case 1:	
	case 3:	if(pMainGame->theMap->GameMap[row][column]==3)
			{
				pMainGame->theMap->GameMap[row][column]=0;
				pMainGame->theMap->GameMap[row-1][column]=0;
				returnValue=3;
			}
			if(pMainGame->theMap->GameMap[row+1][column]==3)
			{
				pMainGame->theMap->GameMap[row+1][column]=0;
				pMainGame->theMap->GameMap[row+2][column]=0;
				returnValue=3;
			}
			if(pMainGame->theMap->GameMap[row][column]==1||pMainGame->theMap->GameMap[row+1][column]==1)
			{
				returnValue=1;
			}
			if(pMainGame->theMap->GameMap[row][column]==5||pMainGame->theMap->GameMap[row+1][column]==5)
			{
			//	pMainGame->LossGame();
				sndPlaySound("sound//bang.wav",SND_ASYNC|SND_NODEFAULT);
				pMainGame->theMap->fugai=0;
				pMainGame->gameover=0;
				if(pMainGame->player1)pMainGame->playerTank1->speed=0;
				if(pMainGame->player2)pMainGame->playerTank2->speed=0;
				pMainGame->player1=0;
				pMainGame->player2=0;
				return 1;
			}
			break;
	}
	if(returnValue!=0)
		return returnValue;
	else return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -