如何使用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语言编写推箱子游戏”这篇文章对大家有帮助,同时也希望大家多多支持恰卡编程网,关注恰卡编程网行业资讯频道,更多相关知识等着你来学习!
推荐阅读
-
windows安装touble c
近期有些网友想要了解windows?安装touble的相关情况,小编通过整理给您分享一下。为什么现在还需要TurboC?在当今V...
-
C/C++如何获取CAN信号
C/C++如何获取CAN信号本篇内容主要讲解“C/C++如何获取C...
-
C语言怎么通过二分查找实现猜数字游戏
C语言怎么通过二分查找实现猜数字游戏本文小编为大家详细介绍“C语言...
-
C语言数据结构中的线性表怎么使用
C语言数据结构中的线性表怎么使用这篇文章主要介绍“C语言数据结构中...
-
C语言的数据结构怎么理解
C语言的数据结构怎么理解这篇文章主要介绍了C语言的数据结构怎么理解...
-
C语言与C++中内存管理的方法
C语言与C++中内存管理的方法这篇文章主要介绍了C语言与C++中内...
-
C语言链式队列与循环队列怎么实现
C语言链式队列与循环队列怎么实现这篇文章主要介绍了C语言链式队列与...
-
C语言冒泡排序怎么实现
C语言冒泡排序怎么实现这篇文章主要介绍了C语言冒泡排序怎么实现的相...
-
C语言如何实现斐波那契数列
C语言如何实现斐波那契数列这篇文章主要介绍了C语言如何实现斐波那契...
-
C语言如何实现无符号数和有符号数间的运算
C语言如何实现无符号数和有符号数间的运算本篇内容主要讲解“C语言如...
