5.10推箱子2.0

Posted galileo9527

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5.10推箱子2.0相关的知识,希望对你有一定的参考价值。

一 升级说明

修复第一版 bug 真正可运行的推箱子 增加重开系统;

增加 多关卡地图 复制到编译器 编译运行 即可开玩 能过第四关算我输;

二 代码

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include "Map.h"//地图关卡
#define WIDTH 8 //地图宽度
#define HEIGHT 8 //地图长度
//定义地图 后期会做多地图
/*地图表示
0 空地
1 墙
2 人
3 箱子
4 箱子的目的地
5 到达目的地的箱子
*/
//原地图 重开用的
int map0[WIDTH][HEIGHT] =

1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 3, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 2, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 4, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
;//地图初始第一关

int map[WIDTH][HEIGHT] = 0 ;

//人的位置
int x, y;
//箱子个数 成功的标志是箱子=0;关卡数mapn
int boxs = 0; int mapn = 1;
//数据初始化
void initDate();
//打印地图
void drawMap();
//向上移动
void moveUp();
//向左移动
void moveLeft();
//向下移动
void moveDown();
//向右移动
void moveRight();
//更换地图
void tmap();

int main()

void intmap();//初始第一关
t1:

char direction; //存储输入
initDate(); //数据初始化
//开始循环 每按一次按键都会刷新直至胜利
while (1)

system("cls");//清屏函数
drawMap();//画图
//彩蛋
if(mapn==4)

cout << "能过算我输" << endl;

//接受输入
direction = _getch();
//用switch选择语句判断输入
switch (direction)

case\'w\':
moveUp();//向上移动
break;
case \'a\':
moveLeft();
break;
case\'s\':
moveDown();
break;
case\'d\':
moveRight();
break;
case\'r\':
goto t1;


//箱子数量为0时胜利
if(!boxs)

break;


