是男人就挺过二十秒"源代

//========================================================================
//TITLE:
// "是男人就挺过二十秒"源代码
//AUTHOR:
// norains
//DATE:
//wednesday 25-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================

"是男人就挺过二十秒"简单的源代码,但基本结构已经完备,编译完毕在wince下便可正常游戏.

// Bullets.h:interfacefortheCBulletsclass.
//
/**/ //

#ifndefBULLETS_H
#define BULLETS_H

class CBullets
... {
public:
BOOLCheckCollision(
constRECTrcArea);
voidDestroy();
voidMove();
voidDraw(HDChdc);
BOOLInitialize(
intiCount,intiMaxMoveDistance,constRECT*prcWnd);
CBullets();
virtual~CBullets();

protected:
doubleAverageRandom(doublemin,doublemax);
intm_iCount;
RECTm_rcWnd;
intm_iMaxMoveDistance;
CRITICAL_SECTIONm_csBulletData;
//TheMove()andtheCheckCollision()couldnotbecallinthesametime

typedef
struct
...{
LONGx;
LONGy;
intiMoveDistX;
intiMoveDistY;
}
BULLETDATA,*LPBULLETDATA;
LPBULLETDATAlpBullet;
//Pointertothebullet
voidInitializeBullet(LPBULLETDATAlpBullet);
}
;

#endif // #ifndefBULLETS_H

// Bullets.cpp:implementationoftheCBulletsclass.
//
/**/ //

#include
" stdafx.h "
#include
" Bullets.h "


// -------------------------------------------------------------------
// Macrodefine

// Theradiusofthebullet
#define BULLET_RADIUS2
// Thecolorofthebullet
#define BULLET_COLORRGB(0,0,0)


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

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

//Initializethecriticalsection
InitializeCriticalSection(&m_csBulletData);
}


CBullets::
~ CBullets()
... {
DeleteCriticalSection(
&m_csBulletData);
}

// --------------------------------------------------------------------
// Description:
// Initializethebullets
//
// Parameters:
// iCount:[in]Thecountofthebullettocreate
// iMaxMoveDistance:[in]Themaxdistancetomovebyeachmovingaction,
// andthevalueshouldnotmorethanthehalfoftheplane
// prcWnd:[in]Therectofthewindowtoplaythebullet
//
// ReturnValues:
// TRUE:Succeed
// FALSE:Failed
// --------------------------------------------------------------------
BOOLCBullets::Initialize( int iCount, int iMaxMoveDistance, const RECT * prcWnd)
... {
m_iCount
=iCount;
m_rcWnd
=*prcWnd;
m_iMaxMoveDistance
=iMaxMoveDistance;
lpBullet
=newBULLETDATA[m_iCount];

if(lpBullet==NULL)
...{
returnFALSE;
}


//SettheseedfortheAverageRandom()function
srand(GetTickCount());

for(inti=0;i<m_iCount;i++)
...{
InitializeBullet(
&lpBullet[i]);
}


returnTRUE;
}



// --------------------------------------------------------------------
// Description:
// Initializethesinglebulletsposition
// --------------------------------------------------------------------
void CBullets::InitializeBullet(LPBULLETDATAlpBullet)
... {
//BecausethereturnvalueofAverageRandom()isdoubletype,
//andchangthevaluetoint.
//Ifyourusinglikethat:AverageRandom(1,4);
//thenumber4ishardtocreate!
intiRandom=(int)AverageRandom(1,5);


//Thebulletmustbeginintheedge
if(iRandom==1)
...{
lpBullet
->x=m_rcWnd.left;
lpBullet
->y=(int)AverageRandom(m_rcWnd.top,m_rcWnd.bottom);

//Setthemovedirection
lpBullet->iMoveDistX=1;
intiDirection=(int)AverageRandom(1,3);
if(iDirection==1)
...{
lpBullet
->iMoveDistY=1;
}

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

}

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

//Setthemovedirection
lpBullet->iMoveDistX=-1;
intiDirection=(int)AverageRandom(1,3);
if(iDirection==1)
...{
lpBullet
->iMoveDistY=1;
}

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

}

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

//Setthemovedirection
lpBullet->iMoveDistY=1;
intiDirection=(int)AverageRandom(1,3);
if(iDirection==1)
...{
lpBullet
->iMoveDistX=1;
}

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

}

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

