如何停止角色在控制台中移动

How do I stop characters moving in the console?

本文关键字:移动 控制台 何停止 角色      更新时间:2023-10-16

很抱歉标题令人困惑,我对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的提醒。