這篇“C++基于EasyX庫如何實(shí)現(xiàn)拼圖小游戲”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C++基于EasyX庫如何實(shí)現(xiàn)拼圖小游戲”文章吧。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比修水網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式修水網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋修水地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
具體代碼如下:
#pragma once #include#include #include #include #include #include #include #include using namespace std; static const int MAX_MAP = 30; //定義最大行或者列分塊常量 int check[MAX_MAP][MAX_MAP]; //檢查數(shù)組 int map[MAX_MAP][MAX_MAP]; //序號儲存 int random[MAX_MAP * MAX_MAP]; //隨機(jī)化數(shù)組 IMAGE img_total; //原圖片 IMAGE img_blank; //白底 IMAGE img[MAX_MAP][MAX_MAP]; //儲存分塊圖片 int level = 3; //關(guān)卡難度 int width_temp = 0; //分塊寬度 int height_temp = 0; //分塊高度 int flagi = 0; //標(biāo)記塊行位置 int flagj = 0; //標(biāo)記塊列位置 int mousei = 0; //標(biāo)記鼠標(biāo)位置 int mousej = 0; //標(biāo)記鼠標(biāo)位置 int FLAG = 0; //勝利標(biāo)記 void Get_graphics(); //讀取圖片并預(yù)載到原圖中 void Set_graphics(); //設(shè)置好圖片位置及對應(yīng)關(guān)系 void Line_flush(); //畫線條分割圖片 void Rand_array(); //初始化隨機(jī)數(shù)組 void Get_mouse(); //獲取鼠標(biāo)操作 void Judge_graphics(); //判定是否通關(guān)并選擇是否下一關(guān) void Show_graphics(); //顯示分塊圖片 inline void Get_graphics() //讀取圖片并預(yù)載到原圖中 { loadimage(&img_total, L"1.png"); loadimage(&img_blank, L"0.png"); initgraph(img_total.getwidth(), img_total.getheight()); } inline void Set_graphics() //設(shè)置好圖片位置及對應(yīng)關(guān)系 { 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(); //校驗(yàn)數(shù)組初始化 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); //可以更改線條顏色 默認(rèn)白色 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() //初始化隨機(jī)數(shù)組 { for (int i = 0; i < level * level; i++) random[i] = i; random_device rd; mt19937 g(rd()); // 隨機(jī)數(shù)引擎 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]; //逆轉(zhuǎn)賦值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("過關(guān)了."), _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) //逆轉(zhuǎn)賦值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; }
以上就是關(guān)于“C++基于EasyX庫如何實(shí)現(xiàn)拼圖小游戲”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。