Visual C++游戏编程基础之键盘消息
一、鍵盤(pán)
1.虛擬鍵碼:Windows系統(tǒng)下所有的按鍵被視為虛擬鍵(包含鼠標(biāo)在內(nèi)),每一個(gè)虛擬鍵都有其對(duì)應(yīng)的虛擬鍵碼;
2.鍵盤(pán)消息
(1)VM_KEWDOWN:按下按鍵消息;
(2)VM_KEYUP?????? :松開(kāi)按鍵消息;
(3)VM_CHAR???????? :字符消息,當(dāng)按下的按鍵為定義于ASCII碼中的可打印字符時(shí),便發(fā)出此字符消息;
3.系統(tǒng)鍵
(1)VM_SYSKEYDOWN:按下系統(tǒng)鍵消息;
(2)VM_SYSKEYUP????? :松開(kāi)系統(tǒng)鍵消息;
二、鍵盤(pán)消息處理
1.LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
(1)wParam:表示按下按鍵的虛擬鍵碼;
(2)lParam? :存儲(chǔ)按鍵的相關(guān)狀態(tài)信息;
三、利用鍵盤(pán)上、下、左、右控制人物
1.首先設(shè)置人物的起始貼圖坐標(biāo)和起始方向,用0、1、2、3表示上、下、左、右,起始設(shè)為0;
2.初始化函數(shù)中,除貼背景圖外,根據(jù)方向選擇要貼的人物,還有根據(jù)方向判斷人物圖的寬和高,以便進(jìn)行透明處理和貼圖
?? 操作,此時(shí)若沒(méi)有按鍵操作,WinMain函數(shù)將每隔一段時(shí)間進(jìn)行貼圖操作,從而實(shí)現(xiàn)了人物的原地跑動(dòng);
3.若此時(shí)按下向下的按鍵,消息處理函數(shù)接收按鍵消息,接著要判斷當(dāng)前人物所處的方向,根據(jù)不同的方向,適當(dāng)修正,只要有
?? 明顯下移就可以,然后判斷y的值不能比地圖的高還大,要設(shè)臨界值;
4.更改了貼圖坐標(biāo)后,把方向設(shè)為1(下是1),然后就去指向繪圖函數(shù),然后又是重復(fù)2的操作;
四、效果
五、代碼如下
#include "stdafx.h" #include <stdio.h>HINSTANCE hInst; HBITMAP girl[4],bg; HDC hdc,mdc,bufdc; HWND hWnd; DWORD tPre,tNow; int num,dir,x,y;//num:連續(xù)圖中的小圖編號(hào);dir:人物移動(dòng)的方向;x,y為人物貼圖坐標(biāo)ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void MyPaint(HDC hdc);int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {MSG msg;MyRegisterClass(hInstance);if (!InitInstance (hInstance, nCmdShow)) {return FALSE;}while( msg.message!=WM_QUIT ){if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ){TranslateMessage( &msg );DispatchMessage( &msg );}else{tNow = GetTickCount();if(tNow-tPre >= 40)MyPaint(hdc);}}return msg.wParam; }ATOM MyRegisterClass(HINSTANCE hInstance) {WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = NULL;wcex.hCursor = NULL;wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = "canvas";wcex.hIconSm = NULL;return RegisterClassEx(&wcex); }BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {HBITMAP bmp;hInst = hInstance;hWnd = CreateWindow("canvas", "繪圖窗口" , WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if (!hWnd){return FALSE;}MoveWindow(hWnd,10,10,640,500,true);ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);hdc = GetDC(hWnd);mdc = CreateCompatibleDC(hdc);bufdc = CreateCompatibleDC(hdc);bmp = CreateCompatibleBitmap(hdc,640,480);SelectObject(mdc,bmp);x = 300;y = 250;//人物的起始貼圖位置(300,250)dir = 0;//起始面向上num = 0;girl[0] = (HBITMAP)LoadImage(NULL,"girl0.bmp",IMAGE_BITMAP,440,148,LR_LOADFROMFILE);girl[1] = (HBITMAP)LoadImage(NULL,"girl1.bmp",IMAGE_BITMAP,424,154,LR_LOADFROMFILE);girl[2] = (HBITMAP)LoadImage(NULL,"girl2.bmp",IMAGE_BITMAP,480,148,LR_LOADFROMFILE);girl[3] = (HBITMAP)LoadImage(NULL,"girl3.bmp",IMAGE_BITMAP,480,148,LR_LOADFROMFILE);bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);MyPaint(hdc);return TRUE; }void MyPaint(HDC hdc) {int w,h;SelectObject(bufdc,bg);BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);SelectObject(bufdc,girl[dir]);switch(dir){case 0:w = 55;h = 74;break;case 1:w = 53;h = 77;break;case 2:w = 60;h = 74;break;case 3:w = 60;h = 74;break;}BitBlt(mdc,x,y,w,h,bufdc,num*w,h,SRCAND);BitBlt(mdc,x,y,w,h,bufdc,num*w,0,SRCPAINT);BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);tPre = GetTickCount(); num++;if(num == 8)num = 0;}//Esc鍵結(jié)束程序 //方向鍵重設(shè)貼圖坐標(biāo) LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message){case WM_KEYDOWN: switch (wParam) {case VK_ESCAPE: PostQuitMessage( 0 );break;case VK_UP: switch(dir){case 0: y -= 10;break;case 1:x -= 1;y -= 8;break;case 2: x += 2;y -= 10;break;case 3:x += 2;y -= 10;break;}if(y < 0)y = 0;dir = 0;break;case VK_DOWN: switch(dir){case 0:x += 1;y += 8;break;case 1:y += 10;break;case 2:x += 3;y += 6;break;case 3:x += 3;y += 6;break;}if(y > 375)y = 375;dir = 1;break;case VK_LEFT: switch(dir){case 0:x -= 12;break;case 1:x -= 13;y += 4;break;case 2:x -= 10;break;case 3:x -= 10;break;}if(x < 0)x = 0;dir = 2;break;case VK_RIGHT: switch(dir){case 0:x += 8;break;case 1:x += 7;y += 4;break;case 2:x += 10;break;case 3:x += 10;break;}if(x > 575) //640-60x = 575;dir = 3;break;}break;case WM_DESTROY: int i;DeleteDC(mdc);DeleteDC(bufdc);for(i=0;i<4;i++)DeleteObject(girl[i]);DeleteObject(bg);ReleaseDC(hWnd,hdc);PostQuitMessage(0);break;default: return DefWindowProc(hWnd, message, wParam, lParam);}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的Visual C++游戏编程基础之键盘消息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 大鼠原代肾小管上皮细胞培养扩增方案
- 下一篇: Excel 关闭网格线
