您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用C語言寫推箱子游戲”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
#include <stdio.h>#include <stdlib.h>#include<string.h>#include <conio.h>//行和列 #define ROW 10#define COL 11/* run this program using the console pauser or add your own getch, system("pause") or input loop *//*****///地圖char map[ROW][COL] = { "##########",//0 "### ##",//1 "### ##",//2 "##AX # ##",//3 "### ## ",//4 "##### #",//5 "## #",//6 "# ####",//7 "### ",//8 "##########" //9 //A:人 , X:箱子 } ;//打印地圖 void showMap();//接收小人的方向char enterDirection(); //小人向上移動(dòng)的方法void moveToUp(); //小人向下移動(dòng)的方法void moveToDown(); //小人向右移動(dòng)的方法void moveToRight(); //小人向左移動(dòng)的方法void moveToLeft(); //當(dāng)前小人的坐標(biāo)int currentPersonRow = 3;int currentPersonCol = 2;//當(dāng)前箱子的坐標(biāo) int currentBoxRow = 3;int currentBoxCol = 3; int main(int argc, char *argv[]) { //system("clear"); printf("點(diǎn)擊回車鍵開始游戲 ^_^\n\n"); //1代表運(yùn)行 0停止 int flag = 1; while(flag==1){ //顯示地圖 showMap(); //接收小人的方向 char dir = enterDirection(); switch(dir){ //小人向上移動(dòng) case 'w': case 'W': moveToUp(); break; //小人向下移動(dòng) case 's': case 'S': moveToDown(); break; //小人向右移動(dòng) case 'd': case 'D': moveToRight(); break; //小人向左移動(dòng) case 'a': case 'A': moveToLeft(); break; //停止運(yùn)行 case 'q': case 'Q': printf("你的智商真低!T_T\n"); flag = 0; break; } showMap(); if(currentBoxRow==8&¤tBoxCol==9){ printf("你的智商真高^_^!!!"); flag = 0; } } }/*方法的實(shí)現(xiàn) */ //打印地圖 void showMap(){ int i; for(i = 0;i < ROW; i++){ printf("%s\n",map[i]); } printf("\n\n\n\n\n"); printf("W:上,S:下, A:左, D:右。Q:退出"); printf("\n\n\n\n\n");}//接收小人的方向char enterDirection(){ //清除SCANF中的緩沖區(qū) rewind(stdin); char dir; dir = getch(); //scanf("%c",&dir); return dir;}//小人向上移動(dòng)的方法void moveToUp(){ //小人的下一個(gè)坐標(biāo) int nextPersonCol = currentPersonCol; int nextPersonRow = currentPersonRow - 1; //箱子的下一個(gè)坐標(biāo) int nextBoxRow = currentBoxRow - 1; int nextBoxCol = currentBoxCol; //如果小人的下一個(gè)坐標(biāo)是路 if(map[nextPersonRow][nextPersonCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; } //如果小人的下一個(gè)坐標(biāo)是墻 if(map[nextPersonRow][nextPersonCol]=='#'){ //什么也不做 } //如果小人的下一個(gè)坐標(biāo)是箱子 if(map[nextPersonRow][nextPersonCol]=='X'){ if(map[nextBoxRow][nextBoxCol] == ' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; map[nextBoxRow][nextBoxCol] = 'X'; map[currentBoxRow][currentBoxCol] = 'A'; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; currentBoxRow = nextBoxRow; currentBoxCol = nextBoxCol; } }}//小人向下移動(dòng)的方法void moveToDown(){ //小人的下一個(gè)坐標(biāo) int nextPersonCol = currentPersonCol; int nextPersonRow = currentPersonRow + 1; //箱子的下一個(gè)坐標(biāo) int nextBoxRow = currentBoxRow + 1; int nextBoxCol = currentBoxCol; //如果小人的下一個(gè)坐標(biāo)是路 if(map[nextPersonRow][nextPersonCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; } //如果小人的下一個(gè)坐標(biāo)是墻 if(map[nextPersonRow][nextPersonCol]=='#'){ //什么也不做 } //如果小人的下一個(gè)坐標(biāo)是箱子 if(map[nextPersonRow][nextPersonCol]=='X'){ if(map[nextBoxRow][nextBoxCol] == ' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; map[nextBoxRow][nextBoxCol] = 'X'; map[currentBoxRow][currentBoxCol] = 'A'; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; currentBoxRow = nextBoxRow; currentBoxCol = nextBoxCol; } }} //小人向右移動(dòng)的方法void moveToRight(){ //小人的下一個(gè)坐標(biāo) int nextPersonCol = currentPersonCol + 1; int nextPersonRow = currentPersonRow; //箱子的下一個(gè)坐標(biāo) int nextBoxRow = currentBoxRow; int nextBoxCol = currentBoxCol + 1; //如果小人的下一個(gè)坐標(biāo)是路 if(map[nextPersonRow][nextPersonCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; } //如果小人的下一個(gè)坐標(biāo)是墻 if(map[nextPersonRow][nextPersonCol]=='#'){ //什么也不做 } //如果小人的下一個(gè)坐標(biāo)是箱子 if(map[nextPersonRow][nextPersonCol]=='X'){ if(map[nextBoxRow][nextBoxCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; map[nextBoxRow][nextBoxCol] = 'X'; map[currentBoxRow][currentBoxCol] = 'A'; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; currentBoxRow = nextBoxRow; currentBoxCol = nextBoxCol; } }}//小人向左移動(dòng)的方法void moveToLeft(){ //小人的下一個(gè)坐標(biāo) int nextPersonCol = currentPersonCol - 1; int nextPersonRow = currentPersonRow; //箱子的下一個(gè)坐標(biāo) int nextBoxRow = currentBoxRow; int nextBoxCol = currentBoxCol - 1; //如果小人的下一個(gè)坐標(biāo)是路 if(map[nextPersonRow][nextPersonCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; } //如果小人的下一個(gè)坐標(biāo)是墻 if(map[nextPersonRow][nextPersonCol]=='#'){ //什么也不做 } //如果小人的下一個(gè)坐標(biāo)是箱子 if(map[nextPersonRow][nextPersonCol]=='X'){ if(map[nextBoxRow][nextBoxCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; map[nextBoxRow][nextBoxCol] = 'X'; map[currentBoxRow][currentBoxCol] = 'A'; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; currentBoxRow = nextBoxRow; currentBoxCol = nextBoxCol; } }}
“怎么用C語言寫推箱子游戲”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。