如何停止角色在控制台中移动
How do I stop characters moving in the console?
很抱歉标题令人困惑,我对C++相当陌生,正在尝试在命令行中制作迷宫游戏。它基本上工作正常,但是每当我移动我的角色(或将其他任何内容写到该行(时,它都会移动其他任何写回的内容,这使得解决迷宫变得非常困难。
我一直在调整本教程中的代码 https://www.youtube.com/watch?v=W1e5wO7XR2w 它似乎没有这个问题(尽管我确定我正确遵循了它(。我正在使用Visual Studio 2017中的C++编写
#include <iostream>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;
bool gameover, win; //Condition for gameover or a win
const int width = 20; //Maze Width
const int height = 20; //Maze Height
int x, y, endX, endY; //Coordinates of diffrent objects
bool mazeWallX[20] = { false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}; //Layers of walls will build up the maze, the bool statments state where there are and arent any walls.
bool mazeWallY[20] = { true, true, true, true, true, true, false, false, true, true, true, true, true, true, true, true, true, true, false, false };
bool mazeWallX2[20] = { false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false };
bool mazeWallY2[20] = { true, true, true, true, true, true, false, false, true, true, true, true, true, true, true, false, false, true, true, true };
bool mazeWallX3[20] = { false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false };
bool mazeWallY3[20] = { false, false, true, true, true, true, true, true, true, true, false, false, false, true, true, true, true, true, true, true };
bool mazeWallX4[20] = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false };
bool mazeWallY4[20] = { true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false };
bool mazeWallX5[20] = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false };
bool mazeWallY5[20] = { true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false };
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
void Setup() //Occurs at start of game, setting initial conditions.
{
gameover = false;
win = false;
dir = STOP;
x = 1;
y = 1;
endX = 18;
endY = 18;
}
void Draw() //Draws the player and walls on board, in command line.
{
system("cls");
for (int i = 0; i < width+2; i++) //Draws top boarder
cout << "#";
cout << endl;
for (int i = 0; i < height; i++) //Draws vertical walls, exit and player
{
for (int j = 0; j < width; j++)
{
if (j == 0 || (mazeWallX[j]&&mazeWallY[i]) || (mazeWallX2[j] && mazeWallY2[i]) || (mazeWallX3[j] && mazeWallY3[i]) || (mazeWallX4[j] && mazeWallY4[i]) || (mazeWallX5[j] && mazeWallY5[i])) //Will draw a wall as indicated by the
cout << "#";
if (i == y && j == x) //Draws player
cout << "O";
else if (i == endY && j == endX) //Draws exit
cout << "E";
cout << " ";
if (j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width+2; i++) //Bottom wall
cout << "#";
cout << endl;
}
void Input() //Handles Controls
{
if (_kbhit())
{
switch (_getch()) //Gets character key pressed.
{
case 'w':
dir = UP;
break;
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 's':
dir = DOWN;
break;
}
}
}
void Logic()
{
switch (dir)
{
case UP:
y--;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x > width || x<0 || y>height || y < 0) //Specifies conditions for gameover.
{
gameover = true;
}
if (x == endX && y == endY || (x == endX+1 && y == endY) || (x == endX && y == endY+1) || (x == endX - 1 && y == endY) || (x == endX && y == endY-1))
{
win = true;
}
}
int main()
{
Setup();
while (!gameover && !win) //While the game hasn't been won or lost, it will keep on redrawing the map to the command line interface.
{
Draw();
Input();
Logic();
_sleep(200); //This tells the system to wait before looping again, this reduces flickering, slows the character down and makes the game a little easier to control.
}
if (win == true)
{
cout << "Congratulations, you've reached the end!" << endl;
}
return 0;
}
墙壁(#(应该保持固定...但他们没有。
在 for 循环中给出屏幕端点的条件,就像在图形中一样,我们可以给 getmaxx(( 自动检测 x 像这样坐标屏幕的最大长度,您可以添加头文件
图形.h ##header 文件
然后使用 getmaxx((检测 x,然后使用 getmaxy(( 检测 y,如果它不起作用,您可以使用,然后评论我,我会用我自己的.
好的,我想我已经想通了
void Draw() //Draws the player and walls on board, in command line.
{
system("cls");
for (int i = 0; i < width+2; i++) //Draws top boarder
cout << "#";
cout << endl;
for (int i = 0; i < height; i++) //Draws vertical walls, exit and player
{
for (int j = 0; j < width; j++)
{
if (j == 0 || (mazeWallX[j]&&mazeWallY[i]) || (mazeWallX2[j] && mazeWallY2[i]) || (mazeWallX3[j] && mazeWallY3[i]) || (mazeWallX4[j] && mazeWallY4[i]) || (mazeWallX5[j] && mazeWallY5[i])) //Will draw a wall as indicated by the
cout << "#";
else if (i == y && j == x) //Draws player
cout << "O";
else if (i == endY && j == endX) //Draws exit
cout << "E";
else
cout << " ";
if (j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width+2; i++) //Bottom wall
cout << "#";
cout << endl;
}
我在 cout 前面添加了 else <<" ";并更改了//将退出绘制为 else if,以便每行写入正确的字符数。谢谢@1201ProgramAlarm的提醒。
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- C++:将控制台输出存储在宏中更好吗
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- MSVC将仅移动结构参数解释为指针
- C++控制台应用程序阻止退出
- 如何修复此iFile以将.txt输出到控制台
- 为什么控制台要求输入,即使代码中没有输入
- 自定义先决条件对移动分配运算符有效吗
- Win32 API 控制台光标在 WriteConsole 后不移动
- 有没有办法像动画一样移动控制台窗口?
- 在控制台C 中移动字母
- 将函数从控制台应用程序移动到共享库项目似乎会带来不相关的编译错误
- 在控制台C++中移动图形
- 将光标移动到控制台的左下角
- 在控制台应用程序和ssh中的鼠标移动