用C++的EasyX库做的拼图小游戏,供大家参考,具体内容如下
记录一下自己做的第一个项目,还有一些改进空间QWQ,可以支持难度升级,但是通关判断似乎有点小问题肯定不是我菜通不了关 。
#pragma once
#include <iostream>
#include <graphics.h>
#include <Windows.h>
#include <algorithm>
#include <easyx.h>
#include <cstdlib>
#include <random>
#include <cmath>
using namespace std;
static const int MAX_MAP = 30; //定义最大行或者列分块常量
int check[MAX_MAP][MAX_MAP]; //检查数组
int map[MAX_MAP][MAX_MAP]; //序号储存
int random[MAX_MAP * MAX_MAP]; //随机化数组
IMAGE img_total; //原图片
IMAGE img_blank; //白底
IMAGE img[MAX_MAP][MAX_MAP]; //储存分块图片
int level = 3; //关卡难度
int width_temp = 0; //分块宽度
int height_temp = 0; //分块高度
int flagi = 0; //标记块行位置
int flagj = 0; //标记块列位置
int mousei = 0; //标记鼠标位置
int mousej = 0; //标记鼠标位置
int FLAG = 0; //胜利标记
void Get_graphics(); //读取图片并预载到原图中
void Set_graphics(); //设置好图片位置及对应关系
void Line_flush(); //画线条分割图片
void Rand_array(); //初始化随机数组
void Get_mouse(); //获取鼠标操作
void Judge_graphics(); //判定是否通关并选择是否下一关
void Show_graphics(); //显示分块图片
inline void Get_graphics() //读取图片并预载到原图中
{
loadimage(&img_total, L"1.png");
loadimage(&img_blank, L"0.png");
initgraph(img_total.getwidth(), img_total.getheight());
}
inline void Set_graphics() //设置好图片位置及对应关系
{
width_temp = img_total.getwidth() / level;
height_temp = img_total.getheight() / level;
//载入各分块的图片
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();
//校验数组初始化
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() //画线条分割图片
{
for (int i = 0; i < level; i++)
{
//setlinecolor(RED); //可以更改线条颜色 默认白色
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() //初始化随机数组
{
for (int i = 0; i < level * level; i++)
random[i] = i;
random_device rd;
mt19937 g(rd()); // 随机数引擎
shuffle(random, random + level * level, g); // 打乱顺序
int cnt = 0;
for (int i = 0; i < level; i++)
{
for (int j = 0; j < level; j++)
{
map[j][i] = random[cnt]; //逆转赋值1
cnt++;
}
}
}
void Get_mouse()
{
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN)
{
mousei = msg.x / width_temp;
mousej = msg.y / height_temp;
if ((mousei + 1 == flagi && mousej == flagj) ||
(mousei == flagi && mousej + 1 == flagj) ||
(mousei - 1 == flagi && mousej == flagj) ||
(mousei == flagi && mousej - 1 == flagj))
{
//交换图片分块
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("过关了."), _T("消息提示."), MB_OK);
FLAG = 1;
exit(0);
}
}
inline void Show_graphics() //显示分块图片
{
for (int i = 0; i < level; i++)
{
for (int j = 0; j < level; j++)
{
if (map[j][i] == level * level - 1) //逆转赋值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(); //双缓冲防止闪烁
Get_mouse();
Show_graphics();
EndBatchDraw(); //双缓冲防止闪烁
Judge_graphics();
}
if (FLAG)
{
putimage(0, 0, &img_total);
FLAG = 0;
}
system("pause");
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。