游戏窗口双缓冲模板,可直接使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <windows.h>
#include <tchar.h>
#include "StdAfx.h"

HINSTANCE hInst;
HWND hWnd;
DWORD tPre, tNow;
int TOP, RIGHT, WIDTH = 1000, HEIGHT = 571;
int MX, MY; // 鼠标位置

HDC hdc, mdc, bufdc;
PAINTSTRUCT ps;
RECT rect;
HBITMAP fullmap, hBgBmp;

ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);

//***WinMain函数,程序入口点函数**************************************
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
//初始化
if (!InitInstance (hInstance, nCmdShow))
return FALSE;

//消息循环
GetMessage(&msg, NULL, NULL, NULL); //初始化msg
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)
{
hInst = hInstance;
hWnd = CreateWindow("canvas", "演示" , WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);

if (!hWnd)
return FALSE;

// MoveWindow(hWnd, 10, 10, WIDTH, HEIGHT, true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
fullmap = CreateCompatibleBitmap(hdc, WIDTH, HEIGHT);
SelectObject(mdc, fullmap);

/* ... 初始化各值 ... */

MyPaint(hdc);

return TRUE;
}

void MyPaint(HDC hdc)
{
/* ... 画图操作,可以生成动画 ... */

tPre = GetTickCount();
}

//****消息处理函数***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i, j;
switch (message)
{
case WM_PAINT :
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
MyPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_MOUSEMOVE :
MX = LOWORD(lParam) - roleFlyBmp[action][actionFrame].bmWidth / 2;
MY = HIWORD(lParam) - roleFlyBmp[action][actionFrame].bmHeight / 2;
break;
case WM_LBUTTONDOWN :
break;
case WM_KEYDOWN :
switch (wParam)
{
case VK_UP :
break;
}
break;
case WM_CHAR :
switch (wParam)
{
case ' ' :
break;
}
break;
case WM_DESTROY: //窗口结束消息,撤销各种DC
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(fullmap);
ReleaseDC(hWnd, hdc);
PostQuitMessage(0);
break;
default: //其他消息
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

游戏模板

参照上面的双缓冲模板。

之前做的一个作业样例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "StdAfx.h"
#include "testOutput.h"
//using namespace std;

#define LIMIT_RECT 1 // 矩形限制区域
#define LIMIT_CIRC 2 // 圆形限制区域

#define ACTION_STARTFLY 0 // 开始飞行
#define ACTION_FLY 1 // 向右飞
#define ACTION_FLY2 2 // 向左飞
#define ACTION_ATTACK 3 // 向右攻击
#define ACTION_ATTACK2 4 // 向左攻击

HINSTANCE hInst;
HWND hWnd;
DWORD tPre, tNow;
int TOP, RIGHT, WIDTH = 1000, HEIGHT = 571;
int OX = 370, OY = 180, MX, MY; // 实例位置
int SX = 10, SY = 10; // 每步移动的距离
int EX = 0, EY = 0; // 目标位置

HDC hdc, mdc, bufdc;
PAINTSTRUCT ps;
RECT rect;
HBITMAP fullmap, hBgBmp, hRoleBmp[10][100], _hRoleBmp[10][100];
BITMAP bgBmp, roleFlyBmp[10][100], _roleFlyBmp[10][100];
int action = ACTION_STARTFLY /*0 normal, 1 2 fly*/, actionFrame = 0, actionFrameContrler = 0;

int limitsMap[][5] =
{
{ 0, 0, 1000, 55, LIMIT_RECT },
{ 258, 0, 850, 55, LIMIT_RECT },
{ 342, 0, 500, 70, LIMIT_RECT },
{ 0, 225, 170, 50, LIMIT_RECT },
{ 0, 275, 54, 72, LIMIT_RECT },
{ 0, 455, 72, 119, LIMIT_RECT },
{ 310, 360, 324, 80, LIMIT_RECT },
{ 404, 300, 200, 237, LIMIT_RECT },
{ 504, 468, 522, 115, LIMIT_RECT },
{ 0, 0, 43, 306, LIMIT_RECT },
{ 940, 0, 42, 187, LIMIT_RECT }
}, limitsMapNum = 11;

ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);

//***WinMain函数,程序入口点函数**************************************
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
//初始化
if (!InitInstance (hInstance, nCmdShow))
return FALSE;

//消息循环
GetMessage(&msg, NULL, NULL, NULL); //初始化msg
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;
}

int max(int a, int b) {
return a > b ? a : b;
}

