前年的这个时候,我还是一名高中生,刚接触 Win32 编程。也为消遣,做了一个坦克大战的游戏,使用 Win32 API ,C/C++ 语言写的,工具是 VC6.0 。
现在翻出这个古老的程序,真有点怀念高中。。。我们这学期开始学习 Win32 编程,用 C 语言使用 API 。。。我计算机专业
尽管现在看来,这份代码好丑好幼稚。。。显示方面还有点小bug。。。
(图片和声音资源是网上下载的,来源未知)
先贴几张截图:
源代码:
(图片和声音资源是网上下的,就不贴了。。。)
Functions.h
1// Functions.h
2//
3#ifndef INCLUDE_FUNCTIONS_H
4#define INCLUDE_FUNCTIONS_H
5
6#include "GlobalVar.h"
7
8// *********************************************************************************
9// declarations of functions
10// *********************************************************************************
11
12// game
13BOOL CreateGameResources( HINSTANCE hI, HWND hW );
14BOOL DeleteGameResources( VOID );
15
16BOOL CheckGameState( VOID );
17VOID CALLBACK TimerProc( HWND hW, UINT uMsg, UINT idEvent, DWORD dwTime );
18
19BOOL Loading( VOID );
20BOOL OnChose( VOID );
21BOOL Paint( VOID );
22BOOL OnWin( VOID );
23BOOL OnLose( VOID );
24BOOL BeginLevel( VOID );
25
26// tank
27BOOL CanTankGo( INT x, INT y );
28BOOL CheckTankLife( VOID );
29BOOL CheckTankMove( VOID );
30BOOL CheckTankFire( VOID );
31
32// bullet
33BOOL NewBullet( INT xTank, INT yTank, INT dirTank, BOOL bEnemyFire );
34BOOL MoveBullet( VOID );
35BOOL CanBulletGo( INT x, INT y, INT dir, BOOL bEnemyFire );
36
37// explode
38BOOL NewExplode( INT xExp, INT yExp );
39BOOL CheckExplode( VOID );
40
41// tile
42BOOL CheckTile( VOID );
43
44// *********************************************************************************
45// define of functions
46// *********************************************************************************
47
48// game
49BOOL CreateGameResources( HINSTANCE hI, HWND hW )
50{
51 // window
52 hInst = hI;
53 hWnd = hW;
54 hdcWnd = ::GetDC( hWnd );
55 hbrBg = ::CreateSolidBrush( RGB( 0, 0, 0 ) );
56 ::SelectObject( hdcWnd, hbrBg );
57
58 // game
59 hdcLoading = ::CreateCompatibleDC( hdcWnd );
60 hbmpLoading = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_LOADING) );
61 ::SelectObject( hdcLoading, hbmpLoading );
62
63 hbmpMenu = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_MENU) );
64 hbmpPointer = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_POINTER) );
65 hdcMenu = ::CreateCompatibleDC( hdcWnd );
66 hdcPointer = ::CreateCompatibleDC( hdcWnd );
67 ::SelectObject( hdcMenu, hbmpMenu );
68 ::SelectObject( hdcPointer, hbmpPointer );
69
70 // tank
71 hbmpTank[0] = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_PLAYER) );
72 hdcTank[0] = ::CreateCompatibleDC( hdcWnd );
73 ::SelectObject( hdcTank[0], hbmpTank[0] );
74 hbmpTank[1] = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_ENEMY) );
75 hdcTank[1] = ::CreateCompatibleDC( hdcWnd );
76 ::SelectObject( hdcTank[1], hbmpTank[1] );
77
78 // bullet
79 hbmpBullet = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_BULLET) );
80 hdcBullet = ::CreateCompatibleDC( hdcWnd );
81 ::SelectObject( hdcBullet, hbmpBullet );
82
83 // explode
84 hbmpExplode = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_EXPLODE) );
85 hdcExplode = ::CreateCompatibleDC( hdcWnd );
86 ::SelectObject( hdcExplode, hbmpExplode );
87
88 // tile
89 hdcTile = ::CreateCompatibleDC( hdcWnd );
90 hbmpTile = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_TILE) );
91 ::SelectObject( hdcTile, hbmpTile );
92 return 1;
93}
94
95BOOL DeleteGameResources( VOID )
96{
97 // window
98 if( hdcWnd ) ::ReleaseDC( hWnd, hdcWnd );
99 if( hbrBg ) ::DeleteObject( hbrBg );
100
101 //game
102 if( hbmpLoading) { ::DeleteObject( hbmpLoading); hbmpLoading = NULL; }
103 if( hdcLoading ) { ::DeleteObject( hdcLoading ); hdcLoading = NULL; }
104
105 if( hbmpMenu ) { ::DeleteObject( hbmpMenu ); hbmpMenu = NULL; }
106 if( hbmpPointer) { ::DeleteObject( hbmpPointer ); hbmpPointer = NULL; }
107 if( hdcMenu ) { ::DeleteObject( hdcMenu ); hdcMenu = NULL; }
108 if( hdcPointer ) { ::DeleteObject( hdcPointer ); hdcPointer = NULL; }
109
110 // tank
111 if( hbmpTank[0]) { ::DeleteObject( hbmpTank[0]); hbmpTank[0] = NULL; }
112 if( hdcTank[0] ) { ::DeleteObject( hdcTank[0] ); hdcTank[0] = NULL; }
113 if( hbmpTank[1]) { ::DeleteObject( hbmpTank[1]); hbmpTank[1] = NULL; }
114 if( hdcTank[1] ) { ::DeleteObject( hdcTank[1] ); hdcTank[1] = NULL; }
115
116 // bullet
117 if( hbmpBullet ) { ::DeleteObject( hbmpBullet ); hbmpBullet = NULL; }
118 if( hdcBullet ) { ::DeleteObject( hdcBullet ); hdcBullet = NULL; }
119
120 // explode
121 if( hbmpExplode) { ::DeleteObject( hbmpExplode); hbmpExplode = NULL; }
122 if( hdcExplode ) { ::DeleteObject( hdcExplode ); hdcExplode = NULL; }
123
124 // tile
125 if( hdcTile ) { ::DeleteObject( hdcTile ); hdcTile = NULL; }
126 if( hbmpTile ) { ::DeleteObject( hbmpTile ); hbmpTile = NULL; }
127
128 return 1;
129}
130
131BOOL CheckGameState( VOID )
132{
133 switch( iGameState )
134 {
135 case GS_LOADING :
136 ::srand( time( NULL ) );
137 ::SetTimer( hWnd, IDT_GAME, ELAPSE_GAME, TIMERPROC(TimerProc) );
138
139 case GS_RESTART_GAME :
140 iGameLevel = -1;
141 ::Loading();
142 ::Sleep( 500 );
143
144 case GS_NEW_LEVEL :
145 ++iGameLevel;
146
147 case GS_RESTART_LEVEL :
148 ::Sleep( 500 );
149 ::BeginLevel();
150 ::Paint();
151 return 1;
152
153 case GS_EXIT :
154 ::DestroyWindow( hWnd );
155 break;
156
157 case GS_PAUSE :
158 ::Sleep( 500 );
159 return 1;
160
161 case GS_LOSE :
162 ::OnLose();
163 return 1;
164
165 case GS_WIN :
166 ::OnWin();
167 return 1;
168
169 case GS_BACK_GAME :
170 ::Paint();
171 ::Sleep( 5000 );
172 iGameState = GS_CHOSE;
173 return 1;
174
175 case GS_CHOSE :
176 ::OnChose();
177 return 1;
178 }
179 iGameState = GS_NULL;
180 return 1;
181}
182
183VOID CALLBACK TimerProc( HWND hW, UINT uMsg, UINT idEvent, DWORD dwTime )
184{
185 if( iGameState != GS_NULL ) return;
186 CheckExplode();
187 CheckTile();
188 CheckTankLife();
189 CheckTankMove();
190 CheckTankFire();
191 MoveBullet();
192 return;
193}
194
195BOOL Loading( VOID )
196{
197 ::BitBlt( hdcWnd, 0, 0, WND_WIDTH, WND_HEIGHT,
198 hdcLoading, 0, 0, SRCCOPY );
199 return 1;
200}
201
202BOOL OnChose( VOID )
203{
204 ::PlaySound( MAKEINTRESOURCE(IDR_MENU), hInst, SND_ASYNC | SND_RESOURCE | SND_LOOP );
205 CONST INT W_MENU = 351, H_MENU = 240;
206 CONST INT X_MENU = (WND_WIDTH-W_MENU)/2, Y_MENU = (WND_HEIGHT-H_MENU)/2;
207 ::BitBlt( hdcWnd, X_MENU, Y_MENU, W_MENU, H_MENU, hdcMenu, 0, 0, SRCCOPY );
208 INT iChoice=0;
209 CONST INT VPOINTER = 60, DPOINTER = 6, LPOINTER = 48;
210 ::BitBlt( hdcWnd, X_MENU+DPOINTER, iChoice+Y_MENU+DPOINTER, LPOINTER, LPOINTER,
211 hdcPointer, 0, 0, SRCCOPY );
212 BOOL bNotPressKey=1;
213 for(;;)
214 {
215 if( bNotPressKey )
216 ::Sleep( 150 );
217 bNotPressKey = 1;
218 if( ::GetAsyncKeyState( VK_DOWN ) < 0 )
219 {
220 bNotPressKey = 0;
221 ::PlaySound( MAKEINTRESOURCE(IDR_PRESSBUTTON), hInst, SND_SYNC | SND_RESOURCE );
222 ::BitBlt( hdcWnd, X_MENU+DPOINTER, iChoice+Y_MENU+DPOINTER, LPOINTER, LPOINTER,
223 hdcPointer, 0, 0, SRCINVERT );
224 iChoice = ( iChoice + VPOINTER ) % H_MENU;
225 ::BitBlt( hdcWnd, X_MENU+DPOINTER, iChoice+Y_MENU+DPOINTER, LPOINTER, LPOINTER,
226 hdcPointer, 0, 0, SRCINVERT );
227 }
228 if( ::GetAsyncKeyState( VK_UP ) < 0 )
229 {
230 bNotPressKey = 0;
231 ::PlaySound( MAKEINTRESOURCE(IDR_PRESSBUTTON), hInst, SND_SYNC | SND_RESOURCE );
232 ::BitBlt( hdcWnd, X_MENU+DPOINTER, iChoice+Y_MENU+DPOINTER, LPOINTER, LPOINTER,
233 hdcPointer, 0, 0, SRCINVERT );
234 iChoice = ( iChoice + H_MENU - VPOINTER ) % H_MENU;
235 ::BitBlt( hdcWnd, X_MENU+DPOINTER, iChoice+Y_MENU+DPOINTER, LPOINTER, LPOINTER,
236 hdcPointer, 0, 0, SRCINVERT );
237 }
238 if( ::GetAsyncKeyState( VK_RETURN ) < 0 )
239 {
240 bNotPressKey = 0;
241 ::PlaySound( MAKEINTRESOURCE(IDR_SURE), hInst, SND_SYNC | SND_RESOURCE );
242 switch( iChoice )
243 {
244 case 0 :
245 iGameState = GS_RESTART_GAME;
246 break;
247
248 case 60 :
249 iGameState = GS_RESTART_LEVEL;
250 break;
251
252 case 120 :
253 iGameState = GS_BACK_GAME;
254 break;
255
256 case 180 :
257 iGameState = GS_EXIT;
258 break;
259 }
260 break;
261 }
262 }
263 return 1;
264}
265
266BOOL Paint( VOID )
267{
268 ::Rectangle( hdcWnd, 0, 0, WND_WIDTH, WND_HEIGHT );
269 INT i, j;
270 // tile
271 for( i=0; i<WIDTH; ++i )
272 for( j=0; j<HEIGHT; ++j )
273 ::BitBlt( hdcWnd, i*UNIT, j*UNIT, UNIT, UNIT,
274 hdcTile, map[i][j], 0, SRCINVERT );
275 // tank
276 for( i=0; i<TANK_ARRAY_LENGTH; ++i )
277 {
278 if( !bKilledTank[i] )
279 {
280 j = 0;
281 if( i ) j = 1;
282 ::BitBlt( hdcWnd, xTank[i], yTank[i], LTANK, LTANK,
283 hdcTank[j], xDirTank[i], yDirTank[i], SRCINVERT );
284 }
285 }
286
287 // bullet
288 for( i=0; i<TOTAL_BULLET; ++i )
289 if( bAliveBullet[i] )
290 {
291 ::BitBlt( hdcWnd, xBullet[i], yBullet[i], LBULLET, LBULLET,
292 hdcBullet, 0, 0, SRCINVERT );
293 }
294
295 // explode
296 for( i=0; i<TOTAL_EXPLODE; ++i )
297 if( bAliveExplode[i] )
298 {
299 ::BitBlt( hdcWnd, xExplode[i], yExplode[i], LEXPLODE, LEXPLODE,
300 hdcExplode, 0, 0, SRCINVERT );
301 }
302 return 1;
303}
304
305BOOL OnWin( VOID )
306{
307 CONST INT W = 248, H = 160;
308 HBITMAP hbmpWin = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_WIN) );
309 HDC hdcWin = ::CreateCompatibleDC( hdcWnd );
310 ::SelectObject( hdcWin, hbmpWin );
311 ::BitBlt( hdcWnd, (WND_WIDTH-W)/2, (WND_HEIGHT-H)/2, W, H,
312 hdcWin, 0, 0, SRCCOPY );
313 ::DeleteObject( hbmpWin );
314 ::DeleteObject( hdcWin );
315 ::PlaySound( MAKEINTRESOURCE(IDR_WIN), hInst, SND_RESOURCE | SND_SYNC );
316 ::Sleep( 200 );
317 ::PlaySound( MAKEINTRESOURCE(IDR_WIN), hInst, SND_RESOURCE | SND_SYNC );
318 ::Sleep( 200 );
319 ::PlaySound( MAKEINTRESOURCE(IDR_WIN), hInst, SND_RESOURCE | SND_SYNC );
320 ::Sleep( 1600 );
321 iGameState = GS_CHOSE;
322 return 1;
323}
324
325BOOL OnLose( VOID )
326{
327 ::PlaySound( MAKEINTRESOURCE(IDR_GAMEOVER0), hInst, SND_RESOURCE | SND_SYNC );
328 CONST INT W = 248, H = 160;
329 HBITMAP hbmpGameover = ::LoadBitmap( hInst, MAKEINTRESOURCE(IDB_GAMEOVER) );
330 HDC hdcGameover = ::CreateCompatibleDC( hdcWnd );
331 ::SelectObject( hdcGameover, hbmpGameover );
332 ::BitBlt( hdcWnd, (WND_WIDTH-W)/2, (WND_HEIGHT-H)/2, W, H,
333 hdcGameover, 0, 0, SRCCOPY );
334 ::DeleteObject( hbmpGameover );
335 ::DeleteObject( hdcGameover );
336 ::PlaySound( MAKEINTRESOURCE(IDR_GAMEOVER1), hInst, SND_RESOURCE | SND_ASYNC );
337 ::Sleep( 2000 );
338 iGameState = GS_CHOSE;
339 return 1;
340}
341
342BOOL BeginLevel( VOID )
343{
344 INT i, j;
345 // game
346 if( iGameLevel >= TOTAL_LEVEL )
347 {
348 iGameLevel = TOTAL_LEVEL-1;
349 iGameState = GS_WIN;
350 return 1;
351 }
352 else
353 {
354 iGameState = GS_NULL;
355 }
356
357 // tank
358 for( i=0; i<TANK_ARRAY_LENGTH; ++i )
359 {
360 iTotKilledTank[i] = -1;
361 iFireDeltaTime[i] = FIRE_DELTA_TIME[i];
362 bKilledTank[i] = 1;
363 }
364 // bullet
365 ::memset( bAliveBullet, 0, sizeof(bAliveBullet) );
366 // explode
367 memset( bAliveExplode, 0, sizeof(bAliveExplode) );
368 // tile
369 for( i=0; i<WIDTH; ++i )
370 for( j=0; j<HEIGHT; ++j )
371 map[i][j] = MAP_SOURCE[iGameLevel][j][i] * UNIT;
372 memset( bEraseTile, 0, sizeof(bEraseTile) );
373 return 1;
374}
375
376// tank
377BOOL CanTankGo( INT x, INT y ) // other tank
378{
379 if( (x<0) || (y<0) || (x+LTANK>WND_WIDTH) || (y+LTANK>WND_HEIGHT) )
380 return 0;
381 INT l = x / UNIT;
382 INT r = ( x + LTANK ) / UNIT;
383 INT u = y / UNIT;
384 INT d = ( y + LTANK ) / UNIT;
385 if( (map[l][u]!=TREE) && (map[l][u]!=SPACE) ) return 0;
386 if( (map[l][d]!=TREE) && (map[l][d]!=SPACE) ) return 0;
387 if( (map[r][u]!=TREE) && (map[r][u]!=SPACE) ) return 0;
388 if( (map[r][d]!=TREE) && (map[r][d]!=SPACE) ) return 0;
389 return 1;
390}
391
392BOOL CheckTankLife( VOID )
393{
394 INT i, j;
395 for( i=0; i<TANK_ARRAY_LENGTH; ++i )
396 {
397 if( bKilledTank[i] )
398 {
399 j = 0;
400 if( i ) j = 1;
401 bKilledTank[i] = 0;
402 ++iTotKilledTank[i];
403 if( (iTotKilledTank[i]) && (iTotKilledTank[i]<=TOTAL_TANK[i]) )
404 {
405 ::BitBlt( hdcWnd, xTank[i], yTank[i], LTANK, LTANK,
406 hdcTank[j], xDirTank[i], yDirTank[i], SRCINVERT );
407 }
408 if( iTotKilledTank[i] >= TOTAL_TANK[i] )
409 {
410 iTotKilledTank[i] = TOTAL_TANK[i] + 1;
411 bKilledTank[i] = 1;
412 if( j == 0 )
413 {
414 iGameState = GS_LOSE;
415 }
416 }
417 else
418 {
419 xTank[i] = X_TANK_ORG[i];
420 yTank[i] = Y_TANK_ORG[i];
421 xDirTank[i] = X_DIR_TANK_ORG[i];
422 yDirTank[i] = Y_DIR_TANK_ORG[i];
423 iFireDeltaTime[i] = FIRE_DELTA_TIME[i];
424 ::BitBlt( hdcWnd, xTank[i], yTank[i], LTANK, LTANK,
425 hdcTank[j], xDirTank[i], yDirTank[i], SRCINVERT );
426 }
427 }
428 }
429 if( iGameState == GS_LOSE )
430 return 1;
431 for( i=1; (i<TANK_ARRAY_LENGTH)&&(iTotKilledTank[i]>=TOTAL_TANK[i]); ++i )
432 ;
433 if( i >= TANK_ARRAY_LENGTH )
434 {
435 iGameState = GS_NEW_LEVEL;
436 }
437 return 1;
438}
439
440BOOL CheckTankMove( VOID )
441{
442 INT dir[TANK_ARRAY_LENGTH], i, j, x, y;
443 for( dir[0]=VK_LEFT; dir[0]<=VK_DOWN; ++dir[0] )
444 if( ::GetAsyncKeyState( dir[0] ) < 0 ) break;
445 dir[0] -= DIRECTION_BASE;
446 for( i=1; i<TANK_ARRAY_LENGTH; ++i )
447 {
448 if( rand() % 7 == 0 )
449 {
450 if( rand() % 4 )
451 {
452 dir[i] = rand() % 4;
453 }
454 else
455 {
456 dir[i] = 3;
457 }
458 }
459 else
460 {
461 for( dir[i]=0; DIRECTION[dir[i]]!=yDirTank[i]; ++dir[i] )
462 ;
463 }
464 }
465 for( i=0; i<TANK_ARRAY_LENGTH; ++i )
466 {
467 if( dir[i] > 3 ) continue;
468 if( bKilledTank[i] ) continue;
469 j = 0;
470 if( i ) j = 1;
471 ::BitBlt( hdcWnd, xTank[i], yTank[i], LTANK, LTANK,
472 hdcTank[j], xDirTank[i], yDirTank[i], SRCINVERT );
473 if( DIRECTION[dir[i]] == yDirTank[i] )
474 {
475 x = xTank[i] + X_TANK_GO[dir[i]];
476 y = yTank[i] + Y_TANK_GO[dir[i]];
477 if( ::CanTankGo( x, y ) )
478 {
479 xTank[i] = x;
480 yTank[i] = y;
481 }
482 }
483 else
484 {
485 yDirTank[i] = DIRECTION[dir[i]];
486 }
487 ::BitBlt( hdcWnd, xTank[i], yTank[i], LTANK, LTANK,
488 hdcTank[j], xDirTank[i], yDirTank[i], SRCINVERT );
489 }
490 return 1;
491}
492
493BOOL CheckTankFire( VOID )
494{
495 INT i;
496 for( i=0; i<TANK_ARRAY_LENGTH; ++i )
497 {
498 if( bKilledTank[i] ) continue;
499 if( ++iFireDeltaTime[i] >= FIRE_DELTA_TIME[i] )
500 {
501 if( i )
502 {
503 if( rand() % 3 == 0 )
504 {
505 iFireDeltaTime[i] = 0;
506/**//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
507// test
508 ::NewBullet( xTank[i], yTank[i], yDirTank[i], 1 ); // not bullet error
509 // ::NewBullet( xTank[i], yTank[i], yDirTank[i], 0 );// have bullet error
510// test end
511/**///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
512 }
513 else
514 {
515 iFireDeltaTime[i] = FIRE_DELTA_TIME[i];
516 }
517 }
518 else
519 {
520 if( ::GetAsyncKeyState( VK_SPACE ) < 0 )
521 {
522 ::PlaySound( MAKEINTRESOURCE(IDR_PLAYERSHOT), hInst, SND_RESOURCE | SND_ASYNC );
523 iFireDeltaTime[i] = 0;
524/**//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
525// test
526 ::NewBullet( xTank[i], yTank[i], yDirTank[i], 1 ); // not bullet error
527 // ::NewBullet( xTank[i], yTank[i], yDirTank[i], 0 );// have bullet error
528// test end
529/**///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
530 }
531 else
532 {
533 iFireDeltaTime[i] = FIRE_DELTA_TIME[i];
534 }
535 }
536 }
537 }
538 return 1;
539}
540
541// bullet
542BOOL NewBullet( INT xTank, INT yTank, INT dirTank, BOOL bEnemyFire )
543{
544 INT i;
545 for( i=0; (i<TOTAL_BULLET)&&(bAliveBullet[i]); ++i )
546 ;
547 if( i >= TOTAL_BULLET )
548 return 0;
549 bAliveBullet[i] = 1;
550 bNotFirst[i] = 0;
551 bByEnemy[i] = bEnemyFire;
552 xBullet[i] = xTank - DTANK + DBULLET;
553 yBullet[i] = yTank - DTANK + DBULLET;
554 dirBullet[i] = dirTank / BULLET_DIR_BASE;
555 return 1;
556}
557
558BOOL MoveBullet( VOID )
559{
560 INT i, x, y;
561 for( i=0; i<TOTAL_BULLET; ++i )
562 {
563 if( bAliveBullet[i] )
564 {
565 if( bNotFirst[i] )
566 {
567 ::BitBlt( hdcWnd, xBullet[i], yBullet[i], LBULLET, LBULLET,
568 hdcBullet, 0, 0, SRCINVERT );
569 }
570 else
571 {
572 bNotFirst[i] = 1;
573 }
574 x = xBullet[i] + X_BULLET_GO[dirBullet[i]];
575 y = yBullet[i] + Y_BULLET_GO[dirBullet[i]];
576 if( CanBulletGo( x, y, dirBullet[i], bByEnemy[i] ) )
577 {
578 xBullet[i] = x;
579 yBullet[i] = y;
580 ::BitBlt( hdcWnd, xBullet[i], yBullet[i], LBULLET, LBULLET,
581 hdcBullet, 0, 0, SRCINVERT );
582 }
583 else
584 {
585 bAliveBullet[i] = 0;
586 }
587 }
588 }
589 return 1;
590}
591
592BOOL CanBulletGo( INT x, INT y, INT dir, BOOL bEnemyFire )
593{
594 BOOL bAns = 1;
595 // edge
596 if( x < 0 )
597 {
598 ::NewExplode( -LEXPLODEHALF, y-DBULLET2EXPLODE ); bAns = 0;
599 }
600 if( y < 0 )
601 {
602 ::NewExplode( x-DBULLET2EXPLODE, -LEXPLODEHALF ); bAns = 0;
603 }
604 if( x+LBULLET > WND_WIDTH )
605 {
606 ::NewExplode( WND_WIDTH-LEXPLODEHALF, y-DBULLET2EXPLODE ); bAns = 0;
607 }
608 if( y+LBULLET > WND_HEIGHT )
609 {
610 ::NewExplode( x-DBULLET2EXPLODE, WND_HEIGHT-LEXPLODEHALF ); bAns = 0;
611 }
612 // tank
613 INT i;
614 for( i=0; i<TANK_ARRAY_LENGTH; ++i )
615 {
616 if( bKilledTank[i] ) continue;
617/**////////////////////////////////////////////////////////////////////////////////////////////////
618// test
619 // continue;
620 // if( (i)&&(bEnemyFire) ) continue;
621 // if( (i==0)&&(bEnemyFire==0) ) continue;
622// test end
623/**///////////////////////////////////////////////////////////////////////////////////////////////////
624 if( (
625 ((xTank[i]<=x)&&(x<=xTank[i]+LTANK))||
626 ((xTank[i]<=x+LBULLET)&&(x+LBULLET<=xTank[i]+LTANK))
627 )
628 &&
629 (
630 ((yTank[i]<=y)&&(y<=yTank[i]+LTANK))||
631 ((yTank[i]<=y+LBULLET)&&(y+LBULLET<=yTank[i]+LTANK))
632 )
633 )
634 {
635 ::PlaySound( MAKEINTRESOURCE(IDR_BANG), hInst, SND_RESOURCE | SND_ASYNC );
636 bAns = 0;
637 ::NewExplode( xTank[i], yTank[i] );
638 bKilledTank[i] = 1;
639 }
640 }
641 // tile return
642 INT l = x / UNIT;
643 INT r = ( x + LBULLET ) / UNIT;
644 INT u = y / UNIT;
645 INT d = ( y + LBULLET ) / UNIT;
646 switch( dir )
647 {
648 case BULLET_UP :
649 if( (map[l][u]==SOIL) || (map[l][u]==STEEL) || (map[l][u]==HOME) ||
650 (map[r][u]==SOIL) || (map[r][u]==STEEL) || (map[r][u]==HOME) )
651 {
652 ::NewExplode( x-DBULLET2EXPLODE, (u+1)*UNIT-LEXPLODEHALF );
653 bAns = 0;
654 }
655 break;
656
657 case BULLET_RIGHT :
658 if( (map[r][d]==SOIL) || (map[r][d]==STEEL) || (map[r][d]==HOME) ||
659 (map[r][u]==SOIL) || (map[r][u]==STEEL) || (map[r][u]==HOME) )
660 {
661 ::NewExplode( r*UNIT-LEXPLODEHALF, y-DBULLET2EXPLODE );
662 bAns = 0;
663 }
664 break;
665
666 case BULLET_DOWN :
667 if( (map[l][d]==SOIL) || (map[l][d]==STEEL) || (map[l][d]==HOME) ||
668 (map[r][d]==SOIL) || (map[r][d]==STEEL) || (map[r][d]==HOME) )
669 {
670 ::NewExplode( x-DBULLET2EXPLODE, d*UNIT-LEXPLODEHALF );
671 bAns = 0;
672 }
673 break;
674
675 case BULLET_LEFT :
676 if( (map[l][d]==SOIL) || (map[l][d]==STEEL) || (map[l][d]==HOME) ||
677 (map[l][u]==SOIL) || (map[l][u]==STEEL) || (map[l][u]==HOME) )
678 {
679 ::NewExplode( (l+1)*UNIT-LEXPLODEHALF, y-DBULLET2EXPLODE );
680 bAns = 0;
681 }
682 break;
683 }
684 return bAns;
685}
686
687// explode
688BOOL NewExplode( INT xExp, INT yExp )
689{
690 INT i;
691 for( i=0; (i<TOTAL_EXPLODE)&&(bAliveExplode[i]); ++i )
692 ;
693 if( i >= TOTAL_EXPLODE )
694 return 0;
695 xExplode[i] = xExp;
696 yExplode[i] = yExp;
697 bAliveExplode[i] = 1;
698 ::BitBlt( hdcWnd, xExp, yExp, LEXPLODE, LEXPLODE,
699 hdcExplode, 0, 0, SRCINVERT );
700 return 1;
701}
702
703BOOL CheckExplode( VOID )
704{
705 INT i, x[2], y[2], j, k;
706 for( i=0; i<TOTAL_EXPLODE; ++i )
707 {
708 if( !bAliveExplode[i] ) continue;
709 bAliveExplode[i] = 0;
710 ::BitBlt( hdcWnd, xExplode[i], yExplode[i], LEXPLODE, LEXPLODE,
711 hdcExplode, 0, 0, SRCINVERT );
712 // tile
713 x[0] = xExplode[i] / UNIT;
714 x[1] = ( xExplode[i] + LEXPLODE ) / UNIT;
715 y[0] = yExplode[i] / UNIT;
716 y[1] = ( yExplode[i] + LEXPLODE ) / UNIT;
717 for( j=0; j<2; ++j )
718 for( k=0; k<2; ++k )
719 {
720 if( (map[x[j]][y[k]]==SOIL) || (map[x[j]][y[k]]==HOME) )
721 {
722 bEraseTile[x[j]][y[k]] = 1;
723 }
724 }
725 }
726 return 1;
727}
728
729// tile
730BOOL CheckTile( VOID )
731{
732 INT i, j;
733 for( i=0; i<WIDTH; ++i )
734 for( j=0; j<HEIGHT; ++j )
735 {
736 if( !bEraseTile[i][j] ) continue;
737 bEraseTile[i][j] = 0;
738 if( map[i][j] == DEAD ) continue;
739 ::BitBlt( hdcWnd, i*UNIT, j*UNIT, UNIT, UNIT,
740 hdcTile, map[i][j], 0, SRCINVERT );
741 if( map[i][j] != HOME )
742 {
743 map[i][j] = SPACE;
744 }
745 else
746 {
747 map[i][j] = DEAD;
748 iGameState = GS_LOSE;
749 ::BitBlt( hdcWnd, i*UNIT, j*UNIT, UNIT, UNIT,
750 hdcTile, map[i][j], 0, SRCINVERT );
751 // ::PlaySound( MAKEINTRESOURCE(IDR_GAMEOVER0), hInst, SND_RESOURCE | SND_SYNC );
752 }
753 }
754 return 1;
755}
756
757
758
759
760
761#endif
762
GlobalConstVar.h
1// GlobalConstVar.h
2//
3#ifndef INCLUDE_GLOBAL_CONST_VAR_H
4#define INCLUDE_GLOBAL_CONST_VAR_H
5
6#include "resource.h"
7#include "windows.h"
8#include "ctime"
9#include "cstdlib"
10
11// window
12CONST INT UNIT = 32;
13CONST INT WIDTH = 23;
14CONST INT HEIGHT = 20;
15CONST INT WND_WIDTH = WIDTH * UNIT;
16CONST INT WND_HEIGHT = HEIGHT * UNIT;
17CONST INT WND_DELTA_H = 32;
18CONST INT WND_DELTA_W = 6;
19CONST TCHAR WND_CLASS[] = "WND_CLASS";
20CONST TCHAR WND_CAPTION[] = "-Tank -- ZJ -";
21
22// game
23CONST UINT TOTAL_LEVEL = 6;
24CONST UINT ELAPSE_GAME = 110;
25CONST UINT IDT_GAME = 2;
26// game state
27CONST INT GS_NULL = 0;
28CONST INT GS_LOADING = 1;
29CONST INT GS_EXIT = 2;
30CONST INT GS_PAUSE = 3;
31CONST INT GS_LOSE = 4;
32CONST INT GS_WIN = 5;
33CONST INT GS_RESTART_GAME = 6;
34CONST INT GS_RESTART_LEVEL = 7;
35CONST INT GS_BACK_GAME = 8;
36CONST INT GS_STILL_PAUSE = 9;
37CONST INT GS_NEW_LEVEL = 10;
38CONST INT GS_CHOSE = 11;
39
40// tank
41CONST INT LTANK = 28;
42CONST INT DTANK = 2;
43CONST INT VTANK = UNIT / 4;
44CONST INT UP = 0 * LTANK;
45CONST INT RIGHT = 1 * LTANK;
46CONST INT DOWN = 2 * LTANK;
47CONST INT LEFT = 3 * LTANK;
48CONST INT DIRECTION_BASE = VK_LEFT;
49CONST INT DIRECTION[4] = { LEFT, UP, RIGHT, DOWN };
50CONST INT X_TANK_GO[4] = { -VTANK, 0, VTANK, 0 };
51CONST INT Y_TANK_GO[4] = { 0, -VTANK, 0, VTANK };
52// 0 player, else enemy
53CONST INT TANK_ARRAY_LENGTH = 4;
54CONST INT X_TANK_ORG[TANK_ARRAY_LENGTH] = { 9*UNIT+DTANK, DTANK, 11*UNIT+DTANK, 22*UNIT+DTANK };
55CONST INT Y_TANK_ORG[TANK_ARRAY_LENGTH] = { 19*UNIT+DTANK, DTANK, DTANK, DTANK };
56CONST INT X_DIR_TANK_ORG[TANK_ARRAY_LENGTH] = { 0, 0, LTANK*2, LTANK*4 };
57CONST INT Y_DIR_TANK_ORG[TANK_ARRAY_LENGTH] = { UP, DOWN, DOWN, DOWN };
58CONST INT TOTAL_TANK[TANK_ARRAY_LENGTH] = { 3, 3, 3, 3 };
59CONST INT FIRE_DELTA_TIME[TANK_ARRAY_LENGTH] = { 5, 8, 8, 8 };
60
61// bullet
62CONST INT TOTAL_BULLET = 200;
63CONST INT DBULLET = 12;
64CONST INT LBULLET = 8;
65CONST INT VBULLET = UNIT;
66CONST INT BULLET_DIR_BASE = LTANK;
67CONST INT BULLET_UP = 0;
68CONST INT BULLET_RIGHT = 1;
69CONST INT BULLET_DOWN = 2;
70CONST INT BULLET_LEFT = 3;
71CONST INT X_BULLET_GO[4] = { 0, VBULLET, 0, -VBULLET}; // UP, RIGHT, DOWN, LEFT
72CONST INT Y_BULLET_GO[4] = { -VBULLET, 0, VBULLET, 0 };
73
74// explode
75CONST INT LEXPLODE = 28;
76CONST INT DEXPLODE = 2;
77CONST INT TOTAL_EXPLODE = 200;
78CONST INT DBULLET2EXPLODE = 10;
79CONST INT LEXPLODEHALF = LEXPLODE / 2;
80
81// tile
82CONST INT SOIL = 0 * UNIT;
83CONST INT STEEL = 1 * UNIT;
84CONST INT TREE = 2 * UNIT;
85CONST INT SPACE = 3 * UNIT;
86CONST INT WATER = 4 * UNIT;
87CONST INT HOME = 5 * UNIT;
88CONST INT DEAD = 6 * UNIT;
89
90// game map source [level][y][x]
91CONST INT MAP_SOURCE[TOTAL_LEVEL][HEIGHT][WIDTH] =
92{
93 {
94 //0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
95 { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 },
96 { 0,3,0,3,0,3,0,3,0,3,3,0,3,0,3,3,0,0,3,3,0,3,0 },
97 { 0,3,0,3,0,3,0,3,0,3,3,0,3,0,3,3,0,0,3,3,0,3,0 },
98 { 0,3,0,3,0,3,0,3,0,3,3,0,3,0,3,3,0,0,3,3,0,3,0 },
99 { 0,1,1,1,3,3,3,3,3,3,0,0,0,3,3,3,3,3,3,1,1,1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3,3,3,3,3,3,3,0,1,2 },
228 { 2,2,1,0,3,3,3,3,3,3,0,0,0,3,3,3,3,3,3,0,1,2,2 },
229 { 2,2,2,2,0,3,3,3,3,3,0,5,0,3,3,3,3,3,0,2,2,2,2 }
230 }
231};
232
233#endif
234
GlobalVar.h
1// GlobalVar.h
2//
3#ifndef INCLUDE_GLOBAL_VAR_H
4#define INCLUDE_GLOBAL_VAR_H
5
6#include "GlobalConstVar.h"
7
8// window
9HINSTANCE hInst = NULL;
10HWND hWnd = NULL;
11HDC hdcWnd = NULL;
12HBRUSH hbrBg = NULL;
13
14// game
15INT iGameLevel, iGameState = GS_LOADING;
16
17HDC hdcLoading = NULL;
18HBITMAP hbmpLoading = NULL;
19
20HBITMAP hbmpMenu = NULL;
21HBITMAP hbmpPointer = NULL;
22HDC hdcMenu = NULL;
23HDC hdcPointer = NULL;
24
25// tank
26HBITMAP hbmpTank[2] = { NULL, NULL };
27HDC hdcTank[2] = { NULL, NULL };
28INT iTotKilledTank[TANK_ARRAY_LENGTH], iFireDeltaTime[TANK_ARRAY_LENGTH];
29BOOL bKilledTank[TANK_ARRAY_LENGTH];
30INT xTank[TANK_ARRAY_LENGTH], yTank[TANK_ARRAY_LENGTH];
31INT xDirTank[TANK_ARRAY_LENGTH], yDirTank[TANK_ARRAY_LENGTH];
32
33// bullet
34INT xBullet[TOTAL_BULLET], yBullet[TOTAL_BULLET], dirBullet[TOTAL_BULLET];
35BOOL bAliveBullet[TOTAL_BULLET], bNotFirst[TOTAL_BULLET], bByEnemy[TOTAL_BULLET];
36HBITMAP hbmpBullet = NULL;
37HDC hdcBullet = NULL;
38
39// explode
40INT xExplode[TOTAL_EXPLODE], yExplode[TOTAL_EXPLODE];
41BOOL bAliveExplode[TOTAL_EXPLODE];
42HBITMAP hbmpExplode = NULL;
43HDC hdcExplode = NULL;
44
45// tile
46HBITMAP hbmpTile = NULL;
47HDC hdcTile = NULL;
48INT map[WIDTH][HEIGHT];
49BOOL bEraseTile[WIDTH][HEIGHT];
50
51#endif
resource.h
1//{{NO_DEPENDENCIES}}
2// Microsoft Developer Studio generated include file.
3// Used by Tank.rc
4//
5#define IDC_GRAB 101
6#define IDI_TANK 102
7#define IDI_TANK_SMALL 105
8#define IDB_TILE 106
9#define IDB_PLAYER 107
10#define IDB_BULLET 109
11#define IDB_ENEMY 110
12#define IDB_GAMEOVER 111
13#define IDB_EXPLODE 112
14#define IDB_LOADING 113
15#define IDB_MENU 114
16#define IDB_POINTER 115
17#define IDB_WIN 117
18#define IDR_PRESSBUTTON 118
19#define IDR_SURE 119
20#define IDR_MENU 120
21#define IDR_GAMEOVER0 122
22#define IDR_GAMEOVER1 123
23#define IDR_BANG 124
24#define IDR_PLAYERSHOT 125
25#define IDR_WIN 128
26
27// Next default values for new objects
28//
29#ifdef APSTUDIO_INVOKED
30#ifndef APSTUDIO_READONLY_SYMBOLS
31#define _APS_NEXT_RESOURCE_VALUE 129
32#define _APS_NEXT_COMMAND_VALUE 40001
33#define _APS_NEXT_CONTROL_VALUE 1000
34#define _APS_NEXT_SYMED_VALUE 101
35#endif
36#endif
37
Tank.h
1// Tank.h
2//
3#ifndef INCLUDE_TANK_H
4#define INCLUDE_TANK_H
5
6#include "Functions.h"
7
8#endif
9
Tank.cpp
1// Tank.cpp
2//
3#include "Tank.h"
4
5LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
6
7int APIENTRY WinMain(HINSTANCE hInstance,
8 HINSTANCE hPrevInstance,
9 LPSTR lpCmdLine,
10 int nCmdShow)
11{
12 WNDCLASSEX wcex;
13 wcex.cbSize = sizeof(WNDCLASSEX);
14 wcex.cbClsExtra = 0;
15 wcex.cbWndExtra = 0;
16 wcex.hbrBackground = (HBRUSH)::GetStockObject( BLACK_BRUSH );
17 wcex.hCursor = ::LoadCursor( hInstance, MAKEINTRESOURCE(IDC_GRAB) );
18 wcex.hIcon = ::LoadIcon( hInstance, MAKEINTRESOURCE(IDI_TANK) );
19 wcex.hIconSm = ::LoadIcon( hInstance, MAKEINTRESOURCE(IDI_TANK_SMALL) );
20 wcex.hInstance = hInstance;
21 wcex.lpfnWndProc = WndProc;
22 wcex.lpszClassName = WND_CLASS;
23 wcex.lpszMenuName = NULL;
24 wcex.style = 0;
25
26 if( !::RegisterClassEx( &wcex ) )
27 {
28 ::MessageBox( NULL, "RegisterClassEx Failed !!!",
29 "Failed !!!", MB_ICONHAND | MB_OK );
30 return 0;
31 }
32
33 HWND hWnd = ::CreateWindowEx( 0, WND_CLASS, WND_CAPTION,
34 WS_POPUP | WS_CAPTION | WS_SYSMENU,
35 70, 20, WND_WIDTH+WND_DELTA_W, WND_HEIGHT+WND_DELTA_H,
36 ::GetDesktopWindow(), NULL, hInstance, NULL );
37 if( hWnd == NULL )
38 {
39 ::MessageBox( NULL, "CreateWindowEx Failed !!!",
40 "Failed", MB_ICONHAND | MB_OK );
41 return 0;
42 }
43 ::SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
44 ::ShowWindow( hWnd, SW_SHOW );
45
46 ::CreateGameResources( hInstance, hWnd );
47 MSG msg;
48 for(;;)
49 {
50 if( ::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
51 {
52 if( msg.message == WM_QUIT ) break;
53 ::TranslateMessage( &msg );
54 ::DispatchMessage( &msg );
55 }
56 else
57 {
58 ::CheckGameState();
59 }
60 }
61 ::DeleteGameResources();
62 ::UnregisterClass( WND_CLASS, wcex.hInstance );
63 return 0;
64}
65
66LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
67{
68 INT iHit;
69 switch( msg )
70 {
71 case WM_DESTROY :
72 ::PostQuitMessage( 0 );
73 return 0;
74
75 case WM_NCHITTEST :
76 iHit = ::DefWindowProc( hWnd, msg, wParam, lParam );
77 if( iHit == HTCAPTION )
78 iHit = HTCLIENT;
79 return iHit;
80
81 case WM_KEYDOWN :
82 switch( wParam )
83 {
84 case VK_PAUSE :
85 if( iGameState == GS_NULL )
86 {
87 iGameState = GS_PAUSE;
88 }
89 else
90 {
91 if( iGameState == GS_PAUSE )
92 {
93 iGameState = GS_NULL;
94 }
95 }
96 return 0;
97 }
98 break;
99
100 case WM_PAINT :
101 ::BeginPaint( hWnd, NULL );
102 ::EndPaint( hWnd, NULL );
103 ::Paint();
104 }
105 return ::DefWindowProc( hWnd, msg, wParam, lParam );
106}
107
Tank.rc
1//Microsoft Developer Studio generated resource script.
2//
3#include "resource.h"
4
5#define APSTUDIO_READONLY_SYMBOLS
6/**//////////////////////////////////////////////////////////////////////////////
7//
8// Generated from the TEXTINCLUDE 2 resource.
9//
10#include "afxres.h"
11
12/**//////////////////////////////////////////////////////////////////////////////
13#undef APSTUDIO_READONLY_SYMBOLS
14
15/**//////////////////////////////////////////////////////////////////////////////
16// Chinese (中国) resources
17
18#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
19#ifdef _WIN32
20LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
21#pragma code_page(936)
22#endif //_WIN32
23
24#ifdef APSTUDIO_INVOKED
25/**//////////////////////////////////////////////////////////////////////////////
26//
27// TEXTINCLUDE
28//
29
301 TEXTINCLUDE DISCARDABLE
31BEGIN
32 "resource.h\0"
33END
34
352 TEXTINCLUDE DISCARDABLE
36BEGIN
37 "#include ""afxres.h""\r\n"
38 "\0"
39END
40
413 TEXTINCLUDE DISCARDABLE
42BEGIN
43 "\r\n"
44 "\0"
45END
46
47#endif // APSTUDIO_INVOKED
48
49
50/**//////////////////////////////////////////////////////////////////////////////
51//
52// Cursor
53//
54
55IDC_GRAB CURSOR DISCARDABLE "Resource\\Grab.cur"
56
57/**//////////////////////////////////////////////////////////////////////////////
58//
59// Icon
60//
61
62// Icon with lowest ID value placed first to ensure application icon
63// remains consistent on all systems.
64IDI_TANK ICON DISCARDABLE "Resource\\Tank.ico"
65IDI_TANK_SMALL ICON DISCARDABLE "Resource\\TankSmall.ico"
66
67/**//////////////////////////////////////////////////////////////////////////////
68//
69// Bitmap
70//
71
72IDB_TILE BITMAP DISCARDABLE "Resource\\Tile.bmp"
73IDB_PLAYER BITMAP DISCARDABLE "Resource\\Player.bmp"
74IDB_BULLET BITMAP DISCARDABLE "Resource\\Bullet.bmp"
75IDB_ENEMY BITMAP DISCARDABLE "Resource\\Enemy.bmp"
76IDB_GAMEOVER BITMAP DISCARDABLE "Resource\\Gameover.bmp"
77IDB_EXPLODE BITMAP DISCARDABLE "Resource\\Explode.bmp"
78IDB_LOADING BITMAP DISCARDABLE "Resource\\Loading.bmp"
79IDB_MENU BITMAP DISCARDABLE "Resource\\Menu.bmp"
80IDB_POINTER BITMAP DISCARDABLE "Resource\\Pointer.bmp"
81IDB_WIN BITMAP DISCARDABLE "Resource\\Win.bmp"
82
83/**//////////////////////////////////////////////////////////////////////////////
84//
85// WAVE
86//
87
88IDR_PRESSBUTTON WAVE DISCARDABLE "Resource\\PressButton.wav"
89IDR_SURE WAVE DISCARDABLE "Resource\\Sure.WAV"
90IDR_MENU WAVE DISCARDABLE "Resource\\Menu.WAV"
91IDR_GAMEOVER0 WAVE DISCARDABLE "Resource\\Gameover0.WAV"
92IDR_GAMEOVER1 WAVE DISCARDABLE "Resource\\Gameover1.WAV"
93IDR_BANG WAVE DISCARDABLE "Resource\\Bang.wav"
94IDR_PLAYERSHOT WAVE DISCARDABLE "Resource\\PShot.wav"
95IDR_WIN WAVE DISCARDABLE "Resource\\Win.wav"
96#endif // Chinese (中国) resources
97/**//////////////////////////////////////////////////////////////////////////////
98
99
100
101#ifndef APSTUDIO_INVOKED
102/**//////////////////////////////////////////////////////////////////////////////
103//
104// Generated from the TEXTINCLUDE 3 resource.
105//
106
107
108/**//////////////////////////////////////////////////////////////////////////////
109#endif // not APSTUDIO_INVOKED
110
111