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

#include

#include

#include

#include

//行和列

#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语言推箱子游戏实习报告,用C语言编写推箱子游戏相关推荐

  1. 极客c语言课程设计,c语言课程设计之实习报告共5天完整.doc

    c语言课程设计之实习报告共5天完整 <C语言程序设计> 短学期实习报告 系名:计算机科学与技术系 专业:软件工程 年级:2015级 姓名: <C语言程序设计>短学期实习成绩评定 ...

  2. C语言程序设计有pjcj吗,C语言程序设计课程设计实习报告

    <C语言程序设计课程设计实习报告>由会员分享,可在线阅读,更多相关<C语言程序设计课程设计实习报告(22页珍藏版)>请在人人文库网上搜索. 1.长江大学C语言程序设计课程设计实 ...

  3. 成绩分析系统c语言,学生成绩分析及排名系统C语言程序设计课程设计实习报告...

    学生成绩分析及排名系统C语言程序设计课程设计实习报告 长江大学计算机上机实习报告题目学生成绩分析及排名系统姓名学院__专业班级学号指导教师20120222目录一设计目的1二课程设计摘要2三课程设计的任 ...

  4. c语言选择结构程序设计实验报告6,c语言-选择结构程序设计实验报告4.doc

    c语言-选择结构程序设计实验报告4.doc 实验报告 实验题目:选择结构程序设计 实验目的: 了解C语言表示逻辑量的方法(以0代表"假"以非0代表"真"). 学 ...

  5. 《c语言程序设计》实验(上机)报告,《c语言程序设计》上机实验报告要求

    <c语言程序设计>上机实验报告要求 1. 实验环境: 软件系统:使用的软件环境 硬件系统:机型说明 2. 实验目的:掌握如何编辑.编译.链接调试运行c程序 3. 实验内容: (1) 掌握顺 ...

  6. c语言数据结构设计纸牌游戏,求一用数据结构c++编写的纸牌游戏程序

    需求: 1.一副没有J.Q.K.A.大小王的扑克牌(40张牌),编号为1-40张牌.第一回合,从40张牌中给双方各随机抽取5张牌,抽出的牌在原数组里删除.第二回合,从剩下30张牌中给双方各随机抽取5张 ...

  7. java猜字母游戏_JAVA编程题-用java编写猜字母游戏

    原标题:JAVA编程题-用java编写猜字母游戏 问题: 猜字母游戏,其游戏规则为: 程序随机产生5个按照一定顺序排列的字符作为猜测的结果,由玩家来猜测此字符串.玩家可以猜测多次,每猜测一次,则由系统 ...

  8. c语言做一个小程序报告,《C语言程序设计实践》课程报告30个小程序组合成一个大程序.doc...

    北方民族大学 课 程 报 告 课 程 名 称: 程序设计实践 专 业 班 级 : 软件工程(2)班 学 生 姓 名 : 李思良 学 号 : 任 课 教 师 : 王晓锋 学 期 : 2015-2016学 ...

  9. c语言程序设计综合性设计实验报告,《C语言程序设计》-综合性实验实验报告(参考格式...

    <<C语言程序设计>-综合性实验实验报告(参考格式>由会员分享,可在线阅读,更多相关<<C语言程序设计>-综合性实验实验报告(参考格式(9页珍藏版)>请 ...

最新文章

  1. php session 二位数组
  2. 在Python中读取MATLAB的数据文件
  3. java继承类大全_Java 面向对象继承部分(示例代码)
  4. 字符串的UPDATE和REPLACE部分
  5. 因为在此系统上禁止运行脚本。有关详细信息_在弃用11年后微软终于允许IT管理员禁用IE中的JScript脚本引擎...
  6. 跟我一起玩Win32开发(20):浏览文件夹
  7. Springboot读取jar包中的MANIFEST.MF文件内容
  8. 计算与推断思维 十二、为什么均值重要
  9. python2版本选择_win下安装python2和python3双版本之全美教程
  10. 如何选举根端口,指定端口,阻塞端口
  11. Tensorflow——placeholder(矩阵运算小实例)
  12. sql插入时返回插入主键id(id位自动增长)
  13. 电子邮件地址中服务器怎么看,你如何检查电子邮件服务器(gmail)中的某个地址,并基于该地址运行一些东西?...
  14. 第四章 爬取西刺免费代理ip 并应用到scrapy
  15. npm install生成的package-lock.json文件有什么作用?
  16. 电脑连接wifi找不到服务器,为什么电脑连不上wifi显示没有有效的ip配置
  17. C++ vector 标准差
  18. Python:实现scoring评分算法(附完整源码)
  19. Linux 服务器上部署web程序
  20. BZOJ 4216 Pig 分块乱搞

热门文章

  1. 移动硬盘接口坏了怎么办解决教程
  2. 围剿苹果Siri:车载语音争夺移动终端
  3. C/C++二维数组总结
  4. 手机突然提示无服务,无法使用蜂窝移动 解决流程
  5. .csd文件怎么读?--CMU_MOSI_Opinion_Labels.csd
  6. iOS UILabel 单词自动换行
  7. llvm libLLVMCore源码分析 13 - Other Operators
  8. LANDESK操作系统部署时,怎么默认恢复到客户端C盘配置
  9. VBS电脑信息检测器
  10. 无限地图生成的制作,一直跑酷一直爽!