游戏思路:
每张图片都有一个数字对应 空用0表示使用二维数组 保存每个图片对应的数据鼠标点击图片 向周围空地移动判断游戏 是否游戏胜利
需要的素材:
游戏效果如下所示:
代码如下所示:
#include <graphics.h>//图形界面库#include <cstdlib>#include <ctime>#include <iostream>#include <mmsystem.h>//音乐头文件#pragma comment (lib,"winmm")//库文件class JigsawPuzzle//拼图的类{private:const int Size = 640;//游戏界面大小IMAGE img[9];int arr[3][3] = {0 };//存储地图元素的意思int arr_fz[9] = {0 };//9 个数字 随机不重复public:void InitRun();//初始化游戏开始界面void InitLinePic();//开始游戏之后的画面void RunGame();//开始游戏bool JudgeGame();//判断游戏};int main(){srand((unsigned)time(nullptr));JigsawPuzzle jigsawpuzzle;jigsawpuzzle.InitRun();jigsawpuzzle.RunGame();return 0;}void JigsawPuzzle::InitRun(){initgraph(Size, Size, SHOWCONSOLE);mciSendString("play bj.mp3 repeat", 0, 0, 0);loadimage(nullptr, "start.jpg", 640, 640);//rectangle(Size / 2 - 80, Size / 2 + 30, Size / 2 + 80, Size / 2 + 90);settextstyle(40, 15, "楷体");settextcolor(RED);setbkmode(TRANSPARENT);outtextxy(Size / 2 - 60, Size / 2 + 40, "开始拼图");MOUSEMSG msg;while (1)//改变颜色{msg = GetMouseMsg();//获取鼠标消息if (msg.x - (Size / 2 - 80) < 160 && msg.x - (Size / 2 - 80) > 0 && msg.y - (Size / 2 + 30) < 60 && msg.y - (Size / 2 + 30) > 0){settextcolor(RGB(0, 255, 255));if (msg.mkLButton)break;}elsesettextcolor(RED);outtextxy(Size / 2 - 60, Size / 2 + 40, "开始拼图");}InitLinePic();// 开始拼图后的画面}void JigsawPuzzle::InitLinePic(){setbkcolor(RGB(0, 255, 255));cleardevice();setlinecolor(GREEN);for (int i = 1; i < 3; i++){line(0, i * 213, Size, i * 213);line(i * 213, 0, i * 213, Size);}for (int i = 0; i < 9; i++)//九个数字不重复//只用一次 赋值给二维数组arr{arr_fz[i] = rand() % 9 + 1;for (int j = i - 1; j >= 0; j--){if (arr_fz[i] == arr_fz[j]){i--;break;}}}for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++)arr[i][j] = arr_fz[i * 3 + j];}//加载图片存放的图片不是按顺序来的 跟arr_fz里面的数据一样char buf[16];for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){memset(buf, 0, sizeof(buf));sprintf(buf, "pic\\%d.jpg", arr[i][j]);loadimage(&img[i], buf, 180, 180);putimage(j * 213 + 20, i * 213 + 20, &img[i]);}}}void JigsawPuzzle::RunGame(){MOUSEMSG msg;//BeginBatchDraw();while (1){msg = GetMouseMsg();if (msg.mkLButton){if (JudgeGame() == true){MessageBox(nullptr, " 游戏胜利", " 拼图游戏", MB_OK);exit(0);}for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){//鼠标点击到了一块拼图if (msg.x - (j * 213 + 20) < 180 && msg.y - (i * 213 + 20) < 180 &&msg.x - (j * 213 + 20) > 0 && msg.y - (i * 213 + 20) > 0){if (i - 1 >= 0 && arr[i - 1][j] == 3)// || arr[i / 3 + 1][i % 3] == 3 || arr[i / 3][i % 3 - 1] == 3 || arr[i / 3][i % 3 + 1] == 3){int t = arr[i - 1][j];arr[i - 1][j] = arr[i][j];arr[i][j] = t;break;}else if (i + 1 <= 2 && arr[i + 1][j] == 3){int t = arr[i + 1][j];arr[i + 1][j] = arr[i][j];arr[i][j] = t;break;}else if (j - 1 >= 0 && arr[i][j - 1] == 3){int t = arr[i][j - 1];arr[i][j - 1] = arr[i][j];arr[i][j] = t;break;}else if (j + 1 <= 2 && arr[i][j + 1] == 3){int t = arr[i][j + 1];arr[i][j + 1] = arr[i][j];arr[i][j] = t;break;}}}}char buf[16];for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){memset(buf, 0, sizeof(buf));sprintf(buf, "pic\\%d.jpg", arr[i][j]);loadimage(&img[i], buf, 180, 180);putimage(j * 213 + 20, i * 213 + 20, &img[i]);}}std::cout << std::endl;for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){std::cout << arr[i][j];}std::cout << std::endl;}}EndBatchDraw();}}bool JigsawPuzzle::JudgeGame(){if (arr[0][0] == 1 && arr[0][1] == 2 && arr[0][2] == 3 &&arr[1][0] == 4 && arr[1][1] == 5 && arr[1][2] == 6 &&arr[2][0] == 7 && arr[2][1] == 8 && arr[2][2] == 9){return true;}elsefalse;}