<= ROW; ++x ){for( int y = 1; y <= COL; ++y ){GridID[x][y] =iCount++ % GridNum + 1;} }}void Shuffle()//打乱棋盘{int ix, iy, jx, jy, grid;for( int k = 0; k < 84; ++k ){ix = rand() % ROW + 1; // 产生 1 - COL 的随机数iy = rand() % COL + 1; // 产生 1 - ROW 的随机数jx = rand() % ROW + 1;// 产生 1 - COL 的随机数jy = rand() % COL + 1; // 产生 1 - ROW 的随机数if( GridID[ix][iy] != GridID[jx][jy] )//如果不相等就交换数据{grid = GridID[ix][iy];GridID[ix][iy] = GridID[jx][jy];GridID[jx][jy] = grid;} } }初始化界面///void InitFace(){srand((unsigned)time(NULL));Load_picture();RandGrid();IMAGE image3;loadimage(&image3,"res\\bg.bmp");putimage(0,0,&image3);getimage(&image2,3 * 42,2 * 48,42, 48);Shuffle();ShowGrid(); }void Load_picture()//加载图片{IMAGE image1,background;loadimage(&image1,"IMAGE","grids"); SetWorkingImage(&image1);for(int i = 1 ;i < GridNum + 1 ;i ++) for(int j = 0;j < 2;j++)getimage(&image[i][j],j * 42,i * 48,42, 48);loadimage(&background,"IMAGE","bg");SetWorkingImage(&background);getimage(&image2,3 * 42,2 * 48,42, 48);SetWorkingImage();putimage(0,0,&background);}void ShowGrid(){int idx,idy;for(int i = 0 ;i < ROW; i ++)for(int j = 0 ;j < COL ; j++){idy = i * 48 + topedge ,idx = j * 42 + leftedge;putimage(idx,idy,&image[GridID[i + 1][j + 1]][0]);}}/鼠标操作void Mousemove (int leftx,int lefty)//鼠标移动时的变化{static int prex,prey,preidx,preidy,curidx,curidy;if(Judg_val(leftx,lefty)){ TranstoDracoor(leftx,lefty,&curidx,&curidy);//转化为图纸坐标if(GridID[curidy][curidx] != 0){ GridPhy_coor(leftx,lefty);if(pre.idx == preidx && pre.idy == preidy)putimage(prex,prey,&image[GridID[preidy][preidx]][1]);elseputimage(prex,prey,&image[GridID[preidy][preidx]][0]);prex = leftx,prey = lefty;preidx = curidx,preidy = curidy;Draw_frame(leftx,lefty);//绘制边框 } }} void Leftbottondown (MOUSEMSG mouse)//左击时的变化{static int click = 0,idx,idy;click++;SeleReact (mouse.x,mouse.y);//显示选中效果 if(click == 1)RecordInfor(mouse.x,mouse.y,pre);if(click == 2) { TranstoDracoor (mouse.x,mouse.y,&idx,&idy);if(idx != pre.idx || idy != pre.idy){RecordInfor (mouse.x,mouse.y,cur);if(pre.GridID == cur.GridID &&DesGrid(pre,cur)){GridID[pre.idy][pre.idx] = GridID[cur.idy][cur.idx] =0;Link ();pn = 0;putimage(pre.leftx,pre.lefty,&image2);putimage(cur.leftx,cur.lefty,&image2);Init_Grid(pre);Init_Grid(cur);click = 0;}else{ExchaVal(dur,pre);ExchaVal(pre,cur);Init_Grid(cur);click = 1;putimage(dur.leftx,dur.lefty,&image[GridID[dur.idy][dur.idx]][0]);} }elseclick = 1; }}void SeleReact (int leftx,int lefty)//选中时效果{ if(Judg_val(leftx,lefty)){int idx,idy;TranstoDracoor (leftx,lefty,&idx,&idy);GridPhy_coor (leftx,lefty);putimage(leftx,lefty,&image[GridID[idy][idx]][1]);} }bool Judg_val(int leftx,int lefty)//判断鼠标是否在游戏区{ return leftx > leftedge && leftx < leftedge + GridW * COL && lefty > topedge&&lefty < topedge + GridH * ROW;}void TranstoDracoor (int mousex,int mousey ,int *idx,int *idy) //鼠标坐标转化为图纸坐标{if(Judg_val(mousex,mousey)){ *idx = (mousex - leftedge) / 42 + 1;*idy = (mousey - topedge) / 48 + 1 ;} }void RecordInfor(int leftx,int lefty,GridInfor &grid)//记录选中的信息{TranstoDracoor(leftx,lefty,&grid.idx,&grid.idy);grid.leftx = (grid.idx - 1) * 42 + leftedge;grid.lefty = (grid.idy - 1) * 48 + topedge;grid.GridID = GridID[grid.idy][grid.idx];}bool Single_click_judge (int mousex,int mousey)//判断单击是否有效{int idx,idy;TranstoDracoor (mousex,mousey,&idx,&idy);//转化为图纸坐标if(Judg_val(mouse.x,mouse.y) && GridID[idy][idx] != 0)return true;return false;}void Draw_frame(int leftx,int lefty)//绘制方框{setcolor(RGB(126,91,68));setlinestyle(PS_SOLID,NULL,1);rectangle(leftx,lefty,leftx+41,lefty+47);rectangle(leftx + 2,lefty + 2,leftx+39,lefty+45);setcolor(RGB(250,230,169));rectangle(leftx + 1,lefty + 1,leftx+40,lefty+46); }判断消除操作/bool DesGrid (GridInfor pre,GridInfor cur)//判断两者是否能相消{bool match = false; POINT ppre,pcur; ppre.x = pre.idx; ppre.y = pre.idy;pcur.x = cur.idx; pcur.y = cur.idy;if(Match_direct(ppre,pcur)) match = true;else if(Match_one_corner(ppre,pcur)) match = true;else if(Match_two_corner(ppre,pcur)) match =true;return match;}bool Match_direct(POINT ppre,POINT pcur)//判断两者是否能够直接相消{int k,t;if(ppre.x == pcur.x){ k = ppre.y > pcur.y ? ppre.y : pcur.y;t = ppre.y
- 使用 Visual Studio 2019 【在更】基础 | C 语言笔记
- C语言实现检测Linux设备网络状态
- Excel用函数求C语言的最高分,excel怎样用函数求最高分
- 《草根自媒体达人运营实战》一一1.4 自媒体的形式
- c语言任意长度小数加减法,[c++]大数运算
- 18 如何安装SCO Unix《精通Unix下C语言编程与项目实践》读书笔记
- 电脑c语言保存键是哪个好,9个值得保存的计算机技巧!
- 使用gosec工具对golang项目进行安全检查
- 雷达编程实战之Firmware内存优化
- ECC OpenCV 项目开发实战--对图像进行对齐