本篇內(nèi)容介紹了“C語(yǔ)言如何實(shí)現(xiàn)拼圖游戲”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、平谷ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的平谷網(wǎng)站制作公司制作一款拼圖小游戲
#include <graphics.h> // 引用圖形庫(kù)頭文件 #include <conio.h> #include<stdio.h> typedef enum { UP, DOWN, LEFT, RIGHT, UNKOWN } DRCT; const int level = 5; DRCT getKey() { char key; DRCT ret; key = _getch(); switch (key) { case 'a': case 'A': ret = LEFT; break; case 's': case 'S': ret = DOWN; break; case 'd': case 'D': ret = RIGHT; break; case 'w': case 'W': ret = UP; break; default: ret = UNKOWN; break; } return ret; } void drawImage(int layout[level][level], IMAGE* pimg) { int w, h; w = h = pimg->getwidth() / level; int x1, y1, x2, y2; setcolor(RGB(255, 255, 255)); setfillcolor(RGB(0, 0, 0)); for (int i = 0; i < level; ++i) { for (int j = 0; j < level; ++j) { x1 = j * w; y1 = i * h; if (layout[i][j] != -1) { x2 = layout[i][j] % level * w; y2 = layout[i][j] / level * h; putimage(x1, y1, w, h, pimg, x2, y2); } else { fillrectangle(x1, y1, x1 + w, y1 + h); } } } for (int i = 1; i < level; ++i) { line(0, i*h, pimg->getwidth(), i*h); } for (int i = 1; i < level; ++i) { line(i*w, 0, i*w, pimg->getheight()); } } int isGameOver(int layout[level][level]) { for (int i = 0; i < level; i++) { for (int j = 0; j < level; j++) { if (!(i == level - 1 && j == level - 1)) { if (layout[i][j] != i*level + j) { return 0; } } } } return 1; } int updateLayout(int layout[level][level], DRCT d) { int x = 0, t = 0; int row, col, i, j, a, b; for (i = 0; i < level; i++)//先找黑框坐標(biāo) i,j { for (j = 0; j < level; j++) { if (layout[i][j] == -1) { a = i; b = j; } } } switch (d) { case UP: { row = a + 1; col = b; break; } case DOWN: { row = a - 1; col = b; break; } case LEFT: { row = a; col = b + 1; break; } case RIGHT: { row = a; col = b - 1; break; } default: {row = a, col = b; break; } } //根據(jù)d的值算出與(i,j)交換的方塊的坐標(biāo)(row,col) if (row<0 || row>level - 1 || col<0 || col>level - 1) { x = 0; } else { x = 1; t = layout[a][b]; layout[a][b] = layout[row][col]; layout[row][col] = t; } //坐標(biāo)(row,col)合法:交換 并且ret=1 // 否則: ret=0 return x; } int main() { int layout[level][level] = { 0,1,2,3,4, 5,6,7,8,9, 10,11,12,13,14, 15,16,17,18,19, 20,21,-1,22,23 }; initgraph(850, 500); // 創(chuàng)建繪圖窗口,大小為 640x480 像素 IMAGE img,imgTip; loadimage(&img, _T("./image/0.jpg")); //圖片為自己定義的路徑 loadimage(&imgTip, _T("./image/0.jpg"),200,200); drawImage(layout, &img); putimage(650, 0, &imgTip); while (!isGameOver(layout)) { DRCT d = getKey(); //根據(jù)d的值修改layout數(shù)組中的數(shù)據(jù) if (updateLayout(layout, d)) drawImage(layout, &img); } putimage(0, 0, &img); outtextxy(100, 50, _T("牛逼")); _getch(); closegraph(); // 關(guān)閉繪圖窗口 }
“C語(yǔ)言如何實(shí)現(xiàn)拼圖游戲”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前名稱:C語(yǔ)言如何實(shí)現(xiàn)拼圖游戲-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://jinyejixie.com/article42/diesec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站改版、用戶體驗(yàn)、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容