c语言推箱子练习人物移动
用C语言基础终端运行:
1.创建二维数组 表示地图画面
- 0:空地
- 1:墙
- 2:目的地
- 3:箱子
- 4:玩家
2.使用枚举类型定义:
- 地图上数字的名字
- 移动键盘上下左右的值
3.将数字替换成图案显示
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>
#include<conio.h>
int map[10][10] = {
0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,0,0,0,0,
0,0,0,1,2,1,0,0,0,0,
0,0,0,1,3,1,0,0,0,0,
1,1,1,1,0,1,1,1,1,1,
1,2,0,3,4,0,3,0,2,1,
1,1,1,1,1,1,3,1,1,1,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,2,1,0,0,
0,0,0,0,0,1,1,1,0,0
};
enum Map
{
space,wall,dest,box,player
}m;
enum Key {
up=72,down=80,left=75,right=77,esc=27
}k;
void draw() {
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
switch (map[i][j])
{
case space:printf(" ");
break;
case wall:printf("■");
break;
case dest:printf("☆");
break;
case box:printf("★");
break;
case player:printf("♀");
break;
}
}
printf("\n");
}
}
int move() {
//找到玩家位置
int x = 0, y = 0,n=0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
if (map[i][j] == player) {
x = i;
y = j;
}
if (map[i][j] == 2) {
n++;
}
}
if (n == 0)//查看箱子是否都到目的地了
return -1;
else
n = 0;
char key=_getch();//从键盘获取上下左右
switch (key)
{
case 'W':
case 'w':
case up://判断玩家上面是空地,还是箱子
if (map[x - 1][y] == space) {
map[x - 1][y] = player;
map[x][y] = space;
}
else if (map[x - 1][y] == box) {
if (map[x - 2][y] == space || map[x - 2][y] == dest) {
map[x][y] = space;
map[x - 2][y] = box;
map[x - 1][y] = player;
}
}
break;
case 'S':
case 's':
case down:
if (map[x + 1][y] == space) {
map[x + 1][y] = player;
map[x][y] = space;
}
else if (map[x + 1][y] == box) {
if (map[x + 2][y] == space || map[x + 2][y] == dest) {
map[x][y] = space;
map[x + 2][y] = box;
map[x + 1][y] = player;
}
}
break;
case 'A':
case 'a':
case left:
if (map[x][y - 1] == space) {
map[x][y - 1] = player;
map[x][y] = space;
}
else if (map[x][y - 1] == box) {
if (map[x][y - 2] == space || map[x][y - 2] == dest) {
map[x][y] = space;
map[x][y - 2] = box;
map[x][y - 1] = player;
}
}
break;
case 'D':
case 'd':
case right:
if (map[x][y + 1] == space) {
map[x][y + 1] = player;
map[x][y] = space;
}
else if (map[x][y + 1] == box) {
if (map[x][y + 2] == space || map[x][y + 2] == dest) {
map[x][y] = space;
map[x][y + 2] = box;
map[x][y + 1] = player;
}
}
break;
case esc:
return -2;
break;
}
system("cls");
draw();
move();
}
int main()
{
draw();
if (move() == -1) {
system("cls");
printf("\n\n\n\t太棒啦!^V^ !恭喜你成功啦!");
}
else {
system("cls");
printf("\n\n\n\t这么简单你都放弃了?");
}
return 0;
}
优化成关卡
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>
#include<conio.h>
int c = 0;
int map[3][10][10] = {
{
0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,0,0,0,0,
0,0,0,1,2,1,0,0,0,0,
0,0,0,1,3,1,0,0,0,0,
1,1,1,1,0,1,1,1,1,1,
1,2,0,3,4,0,3,0,2,1,
1,1,1,1,1,1,3,1,1,1,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,2,1,0,0,
0,0,0,0,0,1,1,1,0,0},
{
0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,1,0,0,
0,1,0,1,0,0,1,0,1,0,
1,0,0,0,1,1,0,0,0,1,
1,0,0,3,4,0,3,2,0,1,
0,1,0,3,0,0,3,0,1,0,
0,0,1,0,0,0,0,1,0,0,
0,0,0,1,0,0,1,0,0,0,
0,0,0,0,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0},
{
1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,1,0,0,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,0,3,0,4,0,3,0,1,
1,0,0,0,0,1,0,3,0,1,
1,0,3,0,0,1,0,2,0,1,
1,0,1,1,1,1,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1}
};
enum Map
{
space,wall,dest,box,player
}m;
enum Key {
up=72,down=80,left=75,right=77,esc=27
}k;
void draw(){
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
switch (map[c][i][j])
{
case space:printf(" ");
break;
case wall:printf("■");
break;
case dest:printf("☆");
break;
case box:printf("★");
break;
case player:printf("♀");
break;
}
}
printf("\n");
}
}
int move() {
//找到玩家位置
int x = 0, y = 0,n=0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
if (map[c][i][j] == player) {
x = i;
y = j;
}
if (map[c][i][j] == 2) {
n++;
}
}
if (n == 0)//查看箱子是否都到目的地了
{
system("cls");
printf("\n\n\n\t太棒啦!^V^ !恭喜你成功啦!\n\n\n");
system("pause");
c++;
if (c == 3) {
system("cls");
printf("\n\n\n\t 恭喜你呀!全部通关!\n\n\n");
return -3;
}
}
else {
n = 0;
}
char key=_getch();//从键盘获取上下左右
switch (key)
{
case 'W':
case 'w':
case up://判断玩家上面是空地,还是箱子
if (map[c][x - 1][y] == space) {
map[c][x - 1][y] = player;
map[c][x][y] = space;
}
else if (map[c][x - 1][y] == box) {
if (map[c][x - 2][y] == space || map[c][x - 2][y] == dest) {
map[c][x][y] = space;
map[c][x - 2][y] = box;
map[c][x - 1][y] = player;
}
}
break;
case 'S':
case 's':
case down:
if (map[c][x + 1][y] == space) {
map[c][x + 1][y] = player;
map[c][x][y] = space;
}
else if (map[c][x + 1][y] == box) {
if (map[c][x + 2][y] == space || map[c][x + 2][y] == dest) {
map[c][x][y] = space;
map[c][x + 2][y] = box;
map[c][x + 1][y] = player;
}
}
break;
case 'A':
case 'a':
case left:
if (map[c][x][y - 1] == space) {
map[c][x][y - 1] = player;
map[c][x][y] = space;
}
else if (map[c][x][y - 1] == box) {
if (map[c][x][y - 2] == space || map[c][x][y - 2] == dest) {
map[c][x][y] = space;
map[c][x][y - 2] = box;
map[c][x][y - 1] = player;
}
}
break;
case 'D':
case 'd':
case right:
if (map[c][x][y + 1] == space) {
map[c][x][y + 1] = player;
map[c][x][y] = space;
}
else if (map[c][x][y + 1] == box) {
if (map[c][x][y + 2] == space || map[c][x][y + 2] == dest) {
map[c][x][y] = space;
map[c][x][y + 2] = box;
map[c][x][y + 1] = player;
}
}
break;
case esc:
system("cls");
printf("\n\n\n\t这么简单你都放弃了?\n\n\n");
system("pause");
return -2;
break;
}
system("cls");
draw();
move();
}
int main()
{
draw();
while (1) {
int a=move();
if (a ==-3||a==-2)return -1;
}
return 0;
}