//Setthemovedirection
lpBullet->iMoveDistY=-1;
intiDirection=(int)AverageRandom(1,3);
if(iDirection==1)
...{
lpBullet
->iMoveDistX=1;
}

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

}



//Setthemovedistance
iRandom=(int)AverageRandom(1,m_iMaxMoveDistance);
lpBullet
->iMoveDistX*=iRandom;
iRandom
=(int)AverageRandom(1,m_iMaxMoveDistance);
lpBullet
->iMoveDistY*=iRandom;
}



// --------------------------------------------------------------------
// Description:
// Createtherandomnumber.Beforecallingthemethod,youmustsettheseed
// byusingsrand()function.
//
// Parameters:
// dMin:[in]Theminnumber
// dMax:[in]Themaxnumber
// --------------------------------------------------------------------
double CBullets::AverageRandom( double dMin, double dMax)
... {
intiMin=(int)(dMin*10000);
intiMax=(int)(dMax*10000);
intiRand=rand()*rand();
intiDiff=iMax-iMin;
doubledResult=(iRand%iDiff+iMin)/10000.0;
returndResult;
}




// --------------------------------------------------------------------
// Description:
// Movethebullets
// ---------------------------------------------------------------------
void CBullets::Move()
... {
EnterCriticalSection(
&m_csBulletData);
for(inti=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:
// DrawthebullettotheDC
// ---------------------------------------------------------------------
void CBullets::Draw(HDChdc)
... {
HBRUSHhBrush
=CreateSolidBrush(BULLET_COLOR);
HGDIOBJhOldSel
=SelectObject(hdc,hBrush);

RECTrcBullet
=...{0};
for(inti=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:
// Destroythebullet
// ---------------------------------------------------------------------
void CBullets::Destroy()
... {
if(lpBullet!=NULL)
...{
delete[]lpBullet;
lpBullet
=NULL;
}

}




// --------------------------------------------------------------------
// Description:
// Checkthecollision
//
// ReturnValues:
// TRUE:Collided.
// FALSE:Nocollision
// ---------------------------------------------------------------------
BOOLCBullets::CheckCollision( const RECTrcArea)
... {
BOOLbCollide
=FALSE;


EnterCriticalSection(
&m_csBulletData);
for(inti=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);


returnbCollide;
}


// GameWnd.h:interfacefortheCGameWndclass.
//
/**/ //
#ifndefGAMEWND_H
#define GAMEWND_H



#include
" Bullets.h "
#include
" Plane.h "
#include
" Text.h "

class CGameWnd
... {
public:
BOOLShowWindow(BOOLbShow);
BOOLInitialize(HINSTANCEhInst);
staticCGameWnd*GetInstance();
virtual~CGameWnd();
protected:
voidCheckMenu();
voidOnMenuLevel(intiLevel);
voidOnCreate(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
voidSetSkewingOnKey();
voidOnKeyUp(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
voidOnKeyDown(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
voidOnLButtonUp(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
voidOnLButtonDown(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
voidOnMouseMove(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
voidEndGame();
staticDWORDWINAPIRefreshScreenThread(PVOIDpArg);
voidStartGame();
voidOnPaint(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
voidOnDestroy(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
staticLRESULTWndProc(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam);
CGameWnd();

staticCGameWnd*m_pInstance;
HINSTANCEm_hInst;
HWNDm_hWnd;
RECTm_rcWndPlay;
//Thewindowtoplaythegame,bebaseonthewindowscale
RECTm_rcWndMain;//Themainwindow
CBulletsm_Bullets;
CPlanem_Plane;
BOOLm_bMovePlane;
//Movetheplaneornot
BOOLm_bCollide;
POINTm_ptPlaneSkewing;
//Thenextpointtomovetofortheplane
HWNDm_hWndCB;//Commandbar
ULONGm_ulTimeCount;//Thecontinuetimecount
CTextm_TxtTime;

//Thegamesettingdata
typedefstruct
...{
//Fortheplane
intiPlaneMoveDistance;//Thedistancetomoveperacttion

//Forthebullet
intiBulletMaxMoveDistance;//Themaxdistancetomove
intiBulletCount;//Thecountofbullets

//Forthegame
intiRefreshInterval;//Theintervaltimetorefreshscreen
}
SETTINGDATA,*LPSETTINGDATA;
SETTINGDATAm_Setting;

//Thekeypushedstatus
typedefstruct
...{
BOOLbPushKeyUp;
BOOLbPushKeyDown;
BOOLbPushKeyLeft;
BOOLbPushKeyRight;
}
PUSHKEYSTATUS,*LPPUSHKEYSTATUS;
PUSHKEYSTATUSm_KeyStatus;
}
;



#endif // #ifndefGAMEWND_H


// GameWnd.cpp:implementationoftheCGameWndclass.
//
/**/ //

#include
" stdafx.h "
#include
" GameWnd.h "
#include
" resource.h "
#include
" commctrl.h "

// ----------------------------------------------------------------
// Macrodefine
#define WND_CLASSTEXT("Evade_Class")
#define WND_TITLETEXT("Evade_Title")

// TheIDforthecommandbar
#define ID_COMMANDBAR100

// Screenwidth
#define SCREEN_WIDTHGetSystemMetrics(SM_CXSCREEN)
// Screenheight
#define SCREEN_HEIGHTGetSystemMetrics(SM_CYSCREEN)


// Thewindowposition
#define MAINWND_POS_X0
#define MAINWND_POS_Y0


// Level_1value
#define LEVEL1_BULLET_COUNT100
#define LEVEL1_BULLET_MAXMOVEDISTANCE3
#define LEVEL1_PLANE_MOVEDISTANCE3

// Level_2value
#define LEVEL2_BULLET_COUNT150
#define LEVEL2_BULLET_MAXMOVEDISTANCE4
#define LEVEL2_PLANE_MOVEDISTANCE3

// Level_3value
#define LEVEL3_BULLET_COUNT200
#define LEVEL3_BULLET_MAXMOVEDISTANCE5
#define LEVEL3_PLANE_MOVEDISTANCE3



// Defaultvalue
#define DEFAULT_BULLET_COUNTLEVEL1_BULLET_COUNT
#define DEFAULT_BULLET_MAXMOVEDISTANCELEVEL1_BULLET_MAXMOVEDISTANCE
#define DEFAULT_PLANE_MOVEDISTANCELEVEL1_PLANE_MOVEDISTANCE

#define DEFAULT_REFRESH_INTERVAL50 // 0.05s

#define DEFAULT_TEXT_TIME_COLORRGB(0,0,255)
#define DEFAULT_TEXT_TIME_HEIGHT16

// Theoffsetofthetimetext
#define TXT_TIME_OFFSET_TOP2
#define TXT_TIME_OFFSET_LEFT(SCREEN_WIDTH-100)
#define TXT_TIME_OFFSET_RIGHT4
#define TXT_TIME_HEIGHT40
// -----------------------------------------------------------------
// Initialize
CGameWnd * CGameWnd::m_pInstance = NULL;


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

CGameWnd::CGameWnd()
... {
m_hWndCB
=NULL;
m_bMovePlane
=FALSE;
m_bCollide
=FALSE;
m_hInst
=NULL;
m_hWnd
=NULL;

memset(
&m_rcWndPlay,0,sizeof(m_rcWndPlay));
memset(
&m_ptPlaneSkewing,0,sizeof(m_ptPlaneSkewing));
memset(
&m_KeyStatus,0,sizeof(m_KeyStatus));
memset(
&m_rcWndMain,0,sizeof(m_rcWndMain));

m_Setting.iBulletCount
=DEFAULT_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance
=DEFAULT_BULLET_MAXMOVEDISTANCE;
m_Setting.iRefreshInterval
=DEFAULT_REFRESH_INTERVAL;
m_Setting.iPlaneMoveDistance
=DEFAULT_PLANE_MOVEDISTANCE;


m_TxtTime.SetTextColor(DEFAULT_TEXT_TIME_COLOR);
m_TxtTime.SetTextHeight(DEFAULT_TEXT_TIME_HEIGHT);
m_TxtTime.SetFormat(DT_RIGHT
|DT_VCENTER);
}


CGameWnd::
~ CGameWnd()
... {
if(m_pInstance!=NULL)
...{
deletem_pInstance;
m_pInstance
=NULL;
}

}



// ----------------------------------------------------------------
// Description:
// Gettheobjectinstance
// -----------------------------------------------------------------
CGameWnd * CGameWnd::GetInstance()
... {
if(m_pInstance==NULL)
...{
m_pInstance
=newCGameWnd();
}

returnm_pInstance;
}



// ----------------------------------------------------------------
// Description:
// Initializethewindow
// -----------------------------------------------------------------
BOOLCGameWnd::Initialize(HINSTANCEhInst)
... {
m_hInst
=hInst;

WNDCLASSws;
memset(
&ws,0,sizeof(ws));
ws.lpfnWndProc
=WndProc;
ws.hInstance
=hInst;
ws.lpszClassName
=WND_CLASS;
ws.hbrBackground
=(HBRUSH)GetStockObject(WHITE_BRUSH);
if(RegisterClass(&ws)==FALSE)
...{
returnFALSE;
}



//Findthetaskbar
HWNDhWndTaskBar=FindWindow(TEXT("HHTaskBar"),NULL);
RECTrcTaskBar
=...{0};
GetWindowRect(hWndTaskBar,
&rcTaskBar);


m_rcWndMain.left
=MAINWND_POS_X;
m_rcWndMain.top
=MAINWND_POS_Y;
m_rcWndMain.right
=SCREEN_WIDTH;
m_rcWndMain.bottom
=SCREEN_HEIGHT-(rcTaskBar.bottom-rcTaskBar.top);

m_hWnd
=CreateWindow(
WND_CLASS,
WND_TITLE,
WS_POPUP,
m_rcWndMain.left,
m_rcWndMain.top,
m_rcWndMain.right
-m_rcWndMain.left,
m_rcWndMain.bottom
-m_rcWndMain.top,
NULL,
NULL,
hInst,
NULL
);

if(IsWindow(m_hWnd)==FALSE)
...{
returnFALSE;
}



RECTrcCmdBar
=...{0};
GetWindowRect(m_hWndCB,
&rcCmdBar);

m_rcWndPlay.left
=m_rcWndMain.left;
m_rcWndPlay.right
=m_rcWndMain.right;
m_rcWndPlay.top
=m_rcWndMain.top+(rcCmdBar.bottom-rcCmdBar.top);
m_rcWndPlay.bottom
=m_rcWndMain.bottom;

RECTrcTxtTime;
rcTxtTime.top
=m_rcWndPlay.top+TXT_TIME_OFFSET_TOP;
rcTxtTime.left
=m_rcWndPlay.left+TXT_TIME_OFFSET_LEFT;
rcTxtTime.right
=m_rcWndPlay.right-TXT_TIME_OFFSET_RIGHT;
rcTxtTime.bottom
=rcTxtTime.top+TXT_TIME_HEIGHT;
m_TxtTime.SetPosition(
&rcTxtTime);

CheckMenu();

returnTRUE;
}



// ----------------------------------------------------------------
// Description:
// Thewindowprocess
// -----------------------------------------------------------------
LRESULTCGameWnd::WndProc(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
switch(wMsg)
...{
caseWM_DESTROY:
m_pInstance
->OnDestroy(hWnd,wMsg,wParam,lParam);
return0;
caseWM_PAINT:
m_pInstance
->OnPaint(hWnd,wMsg,wParam,lParam);
return0;
caseWM_ERASEBKGND:
//Needn'ttoredrawthebackground
return0;
caseWM_MOUSEMOVE:
m_pInstance
->OnMouseMove(hWnd,wMsg,wParam,lParam);
return0;
caseWM_LBUTTONDOWN:
m_pInstance
->OnLButtonDown(hWnd,wMsg,wParam,lParam);
return0;
caseWM_LBUTTONUP:
m_pInstance
->OnLButtonUp(hWnd,wMsg,wParam,lParam);
return0;
caseWM_KEYDOWN:
m_pInstance
->OnKeyDown(hWnd,wMsg,wParam,lParam);
return0;
caseWM_KEYUP:
m_pInstance
->OnKeyUp(hWnd,wMsg,wParam,lParam);
return0;
caseWM_CREATE:
m_pInstance
->OnCreate(hWnd,wMsg,wParam,lParam);
return0;
caseWM_COMMAND:
switch(LOWORD(wParam))
...{
caseIDM_LEVEL_1:
m_pInstance
->OnMenuLevel(IDM_LEVEL_1);
return0;
caseIDM_LEVEL_2:
m_pInstance
->OnMenuLevel(IDM_LEVEL_2);
return0;
caseIDM_LEVEL_3:
m_pInstance
->OnMenuLevel(IDM_LEVEL_3);
return0;
caseIDM_START:
m_pInstance
->StartGame();
return0;
caseIDM_EXIT:
DestroyWindow(hWnd);
return0;
}

break;
}

returnDefWindowProc(hWnd,wMsg,wParam,lParam);
}



// ----------------------------------------------------------------
// Description:
// Showthewindow
// -----------------------------------------------------------------
BOOLCGameWnd::ShowWindow(BOOLbShow)
... {
if(m_hWnd==NULL)
...{
returnFALSE;
}


if(bShow==TRUE)
...{
::ShowWindow(m_hWnd,SW_SHOW);
}

else
...{
::ShowWindow(m_hWnd,SW_HIDE);
}



returnTRUE;
}



// ----------------------------------------------------------------
// Description:
// OnmessageWM_DESTROY
// -----------------------------------------------------------------
void CGameWnd::OnDestroy(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
PostQuitMessage(
0x00);
}



// ----------------------------------------------------------------
// Description:
// OnmessageWM_PAINT
// -----------------------------------------------------------------
void CGameWnd::OnPaint(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {

PAINTSTRUCTps;
HDChdc
=BeginPaint(hWnd,&ps);

//CreatethememoryDC
HBITMAPhBitmap=CreateCompatibleBitmap(hdc,m_rcWndMain.right-m_rcWndMain.left,m_rcWndMain.bottom-m_rcWndMain.top);

HDChdcMem
=CreateCompatibleDC(hdc);
HGDIOBJhOldSel
=SelectObject(hdcMem,hBitmap);

Rectangle(hdcMem,m_rcWndMain.left,m_rcWndMain.top,m_rcWndMain.right,m_rcWndMain.bottom);

m_Bullets.Draw(hdcMem);
if(m_bCollide==FALSE)
...{
m_Plane.DrawNormal(hdcMem);
}

else
...{
m_Plane.DrawDestroy(hdcMem);
}


m_TxtTime.Draw(hdcMem);

BitBlt(hdc,
m_rcWndMain.left,
m_rcWndMain.top,
m_rcWndMain.right
-m_rcWndMain.left,
m_rcWndMain.bottom
-m_rcWndMain.top,
hdcMem,
0,
0,
SRCCOPY);


SelectObject(hdcMem,hOldSel);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
EndPaint(hWnd,
&ps);
}




// ----------------------------------------------------------------
// Description:
// Startthegame
// -----------------------------------------------------------------
void CGameWnd::StartGame()
... {
m_bCollide
=FALSE;
m_ulTimeCount
=0;

//Theplane
m_Plane.Initialize(&m_rcWndPlay);
POINTptPos
=...{0};
ptPos.x
=(m_rcWndPlay.right-m_rcWndPlay.left)/2;
ptPos.y
=(m_rcWndPlay.bottom-m_rcWndPlay.top)/2;
m_Plane.SetCurrentPos(
&ptPos);

//Thebullets
m_Bullets.Initialize(m_Setting.iBulletCount,m_Setting.iBulletMaxMoveDistance,&m_rcWndPlay);

HANDLEhdThrd;
DWORDdwID;
hdThrd
=CreateThread(NULL,NULL,RefreshScreenThread,NULL,NULL,&dwID);
CloseHandle(hdThrd);
}



// ----------------------------------------------------------------
// Description:
// Endthegame
// -----------------------------------------------------------------
void CGameWnd::EndGame()
... {
InvalidateRect(m_hWnd,
&m_rcWndPlay,TRUE);
}



// ----------------------------------------------------------------
// Description:
// Refreshthescreen
// -----------------------------------------------------------------
DWORDWINAPICGameWnd::RefreshScreenThread(PVOIDpArg)
... {
DWORDdwResult
=0;
while(TRUE)
...{

//Movethebullets
m_pInstance->m_Bullets.Move();

//Movetheplane
m_pInstance->m_Plane.Move(m_pInstance->m_ptPlaneSkewing.x,m_pInstance->m_ptPlaneSkewing.y);

//Checkcollision
RECTrcPlane=...{0};
m_pInstance
->m_Plane.GetCurrentRect(&rcPlane);
m_pInstance
->m_bCollide=m_pInstance->m_Bullets.CheckCollision(rcPlane);
if(m_pInstance->m_bCollide==TRUE)
...{
m_pInstance
->EndGame();
break;
}


m_pInstance
->m_ulTimeCount+=m_pInstance->m_Setting.iRefreshInterval;
TCHARszTime[
80]=...{0};
_stprintf(szTime,TEXT(
"%dms"),m_pInstance->m_ulTimeCount);
m_pInstance
->m_TxtTime.SetText(szTime);

//Refreshthescreen
InvalidateRect(m_pInstance->m_hWnd,&m_pInstance->m_rcWndPlay,TRUE);

Sleep(m_pInstance
->m_Setting.iRefreshInterval);

}


return0;
}




// ----------------------------------------------------------------
// Description:
// OnmessageWM_MOUSEMOVE
// -----------------------------------------------------------------
void CGameWnd::OnMouseMove(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
/**//*
if(m_bMovePlane==FALSE)
{
return;
}


intiPosX=LOWORD(lParam);
intiPosY=HIWORD(lParam);

POINTptPlaneCurPos={0};
m_Plane.GetCurrentPos(&ptPlaneCurPos);

//Thedistancefromcurrentplanepositiontothecurrentmouseposition
doubledDistance=sqrt(pow((iPosX-ptPlaneCurPos.x),2)+pow((iPosY-ptPlaneCurPos.y),2));

if(dDistance!=0)
{
m_ptPlaneSkewing.x=(int)(iPosX*(m_Setting.iPlaneMoveDistance/dDistance));
m_ptPlaneSkewing.y=(int)(iPosY*(m_Setting.iPlaneMoveDistance/dDistance));
}
else
{
m_ptPlaneSkewing.x=0;
m_ptPlaneSkewing.y=0;
}

//Setthedirection
if(iPosX<ptPlaneCurPos.x)
{
m_ptPlaneSkewing.x*=-1;
}
if(iPosY<ptPlaneCurPos.y)
{
m_ptPlaneSkewing.y*=-1;
}


if(ptPlaneCurPos.x<m_rcWndPlay.left||ptPlaneCurPos.x>m_rcWndPlay.right)
{
m_ptPlaneSkewing.x=0;
}


if(ptPlaneCurPos.y<m_rcWndPlay.top||ptPlaneCurPos.y>m_rcWndPlay.bottom)
{
m_ptPlaneSkewing.y=0;
}
*/


}




// ----------------------------------------------------------------
// Description:
// OnmessageWM_LBUTTONDOWN
// -----------------------------------------------------------------
void CGameWnd::OnLButtonDown(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
SetCapture(m_hWnd);
m_bMovePlane
=TRUE;
}



// ----------------------------------------------------------------
// Description:
// OnmessageWM_LBUTTONUP
// -----------------------------------------------------------------
void CGameWnd::OnLButtonUp(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
ReleaseCapture();
m_bMovePlane
=FALSE;
}



// ----------------------------------------------------------------
// Description:
// OnmessageWM_KEYDOWN
// -----------------------------------------------------------------
void CGameWnd::OnKeyDown(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
intiKey=(int)wParam;
switch(iKey)
...{
caseVK_UP:
m_KeyStatus.bPushKeyUp
=TRUE;
break;
caseVK_DOWN:
m_KeyStatus.bPushKeyDown
=TRUE;
break;
caseVK_LEFT:
m_KeyStatus.bPushKeyLeft
=TRUE;
break;
caseVK_RIGHT:
m_KeyStatus.bPushKeyRight
=TRUE;
break;
}


SetSkewingOnKey();

}



// ----------------------------------------------------------------
// Description:
// OnmessageWM_KEYUP
// -----------------------------------------------------------------
void CGameWnd::OnKeyUp(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
intiKey=(int)wParam;
switch(iKey)
...{
caseVK_UP:
m_KeyStatus.bPushKeyUp
=FALSE;
break;
caseVK_DOWN:
m_KeyStatus.bPushKeyDown
=FALSE;
break;
caseVK_LEFT:
m_KeyStatus.bPushKeyLeft
=FALSE;
break;
caseVK_RIGHT:
m_KeyStatus.bPushKeyRight
=FALSE;
break;
}

SetSkewingOnKey();
}



// ----------------------------------------------------------------
// Description:
// Settheskewingbaseonthekeystatus
// -----------------------------------------------------------------
void CGameWnd::SetSkewingOnKey()
... {
memset(
&m_ptPlaneSkewing,0,sizeof(m_ptPlaneSkewing));
if(m_KeyStatus.bPushKeyLeft==TRUE&&m_KeyStatus.bPushKeyRight!=TRUE)
...{
m_ptPlaneSkewing.x
=-1*abs(m_Setting.iPlaneMoveDistance);
}


if(m_KeyStatus.bPushKeyRight==TRUE&&m_KeyStatus.bPushKeyLeft!=TRUE)
...{
m_ptPlaneSkewing.x
=abs(m_Setting.iPlaneMoveDistance);
}


if(m_KeyStatus.bPushKeyUp==TRUE&&m_KeyStatus.bPushKeyDown!=TRUE)
...{
m_ptPlaneSkewing.y
=-1*abs(m_Setting.iPlaneMoveDistance);
}


if(m_KeyStatus.bPushKeyDown==TRUE&&m_KeyStatus.bPushKeyUp!=TRUE)
...{
m_ptPlaneSkewing.y
=abs(m_Setting.iPlaneMoveDistance);
}

}



// ----------------------------------------------------------------
// Description:
// OnmessageWM_CREATE
// -----------------------------------------------------------------
void CGameWnd::OnCreate(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
... {
InitCommonControls();
m_hWndCB
=CommandBar_Create(m_hInst,hWnd,ID_COMMANDBAR);
CommandBar_InsertMenubar(m_hWndCB,m_hInst,IDM_MAIN,
0);



}



// ----------------------------------------------------------------
// Description:
// OnthemenucommandIDM_LEVEL_X
// -----------------------------------------------------------------
void CGameWnd::OnMenuLevel( int iLevel)
... {
switch(iLevel)
...{
caseIDM_LEVEL_1:
m_Setting.iBulletCount
=LEVEL1_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance
=LEVEL1_BULLET_MAXMOVEDISTANCE;
m_Setting.iPlaneMoveDistance
=LEVEL1_PLANE_MOVEDISTANCE;
break;
caseIDM_LEVEL_2:
m_Setting.iBulletCount
=LEVEL2_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance
=LEVEL2_BULLET_MAXMOVEDISTANCE;
m_Setting.iPlaneMoveDistance
=LEVEL2_PLANE_MOVEDISTANCE;
break;
caseIDM_LEVEL_3:
m_Setting.iBulletCount
=LEVEL3_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance
=LEVEL3_BULLET_MAXMOVEDISTANCE;
m_Setting.iPlaneMoveDistance
=LEVEL3_PLANE_MOVEDISTANCE;
break;
}


CheckMenu();
EndGame();
StartGame();
}



// ----------------------------------------------------------------
// Description:
// Checkthemenu
// -----------------------------------------------------------------
void CGameWnd::CheckMenu()
... {
HMENUhMenu
=CommandBar_GetMenu(m_hWndCB,0);

//Uncheckotheritems
CheckMenuItem(hMenu,IDM_LEVEL_1,MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(hMenu,IDM_LEVEL_2,MF_UNCHECKED
|MF_BYCOMMAND);
CheckMenuItem(hMenu,IDM_LEVEL_3,MF_UNCHECKED
|MF_BYCOMMAND);

//Usethecountofbulletsasflag
switch(m_Setting.iBulletCount)
...{
caseLEVEL1_BULLET_COUNT:
CheckMenuItem(hMenu,IDM_LEVEL_1,MF_CHECKED
|MF_BYCOMMAND);
break;
caseLEVEL2_BULLET_COUNT:
CheckMenuItem(hMenu,IDM_LEVEL_2,MF_CHECKED
|MF_BYCOMMAND);
break;
caseLEVEL3_BULLET_COUNT:
CheckMenuItem(hMenu,IDM_LEVEL_3,MF_CHECKED
|MF_BYCOMMAND);
break;
}




}


// Plane.h:interfacefortheCPlaneclass.
//
/**/ //
#ifndefPLANE_H
#define PLANE_H



class CPlane
... {
public:
voidInitialize(constRECT*lprcWndPlay);
voidGetCurrentPos(LPPOINTlpptOut);
voidMove(intiX,intiY);
voidDrawDestroy(HDChdc);
voidGetCurrentRect(RECT*lprcOut);
voidSetCurrentPos(constLPPOINTlppt);
voidDrawNormal(HDChdc);
CPlane();
virtual~CPlane();

protected:
POINTm_ptPos;
RECTm_rcWndPlay;
}
;


#endif // #ifndefPLANE_H

// Plane.cpp:implementationoftheCPlaneclass.
//
/**/ //

#include
" stdafx.h "
#include
" Plane.h "

// ------------------------------------------------------------------
// Macrodefine

// Theradiusoftheplane
#define PLANE_RADIUS4

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

CPlane::CPlane()
... {
memset(
&m_ptPos,0,sizeof(m_ptPos));
memset(
&m_rcWndPlay,0,sizeof(m_rcWndPlay));
}


CPlane::
~ CPlane()
... {

}



// --------------------------------------------------------------------
// Description:
// DrawtheplanenormalstatustotheDC
// ---------------------------------------------------------------------
void CPlane::DrawNormal(HDChdc)
... {
HBRUSHhBrush
=CreateSolidBrush(RGB(0,255,0));
HGDIOBJhOldSel
=SelectObject(hdc,hBrush);

Ellipse(hdc,
m_ptPos.x
-PLANE_RADIUS,
m_ptPos.y
-PLANE_RADIUS,
m_ptPos.x
+PLANE_RADIUS,
m_ptPos.y
+PLANE_RADIUS);


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



// --------------------------------------------------------------------
// Description:
// DrawtheplanedestroystatustotheDC
// ---------------------------------------------------------------------
void CPlane::DrawDestroy(HDChdc)
... {
HBRUSHhBrush
=CreateSolidBrush(RGB(255,0,0));
HGDIOBJhOldSel
=SelectObject(hdc,hBrush);

Ellipse(hdc,
m_ptPos.x
-PLANE_RADIUS,
m_ptPos.y
-PLANE_RADIUS,
m_ptPos.x
+PLANE_RADIUS,
m_ptPos.y
+PLANE_RADIUS);


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

// --------------------------------------------------------------------
// Description:
// Setcurrentposition
// ---------------------------------------------------------------------
void CPlane::SetCurrentPos( const LPPOINTlppt)
... {
m_ptPos
=*lppt;
}



// --------------------------------------------------------------------
// Description:
// Getthecurrentrectofplane
// ---------------------------------------------------------------------
void CPlane::GetCurrentRect(RECT * lprcOut)
... {
lprcOut
->left=m_ptPos.x-PLANE_RADIUS;
lprcOut
->top=m_ptPos.y-PLANE_RADIUS;
lprcOut
->right=m_ptPos.x+PLANE_RADIUS;
lprcOut
->bottom=m_ptPos.y+PLANE_RADIUS;
}



// --------------------------------------------------------------------
// Description:
// Movethedistancebaseonthecurrentposition
// ---------------------------------------------------------------------
void CPlane::Move( int iX, int iY)
... {
m_ptPos.x
+=iX;
m_ptPos.y
+=iY;

if(m_ptPos.x<m_rcWndPlay.left)
...{
m_ptPos.x
=0;
}


if(m_ptPos.x>m_rcWndPlay.right)
...{
m_ptPos.y
=m_rcWndPlay.right;
}


if(m_ptPos.y<m_rcWndPlay.top)
...{
m_ptPos.y
=m_rcWndPlay.top;
}


if(m_ptPos.y>m_rcWndPlay.bottom)
...{
m_ptPos.y
=m_rcWndPlay.bottom;
}

}



// --------------------------------------------------------------------
// Description:
// Getthecurrentpositionofplane
// ---------------------------------------------------------------------
void CPlane::GetCurrentPos(LPPOINTlpptOut)
... {
*lpptOut=m_ptPos;
}



// --------------------------------------------------------------------
// Description:
// Initializetheplayingwindow
// ---------------------------------------------------------------------
void CPlane::Initialize( const RECT * lprcWndPlay)
... {
m_rcWndPlay
=*lprcWndPlay;
}

// Evade.cpp:Definestheentrypointfortheapplication.
//

#include
" stdafx.h "
#include
" GameWnd.h "





int WINAPIWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPTSTRlpCmdLine,
int nCmdShow)
... {
//TODO:Placecodehere.

CGameWnd
*pGameWnd=CGameWnd::GetInstance();
if(pGameWnd==NULL)
...{
return0x05;
}


if(pGameWnd->Initialize(hInstance)==FALSE)
...{
return0x10;
}


pGameWnd
->ShowWindow(TRUE);



MSGmsg;
while(GetMessage(&msg,NULL,0,0))
...{
TranslateMessage(
&msg);
DispatchMessage(
&msg);
}


return0;
}



注:代码出现的CText类参见我这篇文章:http://blog.csdn.net/norains/archive/2007/04/17/1568429.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值