C++ 风靡一时的连连看游戏的实现流程详解

时间:2022-04-26 04:51:26

C++ 风靡一时的连连看游戏的实现流程详解

随着Flash应用的流行,网上出现了多种在线Flash版本“连连看”。如“水晶连连看”、“果蔬连连看”等,流行的“水晶连连看”以华丽界面吸引了一大批的女性玩家。 2008年,随着社交网络的普及和开放平台的兴起,“连连看”被引入了社交网络。“连连看”与个人空间相结合,被快速的传播,成为一款热门的社交游戏,其中以开发者Jonevey在Manyou开放平台上推出的“宠物连连看”最为流行。 网络小游戏、网页游戏越来越受网民欢迎,除了玩的方法简单外(不像其他游戏还需要注册下载繁琐过程),很多游戏不乏经典。连连看游戏就是典型。 不管走到哪个网页游戏网站,连连看游戏总是排在受玩家欢迎排名的前5位,休闲、趣味、益智是连连看玩不厌的精华,且不分男女老少、工薪白领,是一款适合大众的经典网络、单机休闲小游戏。 我们今天就来看看我们自己能不能写出这样一个游戏呢?

来,话不多说,直接开始,gogogo!!!

今天的代码不是很多,好好看好好学

首先是我们的老朋友,构造结构体以及一切准备工作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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点附近的空位置

接下来是我们随机产生图片的逻辑函数,这个要好好理解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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击破了内心

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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; 
}

最后用主函数调用,这样就可以啦

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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++ 连连看内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_45713725/article/details/121140559