SoFunction
Updated on 2025-03-04

C++ implements puzzle game based on EasyX library

The puzzle game made with the EasyX library of C++ is for your reference. The specific content is as follows

I will record the first project I made and there is also some improvement space QWQ, which can support difficulty upgrades, but the judgment of passing the level seems to be a little problem. It is definitely not that I can't pass the level.

#pragma once
#include <iostream>
#include <>
#include <>
#include <algorithm>
#include <>
#include <cstdlib>
#include <random>
#include <cmath>
using namespace std;

static const int MAX_MAP = 30;     //Define the maximum row or column chunking constantint check[MAX_MAP][MAX_MAP];     //Check the arrayint map[MAX_MAP][MAX_MAP];      //Store number storageint random[MAX_MAP * MAX_MAP];     //Randomized arrayIMAGE img_total;        //Original pictureIMAGE img_blank;        //White backgroundIMAGE img[MAX_MAP][MAX_MAP];     //Save chunked pictures
int level = 3;         //Level difficultyint width_temp = 0;        //Block widthint height_temp = 0;       //Block heightint flagi = 0;         // Mark the block row positionint flagj = 0;         //Tag block column positionint mousei = 0;         //Tag the mouse positionint mousej = 0;         //Tag the mouse positionint FLAG = 0;         //Victory Mark
void Get_graphics();       //Read the picture and preload it to the original picturevoid Set_graphics();       //Set the picture location and correspondencevoid Line_flush();        //Draw lines to divide picturesvoid Rand_array();        //Initialize the random arrayvoid Get_mouse();        //Get the mouse operationvoid Judge_graphics();       //Determine whether to pass the level and select whether to next levelvoid Show_graphics();       //Show chunked pictures
inline void Get_graphics()      //Read the picture and preload it to the original picture{
 loadimage(&img_total, L"");
 loadimage(&img_blank, L"");
 initgraph(img_total.getwidth(), img_total.getheight());
}

inline void Set_graphics()      //Set the picture location and correspondence{
 width_temp = img_total.getwidth() / level;
 height_temp = img_total.getheight() / level;
 //Load the pictures of each block SetWorkingImage(&img_total);
 for (int i = 0; i < level; i++)
 {
  for (int j = 0; j < level; j++)
   getimage(&img[i][j], i * width_temp, j * height_temp, width_temp, height_temp);
 }
 SetWorkingImage();
 //Check array initialization int cnt = 0;
 for (int i = 0; i < level; i++)
 {
  for (int j = 0; j < level; j++)
  {
   check[i][j] = cnt;
   cnt++;
  }
 }
}

inline void Line_flush()        //Draw lines to divide pictures{
 for (int i = 0; i < level; i++)
 {
  //setlinecolor(RED); //The line color can be changed. Default white  line(i * width_temp, 0, i * width_temp, img_total.getheight());
  line(0, i * height_temp, img_total.getwidth(), i * height_temp);
 }
}

inline void Rand_array()        //Initialize the random array{
 for (int i = 0; i < level * level; i++)
  random[i] = i;

 random_device rd;
 mt19937 g(rd());         // Random number engine shuffle(random, random + level * level, g);   // Chaotic order
 int cnt = 0;
 for (int i = 0; i < level; i++)
 {
  for (int j = 0; j < level; j++)
  {
   map[j][i] = random[cnt];     //Reversal assignment 1   cnt++;
  }
 }
}

void Get_mouse()
{
 MOUSEMSG msg = GetMouseMsg();
 if ( == WM_LBUTTONDOWN)
 {
  mousei =  / width_temp;
  mousej =  / height_temp;
  if ((mousei + 1 == flagi && mousej == flagj) ||
   (mousei == flagi && mousej + 1 == flagj) ||
   (mousei - 1 == flagi && mousej == flagj) ||
   (mousei == flagi && mousej - 1 == flagj))
  {
   //Swap pictures into pieces   swap(map[mousej][mousei], map[flagj][flagi]);
  }
 }
}

void Judge_graphics()
{
 int cnt = 0;
 for (int i = 0; i < level; i++)
 {
  for (int j = 0; j < level; j++)
  {
   if (map[i][j] == check[i][j])
    cnt++;
  }
 }
 if (cnt == level * level)
 {
  MessageBox(GetHWnd(), _T("Passed."), _T("Message prompt."), MB_OK);
  FLAG = 1;
  exit(0);
 }
}

inline void Show_graphics()         //Show chunked pictures{
 for (int i = 0; i < level; i++)
 {
  for (int j = 0; j < level; j++)
  {
   if (map[j][i] == level * level - 1)    //Reversal assignment 2   {
    flagi = i;
    flagj = j;
    putimage(i * width_temp, j * height_temp, &img_blank);
   }
   else
   {
    int countj = map[j][i] % level;
    int counti = map[j][i] / level;
    putimage(i * width_temp, j * height_temp, &img[countj][counti]);
   }
  }
 }
 Line_flush();
}

int main()
{
 Get_graphics();
 Set_graphics();
 Rand_array();
 Show_graphics();

 while (1)
 {
  BeginBatchDraw();   //Double buffering prevents flickering  Get_mouse();
  Show_graphics();
  EndBatchDraw();    //Double buffering prevents flickering  Judge_graphics(); 
 }

 if (FLAG)
 {
  putimage(0, 0, &img_total);
  FLAG = 0;
 }

 system("pause");
 return 0;
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.