Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ language to realize jigsaw puzzle

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use C++ language to achieve jigsaw puzzles, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Now let's go to know it!

Development environment: Visual Studio 2019 easyx graphics library.

Easyx download official website:

EasyX Graphics Library for C++

Https://easyx.cn/

Easyx usage documentation:

EasyX documentation-function description

Https://docs.easyx.cn/zh-cn/reference

List of game features:

Its main functions are described as follows:

1. Self-adaptive picture size

two。 Dynamic image segmentation

3. View the original

4. Randomly switch pictures

5. Drag the mouse to swap jigsaw puzzles

6. Automatically judge the success of the puzzle

Expand capabilities:

Background music (on, off)

The ESC key returns to the desktop in the game.

Rules of the game window

Game effect

Cover (the music button stretches a little bit)

The initial picture of the game (my heart is cold)

Customs clearance map

one。 Header file and basic quantity # include#include#include#include#include # include / / Music # pragma comment (lib, "Winmm.lib") / / static library, call Music using namespace std;constexpr auto N = 3; / / 3x3 jigsaw puzzle IMAGE img [4], imgs [9]; / / img save the whole picture, imgs temporary jigsaw block int aim_c, aim_r; / / jigsaw block coordinates int map [3] [3] = {0} / / save the puzzle block int NUM = 0; / / count the number of levels two. Cover / / start interface: void start () {loadimage (NULL, L "cover.jpg"); setbkmode (TRANSPARENT); settextcolor (BLACK); settextstyle (60,0, _ T ("italics"), 0memery 0penny false); outtextxy (180,120, L "jigsaw puzzle") / Game name settextstyle (30,0,T); setfillcolor (BROWN); setlinestyle (BS_SOLID, 5); setlinecolor (RED); fillroundrect (220,220,370,270,10,10); settextstyle (30,0,6T), 0,0,6, false, false, false) / start button outtextxy (270,230, L "start"); fillroundrect (220,300,370,350,10,10); outtextxy (240,310, L "rules of the game"); setfillcolor (BROWN); setlinestyle (BS_SOLID, 5); setlinecolor (BLACK); fillcircle (490,440,30); / / Music control button: turn on fillcircle (560,440,30) / / Music Control Button: off outtextxy (380,430, L "Music:"); setfillcolor (BLACK); POINT pts [] = {{481425}, {481455}, {507440}}; fillpolygon (pts, 3); fillrectangle (546,425,554,455); fillrectangle (566,425,574,455); rules ();} three. Data initialization / / Game initialization void init () {/ / load resource picture, 4 pictures 4 levels loadimage (& img [0], L "picture1.jpg", 600,600); loadimage (& img [1], L "picture2.jpg", 600,600); loadimage (& img [2], L "picture3.jpg", 600,600) Loadimage (& img [3], L "picture4.jpg", 600,600); / / set the last picture as a blank picture, as the target picture loadimage (& imgs [8], L "white.jpg", 200,200); / / set random seed srand ((unsigned) time (NULL));} four. Cover rule button / / cover rule function int rules () {ExMessage Mou; / / mouse message while (1) {Mou = getmessage (EM_MOUSE) Switch (Mou.message) / / A pair of mouse information to match {case WM_LBUTTONDOWN: / / press the left button if (Mou.x > = 220 & & Mou.x = 300 & & Mou.y = 220 & & Mou.x = 460 & & Mou.x = 410 & & Mou.y = 530 & & Mou.x = 410 & & Mou.y = 1) {/ / blank picture and blank left picture exchange map [aim _ r] [aim_c] = map [aim _ r] [aim_c-1] Map [aim _ r] [aim_c-1] = 8; break;} case 3: / / swap if (aim_c) to the right

