用C语言编写推箱子游戏

所属分类: 软件编程 / C 语言 阅读数: 95
收藏 0 赞 0 分享

本文实例为大家分享了C语言推箱子游戏的具体实现代码,供大家参考,具体内容如下

#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; 
  }
 
}
 
}
/*
方法的实现 
*/
 
 
//打印地图 
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中的缓冲区 
 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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多