您好,登錄后才能下訂單哦!
這篇文章給大家介紹C語言怎么實(shí)現(xiàn)一個(gè)拼圖游戲,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
制作一款拼圖小游戲
#include <graphics.h> // 引用圖形庫頭文件 #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)閉繪圖窗口 }
關(guān)于C語言怎么實(shí)現(xiàn)一個(gè)拼圖游戲就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。