C++ easyX 库函数
// picGame.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // easyX #include "pch.h" #include <iostream> #include <graphics.h> #include <random> #include <time.h> using namespace std; IMAGE image; IMAGE img[24]; int grid[24]; int r0, c0; void GameInit() { // 原图 loadimage(&image, L"test.jpg", 400, 600); SetWorkingImage(&image); for (int i = 0; i < 6; i++) for (int j = 0; j < 4; j++) getimage(&img[i * 4 + j], j * 100, i * 100, 100, 100); loadimage(&img[23], L"blank.jpg", 100, 100); SetWorkingImage(nullptr); } void GamePaint() { for (int i = 0; i < 6; i++) for (int j = 0; j < 4; j++) putimage(j * 100, i * 100, &img[grid[i * 4 + j]]); } void shuffle() { srand((unsigned)time(NULL)); for (int i = 0; i < 24; i++) grid[i] = i; int len = 24; for (int i = 0; i < 24; i++) { int ind = rand() % len; swap(grid[ind], grid[--len]); } for (int i = 0; i < 24; i++) if (grid[i] == 23) { r0 = i / 4, c0 = i % 4; break; } } void playGame() { MOUSEMSG msg = { 0 }; msg = GetMouseMsg(); int r, c; // 点击图片对应下标 switch (msg.uMsg) { case WM_LBUTTONDOWN: r = msg.y / 100; c = msg.x / 100; swap(grid[r * 4 + c], grid[r0 * 4 + c0]); r0 = r, c0 = c; break; } } int main() { initgraph(400, 600); shuffle(); GameInit(); while (1) { GamePaint(); playGame(); } getchar(); return 0; }