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

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

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

// Bullets.h: interface for the CBullets class.
//
//////////////////////////////////////////////////////////////////////

#ifndef BULLETS_H
#define BULLETS_H

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

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

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

#endif // #ifndef BULLETS_H

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

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


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

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


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

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

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


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

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

    
if(lpBullet == NULL)
    
{
        
return FALSE;
    }


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

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


    
return TRUE;
}



//--------------------------------------------------------------------
//Description:
//    Initialize the single bullets position
//--------------------------------------------------------------------
void CBullets::InitializeBullet(LPBULLETDATA lpBullet)
{
    
//Because the return value of AverageRandom() is double type, 
    
//and chang the value to int. 
    
//If your using like that: AverageRandom(1,4);
    
//the number 4 is hard to create !
    int iRandom = (int)AverageRandom(1,5);


    
//The bullet must begin in the edge
    if(iRandom == 1)
    
{
        lpBullet
->= m_rcWnd.left;
        lpBullet
->= (int)AverageRandom(m_rcWnd.top,m_rcWnd.bottom);

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

        
else
        
{
            lpBullet
->iMoveDistY = -1;
        }

    }

    
else if(iRandom == 2)
    
{
        lpBullet
->= m_rcWnd.right;
        lpBullet
->= (int)AverageRandom(m_rcWnd.top,m_rcWnd.bottom);

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

        
else
        
{
            lpBullet
->iMoveDistY = -1;
        }

    }

    
else if(iRandom == 3)
    
{
        lpBullet
->= (int)AverageRandom(m_rcWnd.left,m_rcWnd.right);
        lpBullet
->= m_rcWnd.top;

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

        
else
        
{
            lpBullet
->iMoveDistX = -1;
        }

    }

    
else if(iRandom == 4)
    
{
        lpBullet
->= (int)AverageRandom(m_rcWnd.left,m_rcWnd.right);
        lpBullet
->= m_rcWnd.bottom;

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

        
else
        
{
            lpBullet
->iMoveDistX = -1;
        }

    }

    

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



//--------------------------------------------------------------------
//Description:
//    Create the random number.Before calling the method , you must set the seed 
//by using srand() function.
//
//Parameters:
//    dMin: [in] The min number
//    dMax: [in] The max number
//--------------------------------------------------------------------
double CBullets::AverageRandom(double dMin, double dMax)
{
    
int iMin = (int)(dMin * 10000);
    
int iMax = (int)(dMax * 10000);
    
int iRand = rand() * rand();
    
int iDiff = iMax - iMin;
    
double dResult = (iRand % iDiff + iMin) / 10000.0;
    
return dResult ;
}




//--------------------------------------------------------------------
//Description:
//    Move the bullets
//---------------------------------------------------------------------
void CBullets::Move()
{
    EnterCriticalSection(
&m_csBulletData);
    
for(int i = 0; i < m_iCount; i++)
    
{
        lpBullet[i].x 
+= lpBullet[i].iMoveDistX;
        lpBullet[i].y 
+= lpBullet[i].iMoveDistY;
            
        
if(lpBullet[i].x < m_rcWnd.left || lpBullet[i].x > m_rcWnd.right || lpBullet[i].y < m_rcWnd.top || lpBullet[i].y > m_rcWnd.bottom)
        
{
            InitializeBullet(
&lpBullet[i]);
        }

    }

    LeaveCriticalSection(
&m_csBulletData);
}



//--------------------------------------------------------------------
//Description:
//    Draw the bullet to the DC
//---------------------------------------------------------------------
void CBullets::Draw(HDC hdc)
{
    HBRUSH hBrush 
= CreateSolidBrush(BULLET_COLOR);
    HGDIOBJ hOldSel 
= SelectObject(hdc,hBrush);

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


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




//--------------------------------------------------------------------
//Description:
//    Destroy the bullet
//---------------------------------------------------------------------
void CBullets::Destroy()
{
    
if(lpBullet != NULL)
    
{
        delete [] lpBullet;
        lpBullet 
= NULL;
    }

}




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

    
    EnterCriticalSection(
&m_csBulletData);    
    
for(int i = 0; i < m_iCount; i++)
    
{
        
if(lpBullet[i].x >= rcArea.left && lpBullet[i].x <= rcArea.right && lpBullet[i].y >= rcArea.top && lpBullet[i].y <= rcArea.bottom)
        
{
            bCollide 
= TRUE;
            
break;
        }

    }

    LeaveCriticalSection(
&m_csBulletData);


    
return bCollide;
}


// GameWnd.h: interface for the CGameWnd class.
//
//////////////////////////////////////////////////////////////////////
#ifndef GAMEWND_H
#define GAMEWND_H



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

class CGameWnd  
{
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;

    
//The game setting data
    typedef struct
    
{
        
//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

        
//For the game 
        int iRefreshInterval;        //The interval time to refresh screen
    }
SETTINGDATA,*LPSETTINGDATA;
    SETTINGDATA m_Setting;

    
//The key pushed status
    typedef struct
    
{
        BOOL bPushKeyUp;
        BOOL bPushKeyDown;
        BOOL bPushKeyLeft;
        BOOL bPushKeyRight;
    }
PUSHKEYSTATUS,*LPPUSHKEYSTATUS;
    PUSHKEYSTATUS m_KeyStatus;
}
;



#endif // #ifndef GAMEWND_H


// GameWnd.cpp: implementation of the CGameWnd class.
//
//////////////////////////////////////////////////////////////////////

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

//----------------------------------------------------------------
//Macro define
#define WND_CLASS            TEXT("Evade_Class")
#define WND_TITLE            TEXT("Evade_Title")

//The ID for the command bar
#define ID_COMMANDBAR        100

//Screen width
#define SCREEN_WIDTH                GetSystemMetrics(SM_CXSCREEN)
//Screen height
#define SCREEN_HEIGHT                GetSystemMetrics(SM_CYSCREEN)


//The window position
#define MAINWND_POS_X                0
#define MAINWND_POS_Y                0


//Level_1 value
#define LEVEL1_BULLET_COUNT                    100
#define LEVEL1_BULLET_MAXMOVEDISTANCE        3
#define LEVEL1_PLANE_MOVEDISTANCE            3

//Level_2 value
#define LEVEL2_BULLET_COUNT                    150
#define LEVEL2_BULLET_MAXMOVEDISTANCE        4
#define LEVEL2_PLANE_MOVEDISTANCE            3

//Level_3 value
#define LEVEL3_BULLET_COUNT                    200
#define LEVEL3_BULLET_MAXMOVEDISTANCE        5
#define LEVEL3_PLANE_MOVEDISTANCE            3



//Default value
#define DEFAULT_BULLET_COUNT                LEVEL1_BULLET_COUNT
#define DEFAULT_BULLET_MAXMOVEDISTANCE        LEVEL1_BULLET_MAXMOVEDISTANCE
#define DEFAULT_PLANE_MOVEDISTANCE            LEVEL1_PLANE_MOVEDISTANCE

#define DEFAULT_REFRESH_INTERVAL            50 //0.05s

#define DEFAULT_TEXT_TIME_COLOR                RGB(0,0,255)
#define DEFAULT_TEXT_TIME_HEIGHT            16    

//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;


//////////////////////////////////////////////////////////////////////
// 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)
    
{
        delete m_pInstance;
        m_pInstance 
= NULL;
    }

}



