推箱子游戲的運(yùn)行規(guī)則:在街道上上小人推動(dòng)箱子移動(dòng),直到把箱子移動(dòng)到目的地。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了古雷港免費(fèi)建站歡迎大家使用!
思路分析:
小人及箱子的移動(dòng)就是小人或者箱子和路的交換;
1 定義二維字符數(shù)組,存儲(chǔ)地圖
2 顯示地圖,提示游戲玩法
3 記錄小人及箱子位置,并定義字符變量接收用戶輸入方向
4 循環(huán)判斷語(yǔ)句
1).小人的下一步是否為路,如果為路,則移動(dòng)并記錄小人新位置信息
2).小人的下一步如果不是路,在判斷是否為箱子,如果是箱子,在判斷箱子的下一個(gè)位置是否是路,如果是路,則移動(dòng)箱子和小人
3). 刷新地圖
4) .判斷箱子的位置,如果在指定位置,則游戲結(jié)束;
下面是實(shí)現(xiàn)代碼:
#include <stdio.h> //交換字符數(shù)組元素 void swapPosition(char ch[][11],int oldX,int oldY,int newX,int newY) { char temp; temp = *(*(ch + oldX) + oldY); *(*(ch + oldX) + oldY) = *(*(ch + newX) + newY); *(*(ch + newX) + newY) = temp; } //打印數(shù)組 void printArray(char (*ch)[11]) { for(int i = 0;i < 10;i++) { for(int j = 0;j < 10;j++) { printf("%c\t",*(*(ch + i) + j)); } printf("\n"); } } int main(int argc, char* argv[]) { //定義數(shù)組 char ch[10][11] = { {'#','#','#','#','#','#','#','#','#','#','\0'}, {'#','#','#',' ',' ','#','#','#','#','#','\0'}, {'#','0','X',' ',' ','#','#','#','#','#','\0'}, {'#','#','#','#',' ','#','#','#','#','#','\0'}, {'#','#','#',' ',' ',' ',' ','#','#','#','\0'}, {'#','#','#',' ',' ',' ',' ',' ','#','#','\0'}, {'#','#','#','#','#',' ',' ',' ','#','#','\0'}, {'#','#','#','#','#',' ',' ',' ',' ',' ','\0'}, {'#','#','#','#','#','#','#','#',' ',' ','\0'}, {'#','#','#','#','#','#','#','#','#','#','\0'} }; //打印數(shù)組 printArray(ch); //記錄小人及箱子位置 //定義路的標(biāo)志變量street int personX = 2,personY = 1,boxX = 2, boxY = 2; char street = ' '; //提示用戶輸入移動(dòng)方向 printf("請(qǐng)輸入小人移動(dòng)方向:(w-上,s-下,a-左,d-右)\n"); //定義記錄用戶輸入的方向 char direction; //根據(jù)方向定義循環(huán)語(yǔ)句 while(1) { scanf("%c",&direction); getchar();//接收鍵盤(pán)回車符 switch(direction) { case 'w': case 'W': if(*(*(ch+personX-1)+personY) == street) { swapPosition(ch,personX,personY,personX - 1,personY ); personX--; } else if(*(*(ch+personX-1)+personY) == *(*(ch+boxX)+boxY)) { if(*(*(ch+boxX-1)+boxY) == street) { swapPosition(ch,boxX,boxY,boxX - 1,boxY); boxX--; swapPosition(ch,personX,personY,personX - 1,personY); personX--; } } break; case 's': case 'S': if(*(*(ch+personX + 1) + personY) == street) { swapPosition(ch,personX,personY,personX + 1,personY ); personX++; } else if(*(*(ch+personX+1)+personY) == *(*(ch+boxX)+boxY)) { if(*(*(ch+boxX+1)+boxY) == street) { swapPosition(ch,boxX,boxY,boxX + 1,boxY); boxX++; swapPosition(ch,personX,personY,personX + 1,personY); personX++; } } break; case 'a': case 'A': if(*(*(ch+personX)+personY - 1) == street) { swapPosition(ch,personX,personY,personX ,personY - 1 ); personY--; } else if(*(*(ch+personX)+personY - 1) == *(*(ch+boxX)+boxY)) { if(*(*(ch+boxX)+boxY - 1) == street) { swapPosition(ch,boxX,boxY,boxX,boxY - 1); boxY--; swapPosition(ch,personX,personY,personX,personY - 1); personY--; } } break; case 'd': case 'D': if(*(*(ch+personX)+personY + 1) == street) { swapPosition(ch,personX,personY,personX ,personY + 1 ); personY++; } else if(*(*(ch+personX)+personY + 1) == *(*(ch+boxX)+boxY)) { if(*(*(ch+boxX)+boxY + 1) == street) { swapPosition(ch,boxX,boxY,boxX,boxY + 1); boxY++; swapPosition(ch,personX,personY,personX,personY + 1); personY++; } } break; case 'q': case 'Q': return 0; } printArray(ch); //定義結(jié)束標(biāo)志 if (boxY == 9) { printf("恭喜你,游戲過(guò)關(guān)!\n"); return 0; } } return 0; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:C語(yǔ)言推箱子游戲?qū)崿F(xiàn)代碼
當(dāng)前URL:http://muchs.cn/article6/picoog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿(mào)建站、微信小程序、虛擬主機(jī)、建站公司、電子商務(wù)
聲明:本網(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)