最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用C語言編寫推箱子游戲

 更新時(shí)間:2019年10月21日 14:50:45   作者:Pastthewind  
這篇文章主要為大家詳細(xì)介紹了用C語言編寫推箱子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言推箱子游戲的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下

#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;
 }
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 重學(xué)c/c++之?dāng)?shù)據(jù)存儲(chǔ)詳解(整數(shù)、浮點(diǎn)數(shù))

    重學(xué)c/c++之?dāng)?shù)據(jù)存儲(chǔ)詳解(整數(shù)、浮點(diǎn)數(shù))

    C語言給定了一些基本的數(shù)據(jù)類型,下面這篇文章主要給大家介紹了關(guān)于重學(xué)c/c++之?dāng)?shù)據(jù)存儲(chǔ)(整數(shù)、浮點(diǎn)數(shù))的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • linux下c語言中隱藏進(jìn)程命令行參數(shù)(例如輸入密碼等高危操作)

    linux下c語言中隱藏進(jìn)程命令行參數(shù)(例如輸入密碼等高危操作)

    啟動(dòng)程序很多時(shí)候用命令行參數(shù)可以很方便,做到簡(jiǎn)化一些配置,但是輸入用戶名密碼等操作,如果通過進(jìn)程查看工具直接看到密碼就太不安全了,這里就為大家分享一下方法
    2021-01-01
  • C++設(shè)計(jì)模式之訪問者模式

    C++設(shè)計(jì)模式之訪問者模式

    這篇文章主要介紹了C++設(shè)計(jì)模式之訪問者模式,本文講解了什么是訪問者模式、訪問者模式的UML類圖、訪問者模式的實(shí)現(xiàn)代碼等內(nèi)容,需要的朋友可以參考下
    2014-10-10
  • c++ 入門——淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)

    c++ 入門——淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)

    這篇文章主要介紹了c++ 淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)的相關(guān)資料,幫助大家入門c++ 編程,感興趣的朋友可以了解下
    2020-08-08
  • 總結(jié)C/C++面試中可能會(huì)碰到的字符串指針題

    總結(jié)C/C++面試中可能會(huì)碰到的字符串指針題

    C/C++是最能體現(xiàn)程序員能力的語言之一,其功能強(qiáng)大,在IT行業(yè)的各個(gè)方面都有大量的應(yīng)用。下面這篇文章主要介紹了總結(jié)了在C/C++面試中可能會(huì)碰到的字符串指針題,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • 詳解C++中的四種類型轉(zhuǎn)換運(yùn)算符

    詳解C++中的四種類型轉(zhuǎn)換運(yùn)算符

    隱式類型轉(zhuǎn)換是安全的,顯式類型轉(zhuǎn)換是有風(fēng)險(xiǎn)的,C語言之所以增加強(qiáng)制類型轉(zhuǎn)換的語法,就是為了強(qiáng)調(diào)風(fēng)險(xiǎn),讓程序員意識(shí)到自己在做什么,本文將給大家詳細(xì)介紹一下C++中的四種類型轉(zhuǎn)換運(yùn)算符,需要的朋友可以參考下
    2023-09-09
  • C語言基礎(chǔ)知識(shí)點(diǎn)指針的使用

    C語言基礎(chǔ)知識(shí)點(diǎn)指針的使用

    這篇文章主要介紹了C語言基礎(chǔ)知識(shí)點(diǎn)指針的使用,下面文章將讓我們掌握指針的概念和用法、指針與數(shù)組之間的關(guān)系、指針指向的指針、如何使用指針變量做函數(shù)參數(shù)等更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • 基于C語言實(shí)現(xiàn)掃雷游戲

    基于C語言實(shí)現(xiàn)掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了基于C語言實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • c++實(shí)現(xiàn)圖像像素計(jì)算的示例詳解

    c++實(shí)現(xiàn)圖像像素計(jì)算的示例詳解

    我們知道每張圖像都能夠用矩陣來表示,矩陣中每個(gè)元素的值表示了圖像中每個(gè)像素值,像素值的大小就對(duì)應(yīng)著圖像的亮暗,本文主要來和大家介紹一下C++進(jìn)行圖像像素計(jì)算的相關(guān)知識(shí),感興趣的可以了解下
    2023-12-12
  • C++?select模型簡(jiǎn)單聊天室的實(shí)現(xiàn)示例

    C++?select模型簡(jiǎn)單聊天室的實(shí)現(xiàn)示例

    本文主要介紹了C++?select模型簡(jiǎn)單聊天室的實(shí)現(xiàn)示例,使用CMake項(xiàng)目進(jìn)行開發(fā),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評(píng)論

阿拉善右旗| 合江县| 昭通市| 龙游县| 牡丹江市| 丹阳市| 休宁县| 呼伦贝尔市| 绥中县| 上饶市| 莎车县| 乐清市| 张家界市| 通榆县| 凌源市| 昔阳县| 乐亭县| 靖宇县| 永吉县| 元氏县| 泽普县| 兰州市| 高阳县| 霍邱县| 瓦房店市| 舟曲县| 镇巴县| 崇仁县| 吉木乃县| 海原县| 石阡县| 邮箱| 怀集县| 渑池县| 东辽县| 龙门县| 绥德县| 中西区| 安新县| 安徽省| 合山市|