このページはゲームループの基礎講座シリーズの1つです。
今回は第10章の「キャラクタの制御」について説明します。(戻る)
キャラクタの移動制御
// 画面内に移動制御 GetClientRect( hWnd, &rc ); if ( nFaceX < rc.left ){ nFaceX = (rc.left); MessageBeep( MB_OK ); } else if ( nFaceX > (rc.right - nFaceW) ){ nFaceX = (rc.right - nFaceW); MessageBeep( MB_OK ); } if ( nFaceY < rc.top ){ nFaceY = (rc.top); MessageBeep( MB_OK ); } else if ( nFaceY > (rc.bottom - nFaceH) ){ nFaceY = (rc.bottom - nFaceH); MessageBeep( MB_OK ); }
プロシージャ関数に組み込む
//------------------------------------------------ // ウインドウのプロシージャ関数(OK) //------------------------------------------------ static LRESULT CALLBACK mainWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { static HDC hFaceChip; static LONG nFaceW; static LONG nFaceH; static LONG nFaceX; static LONG nFaceY; static LONG nFaceSpeed; switch ( uMsg ){ CASE WM_CREATE: nFaceSpeed = 16; nFaceW = 32; nFaceH = 32; hFaceChip = funcCreateMemDC( hWnd, nFaceW, nFaceH ); funcFaceImage( hFaceChip, nFaceW, nFaceH ); CASE WM_CLOSE: DeleteDC( hFaceChip ); DestroyWindow( hWnd ); CASE WM_DESTROY: PostQuitMessage( 0 ); CASE WM_PAINT: { PAINTSTRUCT ps; HDC hDC; hDC = BeginPaint( hWnd, &ps ); /* 描画処理 */ EndPaint( hWnd, &ps ); } CASE WM_KEYDOWN: { HDC hDC;[←前へ] [目次] [次へ→]RECT rc;switch ( wParam ){ // 矢印キー CASE VK_UP: nFaceY -= nFaceSpeed; CASE VK_DOWN: nFaceY += nFaceSpeed; CASE VK_LEFT: nFaceX -= nFaceSpeed; CASE VK_RIGHT: nFaceX += nFaceSpeed; // テンキー CASE VK_NUMPAD1: nFaceY += nFaceSpeed; nFaceX -= nFaceSpeed; CASE VK_NUMPAD2: nFaceY += nFaceSpeed; CASE VK_NUMPAD3: nFaceY += nFaceSpeed; nFaceX += nFaceSpeed; CASE VK_NUMPAD4: nFaceX -= nFaceSpeed; CASE VK_NUMPAD5: nFaceY += nFaceSpeed; CASE VK_NUMPAD6: nFaceX += nFaceSpeed; CASE VK_NUMPAD7: nFaceY -= nFaceSpeed; nFaceX -= nFaceSpeed; CASE VK_NUMPAD8: nFaceY -= nFaceSpeed; CASE VK_NUMPAD9: nFaceY -= nFaceSpeed; nFaceX += nFaceSpeed; DEFAULT: return 0; }// 画面内に移動制御 GetClientRect( hWnd, &rc ); if ( nFaceX < rc.left ){ nFaceX = (rc.left); MessageBeep( MB_OK ); } else if ( nFaceX > (rc.right - nFaceW) ){ nFaceX = (rc.right - nFaceW); MessageBeep( MB_OK ); } if ( nFaceY < rc.top ){ nFaceY = (rc.top); MessageBeep( MB_OK ); } else if ( nFaceY > (rc.bottom - nFaceH) ){ nFaceY = (rc.bottom - nFaceH); MessageBeep( MB_OK ); }// キャラクタの描画 hDC = GetDC( hWnd ); TransparentBlt( hDC, nFaceX, nFaceY, nFaceW, nFaceH, hFaceChip, 0, 0, nFaceW, nFaceH, RGB(0x00,0x00,0x00) ); ReleaseDC( hWnd, hDC ); } CASE WM_ENDSESSION: PostMessage( hWnd, WM_CLOSE, 0, 0 ); CASE WM_LBUTTONDOWN: SendMessage( hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0 ); CASE WM_LBUTTONDBLCLK: SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, (SWP_NOMOVE|SWP_NOSIZE) ); CASE WM_RBUTTONDBLCLK: SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, (SWP_NOMOVE|SWP_NOSIZE) ); DEFAULT: return DefWindowProc( hWnd, uMsg, wParam, lParam ); } return 0; }
※コメント投稿者のブログIDはブログ作成者のみに通知されます