当前位置:首页 > 通信资讯 > 正文

用C++的EasyX库做的拼图小游戏,供大家参考,具体内容如下  

记录一下自己做的第一个项目,还有一些改进空间QWQ,可以支持难度升级,但是通关判断似乎有点小问题肯定不是我菜通不了关 。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 #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; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_30203239/article/details/118960831

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。