欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用C語言編寫推箱子游戲

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

本文實例為大家分享了C語言推箱子游戲的具體實現(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();
 
//小人向上移動的方法
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;
 }
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++算法學習之分支限界法的應用

    C++算法學習之分支限界法的應用

    分支限界法常以廣度優(yōu)先或以最小耗費(最大效益)優(yōu)先的方式搜索問題的解空間樹。本文將詳細講解分支限界法的應用,需要的可以參考一下
    2022-05-05
  • C語言尋找無向圖兩點間的最短路徑

    C語言尋找無向圖兩點間的最短路徑

    這篇文章主要為大家詳細介紹了C語言尋找無向圖兩點間的最短路徑,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Matlab繪制散點密度圖的教程詳解

    Matlab繪制散點密度圖的教程詳解

    這篇文章主要介紹了如何使用MATLAB繪制散點密度圖(二維核密度),文中的示例代碼講解詳細,對我們學習Matlab有一定幫助,需要的可以參考一下
    2022-02-02
  • C語言改造通訊錄操作詳解

    C語言改造通訊錄操作詳解

    這篇文章主要介紹了C語言文件操作改造通訊錄方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-01-01
  • C++ deque與vector對比的優(yōu)缺點

    C++ deque與vector對比的優(yōu)缺點

    這篇文章主要介紹了C++中deque與vector相比的優(yōu)勢與劣勢,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-01-01
  • c++并查集優(yōu)化(基于size和rank)

    c++并查集優(yōu)化(基于size和rank)

    這篇文章主要介紹了c++并查集優(yōu)化(基于size和rank),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 超詳細解析C++實現(xiàn)歸并排序算法

    超詳細解析C++實現(xiàn)歸并排序算法

    歸并排序是比較穩(wěn)定的排序方法。它的基本思想是把待排序的元素分解成兩個規(guī)模大致相等的子序列。本文將用C++實現(xiàn)這一排序算法,需要的可以參考一下
    2022-09-09
  • C語言 strftime 格式化顯示日期時間的實現(xiàn)

    C語言 strftime 格式化顯示日期時間的實現(xiàn)

    下面小編就為大家?guī)硪黄狢語言 strftime 格式化顯示日期時間的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • C++簡明分析臨時對象是什么

    C++簡明分析臨時對象是什么

    對性能來說,許多的問題都需要和出現(xiàn)頻率及本身執(zhí)行一次的開銷掛鉤,有些問題雖然看似比較開銷較大,但是很少會執(zhí)行到,那也不會對程序有大的影響;同樣一個很小開銷的函數(shù)執(zhí)行很頻繁,同樣會對程序的執(zhí)行效率有很大影響。本章中作者主要根據(jù)臨時對象來闡述這樣一個觀點
    2022-04-04
  • C++ 動態(tài)數(shù)組模版類Vector實例詳解

    C++ 動態(tài)數(shù)組模版類Vector實例詳解

    這篇文章主要為大家詳細介紹了C++動態(tài)數(shù)組模版類Vector實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02

最新評論