课程设计扫雷(c语言)


课程设计一共两个星期,扫雷要图形界面和鼠标,前一个星期基本啥也没看,合计学习下传说中的api啊~~MFC啊~~~结果都木有成功……

后来发现了easyX……好东西啊!(其实就是把TC的图形库graphics转到了vc中)……


书面文件一律瞎编……


上代码:

#####################################################################################################################

/

#include                                     //画图头文件
#include                                        //输入输出头文件
#define		USER_LBUTTONDOWN	101                      //宏定义鼠标左键
#define		USER_RBUTTONDOWN	102                      //宏定义鼠标右键
#define		USER_MBUTTONDOWN	103                      //宏定义鼠标滑轮
#pragma comment(lib,"Winmm.lib")                         //载入鼠标文件
#include                                         //数学函数生成随机数
#include                                       //数组初始化
int a[50][50];                                           //数组雷
#include                                         //计时
#include                                        //_stprintf函数头文件
#include                                       //字符串头文件
/
//全局变量///
time_t first, second;
TCHAR m_time[33];
int flag=0;
int shengyu;
int high,wide,leishu;
float n,mn,p,q,yy;
int biaoji;
char shenglei[5];
FILE *fp;
int nandu;
struct besttime
{
	char name[7];
	float time;
	char t[5];
}zuih[4];
/
/
int DispatchMessage(MOUSEMSG m, int opt);                //鼠标信息处理
void chuangkzh();                                        //窗口转换过程
int GetMessage(MOUSEMSG m);                              //获取鼠标信息
void kaishi();                                           //游戏开始界面
void kaishi2();                                          //中级界面开始
void kaishi3();                                          //高级界面开始
int chushi();                                            //游戏初始化菜单
int saolei();                                            //扫雷鼠标执行函数
int DispatchMessage2(MOUSEMSG m, int opt);               //游戏中鼠标处理
int saolz(MOUSEMSG m);                                   //扫雷中操作
void bulei();                                            //布雷函数
void panduan();                                          //判断是否为雷
int nanduxuanxiang();                                    //难度选项
int DispatchMessage3(MOUSEMSG m, int opt);               //鼠标信息处理(难度选项)
int zerodg(int x,int y);                                 //zero递归函数
void zuigaofen();                                        //最高分鼠标处理
//
//
//
//主函数
void main()
{
	FILE *fp;
	int duru=0;
	fp=fopen("best.dat","rb+");
    for(int i=0;i<4;i++)
    if(fread(&zuih[i],sizeof(besttime),1,fp)!=1&&duru!=1)
	{
		duru=1;
		printf("file read error!\n");
	}
	else
		continue;
	fclose(fp);
	if(duru!=1)
	{
		initgraph(640,480);
		chushi();
		int opt,end=0;
		MOUSEMSG m;
		while(end!=1)
		{
			m = GetMouseMsg();
			opt = GetMessage(m);
			end=DispatchMessage(m, opt);
		}
		setfont(80,0,"黑体");
		for(int i=0;i<20;i++)
		{
			setcolor(WHITE);
			outtextxy(150,200,"再见");
			Sleep(25);
			setcolor(BLACK);
			outtextxy(150,200,"再见");
			Sleep(25);
		}
		mciSendString("stop mymusic", NULL, 0, NULL);
		mciSendString("close mymusic", NULL, 0, NULL);
		cleardevice();
		closegraph();
	}
}
///
//主菜单,选择难度
int chushi()
{
	
	initgraph(640,480);
	
	HCURSOR hcur = (HCURSOR)LoadImage(NULL, "c:\\鼠标.cur", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
	HWND hwnd = GetHWnd();							// 获取绘图窗口句柄
	SetClassLong(hwnd, GCL_HCURSOR, (long)hcur);	// 设置窗口类的鼠标样式
	
	setcolor(GREEN);
	setfont(80,0,"黑体");
	outtextxy(200,100,"扫  雷");
	setfont(30,0,"黑体");
	outtextxy(450,90,"R"); 
	setcolor(WHITE);
	setfont(20,0,"黑体");
	outtextxy(270,250,"1、新游戏");
	outtextxy(270,280,"2、最高分");
	outtextxy(270,310,"3、选  项");
	outtextxy(270,340,"4、帮  助");
	outtextxy(270,370,"5、退  出");
	outtextxy(500,410,"by:zmq  zzh");
	outtextxy(500,440,"v:2.0");
	mciSendString("open 背景音乐.mp3 alias mymusic", NULL, 0, NULL);
	mciSendString("play mymusic", NULL, 0, NULL);
	return 0;
}
//
//游戏开始界面,生成格子,着色
void kaishi()
{
	bulei();
	biaoji=0;
	if(wide<=16)
	{
		n=360/high;mn=360/wide;
		p=60+n*high;q=60+mn*wide;
		yy=0;
	}
	else
	{
		n=360/high;mn=360/16;
		yy=(wide-16)*mn;
		initgraph(640+yy,480);
	}
		p=60+n*high;q=60+mn*wide;
		shengyu=0;
		setlinestyle(PS_SOLID, NULL, 2);
		setcolor(BLUE);
		line(60,60,q,60);
		line(60,p,q,p);
		line(60,60,60,p);
		line(q,60,q,p);
	setfillstyle(RGB(0,0,150));
	floodfill(70,70,BLUE);
	setcolor(WHITE);
	for(float i=60;i<=p;i+=n)
		line(60,i,q,i);
	for(i=60;i<=q;i+=mn)
		line(i,60,i,p);
	setlinestyle(PS_SOLID, NULL, 10);
	setcolor(GREEN);
	line(500+yy,0,500+yy,480);
	setcolor(WHITE);
	setfont(20,0,"宋体");
	outtextxy(510+yy,100,"时  间:0");
	outtextxy(510+yy,200,"炸弹数:");
	outtextxy(530+yy,370,"左键开始");
	outtextxy(530+yy,410,"中键重玩");
	outtextxy(530+yy,450,"回主菜单");
	if(wide==9)
		outtextxy(590+yy,200,"10");
	if(wide==16)
		outtextxy(590+yy,200,"40");
	if(wide==30)
		outtextxy(590+yy,200,"99");
	saolei();

}

//鼠标信息处理(主菜单)
int DispatchMessage(MOUSEMSG m, int opt)
{
	switch(opt)
	{
		case USER_LBUTTONDOWN:
			// 左键
			if(m.x>=270&&m.x<=370&&m.y>=250&&m.y<=270)
			{
                flag=0;
				nandu=1;
				high=9;
				wide=9;
				leishu=10;
				chuangkzh();
				kaishi();
				return 0;
			}
			if(m.x>=270&&m.x<=370&&m.y>=280&&m.y<=300)
			{
                flag=0;
				chuangkzh();
				setcolor(GREEN);
				setfont(25,0,"楷体");
				outtextxy(150,350,"返回主菜单            重置");
				setfont(25,0,"黑体");
				setcolor(WHITE);
				outtextxy(200,150,"初级:");
				outtextxy(200,200,"中级:");
				outtextxy(200,250,"高级:");
				outtextxy(270,150,zuih[1].name);
				outtextxy(270,200,zuih[2].name);
				outtextxy(270,250,zuih[3].name);
				outtextxy(400,150,zuih[1].t);
				outtextxy(400,200,zuih[2].t);
				outtextxy(400,250,zuih[3].t);
				zuigaofen();
				
			}
			if(m.x>=270&&m.x<=370&&m.y>=310&&m.y<=330)
			{
				chuangkzh();
				setcolor(RED);
				setfont(30,0,"黑体");
				outtextxy(180,80,"一、初级【 9x 9】");
				outtextxy(180,180,"二、中级【16x16】");
				outtextxy(180,280,"三、高级【16x30】");
				outtextxy(220,380,"返回主菜单");
                nanduxuanxiang();
				return 0;
			}
			if(m.x>=270&&m.x<=370&&m.y>=340&&m.y<=360)
			{
				chuangkzh();
				setbkmode(TRANSPARENT);
				setfillstyle(WHITE);
				floodfill(15,15,WHITE);
				setcolor(BLUE);
				setfont(50,0,"黑体");
				outtextxy(245,50,"帮  助");
				setcolor(BLACK);
				setfont(20,0,"楷体");
				outtextxy(120,130,"1、请将《鼠标.cur》文件复制到c盘根目录下");
				outtextxy(120,160,"   再进行游戏,否则可能会影响鼠标效果。");
				outtextxy(120,190,"2、请将《背景音乐.mp3》同游戏的应用程");
				outtextxy(120,220,"   序员放在一起。可自己更改背景音乐(将音");
				outtextxy(120,250,"   乐改名后更换背景音乐即可)。");
				outtextxy(120,280,"3、游戏时请勿点击白色表格区域,否则可能会");
				outtextxy(120,310,"   导致失败等不确定情况。");
				outtextxy(120,340,"4、新手初次编程,游戏中存在漏洞请谅解。");
				outtextxy(120,370,"5、如有疑问可发送邮件到[email protected]。");
				setcolor(YELLOW);
				setfont(40,0,"黑体");
				outtextxy(270,420,"游戏适度 沉溺伤身");
				setcolor(RED);
				setfont(40,0,"隶书");
				outtextxy(50,420,"返回");
				setbkmode(OPAQUE);
				int opt,tiaochu=0;
				MOUSEMSG m;
				while(1)
				{
					m = GetMouseMsg();
					opt = GetMessage(m);
					if(opt==USER_LBUTTONDOWN)
					if(m.x>=50&&m.x<=130&&m.y<=460&&m.y>=420)
					{
						chuangkzh();
						main();
					}
				}	
			}
			if(m.x>=270&&m.x<=370&&m.y>=370&&m.y<=390)
			{
				chuangkzh();
				return 1;
			}
			break;
		
	}
return 0;
}
//
//难度选项
int nanduxuanxiang()
{
	int opt,end=0;
	MOUSEMSG m;
	while(end!=1)
	{
		m = GetMouseMsg();
		opt = GetMessage(m);
		end=DispatchMessage3(m, opt);
	}
return 0;
}
///
//鼠标信息处理(最高分)
void zuigaofen()
{
	int opt,end=0;
	MOUSEMSG m;
	while(end!=1)
	{
		m = GetMouseMsg();
		opt = GetMessage(m);
		if(opt==USER_LBUTTONDOWN)
		{
			if(m.x>=150&&m.x<=275&&m.y<=375&&m.y>=350)
			{
				chuangkzh();
				main();
			}
			if(m.x>=435&&m.x<=485&&m.y<=375&&m.y>=350)
			{
				strcpy(zuih[1].name,"wu");
                strcpy(zuih[1].t,"999");
                zuih[1].time=999;
                strcpy(zuih[2].name,"wu");
                strcpy(zuih[2].t,"999");
                zuih[2].time=999;
                strcpy(zuih[3].name,"wu");
                strcpy(zuih[3].t,"999");
                zuih[3].time=999;
				fp=fopen("best.dat","rb+");
				for(int i=0;i<4;i++)
				if(fwrite(&zuih[i],sizeof(besttime),1,fp)!=1)
				printf("file write error!\n");
				fclose(fp);
				chuangkzh();
				zuigaofen();
			}
			else
				zuigaofen();
		}
		else
				zuigaofen();
	}
}
///
//鼠标处理信息(难度选项)
int DispatchMessage3(MOUSEMSG m, int opt)
{
		switch(opt)
	{
		case USER_LBUTTONDOWN:
			// 左键
			if(m.x>=180&&m.x<=400&&m.y>=80&&m.y<=110)
			{
				nandu=1;
				high=9;
				wide=9;
				leishu=10;
				chuangkzh();
				kaishi();
				return 1;
			}
			if(m.x>=180&&m.x<=400&&m.y>=180&&m.y<=210)
			{
				nandu=2;
				high=16;
				wide=16;
				leishu=40;
				chuangkzh();
				kaishi();
				return 1;
			}
			if(m.x>=180&&m.x<=400&&m.y>=280&&m.y<=310)
			{
				nandu=3;
				high=16;
				wide=30;
				leishu=99;
				chuangkzh();
				kaishi();
				return 1;
			}

			if(m.x>=220&&m.x<=420&&m.y>=380&&m.y<=410)
			{
				chuangkzh();
				main();
				return 0;
			}
	}
}

///
//鼠标信息处理(游戏中)
int DispatchMessage2(MOUSEMSG m, int opt)
{
		switch(opt)
	{
		case USER_LBUTTONDOWN:
			// 左键
			if(m.x>=530+yy&&m.x<=610+yy&&m.y>=450&&m.y<=470)
			{
				flag=0;

				chuangkzh();
				main();
			}
			if(m.x>=60&&m.x<=420+yy&&m.y>=60&&m.y<=420)
			{
				if(flag==0)
				{
					first=time(NULL);
					flag=1;
				}
				saolz(m);
			}
			break;
		case USER_MBUTTONDOWN:
			// 中键
			if(flag==0)
				{
					first=time(NULL);
					flag=1;
				}
  			if(flag==1)
			{
				second=time(NULL);
				_stprintf(m_time, _T("%.0lf"), difftime(second,first));
				setfont(20,0,"黑体");
				outtextxy(590+yy,100,m_time);
				if(leishu-biaoji<10&&leishu-biaoji>=0)
				{
					shenglei[0]=' ';
					_stprintf(shenglei+1, _T("%d"), leishu-biaoji);
				}
				else
				{
					_stprintf(shenglei, _T("%d"), leishu-biaoji);
				}
				outtextxy(590+yy,200,shenglei);
			}
			chuangkzh();
			kaishi();
			return 1;
			break;
		case USER_RBUTTONDOWN:
			
			int x,y;
			x=(m.x-60)/n+1;
			y=(m.y-60)/mn+1;
			if(m.x>=60&&m.x<=420+yy&&m.y>=60&&m.y<=420)
			if(a[x][y]!=-1)
			{
				setfillstyle(RED);floodfill(m.x,m.y,WHITE);
				biaoji++;
			}
			saolei();
			break;
	}
}

//
//窗口变化的简单动画效果
void chuangkzh()
{
	int lr=0;
	int i=0,j=640+yy;
	setlinestyle(PS_SOLID, NULL, 5);
	while(i!=j)
	{
			setcolor(WHITE);
			line(i+5,0,i+5,480);
			setcolor(BLACK);
			line(i,0,i,480);
			Sleep(2);
			setcolor(WHITE);
			line(j-5,0,j-5,480);
			setcolor(BLACK);
			line(j,0,j,480);
			Sleep(8);
			i++;
			j--;
	}
	setcolor(BLACK);
	line(j+6,0,j+6,480);
	line(i-4,0,i-4,480);
	setlinestyle(PS_SOLID, NULL, 1);
}

///
// 获取鼠标信息
int GetMessage(MOUSEMSG m)
{
	//鼠标循环
	switch(m.uMsg)
	{
		case WM_LBUTTONDOWN:
			return USER_LBUTTONDOWN;
		case WM_RBUTTONDOWN:
			return USER_RBUTTONDOWN;
		case WM_MBUTTONDOWN:
			return USER_MBUTTONDOWN;
		default:
			if(flag==1)
			{
				second=time(NULL);
				_stprintf(m_time, _T("%.0lf"), difftime(second,first));
				setfont(20,0,"黑体");
				outtextxy(590+yy,100,m_time);
				if(leishu-biaoji<10&&leishu-biaoji>=0)
				{
					shenglei[0]=' ';
					_stprintf(shenglei+1, _T("%d"), leishu-biaoji);
				}
				else
				{
					_stprintf(shenglei, _T("%d"), leishu-biaoji);
				}
				outtextxy(590+yy,200,shenglei);
			}
			break;
	}
	return 0;
}

/
//布雷函数
void bulei()
{
	int x,y;
	memset(a,0,sizeof(a));
	srand( (unsigned)time( NULL )*5 );
	for(int i=1;i<=leishu;i++)
	{

		x=rand()%wide+1;
		y=rand()%high+1;
		if(a[x][y]!=0)
		{
			i--;
			continue;
		}
		else
			a[x][y]=1000;
	}
	for(i=1;i<=wide;i++)
		for(int j=1;j<=high;j++)
		{
			if(a[i][j]==1000)
				continue;
			else
			{
				a[i][j]=a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i][j-1]+a[i][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1];
				a[i][j]/=1000;
			}
		}
	
}

