How to realize Lianliankan games on C++
This article mainly explains "how to realize Lianliankan games on C++". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve Lianliankan games on C++".
Struct GridInfor / / record hit picture information {int idx,idy; / / drawing coordinates int leftx,lefty; / / screen coordinates int GridID; / / picture type} pre,cur,dur; struct / / record connection point {int x; int y;} point [4]; static int pn / / record the number of connection points void InitFace (); / / initialize the interface void Shuffle () / / immediately disturb the picture void ShowGrid (); / / display the picture void RandGrid () / / drawing map void Link (); / / connecting two images void Des_direct () / / directly cancel void Des_one_corner (); / / 90% discount cancel void Des_two_corner (); / / 80% discount cancel void Load_picture () / / load image void Init_Grid (GridInfor& pre); / / initialize lattice information void Leftbottondown (MOUSEMSG mouse) / / to achieve the effect of left mouse click void Draw_frame (int leftx,int lefty); / / draw the border void Mousemove (int leftx,int lefty) / / realize the mouse movement effect bool Judg_val (int leftx,int lefty); / / determine whether the mouse is in the game area void SeleReact (int leftx,int lefty) / / display the selected effect void TranstoPhycoor (int* idx,int* idy); / / convert the drawing coordinates to screen coordinates void GridPhy_coor (int& leftx,int& lefty) / / canonical physical coordinates void iPaint (long x1 magnum long y1 Magnum long x2); / / destroy the straight line void DrawLine (int x1 memint y1 int x2 mint y2); / / connect two graphs bool DesGrid (GridInfor pre,GridInfor cur) with a straight line / / determine whether the two can cancel bool Match_direct (POINT ppre,POINT pcur); / / determine whether the two can directly cancel bool Match_one_corner (POINT ppre,POINT pcur) / / determine whether the two can cancel the 90% discount bool Match_two_corner (POINT ppre,POINT pcur); / / judge whether the two can cancel the 80% discount void ExchaVal (GridInfor& pre,GridInfor& cur) / / Exchange picture information bool Single_click_judge (int mousex,int mousey); / / determine whether the click is valid void RecordInfor (int leftx,int lefty,GridInfor & grid); / / record the selected information void TranstoDracoor (int mousex,int mousey,int * idx,int * idy) / / convert mouse coordinates to drawing coordinates void Explot (POINT point,int * left,int * right,int * top,int * bottel); / / explore the empty position near the point point
Next is the logic function that we randomly generate the picture, which needs to be well understood.
Void RandGrid () / / the tag {for (int iCount = 0, x = 1; x topedge & & lefty) that produces the picture
< 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); } 另外一个重点就是我们判断函数了,第一次使用鼠标点击棋盘中的棋子,该棋子此时为"被选中",以特殊方式显示;再次以鼠标点击其他棋子,若该棋子与被选中的棋子图案相同,且把第一个棋子到第二个棋子连起来,中间的直线不超过3根,则消掉这一对棋子,否则第一颗棋子恢复成未被选中状态,而第二颗棋子变成被选中状态。这个是重中之重,一定好好学,把其中的逻辑理解清楚,别只会Ctrl+c和Ctrl+v 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
< pcur.y ? ppre.y : pcur.y; if(t + 1 == k) goto FIND; for(int i = t + 1;i < k ;i++) if(GridID[i][ppre.x] != 0) return false; if(i == k) goto FIND; } else if(ppre.y == pcur.y) { k = ppre.x >Pcur.x? Ppre.x: pcur.x; t = ppre.x < pcur.x? Ppre.x: pcur.x; if (t + 1 = = k) goto FIND; for (int I = t + 1) if (GridID [ppre.y] [I]! = 0) return false; if (I = = k) goto FIND } return false;FIND: point[ pn] .x = pcur.x, point.y = pcur.y; pn++; point[ pn] .x = ppre.x, point[ pn] .y = ppre.y; pn++; return true } bool Match_one_corner (POINT ppre,POINT pcur) / / to determine whether the two can cancel out the 90% discount {int left,right,top,bottel,x = ppre.x,y = ppre.y; Explot (ppre,&left,&right,&top,&bottel); ppre.y = top-1 / RESEARCHX: if (ppre.y < bottel) ppre.y++ Else goto BACK; if (Match_direct (ppre,pcur)) goto FIND; else goto RESEARCHX;BACK: ppre.y = y; ppre.x = left-1 political Researchy: if (ppre.x < right) ppre.x++; else goto REBACK If (Match_direct (ppre,pcur)) goto FIND; else goto RESEARCHY;REBACK: pn = 0; return false;FIND: point[ pn] .x = x, point[ pn] .y = yrecoverpnkeeper; return true } bool Match_two_corner (POINT ppre,POINT pcur) / / to determine whether the two can cancel each other by 80% discount {int left,right,top,bottel,x = ppre.x,y = ppre.y; Explot (ppre,&left,&right,&top,&bottel); ppre.y = top-1 / RESEARCHX: if (ppre.y < bottel) ppre.y++ Else goto BACK; if (Match_one_corner (ppre,pcur)) goto FIND; else goto RESEARCHX;BACK: ppre.y = y; ppre.x = left-1 political Researchy: if (ppre.x < right) ppre.x++; else goto REBACK If (Match_one_corner (ppre,pcur)) goto FIND; else goto RESEARCHY;REBACK: pn = 0 * * return false;FIND: point[ pn] .x = x, point[ pn] .y = y <... } void Explot (POINT point,int * left,int * right,int * top,int * bottel) {int x = point.x,y = point.y; x colors; while (x = 0 & & GridID [y] [x] = 0) xmuri; * left = x + 1; x = point.x; ycolors; while (y = 0 & GridID [y] [x] = 0) ymuri- * top = y + 1;}
Finally, it is called with the main function, which is fine.
Void main () {initgraph (MJN); mciSendString ("play game_begin.mp3 repeat", NULL, 0, NULL); InitFace (); while (1) {mouse = GetMouseMsg () Switch (mouse.uMsg) {case WM_MOUSEMOVE: Mousemove (mouse.x,mouse.y); break Case WM_LBUTTONDOWN: if (Single_click_judge (mouse.x,mouse.y)) {Leftbottondown (mouse);} break Default: break;}} closegraph ();} at this point, I believe you have a deeper understanding of "how to achieve Lianliankan on C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!