< 2) { //空白图片和空白处右边的图片交换 map[aim_r][aim_c] = map[aim_r][aim_c + 1]; map[aim_r][aim_c + 1] = 8; break; } } }}六.绘图函数//绘图函数void DrawMap(){ FlushBatchDraw(); //开始渲染图片 for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { putimage(x * 200, y * 200, &imgs[map[y][x]]); } } EndBatchDraw();}七.背景音乐//背景音乐函数void BGM(){ //打开音乐,播放音乐 mciSendStringW(L"open ./Thrills.mp3 alias back", NULL, 0, NULL); mciSendStringW(_T("play back repeat"), 0, 0, 0);}八.数据更新//数据更新函数void play(){ int col, row; //鼠标点击的位置 ExMessage msg; //鼠标消息 msg = getmessage(EM_MOUSE|EM_KEY); //获取鼠标消息 switch (msg.message) //对鼠标消息进行匹配 { case WM_LBUTTONDOWN: //当鼠标消息是左键按下时 //获取鼠标按下所在列 col = msg.x / 200; if (msg.x == 600) col = 2; //获取鼠标按下所在行 row = msg.y / 200; if (msg.y == 600) row = 2; //得到目标所在行和列 for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (map[i][j] == 8) //空白处为交换目标 { aim_r = i; aim_c = j; } } } //判断鼠标点击位置和目标是否相邻,相邻交换数据 if (row == aim_r && col == aim_c + 1 || row == aim_r && col == aim_c - 1 || row == aim_r + 1 && col == aim_c || row == aim_r - 1 && col == aim_c) { //鼠标点击图片和空白目标图片交换 map[aim_r][aim_c] = map[row][col]; map[row][col] = 8; } DrawMap(); break; case WM_RBUTTONDOWN: //当鼠标消息是右键按下时 putimage(0, 0, &img[NUM]); //将关卡图片贴到窗口上 break; case WM_RBUTTONUP: //当鼠标消息是右键抬起时 DrawMap(); break; case WM_MBUTTONDOWN: NUM++; if (NUM == 4) NUM = 0; //返回第一张图 //重新开始游戏 GameInit(); //游戏初始化 DrawMap(); //渲染地图 break; case WM_KEYDOWN: if (msg.vkcode == VK_ESCAPE) //按Esc键返回封面 { start(); break; } } }九.通关判断//通关判断函数void Judge(){ //判断当前每张图片是否在对应位置 if (map[0][0] == 0 && map[0][1] == 1 && map[0][2] == 2 && map[1][0] == 3 && map[1][1] == 4 && map[1][2] == 5 && map[2][0] == 6 && map[2][1] ==7 && map[2][2] == 8 ) { //挑战成功之后将全图贴上 putimage(0, 0, &img[NUM++]); //四个关卡都胜利之后退出程序 if (NUM == 4) { MessageBox(GetHWnd(), L"挑战成功", L"Vectory", MB_OK); exit(0); return; } //每过一个关卡判断是否进入下一个关卡 if (MessageBox(GetHWnd(), L"是否进入下一关", L"Vectory", MB_YESNO) == IDYES) { //重新开始游戏 GameInit(); //游戏初始化 DrawMap(); //渲染地图 } //退出游戏 else exit(0); }}十.完整程序#include#include#include#include#include#include#pragma comment(lib,"Winmm.lib") using namespace std;constexpr auto N = 3;IMAGE img[4], imgs[9];int aim_c, aim_r;int map[3][3] = { 0 };int NUM = 0;//游戏规则,开始界面设计void start();//封面按钮int rules();//加载资源void init();//游戏数据初始化void GameInit();//游戏渲染void DrawMap();//播放音乐void BGM();//玩家操作void play();//判断输赢void Judge();int main(){ //设置窗口大小 initgraph(6 * 100, 6 * 100); //设置图片 start(); init(); GameInit(); DrawMap(); while (1) { play(); Judge(); } system("pause");//等待用户按键 closegraph(); return 0;}//开始界面void start(){ loadimage(NULL, L"cover.jpg"); setbkmode(TRANSPARENT); settextcolor(BLACK); settextstyle(60, 0, _T("楷体"),0,0,4,false,false,false); outtextxy(180, 120, L"拼图游戏"); //游戏名称 settextstyle(30, 0, _T("微软雅黑")); setfillcolor(BROWN); setlinestyle(BS_SOLID, 5); setlinecolor(RED); fillroundrect(220, 220, 370, 270, 10, 10); settextstyle(30, 0, _T("宋体"), 0, 0, 6, false, false, false); //开始按钮 outtextxy(270, 230, L"开始"); fillroundrect(220, 300, 370, 350, 10, 10); outtextxy(240, 310, L"游戏规则"); setfillcolor(BROWN); setlinestyle(BS_SOLID, 5); setlinecolor(BLACK); fillcircle(490, 440, 30); //音乐控制按钮:开 fillcircle(560, 440, 30); //音乐控制按钮:关 outtextxy(380, 430, L"音乐:"); setfillcolor(BLACK); POINT pts[] = { {481,425},{481,455},{507,440} }; fillpolygon(pts, 3); fillrectangle(546, 425, 554, 455); fillrectangle(566, 425, 574, 455); rules();}//游戏初始化void init(){ //加载资源图片,4张图4个关卡 loadimage(&img[0], L"picture1.jpg", 600, 600); loadimage(&img[1], L"picture2.jpg", 600, 600); loadimage(&img[2], L"picture3.jpg", 600, 600); loadimage(&img[3], L"picture4.jpg", 600, 600); //设置最后一张图片为空白图片,作为目标图片 loadimage(&imgs[8], L"white.jpg", 200, 200); //设置随机种子 srand((unsigned)time(NULL));}//封面选项函数int rules(){ ExMessage Mou; //鼠标消息 while (1) { Mou = getmessage(EM_MOUSE); switch (Mou.message) //对鼠标信息进行匹配 { case WM_LBUTTONDOWN: //按下左键 if (Mou.x >

= 220 & & Mou.x = 300 & & Mou.y = 220 & & Mou.x = 460 & & Mou.x = 410 & & Mou.y = 530 & & Mou.x = 410 & & Mou.y = 1) {/ / blank picture and the picture on the left side of the blank space Map [aim _ r] [aim_c] = Map [aim _ r] [aim_c-1] Map [aim _ r] [aim_c-1] = 8; break } case 3: / / swap if to the right (aim_c < 2) {/ / blank picture and picture exchange on the right side of the blank space [aim _ r] [ Aim_c] = map [aim _ r] [aim_c + 1] Map [aim _ r] [aim_c + 1] = 8; break;}} / / drawing function void DrawMap () {FlushBatchDraw (); / / start rendering for (int y = 0; y < N) ) {for (int x = 0; x < N; x +) {putimage (x * 200, y * 200, & imgs [map [y] [x]]);}} EndBatchDraw () } / / background music function void BGM () {/ / Open music and play music mciSendStringW (L "open. / Thrills.mp3 alias back", NULL, 0, NULL); mciSendStringW (_ T ("play back repeat"), 0,0,0);} / / data update function void play () {int col, row; / mouse click location ExMessage msg / / mouse message msg = getmessage (EM_MOUSE | EM_KEY); / / get mouse message switch (msg.message) / / match a pair of mouse messages {case WM_LBUTTONDOWN: / / when the mouse message is pressed with the left button / / get the column col = mouse / 200 where the mouse is pressed If (msg.x = = 600) col = 2; / / get the line where the mouse is pressed row = msg.y / 200; if (msg.y = = 600) row = 2 / / get the row and column for of the target (int I = 0; I < N; iTunes +) {for (int j = 0; j < N) {if (map [I] [j] = = 8) / / the blank space is the exchange target {aim_r = I; aim_c = j To determine whether the mouse click position is adjacent to the target. Adjacent exchange data if (row = = aim_r & & col = = aim_c + 1 | | row = = aim_r & & col = = aim_c-1 | | row = = aim_r + 1 & & col = = aim_c | | row = = aim_r-1 & & col = = aim_c) { / / Mouse click to exchange a picture with a blank target picture [aim _ r] [aim_c] = map [row] [col] Map [row] [col] = 8;} DrawMap (); break; case WM_RBUTTONDOWN: / / when the mouse message is right pressed, putimage (0,0, & IMG [num]); / / paste the level picture to the window break Case WM_RBUTTONUP: / / DrawMap () when the mouse message is raised with the right button; break; case WM_MBUTTONDOWN: NUM++; if (NUM = = 4) NUM = 0; / / return to the first picture / / restart the game GameInit () / / Game initialization DrawMap (); / render Map break; case WM_KEYDOWN: if (msg.vkcode = = VK_ESCAPE) / / Press the Esc key to return to the cover {start (); break } / / the customs clearance function void Judge () {/ / determines whether each picture is in the corresponding position if (map [0] [0] = = 0 & & map [0] [1] = = 1 & & map [0] [2] = = 2 & & map [1] [0] = = 3 & & map [1] [1] = = 4) & & map [1] [2] = = 5 & & map [2] [0] = 6 & & map [2] [1] = 7 & & map [2] [2] = 8) {/ / paste the whole picture with putimage (0) after a successful challenge 0, & IMG [NUM + +]) / / exit the program if (NUM = = 4) {MessageBox (GetHWnd (), L "Challenge success", L "Vectory", MB_OK); exit (0); return after winning all four levels } / / each pass to determine whether to enter the next level if (MessageBox (GetHWnd (), L "whether to enter the next level", L "Vectory", MB_YESNO) = = IDYES) {/ / restart the game GameInit () / / initialize the game DrawMap (); / render the map} / / exit the game else exit (0);}} above is all the contents of the article "how to use C++ language to implement jigsaw puzzles". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report