//跳出循环时恭喜胜利
system("cls");
cout << "恭喜你过关了"<<endl;
cout << "按z开始下一关" << endl;
cout << "按p后按任意键退出" << endl;
t2:
direction = _getch();
if (direction == \'z\')

tmap();
goto t1;

else if (direction == \'p\') return 0;
else goto t2;


void initDate()//数据初始化

int i, j;
boxs = 0;//箱子初始0
cout << "游戏加载中...";
//遍历地图数据
for (i = 0; i < HEIGHT; i++)

for (j = 0; j < WIDTH; j++)
//地图初始化
map[i][j] = map0[i][j];
//遍历到2时记录人的坐标
if (map[i][j] == 2)

x = j;
y = i;

//遍历到3时 箱子数目增加
if (map[i][j] == 3)

boxs++;





void drawMap()

int i, j;
for (i = 0; i < WIDTH; i++)

for (j = 0; j < HEIGHT; j++)

switch(map[i][j])

case 0:
cout << " ";
break;
case 1:
cout << "墙";
break;
case 2:
cout << "你";
break;
case 3:
cout << "箱";
break;
case 4:
cout << "标";
break;
case 5:
cout << "达";
break;


cout << endl;

cout << "按\'w\'向上走,a左,d右,s下,r重开" << endl;

void moveUp()

//定义人物上方的目标
int ux, uy;
//人在最上方不能上移
if (y == 0)

return;

ux = x;
uy = y - 1;
//上方为完成的箱子 或本身
if (map[uy][ux] == 5|| map[uy][ux] == 2)

return;

//上方为墙
if (map[uy][ux] == 1)

return;

//上方为空地
if (map[uy][ux] == 0)

map[y][x] = 0;
map[uy][ux] = 2;
y = uy;//更新人的坐标

//上方为箱子
if (map[uy][ux] == 3)

//箱子上方为墙 箱子 或完成的箱子
if (map[uy - 1][ux] == 1 || map[uy - 1][ux] == 3 || map[uy - 1][ux] == 5)

return;

//箱子上方为目的地
if (map[uy - 1][ux] == 4)

map[uy - 1][ux] = 5;
map[uy][ux] = 2;
map[y][x] = 0;
x = ux;
y = uy;
boxs--;

//箱子上方是空地
if (map[uy - 1][ux] == 0)

map[uy - 1][ux] = 3;
map[uy][ux] = 2;
map[y][x] = 0;
x = ux;
y = uy;

else return;


void moveLeft()

//定义人物左方的目标
int lx, ly;
//人在最左方不能左移
if (x == 0)

return;

lx = x - 1;
ly = y;
//左方为完成的箱子
if (map[ly][lx] == 5)

return;

//左方为墙
if (map[ly][lx] == 1)

return;

//左方为空地
if (map[ly][lx] == 0)

map[y][x] = 0;
map[ly][lx] = 2;
x = lx;//更新人的坐标

//左方为箱子
if (map[ly][lx] == 3)

//箱子左方为墙 箱子 或完成的箱子
if (map[ly][lx - 1] == 1 || map[ly][lx - 1] == 3 || map[ly][lx - 1] == 5)

return;

//箱子左方为目的地
if (map[ly][lx - 1] == 4)

map[ly][lx - 1] = 5;
map[ly][lx] = 2;
map[y][x] = 0;
x = lx;
y = ly;
boxs--;

//箱子左方是空地
if (map[ly][lx - 1] == 0)

map[ly][lx - 1] = 3;
map[ly][lx] = 2;
map[y][x] = 0;
x = lx;
y = ly;

else return;


void moveDown()

//定义人物下方的目标
int dx, dy;
//人在最下方不能下移
if (y == 7)

return;

dx = x;
dy = y + 1;
//下方为完成的箱子
if (map[dy][dx] == 5)

return;

//下方为墙
if (map[dy][dx] == 1)

return;

//下方为空地
if (map[dy][dx] == 0)

map[y][x] = 0;
map[dy][dx] = 2;
y = dy;//更新人的坐标

//下方为箱子
if (map[dy][dx] == 3)

//箱子下方为墙 箱子 或完成的箱子
if (map[dy + 1][dx] == 1 || map[dy + 1][dx] == 3 || map[dy + 1][dx] == 5)

return;

//箱子下方为目的地
if (map[dy + 1][dx] == 4)

map[dy + 1][dx] = 5;
map[dy][dx] = 2;
map[y][x] = 0;
x = dx;
y = dy;
boxs--;

//箱子下方是空地
if (map[dy + 1][dx] == 0)

map[dy + 1][dx] = 3;
map[dy][dx] = 2;
map[y][x] = 0;
x = dx;
y = dy;

else return;


void moveRight()

//定义人物右方的目标
int rx, ry;
//人在最右方不能右移
if (x == 7)

return;

rx = x + 1;
ry = y;
//右方为完成的箱子
if (map[ry][rx] == 5)

return;

//右方为墙
if (map[ry][rx] == 1)

return;

//右方为空地
if (map[ry][rx] == 0)

map[y][x] = 0;
map[ry][rx] = 2;
x = rx;//更新人的坐标

//右方为箱子
if (map[ry][rx] == 3)

//箱子右方为墙 箱子 或完成的箱子
if (map[ry][rx + 1] == 1 || map[ry][rx + 1] == 3 || map[ry][rx + 1] == 5)

return;

//箱子右方为目的地
if (map[ry][rx + 1] == 4)

map[ry][rx + 1] = 5;
map[ry][rx] = 2;
map[y][x] = 0;
x = rx;
y = ry;
boxs--;

//箱子右方是空地
if (map[ry][rx + 1] == 0)

map[ry][rx + 1] = 3;
map[ry][rx] = 2;
map[y][x] = 0;
x = rx;
y = ry;



void tmap()

if (mapn == 1)

for (int i = 0; i < HEIGHT; i++)

for (int j = 0; j < WIDTH; j++)

map0[i][j] = Map2[i][j];



if (mapn == 2)

for (int i = 0; i < HEIGHT; i++)

for (int j = 0; j < WIDTH; j++)

map0[i][j] = Map3[i][j];



if (mapn == 3)

for (int i = 0; i < HEIGHT; i++)

for (int j = 0; j < WIDTH; j++)

map0[i][j] = Map4[i][j];



mapn++;

#ifndef A
#define A


int Map1[8][8] =

1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 3, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 2, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 4, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
;
int Map2[8][8] =

1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 3, 0, 1, 0, 1,
1, 0, 3, 0, 0, 0, 0, 1,
1, 0, 0, 2, 0, 0, 0, 1,
1, 0, 1, 0, 0, 1, 0, 1,
1, 0, 0, 0, 0, 4, 4, 1,
1, 1, 1, 1, 1, 1, 1, 1
;
int Map3[8][8] =

1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 3, 3, 0, 0, 1,
1, 0, 1, 0, 0, 0, 0, 1,
1, 0, 1, 2, 0, 0, 0, 1,
1, 4, 1, 1, 0, 0, 0, 1,
1, 0, 4, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
;
int Map4[8][8] =

1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 3, 0, 1,
1, 0, 1, 2, 1, 4, 0, 1,
1, 0, 1, 1, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
;

#endif

三 游戏截图

 

 

推箱子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 推箱子
{
class Program
{
static void Main(string[] args)
{
//1、编地图

int[,] map = new int[10, 10]{
{1,1,1,1,1,1,1,1,1,1},
{1,4,0,0,0,1,0,0,0,1},
{1,0,0,0,0,1,0,0,0,1},
{1,0,0,2,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,4,0,0,2,0,0,0,0,1},
{1,0,0,0,0,1,0,3,0,1},
{1,0,0,2,0,1,0,0,0,1},
{1,4,0,0,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};

int x = 7;
int y = 6;

//2、设置地图中的内容
//空地=0, 墙=1,箱子=2,小人=3,目标点=4,完成点=5

while (true)
{
Console.Clear();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (map[i, j] == 0)
Console.Write(" ");
else if (map[i, j] == 1)
Console.Write("■");
else if (map[i, j] == 2)
Console.Write("□");
else if (map[i, j] == 3)
Console.Write("♀");
else if (map[i, j] == 4)
Console.Write("☆");
else if (map[i, j] == 5)
Console.Write("★");

}
Console.WriteLine();
}

//3、动起来

ConsoleKeyInfo key = Console.ReadKey();

//向上
if (key.Key.ToString() == "UpArrow")
{
if (map[y - 1, x] == 0)//如果下一步是空地
{
int z = map[y, x];
map[y, x] = map[y - 1, x];
map[y - 1, x] = z;
y--;
}
else if (map[y - 1, x] == 2)//小人的下一步是箱子
{
if (map[y - 2, x] != 1 && map[y - 2, x] != 4)
{
//先让箱子和下一步调换
int z = map[y - 2, x];
map[y - 2, x] = map[y - 1, x];
map[y - 1, x] = z;

//让小人和下一步调换
z = map[y, x];
map[y, x] = map[y - 1, x];
map[y - 1, x] = z;
y--;
}

if (map[y - 2, x] == 4)
{
//先让箱子和下一步调换
map[y - 2, x] = 5;

//让小人和下一步调换
map[y - 1, x] = 0;
int z = map[y, x];
map[y, x] = map[y - 1, x];
map[y - 1, x] = z;
y--;
}

}
}

//向下
if (key.Key.ToString() == "DownArrow")
{
if (map[y + 1, x] == 0)//如果下一步是空地
{
int z = map[y, x];
map[y, x] = map[y + 1, x];
map[y + 1, x] = z;
y++;
}
else if (map[y + 1, x] == 2)
{
if (map[y + 2, x] != 1 && map[y + 2, x] != 4)
{
//先让箱子和下一步调换
int z = map[y + 2, x];
map[y + 2, x] = map[y + 1, x];
map[y + 1, x] = z;

//让小人和下一步调换
z = map[y, x];
map[y, x] = map[y + 1, x];
map[y + 1, x] = z;
y++;
}

if (map[y + 2, x] == 4)
{
//先让箱子和下一步调换
map[y + 2, x] = 5;

//让小人和下一步调换
map[y + 1, x] = 0;
int z = map[y, x];
map[y, x] = map[y + 1, x];
map[y + 1, x] = z;
y++;
}

}


}

//向左
if (key.Key.ToString() == "LeftArrow")
{
if (map[y, x - 1] == 0)//如果下一步是空地
{
int z = map[y, x];
map[y, x] = map[y, x - 1];
map[y, x - 1] = z;
x--;
}
else if (map[y, x-1] == 2)
{
if (map[y, x-2] != 1 && map[y, x-2] != 4)
{
//先让箱子和下一步调换
int z = map[y, x-2];
map[y , x-2] = map[y , x-1];
map[y, x-1] = z;

//让小人和下一步调换
z = map[y, x];
map[y, x] = map[y, x-1];
map[y, x-1] = z;
x--;
}

if (map[y, x-2] == 4)
{
//先让箱子和下一步调换
map[y, x-2] = 5;

//让小人和下一步调换
map[y, x-1] = 0;
int z = map[y, x];
map[y, x] = map[y, x-1];
map[y, x-1] = z;
x--;
}

}
}

//向右
if (key.Key.ToString() == "RightArrow")
{
if (map[y, x + 1] == 1)
continue;

int z = map[y, x];
map[y, x] = map[y, x + 1];
map[y, x + 1] = z;
x++;
}


}


}
}
}

以上是关于5.10推箱子2.0的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring Boot 2.0 时将指标导出到文件

使用 Python (PyQT 5.10) 在 QT 5.10 中加载自己的字体

perl 5.8 和 5.10 之间的区别 [关闭]

5.10 属性的属性

5.10

每日总结 5.10