游戲“連連看”是源自臺灣的桌面小游戲,自從流入大陸以來風靡一時,也吸引眾多程式員開發出多種版本的“連連看”,這其中,顧芳撰寫的“阿達連連看”以其精良的制作廣受好評,這也成為顧方“阿達系列軟體”的核心產品,并于2004年,取得了國家著作權局的計算機軟體登記證書,

隨著Flash應用的流行,網上出現了多種在線Flash版本“連連看”,如“水晶連連看”、“果蔬連連看”等,流行的“水晶連連看”以華麗界面吸引了一大批的女性玩家,
2008年,隨著社交網路的普及和開放平臺的興起,“連連看”被引入了社交網路,“連連看”與個人空間相結合,被快速的傳播,成為一款熱門的社交游戲,其中以開發者Jonevey在Manyou開放平臺上推出的“寵物連連看”最為流行,
網路小游戲、網頁游戲越來越受網民歡迎,除了玩的方法簡單外(不像其他游戲還需要注冊下載繁瑣程序),很多游戲不乏經典,連連看游戲就是典型,
不管走到哪個網頁游戲網站,連連看游戲總是排在受玩家歡迎排名的前5位,休閑、趣味、益智是連連看玩不厭的精華,且不分男女老少、工薪白領,是一款適合大眾的經典網路、單機休閑小游戲,
我們今天就來看看我們自己能不能寫出這樣一個游戲呢?
來,話不多說,直接開始,gogogo!!!
今天的代碼不是很多,好好看好好學
首先是我們的老朋友,構造結構體以及一切準備作業
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();
}
嗯,這個《連連看》游戲專案就解決啦,在寫這個專案的時候,還是遇到一些困難的,重點就是邏輯,一定要想清楚,怎么去判斷他們是可以消除的,好啦,希望可以讓大家從中感受到編程的快樂,也希望大家可以給UP主一個關注,非常感謝大家了!!!
后續UP主還會發布更多的專案原始碼以及學習資料,希望大家可以持續關注,有什么問題可以回帖留言,我盡量回答,想要C/C++學習資料以及其他專案的原始碼的可以加群【1083227756】了解,想要對程式員的未來發展有興趣的也可加群閑聊,也可以關注微信公眾號:【狐貍的編碼時光】,希望和大家一起學習進步!!!
點擊下方鏈接進群更快拿到學習資料以及專案素材
進群領取學習資料以及專案原始碼素材
https://jq.qq.com/?_wv=1027&k=sttR3REF
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/348511.html
標籤:其他
下一篇:最近大火的「元宇宙」究竟是什么

