时间:2023-08-18 11:42:22来源:互联网
这篇文章主要为大家详细介绍了C语言实现——《连连看》小游戏,示例代码介绍的非常详细,具有想当的参考价值,感兴趣的小伙伴们可以参考一下!
游戏介绍:
连连看小游戏速度节奏快,画面清晰可爱,适合细心的玩家。丰富的道具和公共模式的加入,增强游戏的竞争性。多样式的地图,使玩家在各个游戏水平都可以寻找到挑战的目标,长期地保持游戏的新鲜感。本期举例的项目类似宠物连连看,小动物造型的连连看游戏(这个主要看你准备好的图片素材)
游戏玩法
加载游戏后,点击画面中间的图像即进入游戏状态。使用鼠标左键即可,点击相同的两张图卡,用三根内线连在一起就可以消除。只要我们在有限的时间里,用我们智慧消除相连卡片,就可以获得最终的胜利。那么我们了解了规则之后,就动手来试试吧!
编译器:VS2013/2019最佳;
插件:图形库插件easyX,涉及图片素材可以自行百度找也可以关注文末领取;
效果图展示:
源代码示例:
include include pragma comment(lib,"winmm.lib")//150 150 12 7 21 易//60 100 16 9 32 中//100 120 14 8 28 难define topedge 150 //游戏区距上边框距离define ROW 7 //游戏区行数define GridW 42 //游戏图片的长define N 555 //开屏大小(宽) #define M 785 //开屏大小(长)IMAGE image[GridNum 1][2]; //图片库IMAGE image2; //填充图片int GridID[ROW 2][COL 2]; //游戏图纸MOUSEMSG mouse; //记录鼠标信息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 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();}////////////////////////////////////////生成操作//////////////////////////////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;}}}////////////////////////////////初始化界面///////////////////////////////////////void InitFace() {srand((unsigned)time(NULL));Load_picture(); RandGrid();IMAGE image3;loadimage(&image3,"resbg.bmp");putimage(0,0,&image3);getimage(&image2,3 * 42,2 * 48,42, 48); Shuffle(); ShowGrid();}void Load_picture()//加载图片{IMAGE image1,background;loadimage(&image1,"IMAGE","grids"); SetWorkingImage(&image1);for(int i = 1 ;i < GridNum 1 ;i )for(int j = 0;j < 2;j )getimage(&image[i][j],j * 42,i * 48,42, 48);loadimage(&background,"IMAGE","bg");SetWorkingImage(&background);getimage(&image2,3 * 42,2 * 48,42, 48);SetWorkingImage();putimage(0,0,&background);}void ShowGrid(){int idx,idy;for(int i = 0 ;i < ROW; i )for(int j = 0 ;j leftedge && leftx topedge && lefty 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 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 = 0 && GridID[y][x] == 0) x--; *left = x 1; x = point.x; y ;while(y = 0 && GridID[y][x] == 0) y--; *top = y 1; }/////////////////////////////////消除操作////////////////////////////////////////void Link (){switch(pn){case 2:Des_direct(); break;case 3:Des_one_corner();break;case 4:Des_two_corner(); break;default : break;}}void Des_direct (){TranstoPhycoor(&point[0].x,&point[0].y);TranstoPhycoor(&point[1].x,&point[1].y);DrawLine(point[0].x,point[0].y,point[1].x,point[1].y);Sleep(250);iPaint(point[0].x,point[0].y,point[1].x,point[1].y);}void Des_one_corner(){TranstoPhycoor(&point[0].x,&point[0].y);TranstoPhycoor(&point[1].x,&point[1].y);TranstoPhycoor(&point[2].x,&point[2].y);DrawLine(point[0].x,point[0].y,point[1].x,point[1].y);DrawLine(point[1].x,point[1].y,point[2].x,point[2].y);Sleep(250);iPaint(point[0].x,point[0].y,point[1].x,point[1].y);iPaint(point[1].x,point[1].y,point[2].x,point[2].y);}void Des_two_corner(){TranstoPhycoor(&point[0].x,&point[0].y);TranstoPhycoor(&point[1].x,&point[1].y);TranstoPhycoor(&point[2].x,&point[2].y);TranstoPhycoor(&point[3].x,&point[3].y);DrawLine(point[0].x,point[0].y,point[1].x,point[1].y);DrawLine(point[1].x,point[1].y,point[2].x,point[2].y);DrawLine(point[2].x,point[2].y,point[3].x,point[3].y);Sleep(250);iPaint(point[0].x,point[0].y,point[1].x,point[1].y);iPaint(point[1].x,point[1].y,point[2].x,point[2].y);iPaint(point[2].x,point[2].y,point[3].x,point[3].y);}void DrawLine (int x1,int y1,int x2,int y2){setlinestyle(PS_SOLID,NULL,3);setcolor(RGB(90,43,9));line(x1 21,y1 24,x2 21,y2 24);}void iPaint (long x1,long y1,long x2,long y2) { int minx,miny,maxx,maxy;if(x1 == x2){maxy = y1 > y2? y1:y2;miny = y1 < y2? y1:y2;for(int i = miny; i x2? x1:x2;minx = x1 < x2? x1:x2;for(int j = minx; j <= maxx;j = 42 )putimage(j,y1,&image2);}}/////////////////////////////////////////////////////////////////////////////////void GridPhy_coor(int& leftx,int& lefty)//转化为标准物理坐标{leftx = ((leftx - leftedge) / 42) * 42 leftedge;lefty = ((lefty - topedge) / 48) * 48 topedge;}void ExchaVal(GridInfor& pre,GridInfor& cur)//交换格子信息{pre.GridID = cur.GridID;pre.idx = cur.idx;pre.idy = cur.idy;pre.leftx = cur.leftx;pre.lefty = cur.lefty;}void Init_Grid(GridInfor& grid) //初始化格子{grid.GridID = 0;grid.idx= 0; grid.idy = 0;grid.leftx = 0; grid.lefty = 0;}void TranstoPhycoor (int* idx,int* idy) //图纸坐标转变为屏幕坐标{ int x ,y;x =*idx,y = *idy;*idy = (y - 1) * 48 leftedge;*idx = (x - 1) * 42 topedge;}
未完成的部分功能代码,大家也可以自己先去想想试试,每一次的思考就是你进步的过程!
如果学习的过程中有什么问题,以及本项目有什么不懂的地方,都可以来找我交流,我来帮你!
那么今天的分享就到这里了,后续会更新更多精彩项目或者知识内容的,大家要好好学C语言C 哟~
写在最后:对于准备学习C/C 编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
编程学习书籍分享:
编程学习视频分享:
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
对于C/C 感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C 的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!