//========================================================================
//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
#defineBULLETS_H
classCBullets
...{
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
typedefstruct
...{
LONGx;
LONGy;
intiMoveDistX;
intiMoveDistY;
}BULLETDATA,*LPBULLETDATA;
LPBULLETDATAlpBullet;//Pointertothebullet
voidInitializeBullet(LPBULLETDATAlpBullet);
};
#endif//#ifndefBULLETS_H
//Bullets.cpp:implementationoftheCBulletsclass.
//
/**///////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"Bullets.h"

//-------------------------------------------------------------------
//Macrodefine
//Theradiusofthebullet
#defineBULLET_RADIUS2
//Thecolorofthebullet
#defineBULLET_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(intiCount,intiMaxMoveDistance,constRECT*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
//--------------------------------------------------------------------
voidCBullets::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
//--------------------------------------------------------------------
doubleCBullets::AverageRandom(doubledMin,doubledMax)
...{
intiMin=(int)(dMin*10000);
intiMax=(int)(dMax*10000);
intiRand=rand()*rand();
intiDiff=iMax-iMin;
doubledResult=(iRand%iDiff+iMin)/10000.0;
returndResult;
}


//--------------------------------------------------------------------
//Description:
//Movethebullets
//---------------------------------------------------------------------
voidCBullets::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
//---------------------------------------------------------------------
voidCBullets::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
//---------------------------------------------------------------------
voidCBullets::Destroy()
...{
if(lpBullet!=NULL)
...{
delete[]lpBullet;
lpBullet=NULL;
}
}


//--------------------------------------------------------------------
//Description:
//Checkthecollision
//
//ReturnValues:
//TRUE:Collided.
//FALSE:Nocollision
//---------------------------------------------------------------------
BOOLCBullets::CheckCollision(constRECTrcArea)
...{
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
#defineGAMEWND_H


#include"Bullets.h"
#include"Plane.h"
#include"Text.h"
classCGameWnd
...{
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
#defineWND_CLASSTEXT("Evade_Class")
#defineWND_TITLETEXT("Evade_Title")
//TheIDforthecommandbar
#defineID_COMMANDBAR100
//Screenwidth
#defineSCREEN_WIDTHGetSystemMetrics(SM_CXSCREEN)
//Screenheight
#defineSCREEN_HEIGHTGetSystemMetrics(SM_CYSCREEN)

//Thewindowposition
#defineMAINWND_POS_X0
#defineMAINWND_POS_Y0

//Level_1value
#defineLEVEL1_BULLET_COUNT100
#defineLEVEL1_BULLET_MAXMOVEDISTANCE3
#defineLEVEL1_PLANE_MOVEDISTANCE3
//Level_2value
#defineLEVEL2_BULLET_COUNT150
#defineLEVEL2_BULLET_MAXMOVEDISTANCE4
#defineLEVEL2_PLANE_MOVEDISTANCE3
//Level_3value
#defineLEVEL3_BULLET_COUNT200
#defineLEVEL3_BULLET_MAXMOVEDISTANCE5
#defineLEVEL3_PLANE_MOVEDISTANCE3


//Defaultvalue
#defineDEFAULT_BULLET_COUNTLEVEL1_BULLET_COUNT
#defineDEFAULT_BULLET_MAXMOVEDISTANCELEVEL1_BULLET_MAXMOVEDISTANCE
#defineDEFAULT_PLANE_MOVEDISTANCELEVEL1_PLANE_MOVEDISTANCE
#defineDEFAULT_REFRESH_INTERVAL50//0.05s
#defineDEFAULT_TEXT_TIME_COLORRGB(0,0,255)
#defineDEFAULT_TEXT_TIME_HEIGHT16
//Theoffsetofthetimetext
#defineTXT_TIME_OFFSET_TOP2
#defineTXT_TIME_OFFSET_LEFT(SCREEN_WIDTH-100)
#defineTXT_TIME_OFFSET_RIGHT4
#defineTXT_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
//-----------------------------------------------------------------
voidCGameWnd::OnDestroy(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
...{
PostQuitMessage(0x00);
}

//----------------------------------------------------------------
//Description:
//OnmessageWM_PAINT
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::OnLButtonDown(HWNDhWnd,UINTwMsg,WPARAMwParam,LPARAMlParam)
...{
SetCapture(m_hWnd);
m_bMovePlane=TRUE;
}

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

//----------------------------------------------------------------
//Description:
//OnmessageWM_KEYDOWN
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::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
//-----------------------------------------------------------------
voidCGameWnd::OnMenuLevel(intiLevel)
...{
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
//-----------------------------------------------------------------
voidCGameWnd::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
#definePLANE_H


classCPlane
...{
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
#definePLANE_RADIUS4

/**///////////////////////////////////////////////////////////////////////
//Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
CPlane::CPlane()
...{
memset(&m_ptPos,0,sizeof(m_ptPos));
memset(&m_rcWndPlay,0,sizeof(m_rcWndPlay));
}
CPlane::~CPlane()
...{
}

//--------------------------------------------------------------------
//Description:
//DrawtheplanenormalstatustotheDC
//---------------------------------------------------------------------
voidCPlane::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
//---------------------------------------------------------------------
voidCPlane::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
//---------------------------------------------------------------------
voidCPlane::SetCurrentPos(constLPPOINTlppt)
...{
m_ptPos=*lppt;
}

//--------------------------------------------------------------------
//Description:
//Getthecurrentrectofplane
//---------------------------------------------------------------------
voidCPlane::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
//---------------------------------------------------------------------
voidCPlane::Move(intiX,intiY)
...{
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
//---------------------------------------------------------------------
voidCPlane::GetCurrentPos(LPPOINTlpptOut)
...{
*lpptOut=m_ptPos;
}

//--------------------------------------------------------------------
//Description:
//Initializetheplayingwindow
//---------------------------------------------------------------------
voidCPlane::Initialize(constRECT*lprcWndPlay)
...{
m_rcWndPlay=*lprcWndPlay;
}
//Evade.cpp:Definestheentrypointfortheapplication.
//
#include"stdafx.h"
#include"GameWnd.h"




intWINAPIWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPTSTRlpCmdLine,
intnCmdShow)
...{
//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.youkuaiyun.com/norains/archive/2007/04/17/1568429.aspx
4076

被折叠的 条评论
为什么被折叠?