/
//判断是否为雷函数
void panduan()
{	
	int C=255;
	char s[10];
	zuih[0].time=difftime(second,first);
	setfont(150,0,"黑体");
	if(shengyu==high*wide-leishu)
	{
		chuangkzh();
		for(int i=0;i<128;i++)
		{
			setcolor(RGB(C,0,0));
			outtextxy(170+yy/2,200,"胜利");
			C-=8;
			if(C<=0)
				C=255;
			Sleep(20);
		}
		if(zuih[0].time=0)
				{
					shenglei[0]=' ';
					_stprintf(shenglei+1, _T("%d"), leishu-biaoji);
				}
				else
				{
					_stprintf(shenglei, _T("%d"), leishu-biaoji);
				}
				outtextxy(590+yy,200,shenglei);
		}
		if(MouseHit())
		{
			while(end!=1)
			{
				m = GetMouseMsg();
				int x,y;
				x=(m.x-60)/n+1;
				y=(m.y-60)/mn+1;
				if(a[x][y]==-1)
					saolei();
				opt = GetMessage(m);
				end=DispatchMessage2(m, opt);
			}
		}
	}
return 0;
}

///
//扫雷中操作
int saolz(MOUSEMSG m)
{
	
	int x,y,zero=0;
	int x1,y1;
	x=(m.x-60)/n+1;
	y=(m.y-60)/mn+1;
	if(a[x][y]==-1)
		saolei();
	setfillstyle(BLACK);
	floodfill((x-1)*n+63,(y-1)*mn+63,WHITE);
	setfont(n-2,0,"黑体");
	switch(a[x][y])
	{
		case 0:a[x][y]=-1;shengyu++;zerodg(x,y);break;
		case 1:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"1");shengyu++;break;
		case 2:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"2");shengyu++;break;
		case 3:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"3");shengyu++;break;
		case 4:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"4");shengyu++;break;
		case 5:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"5");shengyu++;break;
		case 6:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"6");shengyu++;break;
		case 7:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"7");shengyu++;break;
		case 8:setcolor(WHITE);outtextxy((x-1)*n+60+n/4,(y-1)*mn+61,"8");shengyu++;break;
		default:setfillstyle(RED);floodfill(m.x,m.y,WHITE);panduan();break;
	}
	a[x][y]=-1;
	saolei();
	return 0;
}


//zero递归
int zerodg(int x,int y)
{
	for(int j=y-1;jwide||j>high)
				continue;
			else
			{
				//Sleep(50);
				setfillstyle(BLACK);
				floodfill((i-1)*n+63,(j-1)*mn+63,WHITE);
				switch(a[i][j])
					{
						case 0:a[i][j]=-1;shengyu++;zerodg(i,j);break;
						case 1:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"1");shengyu++;break;
						case 2:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"2");shengyu++;break;
						case 3:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"3");shengyu++;break;
						case 4:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"4");shengyu++;break;
						case 5:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"5");shengyu++;break;
						case 6:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"6");shengyu++;break;
						case 7:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"7");shengyu++;break;
						case 8:setcolor(WHITE);outtextxy((i-1)*n+60+n/4,(j-1)*mn+61,"8");shengyu++;break;
						case -1:break;
					}
				a[i][j]=-1;
				
			}
			return 0;
}

课程设计一共两个星期,扫雷要图形界面和鼠标,前一个星期基本啥也没看,合计学习下传说中的api啊~~MFC啊~~~结果都木有成功……

后来发现了easyX……好东西啊!(其实就是把TC的图形库graphics转到了vc中)……


你可能感兴趣的:(代码)