📄 bullets.cpp
字号:
// 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)
{
bCollide = TRUE;
break;
}
}
LeaveCriticalSection(&m_csBulletData);
return bCollide;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -