Update README and fixes

This commit is contained in:
2024-04-06 17:35:11 +08:00
parent b23a123012
commit 16c763732a
4 changed files with 229 additions and 235 deletions

View File

@ -6,3 +6,8 @@ C++ 控制台 迷宫 使用了自动生成迷宫算法,实现迷宫基本玩
所以该代码只能在windows下编译
程序参数可以在代码头部设置,例如迷宫大小和输出区域大小等
需要合适的字体才能正常显示
![游玩示例](play.git)

BIN
a.exe

Binary file not shown.

131
code.cpp
View File

@ -9,10 +9,10 @@
using namespace std;
// 显示区域的大小
const size_t sWidth = 30, sHeight = 24;
const size_t sWidth = 60, sHeight = 24;
// 迷宫的实际大小(必须是奇数!)
const size_t width = 9, height = 9;
const size_t width = 33, height = 15;
const char wall = 1;
const char path = 0;
const char target = 2;
@ -34,10 +34,7 @@ vector<pair<pair<int, int>, int> > walls;
2
*/
const int up = 0;
const int down = 1;
const int left = 2;
const int right = 3;
enum Dir { Up, Down, Left, Right };
// 四周一步
int xx[] = { 0, 0, -1, 1 };
@ -123,7 +120,7 @@ void showMap()
int dx = sWidth / 2 - playerx;
int dy = sHeight / 2 - playery;
// 移动光标到最开始的位置
gotoxy(1, 1);
gotoxy(0, 0);
// 遍历整个地图
for (int sy = 0; sy < sHeight; ++sy)
@ -171,38 +168,38 @@ void showMap()
continue;
// 到此截断,后面为单字符地图时用
int d = 0;
for (int i = 0; i < 4; ++i)
{
if (!isOutBounds(x + xx[i], y + yy[i]) && map[y + yy[i]][x + xx[i]] == wall)
d |= 1 << i;
// int d = 0;
// for (int i = 0; i < 4; ++i)
// {
// if (!isOutBounds(x + xx[i], y + yy[i]) && map[y + yy[i]][x + xx[i]] == wall)
// d |= 1 << i;
}
// }
const int up = 1;
const int down = 2;
const int left = 4;
const int right = 8;
char ch = 0;
switch (d)
{
case 0:
ch = '&';
break;
//case up:
//case down:
case up | down:
ch = '|';
break;
//case left:
//case right:
case left | right:
ch = '-';
break;
default:
ch = '+';
}
putchar(ch);
// const int up = 1;
// const int down = 2;
// const int left = 4;
// const int right = 8;
// char ch = 0;
// switch (d)
// {
// case 0:
// ch = '&';
// break;
// //case up:
// //case down:
// case up | down:
// ch = '|';
// break;
// //case left:
// //case right:
// case left | right:
// ch = '-';
// break;
// default:
// ch = '+';
// }
// putchar(ch);
}
cout << endl;
}
@ -210,21 +207,6 @@ void showMap()
//putchar('A');
}
/*
// 手动延时函数
void sleep(int n)
{
for (int i = 0; i < n*n*n*n; ++i);
}
*/
// 刷新地图
void refresh()
{
gotoxy(1, 1);
showMap();
}
// 生成地图算法,参数为起始点坐标
void generate(int cx, int cy)
{
@ -258,8 +240,6 @@ void generate(int cx, int cy)
map[wy][wx] = path;
map[wy + yy[d]][wx + xx[d]] = path;
pushAroundWall(wx + xx[d], wy + yy[d]);
//sleep(70);
//refresh();
}
// 打穿后,将该墙移除待凿列表
walls.erase(walls.begin() + index);
@ -273,22 +253,22 @@ void generate(int cx, int cy)
// 玩家移动,参数是移动的方向
void playerMove(int dir)
bool playerMove(Dir dir)
{
if (0 > dir || dir >= 4)
return;
return false;
int tx = playerx + xx[dir];
int ty = playery + yy[dir];
if (isOutBounds(tx, ty))
return;
return false;
if (map[ty][tx] == wall)
return;
return false;
if (map[ty][tx] == target)
{
system("cls");
cout << "游戏胜利!" << endl;
system("pause");
exit(0);
return true;
}
// 只有是路的时候才可以移动
if (map[ty][tx] == path) {
@ -297,47 +277,56 @@ void playerMove(int dir)
}
// 移动完后再次探索迷雾
explore(playerx, playery);
return false;
}
// 每一帧执行更新
void updata()
bool updata()
{
char ch = getch();
bool win = false;
switch (ch)
{
case 'w':
playerMove(up);
win = playerMove(Dir::Up);
break;
case 's':
playerMove(down);
win = playerMove(Dir::Down);
break;
case 'a':
playerMove(2);
win = playerMove(Dir::Left);
break;
case 'd':
playerMove(3);
win = playerMove(Dir::Right);
break;
case 'q':
exit(0);
break;
case 'r':
return false;
break;
}
return !win;
}
int main()
{
// 设置种子
srand((unsigned int)time(0));
do {
printf("WSADÒÆ¶¯£¬QÍ˳ö£¬RÖØ¿ª");
system("pause");
while (true) {
// 生成地图
generate(1, 1);
// 游戏循环
while (1)
{
do {
// 输出地图
showMap();
// 等待输入,更新
updata();
} while (updata());
}
} while (getch() != 0);
return 0;
}

BIN
play.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB