涂格子游戏14051229屠智伟头文件和宏定义#includegraphics.h#includeconio.h#defineMaxNum5//单边最大格子数#defineG_length30//格子边长#defineUSER_LBUTTONDOWN101#defineUSER_RBUTTONDOWN102#defineUSER_MBUTTONDOWN103structGrid{intleftintright;//游戏区域上下左右边界inttop;intbottom;intarray[MaxNum][MaxNum]//用于标记格子的状态intnum;}grid;voidmain(){intopt,end=0;grid.num=2;welcome();PaintGrid(320,240,grid.num,RGB(0,255,0));MOUSEMSGm;while(end!=1){m=GetMouseMsg();opt=GetMessage(m);end=DispatchMessage(m,opt);}goodbye(grid.num);closegraph();}结束voidwelcome(){initgraph(640,480);//初始化窗口//输出屏幕提示settextcolor(RGB(0,255,0));settextstyle(64,0,_T(黑体));outtextxy(70,50,_T(涂格子游戏(点灯)));settextcolor(WHITE);settextstyle(16,0,_T(宋体));outtextxy(100,200,_T(“每点一个格子上下左右的格子也会做出与现状相反的动作));outtextxy(100,240,_T(总共4关,左键填色,右键重来,中键退出));outtextxy(100,280,_T(只是一个功能演示版本,以后再改进));outtextxy(400,320,_T(by:doufuguolyl));//实现按任意键继续的闪烁效果intc=255;while(!kbhit()){settextcolor(RGB(0,c,0));outtextxy(280,400,_T(按任意键继续));c-=8;if(c0)c=255;Sleep(20);}getch();cleardevice();}返回mainkbhit()函数,用以判断键盘是否有输入voidPaintGrid(intMid_x,intMid_y,intnum,intcolor){intx,y,nx,ny;//游戏区域大grid.left=Mid_x-num*G_length/2;grid.right=Mid_x+num*G_length/2;grid.top=Mid_y-num*G_length/2;grid.bottom=Mid_y+num*G_length/2;setlinecolor(color);for(x=grid.left;x=grid.right;x+=G_length)//绘制格子{line(x,grid.top,x,grid.bottom);Sleep(10);}for(y=grid.top;y=grid.bottom;y+=G_length){line(grid.left,y,grid.right,y);Sleep(10);}for(x=0;xnum;x++)//初始化单元格for(y=0;ynum;y++)grid.array[x][y]=-1;for(nx=0;nxnum;nx++)for(ny=0;nynum;ny++){setfillcolor(BLACK);x=nx*G_length+grid.left;y=ny*G_length+grid.top;solidrectangle(x+1,y+1,x+G_length-1,y+G_length-1);}}返回mainfor(x=20;x10;x--)//绘制外边框{line(grid.left-x,grid.top-x,grid.right+x,grid.top-x);line(grid.left-x,grid.bottom+x,grid.right+x,grid.bottom+x);line(grid.left-x,grid.top-x,grid.left-x,grid.bottom+x);line(grid.right+x,grid.top-x,grid.right+x,grid.bottom+x);Sleep(5);}PaintGrid游戏界面和初始化单元格(Mid_x,Mid_y)屏幕中心坐标(320,240),num单边格子数,color格子线条颜色格子生成过程返回PaintGrid()外边框生成过程返回PaintGrid()画一条边框…………画五条边框画十条边框/*structMOUSEMSG{UINTuMsg;boolmkCtrl;boolmkShift;boolmkLButton;boolmkMButton;boolmkRButton;intx;inty;intwheel;//滚轮滚动值};*/GetMouseMsg();//获取一个鼠标消息如果没有就等待关于鼠标返回mainintGetMessage(MOUSEMSGm){switch(m.uMsg){caseWM_LBUTTONDOWN:returnUSER_LBUTTONDOWN;caseWM_RBUTTONDOWN:returnUSER_RBUTTONDOWN;caseWM_MBUTTONDOWN:returnUSER_MBUTTONDOWN;}return0;}作用:按下哪个键,返回对应的值返回maingoodbyevoidgoodbye(intnum){//清屏BeginBatchDraw();for(inty=0;y=480;y+=1){setlinecolor(RGB(0,255,0));line(0,y,640,y);line(0,480-y,640,480-y);FlushBatchDraw();Sleep(1);setlinecolor(RGB(0,0,0));line(0,y,640,y);line(0,480-y,640,480-y);}EndBatchDraw();//显示游戏结果if(num==MaxNum+1){settextcolor(RGB(0,255,0));settextstyle(48,0,_T(黑体));outtextxy(280,200,_T(通关));settextstyle(20,0,_T(黑体));}else{settextcolor(RGB(0,255,0));settextstyle(48,0,_T(黑体));outtextxy(200,200,_T(再接再厉));settextstyle(20,0,_T(黑体));}getch();}返回main返回goodbye闪烁不闪烁#includegraphics.hvoidmain(){initgraph(640,480);setlinecolor(WHITE);setfillcolor(RED);//BeginBatchDraw();for(inti=50;i600;i++){circle(i,100,40);floodfill(i,100,WHITE);//FlushBatchDraw();Sleep(10);cleardevice();}//EndBatchDraw();closegraph();}intDispatchMessage(MOUSEMSGm,intopt){switch(opt){caseUSER_LBUTTONDOWN:OnLButtonDown(m,grid.num);if(JudgeFull(grid.num,grid.array)==1){grid.num++;if(grid.numMaxNum)//如果通关,return1结束{return1;break;}elseNextLevel(grid.num);}break;caseUSER_RBUTTONDOWN:OnRButtonDown(grid.num);break;caseUSER_MBUTTONDOWN:return1;break;}return0;}返回mainvoidOnRButtonDown(intnum){intx,y,nx,ny;for(x=0;xnum;x++)for(y=0;ynum;y++)grid.array[x][y]=-1;for(nx=0;nxnum;nx++)//画黑色填充的矩形涂黑过程for(ny=0;nynum;ny++){setfillcolor(BLACK);x=nx*G_length+grid.left;y=ny*G_length+grid.top;solidrectangle(x+1,y+1,x+G_length-1,y+G_length-1);}}右键按下清除………………一张图片表示一个for循环产生的效果返回DispatchMessagevoidNextLevel(intnum){BeginBatchDraw();//清屏操作,同goodbyefor(doubley=0;y=480;y+=0.5){setlinecolor(RGB(0,255,0));settextcolor(RGB(0,255,0));line(0,y,640,y);line(0,480-y,640,480-y);outtextxy(300,y,_T(下一关));FlushBatchDraw();Sleep(3);setlinecolor(RGB(0,0,0));settextcolor(RGB(0,0,0));line(0,y,640,y);line(0,480-y,640,480-y);outtextxy(300,y,_T(下一关));}EndBatchDraw();//绘制下关格子PaintGrid(320,240,num,RGB(0,255,0));}返回DispatchMessageintJudgeFull(intnum,intarray[MaxNum][MaxNum]){intc=-1;intnx=0,ny=0;while(nxnum&&nynum){for(nx=0;nxnum;nx++)for(ny=0;nynum;ny++)if(array[nx][ny]==1)continue;elsereturnc;}c=1;returnc;}返回DispatchMessage判断格子是否填满参数:num:单边格子数目array:生成数组接收实参voidOnLButtonDown(MOUSEMSGm,intnum){intnx,ny,x,y;if(m.xgrid.left&&m.xgrid.right&&m.ygrid.top&&m.ygrid.bottom){//计算位置nx=(int)(m.x-grid.left)/G_length;ny=(int)(m.y-grid.top)/G_length;//转换格子状态grid.array[nx][ny]=-grid.array[nx][ny];if(nx=0&&nxnum-1)grid.array[nx+1][ny]=-grid.array[nx+1][ny];if(nx0&&nx=num-1)grid.array[nx-1][ny]=-grid.array[nx-1][ny];if(ny=0&&nynum-1)grid.array[nx][ny+1]=-grid.array[nx][ny+1];if(ny0&&ny=num-1)grid.array[nx][ny-1]=-grid.array[nx][ny-1];//扫描填色for(nx=0;nxnum;nx++)for(ny=0;nynum;ny++){if(grid.array[