c语言的推箱子

基础知识点储备

1.简单的C语言语法基础
2.数组与二维数组
3.指针
4.函数
5.全局变量与局部变量
6.动画的简单原理

项目分析

步骤
1.显示游戏的地图
2.输入小人的前进的方向
3.根据前进方向来移动小人

框架搭建

1.显示我们的游戏地图
(1)知道每一个格子的类型,所以要每一个格子都要存起来
(2)使用二维数组来存储
2.让用户输入小人的移动方向

功能实现

1.通过二维数组实现地图
#define ROWS 10
#define COLS 11

#include <stdio.h>
#include <stdlib.h>

//定义一个二维数组
char map[ROWS][COLS] = {
	"##########",
	"#  ####  #",
	"# X####  #",
	"# O      #",
	"######   #",
	"#  ####  #",
	"#        #",
	"#   ######",
	"#         ",
	"##########"
};

int main()
{

	system("pause");
	return 0;
}
显示地图用循环打印就ok了
#define ROWS 10
#define COLS 11

#include <stdio.h>
#include <stdlib.h>

//定义一个二维数组
char map[ROWS][COLS] = {
	"##########",
	"#  ####  #",
	"# X####  #",
	"# O      #",
	"######   #",
	"#  ####  #",
	"#        #",
	"#   ######",
	"#         ",
	"##########"
};

void showMap()
{
	for (int i = 0; i < ROWS; i++)
	{
		printf("%s\n", map[i]);
	}
}

int main()
{

	showMap();
	system("pause");
	return 0;
}
2.移动小人的打桩
#define ROWS 10
#define COLS 11

#include <stdio.h>
#include <stdlib.h>

//定义一个二维数组
char map[ROWS][COLS] = {
	"##########",
	"#  ####  #",
	"# X####  #",
	"# O      #",
	"######   #",
	"#  ####  #",
	"#        #",
	"#   ######",
	"#         ",
	"##########"
};

void showMap()
{
	for (int i = 0; i < ROWS; i++)
	{
		printf("%s\n", map[i]);
	}
}

//返回输入小人的方向
char enterDirection()
{
	printf("请输入小人的方向w,s,a,d,q退出游戏:\n");
	char dir = 'a';
	rewind(stdin);//清空缓存区的数据
	scanf("%c", &dir);
	return dir;
}

void moveLife()
{

}

void moveRight()
{

}

void moveUp()
{

}

void moveDown()
{

}

int main()
{

	showMap();
	//获得用户输入前进方向
	char dir = enterDirection();
	//判断用户输入的前进方向
	switch (dir)
	{
		case 'a':
		case 'A':
			//左
			moveLife();
			break;
		case 'w':
		case 'W':
			moveUp();
			//上
			break;
		case 's':
		case 'S':
			//右
			moveRight();
			break;
		case 'd':
		case 'D':
			//下
			moveDown();
			break;
		case 'q':
		case 'Q':
			//退出
			break;
	}
	system("pause");
	return 0;
}

最终代码
#define ROWS 10
#define COLS 11
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

//定义一个二维数组
char map[ROWS][COLS] = {
	"##########",
	"#  ####  #",
	"# X####  #",
	"#O       #",
	"######   #",
	"#  ####  #",
	"#        #",
	"#   ######",
	"#         ",
	"##########"
};

void showMap()
{
	for (int i = 0; i < ROWS; i++)
	{
		printf("%s\n", map[i]);
	}
}

//返回输入小人的方向
char enterDirection()
{
	printf("请输入小人的方向w,s,a,d,q退出游戏:\n");
	char dir = 'a';
	rewind(stdin);//清空缓存区的数据
	scanf("%c", &dir);
	return dir;
}

int currRow = 3;//当前坐标
int currCol = 1;

int currBoxRow = 2;//箱子的当前坐标
int currBoxCol = 2;

int endRow = 8;
int endCol = 10;

void moveLife()
{
	int nextRow = currRow;
	int nextCol = currCol-1;

	int boxRow = currBoxRow;
	int boxCol = currBoxCol - 1;

	if (map[nextRow][nextCol] == '#')
	{
		return;
	}
	if (map[nextRow][nextCol] == ' ')
	{
		map[nextRow][nextCol] = 'O';
		map[currRow][currCol] = ' ';
		//要复制给全局的坐标
		currRow = nextRow;
		currCol = nextCol;
	}
	if (map[nextRow][nextCol] == 'X')
	{
		if (map[boxRow][boxCol] == '#')
		{
			return;
		}
		else if (map[boxRow][boxCol] == ' ')
		{
			map[boxRow][boxCol] = 'X';
			map[currBoxRow][currBoxCol] = 'O';
			currBoxRow = boxRow;
			currBoxCol = boxCol;
			map[currRow][currCol] = ' ';
			currRow = nextRow;
			currCol = nextCol;
			//是否到重点
			if (boxRow == endRow&&boxCol == endCol)
			{
				printf("好了\n");
			}
		}
	}
}

