您的位置:首页 > 编程语言 > C语言/C++

C语言推箱子游戏实现代码

2019-01-18 00:02 1331 查看

推箱子游戏的运行规则:在街道上上小人推动箱子移动,直到把箱子移动到目的地。

思路分析:

小人及箱子的移动就是小人或者箱子和路的交换;

1 定义二维字符数组,存储地图

2 显示地图,提示游戏玩法

3 记录小人及箱子位置,并定义字符变量接收用户输入方向

4 循环判断语句

   1).小人的下一步是否为路,如果为路,则移动并记录小人新位置信息

   2).小人的下一步如果不是路,在判断是否为箱子,如果是箱子,在判断箱子的下一个位置是否是路,如果是路,则移动箱子和小人

   3). 刷新地图

   4) .判断箱子的位置,如果在指定位置,则游戏结束;

下面是实现代码:

#include <stdio.h>
//交换字符数组元素
void swapPosition(char ch[][11],int oldX,int oldY,int newX,int newY)
{
char temp;
temp = *(*(ch + oldX) + oldY);
*(*(ch + oldX) + oldY) = *(*(ch + newX) + newY);
*(*(ch + newX) + newY) = temp;

}
//打印数组
void printArray(char (*ch)[11])
{
for(int i = 0;i < 10;i++)
{
for(int j = 0;j < 10;j++)
{
printf("%c\t",*(*(ch + i) + j));
}
printf("\n");
}
}
int main(int argc, char* argv[])
{
//定义数组
char ch[10][11] = {
{'#','#','#','#','#','#','#','#','#','#','\0'},
{'#','#','#',' ',' ','#','#','#','#','#','\0'},
{'#','0','X',' ',' ','#','#','#','#','#','\0'},
{'#','#','#','#',' ','#','#','#','#','#','\0'},
{'#','#','#',' ',' ',' ',' ','#','#','#','\0'},
{'#','#','#',' ',' ',' ',' ',' ','#','#','\0'},
{'#','#','#','#','#',' ',' ',' ','#','#','\0'},
{'#','#','#','#','#',' ',' ',' ',' ',' ','\0'},
{'#','#','#','#','#','#','#','#',' ',' ','\0'},
{'#','#','#','#','#','#','#','#','#','#','\0'}
};
//打印数组
printArray(ch);
//记录小人及箱子位置
//定义路的标志变量street
int personX = 2,personY = 1,boxX = 2, boxY = 2;
char street = ' ';
//提示用户输入移动方向
printf("请输入小人移动方向:(w-上,s-下,a-左,d-右)\n");
//定义记录用户输入的方向
char direction;
//根据方向定义循环语句
while(1)
{
scanf("%c",&direction);
getchar();//接收键盘回车符
switch(direction)
{
case 'w':
case 'W':
if(*(*(ch+personX-1)+personY) == street)
{
swapPosition(ch,personX,personY,personX - 1,personY );
personX--;

}
else if(*(*(ch+personX-1)+personY) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX-1)+boxY) == street)
{
swapPosition(ch,boxX,boxY,boxX - 1,boxY);
boxX--;
swapPosition(ch,personX,personY,personX - 1,personY);
personX--;
}
}

break;
case 's':
case 'S':
if(*(*(ch+personX + 1) + personY) == street)
{
swapPosition(ch,personX,personY,personX + 1,personY );
personX++;

}
else if(*(*(ch+personX+1)+personY) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX+1)+boxY) == street)
{
swapPosition(ch,boxX,boxY,boxX + 1,boxY);
boxX++;
swapPosition(ch,personX,personY,personX + 1,personY);
personX++;
}
}
break;
case 'a':
case 'A':
if(*(*(ch+personX)+personY - 1) == street)
{
swapPosition(ch,personX,personY,personX ,personY - 1 );
personY--;

}
else if(*(*(ch+personX)+personY - 1) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX)+boxY - 1) == street)
{
swapPosition(ch,boxX,boxY,boxX,boxY - 1);
boxY--;
swapPosition(ch,personX,personY,personX,personY - 1);
personY--;
}
}
break;
case 'd':
case 'D':

if(*(*(ch+personX)+personY + 1) == street)
{
swapPosition(ch,personX,personY,personX ,personY + 1 );
personY++;

}
else if(*(*(ch+personX)+personY + 1) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX)+boxY + 1) == street)
{
swapPosition(ch,boxX,boxY,boxX,boxY + 1);
boxY++;
swapPosition(ch,personX,personY,personX,personY + 1);
personY++;
}
}
break;
case 'q':
case 'Q':
return 0;

}
printArray(ch);
//定义结束标志
if (boxY == 9) {
printf("恭喜你,游戏过关!\n");
return 0;
}
}

return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: