您好,登錄后才能下訂單哦!
本篇內容主要講解“C++怎么實現連連看游戲”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C++怎么實現連連看游戲”吧!
struct GridInfor //記入擊中圖片信息 { int idx,idy; //圖紙坐標 int leftx,lefty; //屏幕坐標 int GridID; //圖片類型 }pre,cur,dur; struct //記錄連線點 { int x; int y; }point[4]; static int pn; //記錄連線點個數 void InitFace (); //初始化界面 void Shuffle (); //隨即打亂圖片 void ShowGrid (); //顯示圖片 void RandGrid (); //繪制地圖 void Link (); //連接兩圖 void Des_direct (); //直接相消 void Des_one_corner(); //一折相消 void Des_two_corner(); //兩折相消 void Load_picture (); //加載圖片 void Init_Grid (GridInfor& pre); //初始化格子信息 void Leftbottondown (MOUSEMSG mouse); //實現鼠標左擊效果 void Draw_frame (int leftx,int lefty); //繪制邊框 void Mousemove (int leftx,int lefty); //實現鼠標移動效果 bool Judg_val (int leftx,int lefty); //判斷鼠標是否在游戲區 void SeleReact (int leftx,int lefty); //顯示選中效果 void TranstoPhycoor (int* idx,int* idy); //圖紙坐標轉變為屏幕坐標 void GridPhy_coor (int& leftx,int& lefty); //規范物理坐標 void iPaint (long x1,long y1,long x2,long y2); //將直線銷毀 void DrawLine (int x1,int y1,int x2,int y2) ; //用直線連接兩圖 bool DesGrid (GridInfor pre,GridInfor cur); //判斷兩者是否能相消 bool Match_direct (POINT ppre,POINT pcur); //判斷兩者是否能夠直接相消 bool Match_one_corner (POINT ppre,POINT pcur); //判斷兩者是否能一折相消 bool Match_two_corner (POINT ppre,POINT pcur); //判斷兩者是否能兩折相消 void ExchaVal (GridInfor& pre,GridInfor& cur); //交換圖片信息 bool Single_click_judge (int mousex,int mousey); //判斷單擊是否有效 void RecordInfor (int leftx,int lefty,GridInfor &grid); //記錄選中的信息 void TranstoDracoor (int mousex,int mousey,int *idx,int *idy); //鼠標坐標轉化為圖紙坐標 void Explot (POINT point,int *left,int *right,int *top,int *bottel);//探索point點附近的空位置
接下來是我們隨機產生圖片的邏輯函數,這個要好好理解
void RandGrid() //產生圖片的標記 { for(int iCount = 0, x = 1; x <= 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; } } }
下面是我們的重點操作,鼠標控制函數,好好看好好學,寫的時候一定要有耐心,別被bug擊破了內心
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]); else putimage(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]); } } else click = 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); }
另外一個重點就是我們判斷函數了,第一次使用鼠標點擊棋盤中的棋子,該棋子此時為“被選中”,以特殊方式顯示;再次以鼠標點擊其他棋子,若該棋子與被選中的棋子圖案相同,且把第一個棋子到第二個棋子連起來,中間的直線不超過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;i < k ;i++) if(GridID[ppre.y][i] != 0) return false; if(i == k) goto FIND; } return false; FIND: point[pn].x = pcur.x, point[pn].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) //判斷兩者是否能一折相消 { 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; 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 = y,pn++; return true; } bool Match_two_corner(POINT ppre,POINT pcur) //判斷兩者是否能兩折相消 { 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; 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,pn++; return true; } void Explot(POINT point,int *left,int *right,int *top,int *bottel) { int x = point.x,y = point.y; x++; while(x <= COL + 1 && GridID[y][x] == 0) x++; *right = x - 1; x = point.x; x--; while(x >= 0 && GridID[y][x] == 0) x--; *left = x + 1; x = point.x; y++; while(y <= ROW + 1 && GridID[y][x] == 0) y++; *bottel= y - 1; y = point.y; y--; while(y >= 0 && GridID[y][x] == 0) y--; *top = y + 1; }
最后用主函數調用,這樣就可以啦
void main() { initgraph(M,N); 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(); }
到此,相信大家對“C++怎么實現連連看游戲”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。