//========================================================================
//TITLE:
// "是男人就挺过二十秒"源代码
//AUTHOR:
// norains
//DATE:
// wednesday 25-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
"是男人就挺过二十秒"简单的源代码 ,但基本结构已经完备,编译完毕在wince下便可正常游戏.
// Bullets.h: interface for the CBullets class.
//

/**///////////////////////////////////////////////////////////////////////
#ifndef BULLETS_H
#define BULLETS_H

class CBullets

...{
public:
BOOL CheckCollision(const RECT rcArea);
void Destroy();
void Move();
void Draw(HDC hdc);
BOOL Initialize(int iCount,int iMaxMoveDistance,const RECT *prcWnd);
CBullets();
virtual ~CBullets();

protected:
double AverageRandom(double min,double max);
int m_iCount;
RECT m_rcWnd;
int m_iMaxMoveDistance;
CRITICAL_SECTION m_csBulletData; //The Move() and the CheckCollision() could not be call in the same time

typedef struct

...{
LONG x;
LONG y;
int iMoveDistX;
int iMoveDistY;
}BULLETDATA,*LPBULLETDATA;
LPBULLETDATA lpBullet; //Pointer to the bullet
void InitializeBullet(LPBULLETDATA lpBullet);
};

#endif // #ifndef BULLETS_H

// Bullets.cpp: implementation of the CBullets class.
//

/**///////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Bullets.h"


//-------------------------------------------------------------------
//Macro define

//The radius of the bullet
#define BULLET_RADIUS 2
//The color of the bullet
#define BULLET_COLOR RGB(0,0,0)



/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction

/**///////////////////////////////////////////////////////////////////////
CBullets::CBullets()

...{
m_iCount = 0;
m_iMaxMoveDistance = 0;
lpBullet = NULL;
memset(&m_rcWnd,0,sizeof(m_rcWnd));

//Initialize the critical section
InitializeCriticalSection(&m_csBulletData);
}

CBullets::~CBullets()

...{
DeleteCriticalSection(&m_csBulletData);
}
//--------------------------------------------------------------------
//Description:
// Initialize the bullets
//
//Parameters:
// iCount: [in] The count of the bullet to create
// iMaxMoveDistance: [in] The max distance to move by each moving action,
// and the value should not more than the half of the plane
// prcWnd: [in] The rect of the window to play the bullet
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
//--------------------------------------------------------------------
BOOL CBullets::Initialize(int iCount,int iMaxMoveDistance, const RECT *prcWnd)

...{
m_iCount = iCount;
m_rcWnd = *prcWnd;
m_iMaxMoveDistance = iMaxMoveDistance;
lpBullet = new BULLETDATA[m_iCount];

if(lpBullet == NULL)

...{
return FALSE;
}

//Set the seed for the AverageRandom() function
srand(GetTickCount());

for(int i = 0; i < m_iCount; i++)

...{
InitializeBullet(&lpBullet[i]);
}

return TRUE;
}


//--------------------------------------------------------------------
//Description:
// Initialize the single bullets position
//--------------------------------------------------------------------
void CBullets::InitializeBullet(LPBULLETDATA lpBullet)

...{
//Because the return value of AverageRandom() is double type,
//and chang the value to int.
//If your using like that: AverageRandom(1,4);
//the number 4 is hard to create !
int iRandom = (int)AverageRandom(1,5);


//The bullet must begin in the edge
if(iRandom == 1)

...{
lpBullet->x = m_rcWnd.left;
lpBullet->y = (int)AverageRandom(m_rcWnd.top,m_rcWnd.bottom);

//Set the move direction
lpBullet->iMoveDistX = 1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)

...{
lpBullet->iMoveDistY = 1;
}
else

...{
lpBullet->iMoveDistY = -1;
}
}
else if(iRandom == 2)

...{
lpBullet->x = m_rcWnd.right;
lpBullet->y = (int)AverageRandom(m_rcWnd.top,m_rcWnd.bottom);

//Set the move direction
lpBullet->iMoveDistX = -1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)

...{
lpBullet->iMoveDistY = 1;
}
else

