Извините за запутанный заголовок, я довольно новичок в 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;
}
Стены (#) должны оставаться неподвижными ... но это не так.