//----------------------------------------------------------------
//Description:
//    Get the object instance
//-----------------------------------------------------------------
CGameWnd * CGameWnd::GetInstance()
{
    
if(m_pInstance == NULL)
    
{
        m_pInstance 
= new CGameWnd();
    }

    
return m_pInstance;
}



//----------------------------------------------------------------
//Description:
//    Initialize the window
//-----------------------------------------------------------------
BOOL CGameWnd::Initialize(HINSTANCE hInst)
{
    m_hInst 
= hInst;

    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)
    
{
        
return FALSE;
    }



    
//Find the task bar
    HWND hWndTaskBar = FindWindow(TEXT("HHTaskBar"), NULL);  
    RECT rcTaskBar 
= {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)
    
{
        
return FALSE;
    }


    
    RECT rcCmdBar 
= {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;

    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);

    CheckMenu();
    
    
return TRUE;
}



//----------------------------------------------------------------
//Description:
//    The window process
//-----------------------------------------------------------------
LRESULT CGameWnd::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    
switch(wMsg)
    
{
        
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))
            
{
                
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);
}



//----------------------------------------------------------------
//Description:
//    Show the window
//-----------------------------------------------------------------
BOOL CGameWnd::ShowWindow(BOOL bShow)
{
    
if(m_hWnd == NULL)
    
{
        
return FALSE;
    }


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

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



    
return TRUE;
}