//****设计一个窗口类,类似填空题,使用窗口结构体*************************
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)
{
hInst = hInstance;
hWnd = CreateWindow("canvas", "动画演示" , WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);

if (!hWnd)
return FALSE;

// MoveWindow(hWnd, 10, 10, WIDTH, HEIGHT, true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
fullmap = CreateCompatibleBitmap(hdc, WIDTH, HEIGHT);
SelectObject(mdc, fullmap);

hBgBmp = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(hBgBmp, sizeof(BITMAP), &bgBmp);

int i, j;
char actionName[10][20] = { "startfly", "fly", "fly2", "attack", "attack2" };
for (i = 0; i < 5; i++)
{
char fullName[100];
for (j = 0; j < 5; j++)
{
sprintf(fullName, "%s%d.bmp", actionName[i], j);
hRoleBmp[i][j] = (HBITMAP)LoadImage(NULL, fullName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
sprintf(fullName, "_%s%d.bmp", actionName[i], j);
_hRoleBmp[i][j] = (HBITMAP)LoadImage(NULL, fullName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(hRoleBmp[i][j], sizeof(BITMAP), &roleFlyBmp[i][j]);
}
}

MyPaint(hdc);

return TRUE;
}

void limitXY(int nOX, int nOY)
{
int lix, liy, liw, lih;
for (int i = 0; i < limitsMapNum; i++)
{
lix = limitsMap[i][0];
liy = limitsMap[i][1];
liw = limitsMap[i][2];
lih = limitsMap[i][3];


// 画限制区域和人物的范围,调试用
/*HBRUSH hb;
hb = (HBRUSH) CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF));
SelectObject(hdc, hb);
Rectangle(hdc, lix, liy, lix + liw, liy + lih);
Ellipse(hdc, OX, OY, OX + roleFlyBmp[action][actionFrame].bmWidth, OY + roleFlyBmp[action][actionFrame].bmHeight);
DeleteObject(hb);*/

if (OX > lix + liw || OX + roleFlyBmp[action][actionFrame].bmWidth < lix
|| OY > liy + lih || OY + roleFlyBmp[action][actionFrame].bmHeight < liy
|| (OX - lix - liw / 2) * (OX - lix - liw / 2) + (OY - liy - lih / 2) * (OY - liy - lih / 2) > (max(liw , lih) * max(liw , lih)) >> 1) ; // 边角处理
else
{
if (OX > lix + liw || OX + roleFlyBmp[action][actionFrame].bmWidth < lix) ;
else
OX = nOX;
if (OY > liy + lih || OY + roleFlyBmp[action][actionFrame].bmHeight < liy) ;
else
OY = nOY;
if (OX == nOX && OY == nOY)
break;
}
}
}

void MyPaint(HDC hdc)
{
// 运动
int nOX = OX, nOY = OY;

if (EX || EY)
{
int fflag = 0;
if (abs(EX - OX) > SX)
{
fflag = 1;
if (EX > OX) OX += SX;
else OX -= SX;
}
if (abs(EY - OY) > SY)
{
fflag = 1;
if (EY > OY) OY += SY;
else OY -= SY;
}
if (!fflag)
EX = EY = 0;
}

limitXY(nOX, nOY);

// 画背景

SelectObject(bufdc, hBgBmp);
BitBlt(mdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, bufdc, 0, 0, SRCCOPY); // 画背景

SelectObject(bufdc, _hRoleBmp[action][actionFrame]);
BitBlt(mdc, OX, OY, roleFlyBmp[action][actionFrame].bmWidth, roleFlyBmp[action][actionFrame].bmHeight, bufdc, 0, 0, SRCAND); // 画人物
SelectObject(bufdc, hRoleBmp[action][actionFrame]);
BitBlt(mdc, OX, OY, roleFlyBmp[action][actionFrame].bmWidth, roleFlyBmp[action][actionFrame].bmHeight, bufdc, 0, 0, SRCPAINT); // 画人物

BitBlt(hdc, 0, 0, WIDTH, HEIGHT, mdc, 0, 0, SRCCOPY);



// 动画

switch (action)
{
case ACTION_STARTFLY:
if (!EX && !EY && OX==370 && OY==180) return ;
if (actionFrameContrler >= 4)
{
actionFrameContrler = 0;
action = 1;
}
actionFrame = actionFrameContrler;
break;
case ACTION_FLY:
case ACTION_FLY2:
if (actionFrameContrler >= 8) actionFrameContrler = 0;
actionFrame = actionFrameContrler / 2;
break;
case ACTION_ATTACK:
case ACTION_ATTACK2:
if (actionFrameContrler >= 15)
{
actionFrameContrler = 0;
action -= (ACTION_ATTACK - ACTION_FLY);
}
actionFrame = actionFrameContrler / 3;
break;
}
actionFrameContrler++;

tPre = GetTickCount();
}

//****消息处理函数***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i, j;
switch (message)
{
case WM_PAINT :
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
MyPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_MOUSEMOVE :
MX = LOWORD(lParam) - roleFlyBmp[action][actionFrame].bmWidth / 2;
MY = HIWORD(lParam) - roleFlyBmp[action][actionFrame].bmHeight / 2;
break;
case WM_LBUTTONDOWN :
EX = MX;
EY = MY;
if (EX < OX) action = ACTION_FLY2;
else if (action != ACTION_STARTFLY) action = ACTION_FLY;
break;
case WM_KEYDOWN :
switch (wParam)
{
case VK_UP :
OY -= 20;
limitXY(OX, OY + 20);
break;
case VK_DOWN :
OY += 20;
limitXY(OX, OY - 20);
break;
case VK_LEFT :
OX -= 20;
limitXY(OX + 20, OY);
action = 2;
break;
case VK_RIGHT :
OX += 20;
limitXY(OX - 20, OY);
action = 1;
break;
}
break;
case WM_CHAR :
switch (wParam)
{
case 'w' :
OY -= 20;
limitXY(OX, OY + 20);
break;
case 's' :
OY += 20;
limitXY(OX, OY - 20);
break;
case 'a' :
OX -= 20;
limitXY(OX + 20, OY);
action = 2;
break;
case 'd' :
OX += 20;
limitXY(OX - 20, OY);
action = 1;
break;
case ' ' :
if (action != 3 && action != 4)
{
if (action == 1 || action == 2)
action += 2;
else action = 3;
actionFrameContrler = 0;
break;
}

}
break;
case WM_DESTROY: //窗口结束消息,撤销各种DC
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(fullmap);
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
{
DeleteObject(hRoleBmp[i][j]);
DeleteObject(_hRoleBmp[i][j]);
}
ReleaseDC(hWnd, hdc);
PostQuitMessage(0);
break;
default: //其他消息
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}