欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言 完整游戏项目坦克大战详细代码

程序员文章站 2022-06-18 16:56:03
话不多说 我们今天就来创造出属于我们自己的《坦克大战》,gogogo!!! 直接开始吧 这次的源码比较详细,我分了好几个cpp文件,思路更加的清晰,请耐心用心的观看首先就是我们载入图片的函数tupia...

C语言 完整游戏项目坦克大战详细代码

话不多说 我们今天就来创造出属于我们自己的《坦克大战》,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语言 完整游戏项目坦克大战详细代码的文章就介绍到这了,更多相关c语言 坦克大战内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!