//----------------------------------------------------------------
//Description:
//    On message WM_DESTROY
//-----------------------------------------------------------------
void CGameWnd::OnDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    PostQuitMessage(
0x00);
}



//----------------------------------------------------------------
//Description:
//    On message WM_PAINT
//-----------------------------------------------------------------
void CGameWnd::OnPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    
    PAINTSTRUCT ps;
    HDC hdc 
= BeginPaint(hWnd,&ps);

    
//Create the memory DC
    HBITMAP hBitmap = CreateCompatibleBitmap(hdc,m_rcWndMain.right - m_rcWndMain.left,m_rcWndMain.bottom - m_rcWndMain.top);

    HDC hdcMem 
= CreateCompatibleDC(hdc);
    HGDIOBJ hOldSel 
= 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:
//    Start the game
//-----------------------------------------------------------------
void CGameWnd::StartGame()
{
    m_bCollide 
= FALSE;
    m_ulTimeCount 
= 0;

    
//The plane
    m_Plane.Initialize(&m_rcWndPlay);
    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);

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

    HANDLE hdThrd;
    DWORD dwID;
    hdThrd 
= CreateThread(NULL,NULL,RefreshScreenThread,NULL,NULL,&dwID);
    CloseHandle(hdThrd);
}



//----------------------------------------------------------------
//Description:
//    End the game
//-----------------------------------------------------------------
void CGameWnd::EndGame()
{
    InvalidateRect(m_hWnd,
&m_rcWndPlay,TRUE);
}



//----------------------------------------------------------------
//Description:
//    Refresh the screen
//-----------------------------------------------------------------
DWORD WINAPI CGameWnd::RefreshScreenThread(PVOID pArg)
{
    DWORD dwResult 
= 0;
    
while(TRUE)
    
{
        
        
//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
        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)
        
{
            m_pInstance
->EndGame();
            
break;
        }

        
        m_pInstance
->m_ulTimeCount += m_pInstance->m_Setting.iRefreshInterval;
        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);

        Sleep(m_pInstance
->m_Setting.iRefreshInterval);
        
    }


    
return 0;
}




