您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么用C語言實現(xiàn)游戲項目推箱子效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么用C語言實現(xiàn)游戲項目推箱子效果”吧!
首先是我們用二維數(shù)組特定的數(shù)字描繪出這個地圖
int cas = 0; int map[3][8][8] = { 1,1,1,1,1,1,1,1, 1,3,4,0,0,4,3,1, 1,0,1,3,0,1,0,1, 1,0,1,4,0,1,0,1, 1,0,0,5,0,0,0,1, 1,0,1,0,0,1,0,1, 1,3,4,0,0,4,3,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,3,4,0,0,4,3,1, 1,0,1,3,0,1,0,1, 1,0,1,4,0,1,0,1, 1,3,4,5,0,0,0,1, 1,0,1,0,0,1,0,1, 1,3,4,0,0,4,3,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,3,4,0,0,4,3,1, 1,0,1,3,0,1,0,1, 1,0,1,4,0,1,0,1, 1,3,4,5,0,4,3,1, 1,0,1,0,0,1,0,1, 1,3,4,0,0,4,3,1, 1,1,1,1,1,1,1,1 };
然后來繪制我們的推箱子地圖
void drawGraph() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { //算貼圖的坐標 int x = 50 * j; int y = 50 * i; switch (map[cas][i][j]) { case 0: //一個漢字符號占用兩個位置 //printf(" "); putimage(x, y, img + 0); break; case 1: putimage(x, y, img + 1); //printf("■"); break; case 3: putimage(x, y, img + 2); //printf("☆"); break; case 4: putimage(x, y, img + 3); //printf("★"); break; case 5: case 8: putimage(x, y, img + 4); //printf("人"); break; case 7: putimage(x, y, img + 5); //printf("●"); break; } } //printf("\n"); } }
之后就是我們的游戲函數(shù),怎樣去用什么按鍵去控制我們的角色
void keyDown() { int userKey = _getch(); //不可見輸入 //定位:找到人的位置 int i = 0; int j = 0; for (i = 1; i < 8; i++) { for (j = 1; j < 8; j++) { if (map[cas][i][j] == 5 || map[cas][i][j] == 8) { goto NEXT; } } } NEXT: //我們這個游戲用什么按鍵去玩 switch (userKey) { case 'W': case 'w': case 72: if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3) { map[cas][i][j] -= 5; map[cas][i - 1][j] += 5; } if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7) { if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3) { map[cas][i][j] -= 5; map[cas][i - 1][j] += 1; map[cas][i - 2][j] += 4; } } break; case 's': case 'S': case 80: if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3) { map[cas][i][j] -= 5; map[cas][i + 1][j] += 5; } if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7) { if (map[cas][i + 2][j] == 0 || map[cas][i +2][j] == 3) { map[cas][i][j] -= 5; map[cas][i + 1][j] += 1; map[cas][i + 2][j] += 4; } } break; case 'a': case 'A': case 75: if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3) { //a+=1 a=a+1 復(fù)合賦值運算符 map[cas][i][j] -= 5; map[cas][i][j - 1] += 5; } if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7) { if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3) { map[cas][i][j] -= 5; map[cas][i][j - 1] += 1; map[cas][i][j - 2] += 4; } } break; case 'd': case 'D': case 77: if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3) { map[cas][i][j] -= 5; map[cas][i][j + 1] += 5; } if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7) { if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3) { map[cas][i][j] -= 5; map[cas][i][j + 1] += 1; map[cas][i][j + 2] += 4; } } break; } }
再然后就是我們?nèi)绾稳ヅ袛嘤螒虻慕Y(jié)果
//勝負的判斷: int gameOver() { //地圖上沒有箱子就可以結(jié)束 for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (map[cas][i][j] == 4) { return 0; } } } return 1; }
最后運行我們的主函數(shù)就行啦
int main() { loadResource(); mciSendString("open 1.mp3", 0, 0, 0); mciSendString("play 1.mp3 repeat", 0, 0, 0); initgraph(50 * 8, 50 * 8); while (1) { drawGraph(); if (gameOver()) { cas++; //變換關(guān)卡 if(cas==3) break; } keyDown(); //system("cls"); } closegraph(); //printf("GameOver!\n"); return 0; }
到此,相信大家對“怎么用C語言實現(xiàn)游戲項目推箱子效果”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。