C++ 風靡一時的連連看遊戲的實現流程詳解

隨著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主一個關註,非常感謝大傢瞭!!!

點擊下方鏈接觀看詳細視頻講解

C/C++遊戲《連連看》詳細教程
https://www.bilibili.com/video/BV1Tv411M7p1/

到此這篇關於C++ 風靡一時的連連看遊戲的實現流程詳解的文章就介紹到這瞭,更多相關C++ 連連看內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: