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

用C語言編寫推箱子游戲

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

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

#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();
 
//小人向上移動的方法
void moveToUp(); 
//小人向下移動的方法
void moveToDown(); 
//小人向右移動的方法
void moveToRight(); 
//小人向左移動的方法
void moveToLeft(); 
 
//當前小人的坐標
int currentPersonRow = 3;
int currentPersonCol = 2;
//當前箱子的坐標 
int currentBoxRow = 3;
int currentBoxCol = 3;
 
 
 
int main(int argc, char *argv[]) {
 //system("clear");
 printf("點擊回車鍵開始游戲 ^_^\n\n");
 //1代表運行 0停止 
 int flag = 1;
 while(flag==1){
 //顯示地圖 
 showMap();
 //接收小人的方向
 char dir = enterDirection();
 switch(dir){
  //小人向上移動 
  case 'w':
  case 'W':
   moveToUp();
  break;
  
  //小人向下移動 
  case 's':
  case 'S':
   moveToDown();
  break;
  //小人向右移動 
  case 'd':
  case 'D':
   moveToRight();
  break;
  //小人向左移動 
  case 'a':
  case 'A':
   moveToLeft();
  break;
  //停止運行 
  case 'q':
  case 'Q':
   printf("你的智商真低!T_T\n");
   flag = 0;
  break;
 }
 showMap();
 if(currentBoxRow==8&¤tBoxCol==9){
  printf("你的智商真高^_^!!!");
  flag = 0; 
  }
 
}
 
}
/*
方法的實現(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;
}
//小人向上移動的方法
void moveToUp(){
 //小人的下一個坐標 
 int nextPersonCol = currentPersonCol;
 int nextPersonRow = currentPersonRow - 1;
 //箱子的下一個坐標
 int nextBoxRow = currentBoxRow - 1;
 int nextBoxCol = currentBoxCol; 
 
 //如果小人的下一個坐標是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一個坐標是墻 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一個坐標是箱子 
 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;
 }
 }
}
//小人向下移動的方法
void moveToDown(){
  //小人的下一個坐標 
 int nextPersonCol = currentPersonCol;
 int nextPersonRow = currentPersonRow + 1;
 //箱子的下一個坐標
 int nextBoxRow = currentBoxRow + 1;
 int nextBoxCol = currentBoxCol; 
 
 //如果小人的下一個坐標是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一個坐標是墻 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一個坐標是箱子 
 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;
 }
 }
} 
//小人向右移動的方法
void moveToRight(){
 //小人的下一個坐標 
 int nextPersonCol = currentPersonCol + 1;
 int nextPersonRow = currentPersonRow;
 //箱子的下一個坐標
 int nextBoxRow = currentBoxRow;
 int nextBoxCol = currentBoxCol + 1; 
 
 //如果小人的下一個坐標是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一個坐標是墻 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一個坐標是箱子 
 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;
 }
 }
}
//小人向左移動的方法
void moveToLeft(){
 //小人的下一個坐標 
 int nextPersonCol = currentPersonCol - 1;
 int nextPersonRow = currentPersonRow;
 //箱子的下一個坐標
 int nextBoxRow = currentBoxRow;
 int nextBoxCol = currentBoxCol - 1; 
 
 //如果小人的下一個坐標是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一個坐標是墻 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一個坐標是箱子 
 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/c++之數(shù)據存儲詳解(整數(shù)、浮點數(shù))

    重學c/c++之數(shù)據存儲詳解(整數(shù)、浮點數(shù))

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

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

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

    C++設計模式之訪問者模式

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

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

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

    總結C/C++面試中可能會碰到的字符串指針題

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

    詳解C++中的四種類型轉換運算符

    隱式類型轉換是安全的,顯式類型轉換是有風險的,C語言之所以增加強制類型轉換的語法,就是為了強調風險,讓程序員意識到自己在做什么,本文將給大家詳細介紹一下C++中的四種類型轉換運算符,需要的朋友可以參考下
    2023-09-09
  • C語言基礎知識點指針的使用

    C語言基礎知識點指針的使用

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

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

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

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

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

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

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

最新評論

二连浩特市| 伊宁市| 武夷山市| 清水河县| 寿阳县| 鸡东县| 井研县| 白水县| 蒲江县| 河源市| 淅川县| 遵化市| 沐川县| 沈阳市| 剑河县| 河池市| 涞源县| 五指山市| 吉林省| 观塘区| 寿阳县| 华坪县| 苍溪县| 正阳县| 庆城县| 句容市| 双桥区| 浮梁县| 洞头县| 湘阴县| 麦盖提县| 遂平县| 托克逊县| 通许县| 澄江县| 彭泽县| 德保县| 石嘴山市| 汤原县| 冷水江市| 越西县|