...{
lpBullet->iMoveDistY = -1;
}
}
else if(iRandom == 3)

...{
lpBullet->x = (int)AverageRandom(m_rcWnd.left,m_rcWnd.right);
lpBullet->y = m_rcWnd.top;

//Set the move direction
lpBullet->iMoveDistY = 1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)

...{
lpBullet->iMoveDistX = 1;
}
else

...{
lpBullet->iMoveDistX = -1;
}
}
else if(iRandom == 4)

...{
lpBullet->x = (int)AverageRandom(m_rcWnd.left,m_rcWnd.right);
lpBullet->y = m_rcWnd.bottom;

//Set the move direction
lpBullet->iMoveDistY = -1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)

...{
lpBullet->iMoveDistX = 1;
}
else

...{
lpBullet->iMoveDistX = -1;
}
}

//Set the move distance
iRandom = (int)AverageRandom(1,m_iMaxMoveDistance);
lpBullet->iMoveDistX *= iRandom;
iRandom = (int)AverageRandom(1,m_iMaxMoveDistance);
lpBullet->iMoveDistY *= iRandom;
}


//--------------------------------------------------------------------
//Description:
// Create the random number.Before calling the method , you must set the seed
//by using srand() function.
//
//Parameters:
// dMin: [in] The min number
// dMax: [in] The max number
//--------------------------------------------------------------------
double CBullets::AverageRandom(double dMin, double dMax)

...{
int iMin = (int)(dMin * 10000);
int iMax = (int)(dMax * 10000);
int iRand = rand() * rand();
int iDiff = iMax - iMin;
double dResult = (iRand % iDiff + iMin) / 10000.0;
return dResult ;
}



//--------------------------------------------------------------------
//Description:
// Move the bullets
//---------------------------------------------------------------------
void CBullets::Move()

...{
EnterCriticalSection(&m_csBulletData);
for(int i = 0; i < m_iCount; i++)

...{
lpBullet[i].x += lpBullet[i].iMoveDistX;
lpBullet[i].y += lpBullet[i].iMoveDistY;
if(lpBullet[i].x < m_rcWnd.left || lpBullet[i].x > m_rcWnd.right || lpBullet[i].y < m_rcWnd.top || lpBullet[i].y > m_rcWnd.bottom)

...{
InitializeBullet(&lpBullet[i]);
}
}
LeaveCriticalSection(&m_csBulletData);
}


//--------------------------------------------------------------------
//Description:
// Draw the bullet to the DC
//---------------------------------------------------------------------
void CBullets::Draw(HDC hdc)

...{
HBRUSH hBrush = CreateSolidBrush(BULLET_COLOR);
HGDIOBJ hOldSel = SelectObject(hdc,hBrush);


RECT rcBullet = ...{0};
for(int i = 0; i < m_iCount; i++)

...{
rcBullet.left = lpBullet[i].x - BULLET_RADIUS;
rcBullet.top = lpBullet[i].y - BULLET_RADIUS;
rcBullet.right = lpBullet[i].x + BULLET_RADIUS;
rcBullet.bottom = lpBullet[i].y + BULLET_RADIUS;
Ellipse(hdc,rcBullet.left,rcBullet.top,rcBullet.right,rcBullet.bottom);
}

SelectObject(hdc,hOldSel);
DeleteObject(hBrush);
}



//--------------------------------------------------------------------
//Description:
// Destroy the bullet
//---------------------------------------------------------------------
void CBullets::Destroy()

...{
if(lpBullet != NULL)

...{
delete [] lpBullet;
lpBullet = NULL;
}
}



//--------------------------------------------------------------------
//Description:
// Check the collision
//
//Return Values:
// TRUE: Collided .
// FALSE: No collision
//---------------------------------------------------------------------
BOOL CBullets::CheckCollision(const RECT rcArea)

...{
BOOL bCollide = FALSE;

EnterCriticalSection(&m_csBulletData);
for(int i = 0; i < m_iCount; i++)

...{
if(lpBullet[i].x >= rcArea.left && lpBullet[i].x <= rcArea.right && lpBullet[i].y >= rcArea.top && lpBullet[i].y <= rcArea.bottom)
