话不多说 我们今天就来创造出属于我们自己的《坦克大战》,GOGOGO!!! 直接开始吧 这次的源码比较详细,我分了好几个cpp文件,思路更加的清晰,请耐心用心的观看
首先就是我们载入图片的函数tupian.cpp
# include "tanke.h" 障碍物 void LaoWang(int * tilex, int * tiley) { IMAGE img; loadimage(&img, _T("res\\tile.bmp")); putimage(*tilex, *tiley, 32 , 32 , &img, 32 * 5, 0 ); } void tileHong(int * tilex, int * tiley) { IMAGE img; loadimage(&img, _T("res\\tile.bmp")); putimage(*tilex, *tiley, 32, 32, &img, 32 * 0, 0 ); return; } void tileLv(int * tilex, int * tiley) { IMAGE img; loadimage(&img, _T("res\\tile.bmp")); putimage(*tilex, *tiley, 32, 32, &img, 32 * 2, 0 ); return; } void tileBai(int * tilex, int * tiley) { IMAGE img; loadimage(&img, _T("res\\tile.bmp")); putimage(*tilex, *tiley, 32, 32, &img, 32 * 1, 0 ); return; } void tileBlue(int *tilex, int *tiley) { IMAGE img; loadimage(&img, _T("res\\tile.bmp")); putimage(*tilex, *tiley, 32, 32, &img, 32 * 3, 0 ); } //物品 void FaZhang(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img, _T("res\\fazhang.jpg")); putimage(*wupinx, *wupiny, 24, 24, &img, 0, 0 ); } void ShouQiang(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img, _T("res\\shouqiang.jpg")); putimage(*wupinx, *wupiny, 24, 24, &img, 0, 0 ); } void ShangDian(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img,_T("res\\shangdian.jpg")); putimage(*wupinx, *wupiny, 32, 32, &img, 0, 0 ); } void YaoShui(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img, _T("res\\yaoshui.jpg")); putimage(*wupinx, *wupiny, 28, 28, &img, 0, 0 ); } void DunPai(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img, _T("res\\dunpai.jpg")); putimage(*wupinx, *wupiny, 28, 28, &img, 0, 0 ); } void XieZi(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img, _T("res\\xiezi.jpg")); putimage(*wupinx, *wupiny, 28, 28, &img, 0, 0 ); } void Boss(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img, _T("res\\boss.jpg")); putimage(*wupinx, *wupiny, 32, 32, &img, 0, 0 ); } void BigBoss(int *wupinx, int *wupiny) { IMAGE img; loadimage(&img, _T("res\\bigboss.jpg")); putimage(*wupinx, *wupiny, 32, 32, &img, 0, 0 ); }
接下来是初始化的函数waiyuan.cpp
# include "tanke.h" /外部 void JShengMing(int *j) { setcolor(GREEN); settextstyle(0, 0, ("宋体")); char c2[20] = "自己生命值:"; outtextxy(0, 20, c2); char c3[10] ; sprintf(c3, _T("%.1f"), 100* (60 - *j) / 60.0); outtextxy(90, 20, c3); } void DShengMing(int * d,int *k) { setcolor(GREEN); settextstyle(0, 0, ("宋体")); char c2[20] = "敌人生命值:"; outtextxy(0, 0, c2); char c3[10] ; sprintf(c3, _T("%.1f"), 100* (60 - *d) / 60.0); outtextxy(90, 0, c3); char c4[40] = "恭喜~! 现在起金币到2200有惊喜!";//胜利 if ( *k >= 8000 ) { setcolor(YELLOW); settextstyle(30, 0, ("宋体")); outtextxy(150, 0, c4); } } void Gold(int * gold) { setcolor(GREEN); settextstyle(0, 0, ("宋体")); char c2[20] = "金币:"; outtextxy(0, 40, c2); char c3[10] ; sprintf(c3, _T("%d"), *gold); outtextxy(40, 40, c3); } void start(void) { initgraph(200, 130); TCHAR s1[10]="坦克大战"; TCHAR s2[30]="按A 开始游戏 按B 退出游戏"; TCHAR s3[30]="按W S A D控制方向"; TCHAR s4[20]="按J 发射子弹"; TCHAR s5[20]="按C 看攻略"; outtextxy(70, 0, s1); outtextxy(0, 110, s2); outtextxy(60, 90, s5); outtextxy(55, 30, s4); outtextxy(35, 60, s3); while (true) { Sleep(500); if (GetAsyncKeyState('A')) { BeginBatchDraw(); closegraph(); initgraph(640, 480); Sleep(200); Quit(); return ; } if (GetAsyncKeyState('C')) { Sleep(200); GongLue(); } } } void GongLue(void) { initgraph(450, 300); TCHAR s1[20]="游戏攻略:"; TCHAR s2[50]="再打坦克之前先吃法杖打掉白色砖块,"; TCHAR s3[50]="这样敌坦克打白色就不能回血了,boss更应如此。"; TCHAR s15[70]="吃盾牌的作用就是可以碾压对手"; TCHAR s4[50]="打大坦克的时候,别和它对子弹这样会吃亏"; TCHAR s5[70]="可以直接选择上去碾压它 注意:当血足够少的时候走开,"; TCHAR s6[50]="用子弹打它这样才能得到钱,"; TCHAR s7[70]="小boss可以反复刷,虽然挣不到钱,但复活次数更需要。"; TCHAR s14[70]="吃手枪虽然速度快了但伤害会减少,但打绿boss时伤害反而增加"; TCHAR s8[70]="血要多吃,肯定划算,钱少了好挣,复活少了,就难挣了。"; TCHAR s9[50]="打终极boss时,记得要用大子弹打它伤害才能打出来。"; TCHAR s10[90]="最后温馨提示:有块红砖比较可疑~"; TCHAR s11[40]="最后:别想着跑后面去打终极Boss了"; TCHAR s12[30]="因为你超过它会直接被秒。"; TCHAR s13[30]="按A 开始游戏"; outtextxy(0, 0, s1); outtextxy(0, 20, s2); outtextxy(0, 40, s3); outtextxy(0, 60, s15); outtextxy(0, 80, s4); outtextxy(0, 100, s5); outtextxy(0, 120, s6); outtextxy(0, 140, s14); outtextxy(0, 160, s7); outtextxy(0, 180, s8); outtextxy(0, 200, s9); outtextxy(0, 220, s10); outtextxy(0, 240, s11); outtextxy(0, 260, s12); outtextxy(0, 280, s13); while (true) { Sleep(500); if (GetAsyncKeyState('A')) { keybd_event(65,0,0,0); keybd_event(65,0,KEYEVENTF_KEYUP,0); return ; } } } void MiJi(void) { closegraph(); printf("游戏秘籍:\n"); printf("恭喜你通关了,\n"); printf("告诉你些游戏作弊方法~!。\n"); printf("当你一直按住子弹不松的话 ,还有直接控制 子弹功能哦~~\n\n"); printf("哈哈 另外小技巧。打boss前 先把小坦克都压了\n"); printf("只留一个,因为boss出来 基地就危险了\n"); printf("还有 有的人 觉得花了600块的大子弹没伤害没用\n"); printf("我只能说他的用法不对 不是一下一下的点,\n\n"); printf("而是一直按着然后松开 那伤害高到 爆~!终极boss都打一半血!\n\n"); printf("还有 就是 就算Gameover了 不算输,我还留了一手\n\n"); printf("你把所有敌坦克都杀了 再按 Y\n\n"); printf("这时候你的基地就复活了,\n\n"); printf("~~嘘~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"); printf("另外:小坦克靠近基地时按Y它就乖乖回去了\n\n"); printf("按B 游戏结束\n\n"); while (true) { Sleep(500); if (GetAsyncKeyState('B')) { exit(0); } } } void music(void) { mciSendString("open sound\\坦克大战.mp3 alias mymusic", NULL, 0, NULL); mciSendString("play mymusic", NULL, 0, NULL); return; } void start_(void) { music(); srand( (unsigned)time( NULL ) );//随机种子 setcolor(GREEN); setfillcolor(RED); } void win(pHongZhuan phongzhuan,bool *live2, bool *live3, bool *live4, bool *live5,bool *live6,bool *live7, bool *exist_laowang, bool *exist1, bool *wexist1,bool *wexist2,bool *wexist3,bool *wexist8) { char c[10] = "YOU WIN"; settextcolor(YELLOW); settextstyle(80, 0, ("宋体")); outtextxy(190, 100, c); char c1[20] = "按Y键 进入下一关"; settextstyle(30, 0, ("宋体")); outtextxy(230, 200, c1); if (GetAsyncKeyState('Y')) { *live2 = true; *live3 = true; *live4 = true; *live5 = true; *live6 = true; *live7 = true; *exist_laowang =true; *exist1 = true; phongzhuan[0].exist = true; phongzhuan[1].exist = true; *wexist1 = true; *wexist2 = true; *wexist3 = true; *wexist8 = true; } return ; } void lost(bool * live1, int *relive, int *gold) { char c[10] = "YOU LOST"; settextcolor(YELLOW); settextstyle(80, 0, ("宋体")); outtextxy(170, 100, c); char c1[20] = "按I键 复活,金币-60"; settextstyle(30, 0, ("宋体")); outtextxy(200, 200, c1); char c2[20] = "剩余复活次数:"; outtextxy(220, 230, c2); char c3[10] ; sprintf(c3, _T("%d"), 2 - *relive); outtextxy(426, 230, c3); if (GetAsyncKeyState('I')) { keybd_event(73,0,KEYEVENTF_KEYUP,0); (*relive)++; *gold -= 60; if (*relive < 3) *live1 = true; } if (*relive >= 3) GameOver(); return ; } void GameOver(void) { IMAGE img; loadimage(&img,_T("res\\gameover.bmp")); HDC dstDC = GetImageHDC(); HDC srcDC = GetImageHDC(&img); TransparentBlt(dstDC,200,50, 248, 160, srcDC, 0, 0, 248, 160, RGB(0,0,0)); } bool Quit(void) { if(GetAsyncKeyState('B')) { if(MessageBox(NULL, "你确定要退出吗?", "提示", MB_YESNO) == IDYES) return true; } return false; }
再然后是效果函数xiaoguo.cpp
# include "tanke.h" bool LuoShui( int * x_, int * y_, int * tileBlue_x, int *tileBlue_y, int *d,int *gold) { IMAGE imgBoom; loadimage(&imgBoom,_T("res\\explode1.bmp")); if (*x_ + 14 <= *tileBlue_x + 32 && *x_ + 14 >= *tileBlue_x && *y_ + 14 <= *tileBlue_y + 32 && *y_ + 14 >= *tileBlue_y) { putimage(*x_, *y_, 28, 28, &imgBoom,0 ,0 ); (*d)++; *gold -= 2; if (*d >= 40) { *d = 0; return false; } } return true; } bool BossMiaoRen(int *y_,int *y9) { if (*y_ <= *y9 + 67) { mciSendString("open sound//boom.mp3 alias mymusic_1", NULL, 0, NULL); mciSendString("play mymusic_1", NULL, 0, NULL); return false; } return true; } bool HuoWu(int *x_, int *y_, int *wupinx, int *wupiny, int *d, int *HWsd) { IMAGE imgXing; loadimage(&imgXing, _T("res\\bore.bmp")); if (*x_ + 14 <= *wupinx + 32 && *x_ + 14 >= *wupinx && *y_ + 14 <= *wupiny + 32 && *y_ + 14 >= *wupiny) { putimage(*x_ - 4, *y_ - 4, 32, 32, &imgXing, 32 * 3 ,0 ); (*d)++; if (*d >= *HWsd) { if (*HWsd != 0) printf("\a"); *d = 0; return false; } } return true; }
之后是关于子弹的函数zidan.cpp
# include "tanke.h" 坦克 子弹 void JBox(int (*x)[2][3],int * x_, int * y_, int * z,int * r, int * sd, int *zhidan,int *color1) { int h = 0;//图片的方向 int k , k1, k2;//k循环,k1,k2装有的方向 IMAGE img; loadimage(&img, _T("res\\player1.bmp")); if (x[*z + 1][0][1] == 0 && x[*z + 1][0][2] == -1)//w h = 0; if (x[*z + 1][0][1] == 0 && x[*z + 1][0][2] == 1)//s h = 2; if (x[*z + 1][0][1] == -1 && x[*z + 1][0][2] == 0)//a h = 3; if (x[*z + 1][0][1] == 1 && x[*z + 1][0][2] == 0)//d h = 1; if (GetAsyncKeyState('J'))//发射子弹 { *z = *z + 1; x[*z + 1][0][1] = x[*z ][0][1] ;//x坐标中的方向 x[*z + 1][0][2] = x[*z ][0][2] ;// if (x[*z][0][1] == 0 && x[*z][0][2] == -1)//w { x[*z][0][0] = *x_ + 34; x[*z][1][0] = *y_ + 14; } if (x[*z][0][1] == 0 && x[*z][0][2] == 1)//s { x[*z][0][0] = *x_ + 34; x[*z][1][0] = *y_ + 48; } if (x[*z][0][1] == -1 && x[*z][0][2] == 0)//a { x[*z][0][0] = *x_ + 17; x[*z][1][0] = *y_ + 33; } if (x[*z][0][1] == 1 && x[*z][0][2] == 0)//d { x[*z][0][0] = *x_ + 48; x[*z][1][0] = *y_ + 33; } keybd_event(74,0,KEYEVENTF_KEYUP,0); } putimage( *x_, *y_, 28 , 28 , &img, 28 * (*color1) , 28 * h ); for (k = 0; k<40; k++) { k1 = x[k][0][1]; k2 = x[k][0][2]; if(*r % *sd == 0)//己方子弹的的速度 { if ( k1 == 1 && k2 == 0 && x[k][0][0] != 0) x[k][0][0] = x[k][0][0] + 4; if ( k1 == 0 && k2 == 1 && x[k][0][0] != 0) x[k][1][0] = x[k][1][0] + 4; if ( k1 == -1 && k2 == 0 && x[k][0][0] != 0) x[k][0][0] = x[k][0][0] - 4; if ( k1 == 0 && k2 == -1 && x[k][0][0] != 0) x[k][1][0] = x[k][1][0] - 4; } if ( (x[k][0][0] -20) >= 640 || (x[k][0][0] - 20)<= 0) x[k][0][0] = 0 ;//子弹到边界 就初始化数组坐标x为0 if ( (x[k][1][0] -20) >= 480 || (x[k][1][0] - 20)<= 0) x[k][0][0] = 0 ; solidcircle(x[k][0][0] - 20, x[k][1][0] - 20, *zhidan); } if (*z >= 38)//数组 要满时就循环 { x[1][0][1] = x[*z ][0][1] ; x[1][0][2] = x[*z ][0][2] ; x[1][1][1] = x[*z ][1][1] ; x[1][1][2] = x[*z ][1][2] ; *z = 0; } return ; } void DBox(int (*x)[2][3],int * x_, int * y_, int * z, int *r, int * color) { int h = 0; int k , k1, k2; IMAGE img; loadimage(&img,_T("res\\enemy.bmp")); if (x[*z + 1][0][1] == 0 && x[*z + 1][0][2] == -1)//w h = 0; if (x[*z + 1][0][1] == 0 && x[*z + 1][0][2] == 1)//s h = 2; if (x[*z + 1][0][1] == -1 && x[*z + 1][0][2] == 0)//a h = 3; if (x[*z + 1][0][1] == 1 && x[*z + 1][0][2] == 0)//d h = 1; if ((*r) % 80 == 0)//敌坦克发子弹频率 { *z = *z + 1; x[*z + 1][0][1] = x[*z ][0][1] ;//给方向 x[*z + 1][0][2] = x[*z ][0][2] ; if (x[*z][0][1] == 0 && x[*z][0][2] == -1)//w { x[*z][0][0] = *x_ + 34; x[*z][1][0] = *y_ + 14; } if (x[*z][0][1] == 0 && x[*z][0][2] == 1)//s { x[*z][0][0] = *x_ + 34; x[*z][1][0] = *y_ + 48; } if (x[*z][0][1] == -1 && x[*z][0][2] == 0)//a { x[*z][0][0] = *x_ + 17; x[*z][1][0] = *y_ + 33; } if (x[*z][0][1] == 1 && x[*z][0][2] == 0)//d { x[*z][0][0] = *x_ + 48; x[*z][1][0] = *y_ + 33; } } putimage( *x_, *y_, 28 , 28 , &img, *color , 28 * h ); for (k = 0; k<40; k++) { k1 = x[k][0][1]; k2 = x[k][0][2]; if(*r % 1 == 0)//敌坦克子弹速度 { if ( k1 == 1 && k2 == 0 && x[k][0][0] != 0) x[k][0][0] = x[k][0][0] + 2; if ( k1 == 0 && k2 == 1 && x[k][0][0] != 0) x[k][1][0] = x[k][1][0] + 2; if ( k1 == -1 && k2 == 0 && x[k][0][0] != 0) x[k][0][0] = x[k][0][0] - 2; if ( k1 == 0 && k2 == -1 && x[k][0][0] != 0) x[k][1][0] = x[k][1][0] - 2; } if ( (x[k][0][0] -20) >= 640 || (x[k][0][0] - 20)<= 0) x[k][0][0] = 0 ; if ( (x[k][1][0] -20) >= 480 || (x[k][1][0] - 20)<= 0) x[k][0][0] = 0 ; fillcircle(x[k][0][0] - 20, x[k][1][0] - 20, 5); } if (*z >= 38) { x[1][0][1] = x[*z ][0][1] ; x[1][0][2] = x[*z ][0][2] ; x[1][1][1] = x[*z ][1][1] ; x[1][1][2] = x[*z ][1][2] ; *z = 0; } return ; } void DBoxBig(int (*x)[2][3],int * x_, int * y_, int * z, int *r, int * color) { int h = 0; int k , k1, k2; IMAGE img; loadimage(&img,_T("res\\enemy.bmp")); if (x[*z + 1][0][1] == 0 && x[*z + 1][0][2] == -1)//w h = 0; if (x[*z + 1][0][1] == 0 && x[*z + 1][0][2] == 1)//s h = 2; if (x[*z + 1][0][1] == -1 && x[*z + 1][0][2] == 0)//a h = 3; if (x[*z + 1][0][1] == 1 && x[*z + 1][0][2] == 0)//d h = 1; if ((*r) % 30 == 0)//敌坦克发子弹频率 { *z = *z + 1; x[*z + 1][0][1] = x[*z ][0][1] ; x[*z + 1][0][2] = x[*z ][0][2] ; if (x[*z][0][1] == 0 && x[*z][0][2] == -1)//w { x[*z][0][0] = *x_ + 34; x[*z][1][0] = *y_ + 14; } if (x[*z][0][1] == 0 && x[*z][0][2] == 1)//s { x[*z][0][0] = *x_ + 34; x[*z][1][0] = *y_ + 48; } if (x[*z][0][1] == -1 && x[*z][0][2] == 0)//a { x[*z][0][0] = *x_ + 17; x[*z][1][0] = *y_ + 33; } if (x[*z][0][1] == 1 && x[*z][0][2] == 0)//d { x[*z][0][0] = *x_ + 48; x[*z][1][0] = *y_ + 33; } keybd_event(74,0,KEYEVENTF_KEYUP,0); } putimage( *x_, *y_, 28 , 28 , &img, *color , 112 + (28 * h) ); for (k = 0; k<40; k++) { k1 = x[k][0][1]; k2 = x[k][0][2]; if(*r % 2 == 0)//敌坦克子弹速度 { if ( k1 == 1 && k2 == 0 && x[k][0][0] != 0) x[k][0][0] = x[k][0][0] + 15; if ( k1 == 0 && k2 == 1 && x[k][0][0] != 0) x[k][1][0] = x[k][1][0] + 15; if ( k1 == -1 && k2 == 0 && x[k][0][0] != 0) x[k][0][0] = x[k][0][0] - 15; if ( k1 == 0 && k2 == -1 && x[k][0][0] != 0) x[k][1][0] = x[k][1][0] - 15; } if ( (x[k][0][0] -20) >= 640 || (x[k][0][0] - 20)<= 0) x[k][0][0] = 0 ; if ( (x[k][1][0] -20) >= 480 || (x[k][1][0] - 20)<= 0) x[k][0][0] = 0 ; fillcircle(x[k][0][0] - 20, x[k][1][0] - 20, 5); } if (*z >= 38) { x[1][0][1] = x[*z ][0][1] ; x[1][0][2] = x[*z ][0][2] ; x[1][1][1] = x[*z ][1][1] ; x[1][1][2] = x[*z ][1][2] ; *z = 0; } return ; } void DBoxFeiJi(int (*x_9)[2][3],int (*x_10)[2][3],int (*x_11)[2][3],int * x_, int * y_, int * z, int *r) { int k; IMAGE img; loadimage(&img, _T("res\\feiji.jpg")); HDC dstDC = GetImageHDC(); HDC srcDC = GetImageHDC(&img); TransparentBlt(dstDC,*x_,*y_, 65, 65, srcDC, 0, 0, 65, 65, RGB(0,0,0));//飞机 if ((*r) % 13 == 0)//敌坦克发子弹频率 { *z = *z + 1; x_9[*z][0][0] = *x_ + 52; x_9[*z][1][0] = *y_ + 85; x_10[*z][0][0] = *x_ + 52; x_10[*z][1][0] = *y_ + 85; x_11[*z][0][0] = *x_ + 52; x_11[*z][1][0] = *y_ + 85; } for (k = 0; k<40; k++) { if(*r % 5 == 0 && x_10[k][0][0] != 0)//敌坦克子弹速度 { x_9[k][1][0] = x_9[k][1][0] + 15; x_10[k][0][0] = x_10[k][0][0] + 15; x_10[k][1][0] = x_10[k][1][0] + 15; x_11[k][0][0] = x_11[k][0][0] - 15; x_11[k][1][0] = x_11[k][1][0] + 15; } if ( (x_9[k][1][0] -20) >= 480 ) x_9[k][0][0] = 0 ; if ( (x_10[k][1][0] -20) >= 480) x_10[k][0][0] = 0 ; if ( (x_11[k][1][0] -20) >= 480) x_11[k][0][0] = 0 ; fillcircle(x_9[k][0][0] - 20, x_9[k][1][0] - 20, 7); fillcircle(x_10[k][0][0] - 20, x_10[k][1][0] - 20, 7); fillcircle(x_11[k][0][0] - 20, x_11[k][1][0] - 20, 7); } if (*z >= 38) *z = 0; return ; }
接下来是我们子弹暴炸时的函数boom.cpp
# include "tanke.h" /效果 bool DBoom(int (*x)[2][3], int * x_, int *y_, int *d,int *gold) { int i;//循环用 IMAGE imgBoom; loadimage(&imgBoom, _T("res\\explode1.bmp")); for (i=1; i<39; i++)//被击中爆炸 if (x[i][0][0] <= *x_+48 && x[i][0][0] >= *x_ + 20 && x[i][1][0] <= *y_ + 48 && x[i][1][0] >= *y_ + 20) { if (*d % 10 == 0) x[i][0][0] = 0; putimage(*x_, *y_, 28, 28, &imgBoom,0 ,0 ); (*d)++; if (*d >= 60) { mciSendString("open sound//boom.mp3 alias mymusic_1", NULL, 0, NULL); mciSendString("play mymusic_1", NULL, 0, NULL); *d = 0; *gold += 10; return false; } mciSendString("close mymusic_1", NULL, 0, NULL); } return true; } bool QZBoom(int (*x)[2][3], int * x_, int *y_)//强制子弹消失,物体爆炸。 { int i;//循环用 for (i=1; i<39; i++)//被击中爆炸 if (x[i][0][0] <= *x_+52 && x[i][0][0] >= *x_ + 20 && x[i][1][0] <= *y_ + 52 && x[i][1][0] >= *y_ + 20) { x[i][0][0] = 0; return false; } return true; } bool DBoomBig(int (*x)[2][3], int * x_, int *y_, int *d) { int i;//循环用 IMAGE imgBoom; loadimage(&imgBoom,_T("res\\explode2.bmp")); for (i=1; i<39; i++)//被击中爆炸 if (x[i][0][0] <= *x_+52 && x[i][0][0] >= *x_ + 20 && x[i][1][0] <= *y_ + 52 && x[i][1][0] >= *y_ + 20) { putimage(*x_ - 18, *y_ -18, 64, 64, &imgBoom,0 ,0 ); (*d)++; if (*d % 10 == 0) x[i][0][0] = 0; if (*d >= 60) { mciSendString("open sound//boom.mp3 alias mymusic_1", NULL, 0, NULL); mciSendString("play mymusic_1", NULL, 0, NULL); *d = 0; return false; } mciSendString("close mymusic_1", NULL, 0, NULL); } return true; } bool DBossBoomBig(int (*x)[2][3], int * x_, int *y_, int *k, int*zhidan) { int i;//循环用 IMAGE imgBoom; loadimage(&imgBoom,_T("res\\explode2.bmp")); for (i=1; i<39; i++)//被击中爆炸 if (x[i][0][0] <= *x_+70 && x[i][0][0] >= *x_ + 20 && x[i][1][0] <= *y_ + 70 && x[i][1][0] >= *y_ + 20) { putimage(*x_ - 18, *y_ -18, 64, 64, &imgBoom,0 ,0 ); if (*zhidan == 5) (*k)++; if (*zhidan == 15) (*k) += 10; if (*k >= 8000) { x[i][0][0] = 0; keybd_event(89,0,0,0); keybd_event(89,0,KEYEVENTF_KEYUP,0); return false; } } setcolor(GREEN); settextstyle(0, 0, ("宋体")); char c2[20] = "BOSS生命值:"; outtextxy(220, 0, c2); char c3[10] ; sprintf(c3, _T("%.1f"),1000*(8000 - *k)/8000.0 ); outtextxy(320, 0, c3); return true; } bool DBossBoomSmall(int (*x)[2][3], int * x_, int *y_, int *l, int*sd) { int i;//循环用 IMAGE imgBoom; loadimage(&imgBoom, _T("res\\explode2.bmp")); for (i=1; i<39; i++)//被击中爆炸 if (x[i][0][0] <= *x_+48 && x[i][0][0] >= *x_ + 20 && x[i][1][0] <= *y_ + 48 && x[i][1][0] >= *y_ + 20) { putimage(*x_ - 18, *y_ -18, 64, 64, &imgBoom,0 ,0 ); if (*sd == 2) (*l)++; if (*sd == 1) (*l) += 10; if (*l >= 1000) { x[i][0][0] = 0; *l = 0; return false; } } setcolor(GREEN); settextstyle(0, 0, ("宋体")); char c2[20] = "BOSS生命值:"; outtextxy(220, 0, c2); char c3[10] ; sprintf(c3, _T("%.1f"),1000*(1000 - *l)/1000.0 ); outtextxy(320, 0, c3); return true; }
最后就是我们运行的主函数了main.cpp
# include "tanke.h" int main(void) { start();//开始说明 int r = 0;//减速的 int relive = 0;//复活次数 int sd = 2;//子弹速度 int HWsd = 40; int gold = 0;//金币 int zhidan = 5;//己方坦克子弹大小 int o = 0; //boss方向储存 int ydsd = 2;//移动速度 int j = 0;//爆炸图片多次,或者生命值 int d = 0;//爆炸图片多次 int k = 0;//boss生命值 int l = 0;//小boss生命值 int m = 0;//物品生命值 TanKe tanke[7] = { {0,0, 0, 0, true, {0}}, {0,0, 0, 0, true, {0}}, {0,0, 0, 56, true, {0}}, {0,0, 0, 112, true, {0}}, {0,0, 0, 168, true, {0}}, {0,0, 0, 112, true, {0}}, {0,0, 0, 168, true, {0}}}; pTanKe ptanke = NULL; BaiZhuan baizhuan[3] = { {0,0,true},{0,0,true},{0,0,true}}; pBaiZhuan pbaizhuan = NULL; LvZhuan lvzhuan[12]; pLvZhuan plvzhuan = NULL; HongZhuan hongzhuan[2] ={ {0,0,true},{0,0,true}}; pHongZhuan phongzhuan = NULL; tanke[0].d[1][0][1] = 0;//初始化己方第一颗子弹方向上 tanke[0].d[1][0][2] = -1; tanke[1].d[1][0][1] = 0;//初始化己方第一颗子弹方向上 tanke[1].d[1][0][2] = -1; tanke[2].d[1][0][1] = 0;//初始化己方第一颗子弹方向上 tanke[2].d[1][0][2] = -1; tanke[3].d[1][0][1] = 0;//初始化己方第一颗子弹方向上 tanke[3].d[1][0][2] = -1; tanke[4].d[1][0][1] = 0;//初始化己方第一颗子弹方向上 tanke[4].d[1][0][2] = -1; tanke[5].d[1][0][1] = 0;//初始化己方第一颗子弹方向上 tanke[5].d[1][0][2] = -1; tanke[6].d[1][0][1] = 0;//初始化己方第一颗子弹方向上 tanke[6].d[1][0][2] = -1; int z8 = 0;//绿坦克 int z9 = 0;//飞机 int color8 = 0;//绿boss int x_8[40][2][3] = {0};//绿坦克子弹 int x_9[40][2][3] = {0}; int x_10[40][2][3] = {0};//飞机子弹 int x_11[40][2][3] = {0};//飞机子弹 x_8[1][0][1] = 0;//初始化己方第一颗子弹方向上 x_8[1][0][2] = -1; start_(); bool live8 = false;//绿坦克 bool live9 = false;//飞机 bool exist_laowang = true; bool exist1 = true; //boss红砖 bool wexist1 = true;//法杖 bool wexist2 = true;//枪 bool wexist3 = true;//商店 bool wexist4 = true;//药水 bool wexist5 = true;//盾牌 bool wexist8 = true;//鞋子 bool wexist6 = true;//物品—boss bool wexist7 = true;//物品—bigboss int tilelaowang_x = 304;//老王 int tilelaowang_y = 448; int tileHong_x1 = 304;//老王前障碍物 int tileHong_y1 = 416; while(true) { tanke[0].x = 28 + rand() % 584, tanke[0].y = 400 + rand() % 52;//己方方块, tanke[1].x = 28 + rand() % 584, tanke[1].y = 28 + rand() % 124;//敌方方块,↓同理 tanke[2].x = 28 + rand() % 584, tanke[2].y = 28 + rand() % 124; tanke[3].x = 28 + rand() % 584, tanke[3].y = 28 + rand() % 124; tanke[4].x = 28 + rand() % 584, tanke[4].y = 28 + rand() % 124; tanke[5].x = 28 + rand() % 584, tanke[5].y = 28 + rand() % 124; tanke[6].x = 28 + rand() % 584, tanke[6].y = 28 + rand() % 124; //int x8 = 28 + rand() % 584, y8 = 28 + rand() % 124;//放在下面的 int x9 = 320, y9 = -80;//大boss hongzhuan[0].x = 100 + rand() % 376;//红色砖块 hongzhuan[0].y = 200 + rand() % 216; hongzhuan[1].x = 32 + rand() % 576;//红色砖块 hongzhuan[1].y = 32 + rand() % 416; baizhuan[0].x = 32 + rand() % 576; //白色砖块 baizhuan[0].y = 32 + rand() % 416; baizhuan[1].x = 32 + rand() % 576; //白色砖块 baizhuan[1].y = 32 + rand() % 416; baizhuan[2].x = 32 + rand() % 576; //白色砖块 baizhuan[2].y = 32 + rand() % 416; int tileBai_x4 = -20; //老王前白色砖块真的打不烂! int tileBai_y4 = -20; lvzhuan[0].x = 32 + rand() % 576; //绿色砖块 lvzhuan[0].y = 32 + rand() % 416; lvzhuan[1].x = 32 + rand() % 576; lvzhuan[1].y = 32 + rand() % 416; lvzhuan[2].x = 32 + rand() % 576; lvzhuan[2].y = 32 + rand() % 416; lvzhuan[3].x = 32 + rand() % 576; lvzhuan[3].y = 32 + rand() % 416; lvzhuan[4].x = 32 + rand() % 576; lvzhuan[4].y = 32 + rand() % 416; lvzhuan[5].x = 32 + rand() % 576; lvzhuan[5].y = 32 + rand() % 416; lvzhuan[6].x = 32 + rand() % 576; //绿色砖块 lvzhuan[6].y = 32 + rand() % 416; lvzhuan[7].x = 32 + rand() % 576; lvzhuan[7].y = 32 + rand() % 416; lvzhuan[8].x = 32 + rand() % 576; lvzhuan[8].y = 32 + rand() % 416; lvzhuan[9].x = 32 + rand() % 576; lvzhuan[9].y = 32 + rand() % 416; lvzhuan[10].x = 32 + rand() % 576; lvzhuan[10].y = 32 + rand() % 416; lvzhuan[11].x = 32 + rand() % 576; lvzhuan[11].y = 32 + rand() % 416; int tileBlue_x1 = 32 + rand() % 576; //蓝色砖块 int tileBlue_y1 = 32 + rand() % 416; int tileBlue_x2 = 32 + rand() % 576; //蓝色砖块 int tileBlue_y2 = 32 + rand() % 416; int fazhang_x1 = 32 + rand() % 576; //物品—法杖; int fazhang_y1 = 32 + rand() % 416; int shouqiang_x1 = 32 + rand() % 576; //物品—枪; int shouqiang_y1 = 32 + rand() % 416; int shangdian_x1 = 32 + rand() % 576; //物品—商店; int shangdian_y1 = 200 + rand() % 216; int yaoshui_x1 = 32 + rand() % 576; //物品—药水; int yaoshui_y1 = 32 + rand() % 416; int dunpai_x1 = 32 + rand() % 576; //物品—盾牌; int dunpai_y1 = 32 + rand() % 116; int xiezi_x1 = 32 + rand() % 576; //物品—鞋子; int xiezi_y1 = 32 + rand() % 116; int boss_x1 = 100 + rand() % 376; //物品—Boss; int boss_y1 = 100 + rand() % 216; int x8 = boss_x1 , y8 = boss_y1; int bigboss_x1 = hongzhuan[0].x ; //物品—bigBoss; int bigboss_y1 = hongzhuan[0].y ; while (true) { ZhengTiFangXiang(tanke,baizhuan,hongzhuan,x_8, &live8,&live9, &r,&x8,&y8,&x9,&y9, &z8, &o,&ydsd);//方向 cleardevice();//清屏 ZhengTiLaoWang(tanke,x_8,x_9,x_10,x_11, &exist_laowang,&exist1,&gold,&d, &tilelaowang_x,&tilelaowang_y,&tileHong_x1,&tileHong_y1,&tileBai_x4,&tileBai_y4,&live9);//老王 ZhengTiBaiSeZhangAiWu(tanke,x_8,x_9,x_10,x_11, &wexist1, &d,baizhuan);//白砖 ZhengTiLanSeZhangAiWu(&tileBlue_x1,&tileBlue_y1,&tileBlue_x2,&tileBlue_y2);//蓝砖 ZhengTiTanKeBaoZha(tanke,x_8,x_9,x_10,x_11, &live8,&live9, &r,&x8,&y8,&x9,&y9, &sd,&color8, &j,&d,&k,&l,&gold,&zhidan, &z8,&z9);//坦克 、 爆炸 ZhengTiLuoShui(tanke,&tileBlue_x1,&tileBlue_y1,&tileBlue_x2,&tileBlue_y2, &j,&gold,&y9);//落水.boss秒人 ZhengTiLvSeZhangAiWu(lvzhuan);//绿砖 ZhengTiWuPin(tanke,&x8,&y8, &live8,&live9, &fazhang_x1,&fazhang_y1, &shouqiang_x1,&shouqiang_y1, &shangdian_x1,&shangdian_y1, &yaoshui_x1,&yaoshui_y1, &dunpai_x1,&dunpai_y1, &xiezi_x1,&xiezi_y1, &boss_x1,&boss_y1, &bigboss_x1,&bigboss_y1, &wexist1, &wexist2,&wexist3,&wexist4,&wexist5,&wexist6,&wexist7,&wexist8, &sd,&HWsd,&ydsd, &d,&j,&gold,&zhidan,&relive);//物品 ZhengTiHongSeZhangAiWu(tanke,x_8,x_9, &d,hongzhuan);//红砖 后藏boss ZhengTiShengMing(tanke,hongzhuan, &exist_laowang,&exist1,&wexist1,&wexist2,&wexist3,&wexist8, &relive,&j,&d,&gold,&k);//生命 FlushBatchDraw(); if (GetAsyncKeyState('Y')) {break;} if (Quit()) {exit(0);} if (gold == 2200) { MiJi(); } } } EndBatchDraw(); return 0; }
这样一个《坦克大战》就完成了。快去自己动手尝试一下叭!!!
代码很长,希望看完了的同学可以获得自己想要的知识,也感谢大家的耐心观看,在这里想得到大家一波关注,后续UP主还会发布更多的项目源码以及学习资料,有什么问题可以回帖留言,我尽量回答。希望和大家一起学习进步!!!
到此这篇关于C语言 完整游戏项目坦克大战详细代码的文章就介绍到这了,更多相关C语言 坦克大战内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!