如何停止字符在控制台中移动?

时间:2019-03-24 19:33:06

标签: c++ visual-studio visual-c++

抱歉,标题令人迷惑,我在C ++方面还很陌生,正在尝试在命令行中制作迷宫游戏。它通常可以正常工作,但是每当我移动角色(或在该行上写任何其他内容)时,它都会移动回写的其他内容,这使得解决迷宫变得相当困难。

我一直在改编本教程https://www.youtube.com/watch?v=W1e5wO7XR2w中的代码,尽管似乎没有问题,但似乎没有这个问题。 我正在将C ++与Visual Studio 2017一起使用

#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;
}

墙壁(#号)应该保持固定……但不能固定。

2 个答案:

答案 0 :(得分:0)

在for循环中,就像在图形中那样给出屏幕终点的条件,我们可以给getmaxx()自动检测x坐标的最大屏幕长度,这样可以添加头文件

graphics.h ##头文件

然后使用getmaxx()检测x,然后使用getmaxy()检测y,如果它不起作用,可以使用它,然后用我自己的方式评论我。

答案 1 :(得分:0)

好的,我想我已经解决了

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;并更改了// //将Exit绘制到,否则,则每行写入正确的字符数。感谢@ 1201ProgramAlarm的提醒。