如何使用C语言编写推箱子游戏

这篇文章主要介绍了如何使用C语言编写推箱子游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

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

如何使用C语言编写推箱子游戏

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
//行和列
#defineROW10
#defineCOL11
/*runthisprogramusingtheconsolepauseroraddyourowngetch,system("pause")orinputloop*/
/**
*
*
*/
//地图
charmap[ROW][COL]={
"##########",//0
"#####",//1
"#####",//2
"##AX###",//3
"#####",//4
"######",//5
"###",//6
"#####",//7
"###",//8
"##########"//9
//A:人,X:箱子
};
//打印地图
voidshowMap();
//接收小人的方向
charenterDirection();

//小人向上移动的方法
voidmoveToUp();
//小人向下移动的方法
voidmoveToDown();
//小人向右移动的方法
voidmoveToRight();
//小人向左移动的方法
voidmoveToLeft();

//当前小人的坐标
intcurrentPersonRow=3;
intcurrentPersonCol=2;
//当前箱子的坐标
intcurrentBoxRow=3;
intcurrentBoxCol=3;



intmain(intargc,char*argv[]){
//system("clear");
printf("点击回车键开始游戏^_^\n\n");
//1代表运行0停止
intflag=1;
while(flag==1){
//显示地图
showMap();
//接收小人的方向
chardir=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;
}

}

}
/*
方法的实现
*/


//打印地图
voidshowMap(){
inti;
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");
}
//接收小人的方向
charenterDirection(){
//清除SCANF中的缓冲区
rewind(stdin);
chardir;
dir=getch();
//scanf("%c",&dir);
returndir;
}
//小人向上移动的方法
voidmoveToUp(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol;
intnextPersonRow=currentPersonRow-1;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow-1;
intnextBoxCol=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;
}
}
}
//小人向下移动的方法
voidmoveToDown(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol;
intnextPersonRow=currentPersonRow+1;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow+1;
intnextBoxCol=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;
}
}
}
//小人向右移动的方法
voidmoveToRight(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol+1;
intnextPersonRow=currentPersonRow;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow;
intnextBoxCol=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;
}
}
}
//小人向左移动的方法
voidmoveToLeft(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol-1;
intnextPersonRow=currentPersonRow;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow;
intnextBoxCol=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语言编写推箱子游戏”这篇文章对大家有帮助,同时也希望大家多多支持恰卡编程网,关注恰卡编程网行业资讯频道,更多相关知识等着你来学习!

发布于 2021-06-13 23:20:03
收藏
分享
海报
0 条评论
189
上一篇:Java中怎么使用变量与常量 下一篇:如何使用mybatis查询语句
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码