//----------------------------------------------------------------
//Description:
//    On message WM_MOUSEMOVE
//-----------------------------------------------------------------
void CGameWnd::OnMouseMove(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    
/*
    if(m_bMovePlane == FALSE)
    {
        return ;
    }


    int iPosX = LOWORD(lParam); 
    int iPosY = HIWORD(lParam);

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

    //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;
    }

    //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;
    }


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


}




//----------------------------------------------------------------
//Description:
//    On message WM_LBUTTONDOWN
//-----------------------------------------------------------------
void CGameWnd::OnLButtonDown(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    SetCapture(m_hWnd);
    m_bMovePlane 
= TRUE;
}



//----------------------------------------------------------------
//Description:
//    On message WM_LBUTTONUP
//-----------------------------------------------------------------
void CGameWnd::OnLButtonUp(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    ReleaseCapture();
    m_bMovePlane 
= FALSE;
}



//----------------------------------------------------------------
//Description:
//    On message WM_KEYDOWN
//-----------------------------------------------------------------
void CGameWnd::OnKeyDown(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    
int iKey = (int) wParam;
    
switch(iKey)
    
{
        
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;
    }


    SetSkewingOnKey();

}



//----------------------------------------------------------------
//Description:
//    On message WM_KEYUP
//-----------------------------------------------------------------
void CGameWnd::OnKeyUp(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    
int iKey = (int) wParam;
    
switch(iKey)
    
{
        
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();
}



//----------------------------------------------------------------
//Description:
//    Set the skewing base on the key status
//-----------------------------------------------------------------
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:
//    On message WM_CREATE
//-----------------------------------------------------------------
void CGameWnd::OnCreate(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    InitCommonControls();
    m_hWndCB 
= CommandBar_Create(m_hInst,hWnd,ID_COMMANDBAR);
    CommandBar_InsertMenubar(m_hWndCB,m_hInst,IDM_MAIN,
0);



}



//----------------------------------------------------------------
//Description:
//    On the menu command IDM_LEVEL_X
//-----------------------------------------------------------------
void CGameWnd::OnMenuLevel(int iLevel)
{
    
switch(iLevel)
    
{
        
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;
    }


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



//----------------------------------------------------------------
//Description:
//    Check the menu
//-----------------------------------------------------------------
void CGameWnd::CheckMenu()
{
    HMENU hMenu 
= CommandBar_GetMenu(m_hWndCB,0);

    
//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);

    
//Use the count of bullets as flag
    switch(m_Setting.iBulletCount)
    
{
        
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;
    }



    
}


// Plane.h: interface for the CPlane class.
//
//////////////////////////////////////////////////////////////////////
#ifndef    PLANE_H
#define PLANE_H



class CPlane  
{
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();

protected:
    POINT m_ptPos;
    RECT m_rcWndPlay;
}
;


#endif // #ifndef PLANE_H

// Plane.cpp: implementation of the CPlane class.
//
//////////////////////////////////////////////////////////////////////

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

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

//The radius of the plane
#define PLANE_RADIUS            4

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

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


CPlane::
~CPlane()
{

}



//--------------------------------------------------------------------
//Description:
//    Draw the plane normal status to the DC
//---------------------------------------------------------------------
void CPlane::DrawNormal(HDC hdc)
{
    HBRUSH hBrush 
= CreateSolidBrush(RGB(0,255,0));
    HGDIOBJ hOldSel 
= 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:
//    Draw the plane destroy status to the DC
//---------------------------------------------------------------------
void CPlane::DrawDestroy(HDC hdc)
{
    HBRUSH hBrush 
= CreateSolidBrush(RGB(255,0,0));
    HGDIOBJ hOldSel 
= 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:
//    Set current position
//---------------------------------------------------------------------
void CPlane::SetCurrentPos(const LPPOINT lppt)
{
    m_ptPos 
= *lppt;
}



//--------------------------------------------------------------------
//Description:
//    Get the current rect of plane
//---------------------------------------------------------------------
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:
//    Move the distance base on the current position
//---------------------------------------------------------------------
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:
//    Get the current position of plane
//---------------------------------------------------------------------
void CPlane::GetCurrentPos(LPPOINT lpptOut)
{
    
*lpptOut = m_ptPos;
}



//--------------------------------------------------------------------
//Description:
//    Initialize the playing window
//---------------------------------------------------------------------
void CPlane::Initialize(const RECT *lprcWndPlay)
{
    m_rcWndPlay 
= *lprcWndPlay;
}

 
// Evade.cpp : Defines the entry point for the application.
//

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





int WINAPI WinMain(    HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    
int       nCmdShow)
{
     
// TODO: Place code here.

    CGameWnd 
*pGameWnd = CGameWnd::GetInstance();
    
if(pGameWnd == NULL)
    
{
        
return 0x05;
    }

    
    
if(pGameWnd->Initialize(hInstance) == FALSE)
    
{
        
return 0x10;
    }


    pGameWnd
->ShowWindow(TRUE);
    

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


    
return 0;
}



    注:代码出现的CText类参见我这篇文章:http://blog.csdn.net/norains/archive/2007/04/17/1568429.aspx
posted @ 2007-04-25 21:04  我的一天  阅读(167)  评论(0编辑  收藏  举报