//========================================================================
//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.
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#ifndef BULLETS_H
#define BULLETS_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CBullets
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
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();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef struct
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
LONG x;
LONG y;
int iMoveDistX;
int iMoveDistY;
}BULLETDATA,*LPBULLETDATA;
LPBULLETDATA lpBullet; //Pointer to the bullet
void InitializeBullet(LPBULLETDATA lpBullet);
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#endif // #ifndef BULLETS_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Bullets.cpp: implementation of the CBullets class.
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
#include "Bullets.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//-------------------------------------------------------------------
//Macro define
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//The radius of the bullet
#define BULLET_RADIUS 2
//The color of the bullet
#define BULLET_COLOR RGB(0,0,0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CBullets::CBullets()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
m_iCount = 0;
m_iMaxMoveDistance = 0;
lpBullet = NULL;
memset(&m_rcWnd,0,sizeof(m_rcWnd));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Initialize the critical section
InitializeCriticalSection(&m_csBulletData);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CBullets::~CBullets()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
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)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
m_iCount = iCount;
m_rcWnd = *prcWnd;
m_iMaxMoveDistance = iMaxMoveDistance;
lpBullet = new BULLETDATA[m_iCount];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(lpBullet == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return FALSE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Set the seed for the AverageRandom() function
srand(GetTickCount());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i = 0; i < m_iCount; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
InitializeBullet(&lpBullet[i]);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return TRUE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Initialize the single bullets position
//--------------------------------------------------------------------
void CBullets::InitializeBullet(LPBULLETDATA lpBullet)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
//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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//The bullet must begin in the edge
if(iRandom == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->x = m_rcWnd.left;
lpBullet->y = (int)AverageRandom(m_rcWnd.top,m_rcWnd.bottom);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Set the move direction
lpBullet->iMoveDistX = 1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistY = 1;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistY = -1;
}
}
else if(iRandom == 2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->x = m_rcWnd.right;
lpBullet->y = (int)AverageRandom(m_rcWnd.top,m_rcWnd.bottom);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Set the move direction
lpBullet->iMoveDistX = -1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistY = 1;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistY = -1;
}
}
else if(iRandom == 3)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->x = (int)AverageRandom(m_rcWnd.left,m_rcWnd.right);
lpBullet->y = m_rcWnd.top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Set the move direction
lpBullet->iMoveDistY = 1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistX = 1;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistX = -1;
}
}
else if(iRandom == 4)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->x = (int)AverageRandom(m_rcWnd.left,m_rcWnd.right);
lpBullet->y = m_rcWnd.bottom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Set the move direction
lpBullet->iMoveDistY = -1;
int iDirection = (int)AverageRandom(1,3);
if(iDirection == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistX = 1;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
lpBullet->iMoveDistX = -1;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Set the move distance
iRandom = (int)AverageRandom(1,m_iMaxMoveDistance);
lpBullet->iMoveDistX *= iRandom;
iRandom = (int)AverageRandom(1,m_iMaxMoveDistance);
lpBullet->iMoveDistY *= iRandom;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//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)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
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 ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Move the bullets
//---------------------------------------------------------------------
void CBullets::Move()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
EnterCriticalSection(&m_csBulletData);
for(int i = 0; i < m_iCount; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
InitializeBullet(&lpBullet[i]);
}
}
LeaveCriticalSection(&m_csBulletData);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Draw the bullet to the DC
//---------------------------------------------------------------------
void CBullets::Draw(HDC hdc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
HBRUSH hBrush = CreateSolidBrush(BULLET_COLOR);
HGDIOBJ hOldSel = SelectObject(hdc,hBrush);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
RECT rcBullet = ...{0};
for(int i = 0; i < m_iCount; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SelectObject(hdc,hOldSel);
DeleteObject(hBrush);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Destroy the bullet
//---------------------------------------------------------------------
void CBullets::Destroy()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
if(lpBullet != NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
delete [] lpBullet;
lpBullet = NULL;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Check the collision
//
//Return Values:
// TRUE: Collided .
// FALSE: No collision
//---------------------------------------------------------------------
BOOL CBullets::CheckCollision(const RECT rcArea)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
BOOL bCollide = FALSE;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EnterCriticalSection(&m_csBulletData);
for(int i = 0; i < m_iCount; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(lpBullet[i].x >= rcArea.left && lpBullet[i].x <= rcArea.right && lpBullet[i].y >= rcArea.top && lpBullet[i].y <= rcArea.bottom)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
bCollide = TRUE;
break;
}
}
LeaveCriticalSection(&m_csBulletData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return bCollide;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// GameWnd.h: interface for the CGameWnd class.
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////
#ifndef GAMEWND_H
#define GAMEWND_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "Bullets.h"
#include "Plane.h"
#include "Text.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CGameWnd
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public:
BOOL ShowWindow(BOOL bShow);
BOOL Initialize(HINSTANCE hInst);
static CGameWnd * GetInstance();
virtual ~CGameWnd();
protected:
void CheckMenu();
void OnMenuLevel(int iLevel);
void OnCreate(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void SetSkewingOnKey();
void OnKeyUp(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);
void OnKeyDown(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);
void OnLButtonUp(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void OnLButtonDown(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void OnMouseMove(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void EndGame();
static DWORD WINAPI RefreshScreenThread(PVOID pArg);
void StartGame();
void OnPaint(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void OnDestroy(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
static LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
CGameWnd();
static CGameWnd * m_pInstance;
HINSTANCE m_hInst;
HWND m_hWnd;
RECT m_rcWndPlay; //The window to play the game, be base on the window scale
RECT m_rcWndMain; //The main window
CBullets m_Bullets;
CPlane m_Plane;
BOOL m_bMovePlane; //Move the plane or not
BOOL m_bCollide;
POINT m_ptPlaneSkewing; //The next point to moveto for the plane
HWND m_hWndCB; //Command bar
ULONG m_ulTimeCount; //The continue time count
CText m_TxtTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//The game setting data
typedef struct
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//For the plane
int iPlaneMoveDistance; //The distance to move per acttion
//For the bullet
int iBulletMaxMoveDistance; //The max distance to move
int iBulletCount; //The count of bullets
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//For the game
int iRefreshInterval; //The interval time to refresh screen
}SETTINGDATA,*LPSETTINGDATA;
SETTINGDATA m_Setting;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//The key pushed status
typedef struct
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
BOOL bPushKeyUp;
BOOL bPushKeyDown;
BOOL bPushKeyLeft;
BOOL bPushKeyRight;
}PUSHKEYSTATUS,*LPPUSHKEYSTATUS;
PUSHKEYSTATUS m_KeyStatus;
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#endif // #ifndef GAMEWND_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// GameWnd.cpp: implementation of the CGameWnd class.
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
#include "GameWnd.h"
#include "resource.h"
#include "commctrl.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Macro define
#define WND_CLASS TEXT("Evade_Class")
#define WND_TITLE TEXT("Evade_Title")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//The ID for the command bar
#define ID_COMMANDBAR 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Screen width
#define SCREEN_WIDTH GetSystemMetrics(SM_CXSCREEN)
//Screen height
#define SCREEN_HEIGHT GetSystemMetrics(SM_CYSCREEN)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//The window position
#define MAINWND_POS_X 0
#define MAINWND_POS_Y 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Level_1 value
#define LEVEL1_BULLET_COUNT 100
#define LEVEL1_BULLET_MAXMOVEDISTANCE 3
#define LEVEL1_PLANE_MOVEDISTANCE 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Level_2 value
#define LEVEL2_BULLET_COUNT 150
#define LEVEL2_BULLET_MAXMOVEDISTANCE 4
#define LEVEL2_PLANE_MOVEDISTANCE 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Level_3 value
#define LEVEL3_BULLET_COUNT 200
#define LEVEL3_BULLET_MAXMOVEDISTANCE 5
#define LEVEL3_PLANE_MOVEDISTANCE 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//Default value
#define DEFAULT_BULLET_COUNT LEVEL1_BULLET_COUNT
#define DEFAULT_BULLET_MAXMOVEDISTANCE LEVEL1_BULLET_MAXMOVEDISTANCE
#define DEFAULT_PLANE_MOVEDISTANCE LEVEL1_PLANE_MOVEDISTANCE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define DEFAULT_REFRESH_INTERVAL 50 //0.05s
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define DEFAULT_TEXT_TIME_COLOR RGB(0,0,255)
#define DEFAULT_TEXT_TIME_HEIGHT 16
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//The offset of the time text
#define TXT_TIME_OFFSET_TOP 2
#define TXT_TIME_OFFSET_LEFT (SCREEN_WIDTH - 100)
#define TXT_TIME_OFFSET_RIGHT 4
#define TXT_TIME_HEIGHT 40
//-----------------------------------------------------------------
//Initialize
CGameWnd *CGameWnd::m_pInstance = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CGameWnd::CGameWnd()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
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));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Setting.iBulletCount = DEFAULT_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance = DEFAULT_BULLET_MAXMOVEDISTANCE;
m_Setting.iRefreshInterval = DEFAULT_REFRESH_INTERVAL;
m_Setting.iPlaneMoveDistance = DEFAULT_PLANE_MOVEDISTANCE;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_TxtTime.SetTextColor(DEFAULT_TEXT_TIME_COLOR);
m_TxtTime.SetTextHeight(DEFAULT_TEXT_TIME_HEIGHT);
m_TxtTime.SetFormat(DT_RIGHT | DT_VCENTER);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CGameWnd::~CGameWnd()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
if(m_pInstance != NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
delete m_pInstance;
m_pInstance = NULL;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// Get the object instance
//-----------------------------------------------------------------
CGameWnd * CGameWnd::GetInstance()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
if(m_pInstance == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_pInstance = new CGameWnd();
}
return m_pInstance;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// Initialize the window
//-----------------------------------------------------------------
BOOL CGameWnd::Initialize(HINSTANCE hInst)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
m_hInst = hInst;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WNDCLASS ws;
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)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return FALSE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Find the task bar
HWND hWndTaskBar = FindWindow(TEXT("HHTaskBar"), NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
RECT rcTaskBar = ...{0};
GetWindowRect(hWndTaskBar,&rcTaskBar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return FALSE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
RECT rcCmdBar = ...{0};
GetWindowRect(m_hWndCB,&rcCmdBar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RECT rcTxtTime;
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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CheckMenu();
return TRUE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// The window process
//-----------------------------------------------------------------
LRESULT CGameWnd::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
switch(wMsg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case WM_DESTROY:
m_pInstance->OnDestroy(hWnd,wMsg,wParam,lParam);
return 0;
case WM_PAINT:
m_pInstance->OnPaint(hWnd,wMsg,wParam,lParam);
return 0;
case WM_ERASEBKGND:
//Needn't to redraw the background
return 0;
case WM_MOUSEMOVE:
m_pInstance->OnMouseMove(hWnd,wMsg,wParam,lParam);
return 0;
case WM_LBUTTONDOWN:
m_pInstance->OnLButtonDown(hWnd,wMsg,wParam,lParam);
return 0;
case WM_LBUTTONUP:
m_pInstance->OnLButtonUp(hWnd,wMsg,wParam,lParam);
return 0;
case WM_KEYDOWN:
m_pInstance->OnKeyDown(hWnd,wMsg,wParam,lParam);
return 0;
case WM_KEYUP:
m_pInstance->OnKeyUp(hWnd,wMsg,wParam,lParam);
return 0;
case WM_CREATE:
m_pInstance->OnCreate(hWnd,wMsg,wParam,lParam);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case IDM_LEVEL_1:
m_pInstance->OnMenuLevel(IDM_LEVEL_1);
return 0;
case IDM_LEVEL_2:
m_pInstance->OnMenuLevel(IDM_LEVEL_2);
return 0;
case IDM_LEVEL_3:
m_pInstance->OnMenuLevel(IDM_LEVEL_3);
return 0;
case IDM_START:
m_pInstance->StartGame();
return 0;
case IDM_EXIT:
DestroyWindow(hWnd);
return 0;
}
break;
}
return DefWindowProc(hWnd,wMsg,wParam,lParam);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// Show the window
//-----------------------------------------------------------------
BOOL CGameWnd::ShowWindow(BOOL bShow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
if(m_hWnd == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return FALSE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(bShow == TRUE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
::ShowWindow(m_hWnd,SW_SHOW);
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
::ShowWindow(m_hWnd,SW_HIDE);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return TRUE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_DESTROY
//-----------------------------------------------------------------
void CGameWnd::OnDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
PostQuitMessage(0x00);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_PAINT
//-----------------------------------------------------------------
void CGameWnd::OnPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd,&ps);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create the memory DC
HBITMAP hBitmap = CreateCompatibleBitmap(hdc,m_rcWndMain.right - m_rcWndMain.left,m_rcWndMain.bottom - m_rcWndMain.top);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HDC hdcMem = CreateCompatibleDC(hdc);
HGDIOBJ hOldSel = SelectObject(hdcMem,hBitmap);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Rectangle(hdcMem,m_rcWndMain.left,m_rcWndMain.top,m_rcWndMain.right,m_rcWndMain.bottom);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Bullets.Draw(hdcMem);
if(m_bCollide == FALSE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_Plane.DrawNormal(hdcMem);
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_Plane.DrawDestroy(hdcMem);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_TxtTime.Draw(hdcMem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BitBlt(hdc,
m_rcWndMain.left,
m_rcWndMain.top,
m_rcWndMain.right - m_rcWndMain.left,
m_rcWndMain.bottom - m_rcWndMain.top,
hdcMem,
0,
0,
SRCCOPY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SelectObject(hdcMem,hOldSel);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
EndPaint(hWnd,&ps);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// Start the game
//-----------------------------------------------------------------
void CGameWnd::StartGame()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
m_bCollide = FALSE;
m_ulTimeCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//The plane
m_Plane.Initialize(&m_rcWndPlay);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
POINT ptPos = ...{0};
ptPos.x = (m_rcWndPlay.right - m_rcWndPlay.left)/2;
ptPos.y = (m_rcWndPlay.bottom - m_rcWndPlay.top)/2;
m_Plane.SetCurrentPos(&ptPos);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//The bullets
m_Bullets.Initialize(m_Setting.iBulletCount,m_Setting.iBulletMaxMoveDistance,&m_rcWndPlay);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HANDLE hdThrd;
DWORD dwID;
hdThrd = CreateThread(NULL,NULL,RefreshScreenThread,NULL,NULL,&dwID);
CloseHandle(hdThrd);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// End the game
//-----------------------------------------------------------------
void CGameWnd::EndGame()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
InvalidateRect(m_hWnd,&m_rcWndPlay,TRUE);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// Refresh the screen
//-----------------------------------------------------------------
DWORD WINAPI CGameWnd::RefreshScreenThread(PVOID pArg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
DWORD dwResult = 0;
while(TRUE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//Move the bullets
m_pInstance->m_Bullets.Move();
//Move the plane
m_pInstance->m_Plane.Move(m_pInstance->m_ptPlaneSkewing.x,m_pInstance->m_ptPlaneSkewing.y);
//Check collision
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
RECT rcPlane = ...{0};
m_pInstance->m_Plane.GetCurrentRect(&rcPlane);
m_pInstance->m_bCollide = m_pInstance->m_Bullets.CheckCollision(rcPlane);
if(m_pInstance->m_bCollide == TRUE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_pInstance->EndGame();
break;
}
m_pInstance->m_ulTimeCount += m_pInstance->m_Setting.iRefreshInterval;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
TCHAR szTime[80] = ...{0};
_stprintf(szTime,TEXT("%dms"),m_pInstance->m_ulTimeCount);
m_pInstance->m_TxtTime.SetText(szTime);
//Refresh the screen
InvalidateRect(m_pInstance->m_hWnd,&m_pInstance->m_rcWndPlay,TRUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Sleep(m_pInstance->m_Setting.iRefreshInterval);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_MOUSEMOVE
//-----------------------------------------------------------------
void CGameWnd::OnMouseMove(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
if(m_bMovePlane == FALSE)
{
return ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int iPosX = LOWORD(lParam);
int iPosY = HIWORD(lParam);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
POINT ptPlaneCurPos = {0};
m_Plane.GetCurrentPos(&ptPlaneCurPos);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//The distance from current plane position to the current mouse position
double dDistance = 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;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Set the direction
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;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(ptPlaneCurPos.y < m_rcWndPlay.top || ptPlaneCurPos.y > m_rcWndPlay.bottom)
{
m_ptPlaneSkewing.y = 0;
}
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_LBUTTONDOWN
//-----------------------------------------------------------------
void CGameWnd::OnLButtonDown(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
SetCapture(m_hWnd);
m_bMovePlane = TRUE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_LBUTTONUP
//-----------------------------------------------------------------
void CGameWnd::OnLButtonUp(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
ReleaseCapture();
m_bMovePlane = FALSE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_KEYDOWN
//-----------------------------------------------------------------
void CGameWnd::OnKeyDown(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
int iKey = (int) wParam;
switch(iKey)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case VK_UP:
m_KeyStatus.bPushKeyUp = TRUE;
break;
case VK_DOWN:
m_KeyStatus.bPushKeyDown = TRUE;
break;
case VK_LEFT:
m_KeyStatus.bPushKeyLeft = TRUE;
break;
case VK_RIGHT:
m_KeyStatus.bPushKeyRight = TRUE;
break;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetSkewingOnKey();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_KEYUP
//-----------------------------------------------------------------
void CGameWnd::OnKeyUp(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
int iKey = (int) wParam;
switch(iKey)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case VK_UP:
m_KeyStatus.bPushKeyUp = FALSE;
break;
case VK_DOWN:
m_KeyStatus.bPushKeyDown = FALSE;
break;
case VK_LEFT:
m_KeyStatus.bPushKeyLeft = FALSE;
break;
case VK_RIGHT:
m_KeyStatus.bPushKeyRight = FALSE;
break;
}
SetSkewingOnKey();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// Set the skewing base on the key status
//-----------------------------------------------------------------
void CGameWnd::SetSkewingOnKey()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
memset(&m_ptPlaneSkewing,0,sizeof(m_ptPlaneSkewing));
if(m_KeyStatus.bPushKeyLeft == TRUE && m_KeyStatus.bPushKeyRight != TRUE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPlaneSkewing.x = -1 * abs(m_Setting.iPlaneMoveDistance);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_KeyStatus.bPushKeyRight == TRUE && m_KeyStatus.bPushKeyLeft != TRUE )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPlaneSkewing.x = abs(m_Setting.iPlaneMoveDistance);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_KeyStatus.bPushKeyUp == TRUE && m_KeyStatus.bPushKeyDown != TRUE )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPlaneSkewing.y = -1 * abs(m_Setting.iPlaneMoveDistance);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_KeyStatus.bPushKeyDown == TRUE && m_KeyStatus.bPushKeyUp != TRUE )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPlaneSkewing.y = abs(m_Setting.iPlaneMoveDistance);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On message WM_CREATE
//-----------------------------------------------------------------
void CGameWnd::OnCreate(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
InitCommonControls();
m_hWndCB = CommandBar_Create(m_hInst,hWnd,ID_COMMANDBAR);
CommandBar_InsertMenubar(m_hWndCB,m_hInst,IDM_MAIN,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// On the menu command IDM_LEVEL_X
//-----------------------------------------------------------------
void CGameWnd::OnMenuLevel(int iLevel)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
switch(iLevel)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case IDM_LEVEL_1:
m_Setting.iBulletCount = LEVEL1_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance = LEVEL1_BULLET_MAXMOVEDISTANCE;
m_Setting.iPlaneMoveDistance = LEVEL1_PLANE_MOVEDISTANCE;
break;
case IDM_LEVEL_2:
m_Setting.iBulletCount = LEVEL2_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance = LEVEL2_BULLET_MAXMOVEDISTANCE;
m_Setting.iPlaneMoveDistance = LEVEL2_PLANE_MOVEDISTANCE;
break;
case IDM_LEVEL_3:
m_Setting.iBulletCount = LEVEL3_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance = LEVEL3_BULLET_MAXMOVEDISTANCE;
m_Setting.iPlaneMoveDistance = LEVEL3_PLANE_MOVEDISTANCE;
break;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CheckMenu();
EndGame();
StartGame();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//----------------------------------------------------------------
//Description:
// Check the menu
//-----------------------------------------------------------------
void CGameWnd::CheckMenu()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
HMENU hMenu = CommandBar_GetMenu(m_hWndCB,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Uncheck other items
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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Use the count of bullets as flag
switch(m_Setting.iBulletCount)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case LEVEL1_BULLET_COUNT:
CheckMenuItem(hMenu,IDM_LEVEL_1,MF_CHECKED|MF_BYCOMMAND);
break;
case LEVEL2_BULLET_COUNT:
CheckMenuItem(hMenu,IDM_LEVEL_2,MF_CHECKED|MF_BYCOMMAND);
break;
case LEVEL3_BULLET_COUNT:
CheckMenuItem(hMenu,IDM_LEVEL_3,MF_CHECKED|MF_BYCOMMAND);
break;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Plane.h: interface for the CPlane class.
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////
#ifndef PLANE_H
#define PLANE_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CPlane
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public:
void Initialize(const RECT *lprcWndPlay);
void GetCurrentPos(LPPOINT lpptOut);
void Move(int iX,int iY);
void DrawDestroy(HDC hdc);
void GetCurrentRect(RECT * lprcOut);
void SetCurrentPos(const LPPOINT lppt);
void DrawNormal(HDC hdc);
CPlane();
virtual ~CPlane();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
POINT m_ptPos;
RECT m_rcWndPlay;
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#endif // #ifndef PLANE_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Plane.cpp: implementation of the CPlane class.
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
#include "Plane.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//------------------------------------------------------------------
//Macro define
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//The radius of the plane
#define PLANE_RADIUS 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CPlane::CPlane()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
memset(&m_ptPos,0,sizeof(m_ptPos));
memset(&m_rcWndPlay,0,sizeof(m_rcWndPlay));
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CPlane::~CPlane()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Draw the plane normal status to the DC
//---------------------------------------------------------------------
void CPlane::DrawNormal(HDC hdc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));
HGDIOBJ hOldSel = SelectObject(hdc,hBrush);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Ellipse(hdc,
m_ptPos.x - PLANE_RADIUS,
m_ptPos.y - PLANE_RADIUS,
m_ptPos.x + PLANE_RADIUS,
m_ptPos.y + PLANE_RADIUS);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SelectObject(hdc,hOldSel);
DeleteObject(hBrush);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Draw the plane destroy status to the DC
//---------------------------------------------------------------------
void CPlane::DrawDestroy(HDC hdc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
HBRUSH hBrush = CreateSolidBrush(RGB(255,0,0));
HGDIOBJ hOldSel = SelectObject(hdc,hBrush);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Ellipse(hdc,
m_ptPos.x - PLANE_RADIUS,
m_ptPos.y - PLANE_RADIUS,
m_ptPos.x + PLANE_RADIUS,
m_ptPos.y + PLANE_RADIUS);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SelectObject(hdc,hOldSel);
DeleteObject(hBrush);
}
//--------------------------------------------------------------------
//Description:
// Set current position
//---------------------------------------------------------------------
void CPlane::SetCurrentPos(const LPPOINT lppt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
m_ptPos = *lppt;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Get the current rect of plane
//---------------------------------------------------------------------
void CPlane::GetCurrentRect(RECT *lprcOut)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
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;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Move the distance base on the current position
//---------------------------------------------------------------------
void CPlane::Move(int iX, int iY)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
m_ptPos.x += iX;
m_ptPos.y += iY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_ptPos.x < m_rcWndPlay.left)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPos.x = 0;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_ptPos.x > m_rcWndPlay.right)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPos.y = m_rcWndPlay.right;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_ptPos.y < m_rcWndPlay.top)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPos.y = m_rcWndPlay.top;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(m_ptPos.y > m_rcWndPlay.bottom)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
m_ptPos.y = m_rcWndPlay.bottom;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Get the current position of plane
//---------------------------------------------------------------------
void CPlane::GetCurrentPos(LPPOINT lpptOut)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
*lpptOut = m_ptPos;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//--------------------------------------------------------------------
//Description:
// Initialize the playing window
//---------------------------------------------------------------------
void CPlane::Initialize(const RECT *lprcWndPlay)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
m_rcWndPlay = *lprcWndPlay;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// Evade.cpp : Defines the entry point for the application.
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
#include "GameWnd.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
// TODO: Place code here.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CGameWnd *pGameWnd = CGameWnd::GetInstance();
if(pGameWnd == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return 0x05;
}
if(pGameWnd->Initialize(hInstance) == FALSE)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return 0x10;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pGameWnd->ShowWindow(TRUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MSG msg;
while(GetMessage(&msg,NULL,0,0))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
注:代码出现的CText类参见我这篇文章:http://blog.csdn.net/norains/archive/2007/04/17/1568429.aspx