void moveRight()
{
	int nextRow = currRow;
	int nextCol = currCol + 1;

	int boxRow = currBoxRow;
	int boxCol = currBoxCol + 1;

	if (map[nextRow][nextCol] == '#')
	{
		return;
	}
	if (map[nextRow][nextCol] == ' ')
	{
		map[nextRow][nextCol] = 'O';
		map[currRow][currCol] = ' ';
		//要复制给全局的坐标
		currRow = nextRow;
		currCol = nextCol;
	}
	if (map[nextRow][nextCol] == 'X')
	{
		if (map[boxRow][boxCol] == '#')
		{
			return;
		}
		else if (map[boxRow][boxCol] == ' ')
		{
			map[boxRow][boxCol] = 'X';
			map[currBoxRow][currBoxCol] = 'O';
			currBoxRow = boxRow;
			currBoxCol = boxCol;
			map[currRow][currCol] = ' ';
			currRow = nextRow;
			currCol = nextCol;
		}
		//是否到重点
		if (boxRow == endRow&&boxCol == endCol)
		{
			printf("好了\n");
		}
	}
}

void moveUp()
{
	//如何移动
	//判断小人是否能够向上移动
	//1.拿到小人的坐标
	//2.判断像上移动的坐标的类型
	//3.如果是#则不能移动,如果是‘’可以移动
	//4.如果是箱子是否可以推动
	int nextRow = currRow - 1;
	int nextCol = currCol;

	int boxRow = currBoxRow - 1;
	int boxCol = currBoxCol;

	if (map[nextRow][nextCol] == '#')
	{
		return;
	}
	if (map[nextRow][nextCol] == ' ')
	{
		map[nextRow][nextCol] = 'O';
		map[currRow][currCol] = ' ';
		//要复制给全局的坐标
		currRow = nextRow;
		currCol = nextCol;
	}
	if (map[nextRow][nextCol] == 'X')
	{
		if (map[currBoxRow][currBoxCol] == '#')
		{
			return;
		}
		else if (map[currBoxRow][currBoxCol] == ' ')
		{
			if (map[boxRow][boxCol] == '#')
			{
				return;
			}
			else if (map[boxRow][boxCol] == ' ')
			{
				map[boxRow][boxCol] = 'X';
				map[currBoxRow][currBoxCol] = 'O';
				currBoxRow = boxRow;
				currBoxCol = boxCol;
				map[currRow][currCol] = ' ';
				currRow = nextRow;
				currCol = nextCol;
			}

			//是否到重点
			if (boxRow == endRow&&boxCol == endCol)
			{
				printf("好了\n");
			}
		}
	}
}

void moveDown()
{
	int nextRow = currRow+1;
	int nextCol = currCol;

	int boxRow = currBoxRow + 1;
	int boxCol = currBoxCol;

	if (map[nextRow][nextCol] == '#')
	{
		return;
	}
	if (map[nextRow][nextCol] == ' ')
	{
		map[nextRow][nextCol] = 'O';
		map[currRow][currCol] = ' ';
		//要复制给全局的坐标
		currRow = nextRow;
		currCol = nextCol;
	}
	if (map[nextRow][nextCol] == 'X')
	{
		if (map[boxRow][boxCol] == '#')
		{
			return;
		}
		else if (map[boxRow][boxCol] == ' ')
		{
			map[boxRow][boxCol] = 'X';
			map[currBoxRow][currBoxCol] = 'O';
			currBoxRow = boxRow;
			currBoxCol = boxCol;
			map[currRow][currCol] = ' ';
			currRow = nextRow;
			currCol = nextCol;
		}
		//是否到重点
		if (boxRow == endRow&&boxCol == endCol)
		{
			printf("好了\n");
		}
	}
}

int main()
{
	
	while (1)
	{
		system("cls");
		showMap();
		//获得用户输入前进方向
		char dir = enterDirection();
		//判断用户输入的前进方向
		switch (dir)
		{
		case 'a':
		case 'A':
			//左
			moveLife();
			break;
		case 'w':
		case 'W':
			moveUp();
			//上
			break;
		case 's':
		case 'S':
			//右
			moveDown();
			break;
		case 'd':
		case 'D':
			//下
			moveRight();
			break;
		case 'q':
		case 'Q':
			//退出
			printf("您的智商有问题?");
			exit(0);
			break;
		}
	}
	
	
	system("pause");
	return 0;
}

由于这个代码是根据最直接的思路写的,所以没有进行分开的编写,只是给初学者一个思路。
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值