📄 gamewnd.cpp
字号:
{
m_bCollide = FALSE;
m_ulTimeCount = 0;
m_pInstance->MenuItemsControl(TRUE);
//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()
{
m_pInstance->MenuItemsControl(FALSE);
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_TRAINING:
m_Setting.iBulletCount = LEVEL0_BULLET_COUNT;
m_Setting.iBulletMaxMoveDistance = LEVEL0_BULLET_MAXMOVEDISTANCE;
m_Setting.iPlaneMoveDistance = LEVEL0_PLANE_MOVEDISTANCE;
break;
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_TRAINING,MF_UNCHECKED | MF_BYCOMMAND);
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 LEVEL0_BULLET_COUNT:
CheckMenuItem(hMenu,IDM_TRAINING,MF_CHECKED|MF_BYCOMMAND);
break;
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;
}
}
void CGameWnd::MenuItemsControl(bool bStart)
{
HMENU hMenu = CommandBar_GetMenu(m_hWndCB,0);
/*Enable and Disable menu items*/
if(TRUE == bStart)
{
EnableMenuItem(hMenu,IDM_START,MF_GRAYED);
EnableMenuItem(hMenu,IDM_LEVEL_1,MF_GRAYED);
EnableMenuItem(hMenu,IDM_LEVEL_2,MF_GRAYED);
EnableMenuItem(hMenu,IDM_LEVEL_3,MF_GRAYED);
EnableMenuItem(hMenu,IDM_TRAINING,MF_GRAYED);
EnableMenuItem(hMenu,IDM_FINISH,MF_ENABLED);
}
/*Enable and Disable menu items*/
else
{
EnableMenuItem(hMenu,IDM_START,MF_ENABLED);
EnableMenuItem(hMenu,IDM_LEVEL_1,MF_ENABLED);
EnableMenuItem(hMenu,IDM_LEVEL_2,MF_ENABLED);
EnableMenuItem(hMenu,IDM_LEVEL_3,MF_ENABLED);
EnableMenuItem(hMenu,IDM_TRAINING,MF_ENABLED);
EnableMenuItem(hMenu,IDM_FINISH,MF_GRAYED);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -