基础知识点储备
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;
}
由于这个代码是根据最直接的思路写的,所以没有进行分开的编写,只是给初学者一个思路。