-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathPanzerWar.c
1590 lines (1473 loc) · 33.2 KB
/
PanzerWar.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"msimg32.lib")
#define KEY(k) GetAsyncKeyState(k)
#define BUMP(x1,y1,w1,h1,x2,y2,w2,h2) (((x1)+(w1)>(x2))&&((x1)<(x2)+(w2))&&((y1)+(h1)>(y2))&&((y1)<(y2)+(h2)))
#define INFINITE 0xFFFFFFFF
#define DEFRESIST -1
#define RESISTNORMAL 1000
#define ERRORRET -1
#define MAPX g_map[g_curmap].item.x
#define MAPY g_map[g_curmap].item.y
#define MAPWID g_map[g_curmap].item.w
#define MAPHEI g_map[g_curmap].item.h
#define TANKNUM g_map[g_curmap].tanknum
#define BLKNUM g_map[g_curmap].blknum
#define NXTP do{p++;}while(*p!=',');do{p++;}while(*p==' ');//读取配置文件时转到下一个逗号后的参数
#define RMVB do{p++;}while(*p==' ');//消除空格
#define LOOPINC(i,lim) ((i)>=((lim)-1)?(i)=0:++(i))
#define LOOPSUB(i,lim) ((i)<=0?(i)=(lim)-1:--(i))
#define SVPG(buf) fwrite(&(buf),sizeof(buf),1,fp)
#define RDPG(buf) fread(&(buf),sizeof(buf),1,fp)
#define PLAYANI(index) g_ani[(index)].state=ANI_RUN
#define STOPANI(index) g_ani[(index)].state=ANI_INVISIBLE
#define SOUND(name) PlaySound((name),NULL,SND_FILENAME|SND_ASYNC)
#define FULLEXP ((60)*g_curlevel-20)
#define INFO_MAP_X 30
#define INFO_MAP_Y 0
#define INFO_HP_X 30
#define INFO_HP_Y 30
#define INFO_LEVEL_X 30
#define INFO_LEVEL_Y 60
#define INFO_EXP_X 30
#define INFO_EXP_Y 90
#define INFO_TEXTCOLOR(ra) RGB(255,255*(ra),255*(ra))
#define GAP_X 250
#define GAP_Y 150
#define GAP_STEP 30
#define GAP_MAP_X GAP_X
#define GAP_MAP_Y GAP_Y-GAP_STEP
#define GAP_HP_X GAP_X
#define GAP_HP_Y GAP_Y+GAP_STEP*0
#define GAP_LEVEL_X GAP_X
#define GAP_LEVEL_Y GAP_Y+GAP_STEP*1
#define GAP_EXP_X GAP_X
#define GAP_EXP_Y GAP_Y+GAP_STEP*2
#define GAP_POWER_X GAP_X
#define GAP_POWER_Y GAP_Y+GAP_STEP*3
#define GAP_RATE_X GAP_X
#define GAP_RATE_Y GAP_Y+GAP_STEP*4
#define GAP_TEXTCOLOR RGB(50,50,255)
#define EXPADD 20
#define BLOCKEXPRATE 30
#define MAXNAMELEN 50
#define MAXBMPNUM 50
#define MAXANIPAGE 10
#define MAXMAPNUM 30
#define MAXLEVELNUM 30
#define MAXPICITEMNUM 100
#define MAXTANKTYPE 30
#define MAXANINUM 300
#define MAXBLOCKTYPE 30
#define MAXBLOCKNUM 200
#define MAXTANKNUM 50
#define MAXGUNTYPE 30
#define MAXLEVEL 15
#define GAMEOVERDELAY 15
#define WINDELAY 50
#define GAPDELAY 50
#define ADDHPITV 50
#define FILE_PROGRESS "./data/progress.dat"
#define FILE_CONFIG "./data/data.dat"
#define SOUND_GUN "./data/gun0.wav"
#define SOUND_EXPLO "./data/explo0.wav"
#define SOUND_BUMP "./data/bump0.wav"
#define SOUND_MENU "./data/menu0.wav"
//------------------自定数据类型-------------------------
enum E_EORROR{ERR_REMIND,ERR_TERMINAL};
enum E_GAMESTATE{GAME_INIT,GAME_SHOWCOVER,GAME_MENU,GAME_LOADPROGRESS,
GAME_RETURN,GAME_RUN,GAME_OVER,GAME_WIN,
GAME_SHOWGAPSCR,GAME_HELP,GAME_QUIT};
enum E_ANI{ANI_LOGO=0,ANI_MENU=1,ANI_EXPLO=2,ANI_GAMEOVER=3,ANI_EXPLO1=4,ANI_UPGRADE=5,
ANI_WIN=6,ANI_RETMENU=7,ANI_PRESSENTER=8,ANI_BLACKSCR=9,ANI_SAVE=10,ANINUM};
enum E_BLOCK{BLK_GRASS=0};
enum E_DIRECT{DX_LEFT,DX_UP,DX_RIGHT,DX_DOWN};
enum E_ANISTATE{ANI_INVISIBLE,ANI_FREE,ANI_RUN,ANI_SHOWONCE,ANI_RUNTOFREE};
struct tpPicItem
{
int x,y;
int w,h;
int bmp;
}g_picitem[MAXPICITEMNUM];
struct tpScrItem
{
int x,y;
int w,h;
int picid[MAXANIPAGE];
int timecouter;
int curpic;
int picstep;
int picnum;
};
struct tpANI
{
struct tpScrItem item;
int state;
}g_ani[MAXANINUM];
struct tpBlock//地图固定方块
{
struct tpScrItem item;
int life;
int resist;
}g_blk[MAXBLOCKNUM];
struct tpTank
{
struct tpScrItem item;
int life;
int fulllife;
int rate;
int currate;
int gun;
int guninterval;
int guncounter;
enum E_DIRECT dx;
// int resist;
}g_tank[MAXTANKNUM];
struct tpGun
{
struct tpScrItem item;
int rate;
struct tpTank *father;
int power;
enum E_DIRECT dx;
struct tpGun *next;
}*g_gun;
struct tpMap
{
struct tpScrItem item;
int tanknum;
int blknum;
}g_map[MAXMAPNUM];
struct tpLevel
{
int mapid;
}g_level[MAXLEVELNUM];
struct tpConfig
{
char WIN_CLASS[MAXNAMELEN],WIN_TITLE[MAXNAMELEN];
int WIN_WIDTH,WIN_HEIGHT;
int TIME_WAITNUM,TIME_INTERVAL;
DWORD BMP_TRANSP;
int MAP_NUM;
// int LEVEL_NUM;
int BMPNUM;
char BMPNAME[MAXBMPNUM][MAXNAMELEN];
int PICITEMNUM;
}G_CFG;
//----------------全局变量-------------------------
HWND g_hWnd;
HDC g_hDC,g_hSwapDC;
HDC *g_hBmpDC;
HBITMAP g_hSwapBmp;
HBITMAP *g_hBmp;
int g_curlevel;
int g_curexp;
int g_curmap;
int g_offsetX;
int g_offsetY;
char *g_cfgbuf;
struct tpGun *g_pGunHead;
struct tpTank g_tanktype[MAXTANKTYPE];
struct tpBlock g_blktype[MAXBLOCKTYPE];
struct tpGun g_guntype[MAXGUNTYPE];
enum E_GAMESTATE g_gamestate;
//-----------------函数声明---------------------------
void Init(HINSTANCE hInstance,int nCmdShow);
void Destroy();
void GameLoop();
void Draw(struct tpScrItem *scritem);
void ShowMenu();
void ShowRetMenu();
void ShowGapscr();
void ShowHelp();
void GameRun();
void GameInit();
void GameOver();
void GameAIMove(int *x,int *y,struct tpTank *tank);
void GameAIGun(struct tpTank *tank);
void CreateGun(struct tpTank *tank);
void TankEvent();
void GunEvent();
void ClrGun(struct tpGun *gun);
void GameShutdown();
void GameDraw();
void GameWin();
void DrawInfo();
void OnUpgrade();
void SaveProgress();
void LoadProgress();
void CalcTank(int level,struct tpTank* tank);
int ReadInt(char name[]);
int ReadStr(char name[],char *str);
int ReadPicItem(int index,struct tpPicItem* picitem);
int ReadMapInfo(int index,struct tpMap* map);
int ReadLevelInfo(int index,struct tpLevel* level);
int ReadTank(int index,struct tpTank* tank);
int ReadANI(int index,struct tpANI* ani);
int ReadBlock(int index,struct tpBlock* block);
int ReadGun(int index,struct tpGun* gun);
void ReadMapBlock(int index);
void ReadMapTank(int map);
void ReadCfg();
int StartAni(int index,int x,int y,enum E_ANISTATE state);
void OnError(char info[],enum E_EORROR err);
int ProduceRandMap();
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
//------------------主函数及消息模块START-----------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
//初始化
int timewaitnum=0;
Init(hInstance,nCmdShow);
g_gamestate=GAME_SHOWCOVER;
while (g_gamestate!=GAME_QUIT)
{
//消息处理
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(timewaitnum>=G_CFG.TIME_WAITNUM)
{//进入游戏循环
GameLoop();
timewaitnum=0;
}
else timewaitnum++;
//等待下一帧
Sleep(G_CFG.TIME_INTERVAL);
}
Destroy();//释放变量
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(g_hWnd, &ps);
EndPaint(g_hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KILLFOCUS:
break;
case WM_SETFOCUS:
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//--------------主函数及消息模块END----------------------
//-------------初始化模块START-----------------
void Init(HINSTANCE hInstance,int nCmdShow)
{
int i;
unsigned flen;
FILE *fp;
WNDCLASSEX wc={sizeof(WNDCLASSEX),
CS_CLASSDC,WndProc,
0L,
0L,
hInstance,
NULL,
LoadCursor(NULL,IDC_ARROW),
CreateSolidBrush(0),
NULL,
NULL,
NULL};
//全局变量初始化
g_hWnd=0;
g_gamestate=GAME_INIT;
g_pGunHead=NULL;
g_curlevel=1;
g_curexp=0;
g_gun=NULL;
for(i=0;i<MAXANINUM;i++)
{
if(i<ANINUM)
g_ani[i].state=ANI_INVISIBLE;
else g_ani[i].state=ANI_FREE;
g_ani[i].item.timecouter=0;
}
//配置文件初始化
if((fp=fopen(FILE_CONFIG,"rb"))==NULL)
OnError("打开配置文件失败",ERR_TERMINAL);
fseek(fp,0L,SEEK_END);
flen=ftell(fp);
g_cfgbuf=(char*)malloc(flen);
fseek(fp,0L,SEEK_SET);
if(!fread(g_cfgbuf,flen,1,fp))
OnError("读取配置文件错误",ERR_TERMINAL);
fclose(fp);
ReadCfg();
//窗口初始化
wc.lpszClassName=G_CFG.WIN_CLASS;
RegisterClassEx(&wc);
g_hWnd=CreateWindowEx((unsigned)NULL,G_CFG.WIN_CLASS, G_CFG.WIN_TITLE, WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
(GetSystemMetrics(SM_CXSCREEN)-G_CFG.WIN_WIDTH)/2,(GetSystemMetrics(SM_CYSCREEN)-G_CFG.WIN_HEIGHT)/2,
G_CFG.WIN_WIDTH+GetSystemMetrics(SM_CXFIXEDFRAME)*2,
G_CFG.WIN_HEIGHT+GetSystemMetrics(SM_CXFIXEDFRAME)*2+GetSystemMetrics(SM_CYCAPTION),
NULL, NULL, hInstance, NULL);
if (!g_hWnd)OnError("创建窗口失败",ERR_TERMINAL);
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
//绘图初始化
g_hDC=GetDC(g_hWnd);
g_hSwapDC=CreateCompatibleDC(g_hDC);
g_hSwapBmp=CreateCompatibleBitmap(g_hDC,G_CFG.WIN_WIDTH,G_CFG.WIN_HEIGHT);
SelectObject(g_hSwapDC,g_hSwapBmp);
SetTextColor(g_hSwapDC,INFO_TEXTCOLOR(1));
SetBkMode(g_hSwapDC,TRANSPARENT);
for(i=0;i<G_CFG.BMPNUM;i++)
{
g_hBmp[i]=(HBITMAP)LoadImage(hInstance,G_CFG.BMPNAME[i],IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION |LR_LOADFROMFILE);
if(g_hBmp==NULL)OnError("加载位图失败",ERR_TERMINAL);
g_hBmpDC[i]=CreateCompatibleDC(g_hDC);
SelectObject(g_hBmpDC[i],g_hBmp[i]);
}
}
int ProduceRandMap()
{
int i,j,k,blk,tank;
char tankbuf[500]={0},buf[50];
FILE *fp;
int evryblknum=4;
blk=0;
tank=0;
if((fp=fopen("map.txt","wt"))==NULL)
{
printf("Error");
exit(0);
}
srand((int)g_hWnd);
for(k=0;k<16;k++)
{
fprintf(fp,"<MapBlocks%d>\n",k);
for(i=0;i<550;i+=50)
for(j=0;j<750;j+=(rand()%50+50))
{
if(!(rand()%(20-k))||i==0&&j==0)
{
sprintf(buf,"(%d,%d,%d)",j,i+rand()%50,(int)(tank/2)*2);
strcat(tankbuf,buf);
}
else
fprintf(fp,"(%d,%d,%d)",j,i+rand()%50,blk+rand()%evryblknum);
}
if(blk>16)
{
MessageBox(g_hWnd,"错误!","ok",MB_OK);
exit(0);
}
else if((k+1)%evryblknum==0)blk+=evryblknum;
// if(blk>=16) blk=0;
tank++;
fprintf(fp,"\n</MapBlocks%d>\n\n",k);
fprintf(fp,"<MapTanks%d>\n",k);
fputs(tankbuf,fp);
fprintf(fp,"\n</MapTanks%d>\n\n",k);
strcpy(tankbuf,"");
}
MessageBox(g_hWnd,"Done!","ok",MB_OK);
fclose(fp);
return 0;
}
void ReadCfg()
{
int i,r,g,b;
char fname[MAXNAMELEN];
char tmp[MAXNAMELEN];
//读取配置文件
ReadStr("WindowClass",G_CFG.WIN_CLASS);
ReadStr("WindowName",G_CFG.WIN_TITLE);
G_CFG.WIN_WIDTH =ReadInt("WindowWidth");
G_CFG.WIN_HEIGHT =ReadInt("WindowHeight");
G_CFG.TIME_WAITNUM =ReadInt("WaitNumEveryCycle");
G_CFG.TIME_INTERVAL =ReadInt("TimeInterval");
r=ReadInt("TransparentColorRed");
g=ReadInt("TransparentColorGreen");
b=ReadInt("TransparentColorBlue");
G_CFG.BMP_TRANSP=RGB(r,g,b);
/* //Read Level Info
for(i=0;ReadLevelInfo(i,&g_level[i]);i++)
;
if(i<=0)
OnError("读取等级信息时发生错误",ERR_TERMINAL);
G_CFG.LEVEL_NUM=i;
*/
//Read Map Info
for(i=0;ReadMapInfo(i,&g_map[i]);i++)
;
if(i<=0)
OnError("读取地图信息时发生错误",ERR_TERMINAL);
G_CFG.MAP_NUM=i;
//Read Bitmap name
for(i=0;;i++)
{
sprintf(tmp,"BmpName%d",i);
if(!ReadStr(tmp,fname)) break;
strcpy(G_CFG.BMPNAME[i],fname);
}
if(i<=0)
OnError("读取图像时发生错误",ERR_TERMINAL);
G_CFG.BMPNUM=i;
g_hBmp=(HBITMAP*)calloc(G_CFG.BMPNUM,sizeof(HBITMAP));
g_hBmpDC=(HDC*)calloc(G_CFG.BMPNUM,sizeof(HDC));
//Read PicItem
for(i=0;ReadPicItem(i,&g_picitem[i]);i++)
;
if(i<=0)
OnError("读取图像项目时发生错误",ERR_TERMINAL);
G_CFG.PICITEMNUM=++i;
//Read Tank Type
for(i=0;i<MAXTANKTYPE;i++)
if(!ReadTank(i,&g_tanktype[i]))
break;
if(i<=0)
OnError("读取坦克类型时发生错误",ERR_TERMINAL);
//read block type
for(i=0;i<MAXBLOCKTYPE;i++)
if(!ReadBlock(i,&g_blktype[i]))
break;
if(i<=0)
OnError("读取方块类型时发生错误",ERR_TERMINAL);
//read ani
for(i=0;i<MAXANINUM;i++)
if(!ReadANI(i,&g_ani[i]))
break;
if(i<=0)
OnError("读取用户界面时发生错误",ERR_TERMINAL);
//read gun
for(i=0;i<MAXANINUM;i++)
if(!ReadGun(i,&g_guntype[i]))
break;
if(i<=0)
OnError("读取用户界面时发生错误",ERR_TERMINAL);
}
//保存进度
void SaveProgress()
{
FILE *fp;
int i,nblk,ntank,nani;
nblk=BLKNUM;
ntank=TANKNUM;
nani=MAXANINUM;
if((fp=fopen(FILE_PROGRESS,"wb"))==NULL)
{
OnError("打开存档文件错误",ERR_REMIND);
return;
}
SVPG(g_curlevel);
SVPG(g_curexp);
SVPG(g_curmap);
SVPG(g_offsetX);
SVPG(g_offsetY);
//方块
SVPG(nblk);
for(i=0;i<nblk;i++)
SVPG(g_blk[i]);
//坦克
SVPG(ntank);
for(i=0;i<ntank;i++)
SVPG(g_tank[i]);
//动画
SVPG(nani);
for(i=0;i<nani;i++)
SVPG(g_ani[i]);
fclose(fp);
}
void LoadProgress()
{
FILE *fp;
int i,nblk,ntank,nani;
if((fp=fopen(FILE_PROGRESS,"rb"))==NULL)
{
OnError("打开存档文件错误",ERR_REMIND);
return;
}
RDPG(g_curlevel);
RDPG(g_curexp);
RDPG(g_curmap);
RDPG(g_offsetX);
RDPG(g_offsetY);
//方块
RDPG(nblk);
if(nblk<0||nblk>MAXBLOCKNUM)
OnError("读取存档错误",ERR_TERMINAL);
for(i=0;i<nblk;i++)
RDPG(g_blk[i]);
//坦克
RDPG(ntank);
if(ntank<0||ntank>MAXTANKNUM)
OnError("读取存档错误",ERR_TERMINAL);
for(i=0;i<ntank;i++)
RDPG(g_tank[i]);
//动画
RDPG(nani);
if(nani<0||nani>MAXANINUM)
OnError("读取存档错误",ERR_TERMINAL);
for(i=0;i<nani;i++)
RDPG(g_ani[i]);
BLKNUM=nblk;
TANKNUM=ntank;
fclose(fp);
}
int ReadInt(char name[])
{
char *p;
if((p=strstr(g_cfgbuf,name))==NULL)
OnError("读取整数时发生错误",ERR_TERMINAL);
p=strchr(p,'=');
RMVB
return atoi(p);
}
int ReadStr(char name[],char *str)
{
char *p,*q;
int len;
if((p=strstr(g_cfgbuf,name))==NULL)
return 0;
while(*p!='\"'){p++;}
q=++p;
while(*q!='\"'){q++;}
len=q-p;
memcpy(str,p,len);
str[len]='\0';
return 1;
}
int ReadMapInfo(int index,struct tpMap* map)
{
char *p;
char tmp[MAXNAMELEN];
sprintf(tmp,"Map%d",index);
if((p=strstr(g_cfgbuf,tmp))==NULL)
return 0;
p=strchr(p,'=');
RMVB
map->item.w=atoi(p);
NXTP map->item.h=atoi(p);
NXTP map->item.picid[0]=atoi(p);
map->item.picstep=1;
return 1;
}
int ReadANI(int index,struct tpANI* ani)
{
char *p;
char tmp[MAXNAMELEN];
int pit;
sprintf(tmp,"Animation%d",index);
if((p=strstr(g_cfgbuf,tmp))==NULL)
return 0;
p=strchr(p,'=');
RMVB
ani->item.x=atoi(p);
NXTP ani->item.y=atoi(p);
NXTP ani->item.w=atoi(p);
NXTP ani->item.h=atoi(p);
NXTP ani->item.picstep=atoi(p);
NXTP for(pit=0;(ani->item.picid[pit]=atoi(p))!=0&&*p!='0';pit++)
{NXTP}
ani->item.picnum=pit;
ani->item.timecouter=0;
ani->state=ANI_INVISIBLE;
return 1;
}
int ReadBlock(int index,struct tpBlock* block)
{
char *p;
char tmp[MAXNAMELEN];
int pit;
sprintf(tmp,"Block%d",index);
if((p=strstr(g_cfgbuf,tmp))==NULL)
return 0;
p=strchr(p,'=');
RMVB
block->item.x=atoi(p);
NXTP block->item.y=atoi(p);
NXTP block->item.w=atoi(p);
NXTP block->item.h=atoi(p);
NXTP block->resist=atoi(p);
NXTP block->life=atoi(p);
NXTP block->item.picstep=atoi(p);
NXTP for(pit=0;(block->item.picid[pit]=atoi(p))!=0;pit++)
{NXTP}
block->item.timecouter=0;
block->item.picnum=pit;
return 1;
}
int ReadGun(int index,struct tpGun* gun)
{
char *p;
char tmp[MAXNAMELEN];
int pit;
sprintf(tmp,"Gun%d",index);
if((p=strstr(g_cfgbuf,tmp))==NULL)
return 0;
p=strchr(p,'=');
RMVB
gun->item.x=atoi(p);
NXTP gun->item.y=atoi(p);
NXTP gun->item.w=atoi(p);
NXTP gun->item.h=atoi(p);
NXTP gun->rate=atoi(p);
NXTP gun->power=atoi(p);
NXTP gun->item.picstep=atoi(p);
NXTP for(pit=0;(gun->item.picid[pit]=atoi(p))!=0;pit++)
{NXTP}
gun->item.picnum=pit;
gun->item.timecouter=0;
return 1;
}
int ReadTank(int index,struct tpTank* tank)
{
char *p;
char tmp[MAXNAMELEN];
int pit;
sprintf(tmp,"Tank%d",index);
if((p=strstr(g_cfgbuf,tmp))==NULL)
return 0;
p=strchr(p,'=');
RMVB
tank->item.x=atoi(p);
NXTP tank->item.y=atoi(p);
NXTP tank->item.w=atoi(p);
NXTP tank->item.h=atoi(p);
NXTP tank->rate=atoi(p);
NXTP tank->life=atoi(p);
NXTP tank->gun=atoi(p);
NXTP tank->guninterval=atoi(p);
NXTP tank->item.picstep=atoi(p);
NXTP for(pit=0;(tank->item.picid[pit]=atoi(p))!=0;pit++)
{NXTP}
tank->item.picnum=pit;
tank->item.timecouter=0;
tank->guncounter=0;
return 1;
}
int ReadLevelInfo(int index,struct tpLevel* level)
{
char *p;
char tmp[MAXNAMELEN];
sprintf(tmp,"Level%d",index);
if((p=strstr(g_cfgbuf,tmp))==NULL)
return 0;
p=strchr(p,'=');
RMVB
level->mapid=atoi(p);
return 1;
}
int ReadPicItem(int index,struct tpPicItem* picitem)
{
char *p;
char tmp[MAXNAMELEN];
sprintf(tmp,"PictureItem%d",index);
if((p=strstr(g_cfgbuf,tmp))==NULL)
return 0;
p=strchr(p,'=');
RMVB
picitem->x=atoi(p);
NXTP picitem->y=atoi(p);
NXTP picitem->w=atoi(p);
NXTP picitem->h=atoi(p);
NXTP picitem->bmp=atoi(p);
return 1;
}
void ReadMapTank(int map)
{
char *p = NULL, *q = NULL;
int i,x,y,type;
char tmp[MAXNAMELEN],endflag[MAXNAMELEN];
sprintf(tmp,"<MapTanks%d>",map);
sprintf(endflag,"</MapTanks%d>",map);
if((p=strstr(g_cfgbuf,tmp))==NULL||
(q=strstr(g_cfgbuf,endflag))==NULL)
OnError("加载地图失败",ERR_TERMINAL);
i=0;
while(1)
{
p=strchr(p,'(');
if(p>q)break;
sscanf(p,"(%d,%d,%d)",&x,&y,&type);
memcpy(&g_tank[i],&g_tanktype[type],sizeof(struct tpTank));
g_tank[i].item.x=x;
g_tank[i].item.y=y;
g_tank[i].currate=g_tank[i].rate;
g_tank[i].fulllife=g_tank[i].life;
p++;i++;
p=strchr(p,'(');
}
TANKNUM=i;
}
void ReadMapBlock(int index)
{
char *p = NULL,*q = NULL;
int i=0,x,y,type;
char tmp[MAXNAMELEN],endflag[MAXNAMELEN];
sprintf(tmp,"<MapBlocks%d>",index);
sprintf(endflag,"</MapBlocks%d>",index);
if((p=strstr(g_cfgbuf,tmp))==NULL||
(q=strstr(g_cfgbuf,endflag))==NULL)
OnError("加载地图失败",ERR_TERMINAL);
while(1)
{
p=strchr(p,'(');
if(p>q)break;
sscanf(p,"(%d,%d,%d)",&x,&y,&type);
memcpy(&g_blk[i],&g_blktype[type],sizeof(struct tpBlock));
g_blk[i].item.x=x;
g_blk[i].item.y=y;
p++;i++;
}
BLKNUM=i;
}
//-------------初始化模块END-----------------
//-------------游戏流程控制START---------------
void GameLoop()
{
switch(g_gamestate)
{
case GAME_SHOWCOVER:
g_ani[ANI_LOGO].state=ANI_RUN;
g_ani[ANI_PRESSENTER].state=ANI_RUN;
Draw(&g_ani[ANI_LOGO].item);
Draw(&g_ani[ANI_PRESSENTER].item);
if(KEY(VK_RETURN))
{
g_ani[ANI_LOGO].state=ANI_INVISIBLE;
g_ani[ANI_PRESSENTER].state=ANI_INVISIBLE;
g_gamestate=GAME_MENU;
Sleep(100);
}
break;
case GAME_RUN:
if(KEY(VK_ESCAPE))
{
g_gamestate=GAME_RETURN;
}
else GameRun();
break;
case GAME_OVER:
g_offsetX=0;
g_offsetY=0;
GameOver();
break;
case GAME_HELP:
ShowHelp();
break;
case GAME_LOADPROGRESS:
GameShutdown();
LoadProgress();
g_ani[ANI_RETMENU].state=ANI_INVISIBLE;
g_gamestate=GAME_RUN;
break;
case GAME_MENU:
g_offsetX=0;
g_offsetY=0;
ShowMenu();
break;
case GAME_RETURN:
g_offsetX=0;
g_offsetY=0;
ShowRetMenu();
break;
case GAME_SHOWGAPSCR:
g_offsetX=0;
g_offsetY=0;
ShowGapscr();
break;
case GAME_WIN:
g_offsetX=0;
g_offsetY=0;
GameWin();
Draw(&g_ani[ANI_WIN].item);
break;
case GAME_QUIT:
return;
//default:
}
//swap to screen
if(!BitBlt(g_hDC,0,0,G_CFG.WIN_WIDTH,G_CFG.WIN_HEIGHT,g_hSwapDC,0,0,SRCCOPY)) OnError("绘图错误",ERR_TERMINAL);
}
void GameOver()
{
static int timer=0;
g_ani[ANI_WIN].state=ANI_RUN;
if(timer>=WINDELAY)
{
GameShutdown();
g_gamestate=GAME_MENU;
g_ani[GAME_MENU].item.curpic=0;
g_curlevel=1;
g_curexp=0;
}
else
timer++;
Draw(&g_ani[ANI_GAMEOVER].item);
}
void GameWin()
{
static int timer=0;
g_ani[ANI_WIN].state=ANI_RUN;
if(timer>=WINDELAY)
{
g_ani[ANI_WIN].state=ANI_INVISIBLE;
g_curmap++;
GameShutdown();
g_gamestate=GAME_SHOWGAPSCR;
timer=0;
}
else
timer++;
}
void GameInit()
{
//初始化地图方块
ReadMapBlock(g_curmap);
//初始化坦克
ReadMapTank(g_curmap);
CalcTank(g_curlevel,&g_tank[0]);
}
void GameShutdown()
{
ClrGun(g_pGunHead);
g_pGunHead=NULL;
}
void ClrGun(struct tpGun *gun)
{
if(gun==0) return;
if(gun->next==0)
free(gun);
else
{
ClrGun(gun->next);
free(gun);
}
}
void GameRun()
{
static int delay=0;
static unsigned long addhp=0;
int i,ifwin=1;
TankEvent();
GunEvent();
addhp++;
if(!(addhp%ADDHPITV)&&g_tank[0].life<g_tank[0].fulllife)
g_tank[0].life++;
if(g_tank[0].life<=0)
{//游戏结束
if(delay>=GAMEOVERDELAY)
{
g_gamestate=GAME_OVER;
GameShutdown();
}
else delay++;
}
else delay=0;
for(i=1;i<TANKNUM;i++)//检查是否胜利
if(g_tank[i].life>0)
ifwin=0;
if(ifwin) g_gamestate=GAME_WIN;
GameDraw();
}
//-------------游戏流程控制END---------------
//-------------游戏逻辑控制START-------------
void GameAIMove(int *x,int *y,struct tpTank *tank)
{//敌方移动
enum E_DIRECT dx;
int rate;
// dx=(enum E_DIRECT)(rand()%4);
if(rand()%2)
if(g_tank[0].item.x>*x) dx=DX_RIGHT;
else dx=DX_LEFT;
else
if(g_tank[0].item.y>*y) dx=DX_DOWN;
else dx=DX_UP;
tank->dx=dx;
rate=tank->rate;
switch(dx)
{
case DX_LEFT:
*x-=rate;
break;
case DX_RIGHT:
*x+=rate;
break;
case DX_UP:
*y-=rate;
break;
case DX_DOWN:
*y+=rate;
break;
}
}
void GameAIGun(struct tpTank *tank)
{//敌方shot
if(!(rand()%5))
CreateGun(tank);