如何在c++; 所以我试着在C++中做一个迷宫游戏。下面的代码是我到目前为止所拥有的,但我希望让游戏中的角色能够使用箭头键移动

如何在c++; 所以我试着在C++中做一个迷宫游戏。下面的代码是我到目前为止所拥有的,但我希望让游戏中的角色能够使用箭头键移动,c++,visual-studio,maze,C++,Visual Studio,Maze,我应该使用getch()命令,ReadInputConsole()命令吗?我做了很多研究,但都不清楚 任何帮助都将不胜感激 #include "stdafx.h" #include <iostream> #include <windows.h> #include <fstream> #include <conio.h> #include <stdio.h> using namespace std; void maze1(); voi

我应该使用getch()命令,ReadInputConsole()命令吗?我做了很多研究,但都不清楚

任何帮助都将不胜感激

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <stdio.h>

using namespace std;

void maze1();
void maze2();
void welcome();

char ch;

int main()
{
    int enter;

   welcome();

    cin >> enter;

    if (enter == 1)
    {
      system("CLS"); // clear screen
      maze1();
    } 

   system("pause");
    return 0;

}

void welcome()
{
   cout << " Welcome to my Maze Game!\n\n";
    cout << "In order to win this game, you must make your\n";
    cout << " way through the maze and find the O. \n";
    cout << " Try to get the best time you can to be number 1!\n\n";
    cout << " Warning! Don't touch the walls or you will \n";
    cout << " lose points!\n ";
    cout << " Goodluck and have fun!\n\n\n\n";
    cout << " Press 1 to begin";


} 

void maze1 ()
{
    int begin;

    do // do-while loop starts
    {

    cin >> begin; // take in input

        if(begin == 1) // temporary option to check for next screen
            {

                ifstream fin;
                fin.open("lvl1.txt");

                char ch;

                while (!fin.eof())
                 {
                   fin.get(ch);
                   cout << ch;
                 }

                  fin.close();
             }
          else
            {
                cout << "Sorry, you entered the wrong key.";
             }

        }
    while( begin !=1); // condition of do-while loop

}

void maze2 ()
{
  ifstream fin;
    fin.open("lvl2.txt");

        char ch;

    while (!fin.eof())
    {
      fin.get(ch);
      cout << ch;
    }

    fin.close();
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void maze1();
void maze2();
无效欢迎();
char ch;
int main()
{
输入;
欢迎();
cin>>进入;
如果(输入==1)
{
系统(“CLS”);//清除屏幕
maze1();
} 
系统(“暂停”);
返回0;
}
欢迎光临
{

cout像一些流行的运动游戏那样使用W、A、S、D键怎么样?就像你在帖子中所说的,你可以使用

std::cin >> key;
看看有没有WASD的钥匙

您的程序流程(实际上是玩迷宫)似乎不完整。此外,我看不到您的迷宫在内存中的实际表示方式。我可以建议一个合理的算法来帮助您:

load the maze from a file and store the data in a 2-dimensional grid

while playing the maze is in progress
    display the maze
    wait for user input
    attempt the move based on the input
        handle out of bounds case
        handle colliding into a wall case
        handle reaching the goal case
        handle updating grid to the next position
这是一个实现上述算法的完整程序。这是使用C++11编译的。我不是windows开发人员,但我认为最近的VC++可以支持所有包含的功能

#include <algorithm>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>

class Maze {
public:
  void load(std::string&& mazeFilePath);

  void run();

private:
  using Grid = std::vector<std::string>;

  enum class MoveDirection {
    UP,
    DOWN,
    RIGHT,
    LEFT
  };

  enum class MoveResult {
    OK,
    OUT_OF_BOUNDS,
    COLLISION,
    GOAL
  };

  struct Position {
    int row;
    int col;
  };

  void display() const;

  MoveResult movePlayer(MoveDirection direction);

  Grid m_grid;
  Position m_playerPosition;
  Position m_goalPosition;

};

void Maze::load(std::string&& mazeFilePath) {
  m_grid.clear();

  std::ifstream mazeFileStream(mazeFilePath); //todo - readonly flag

  int currentRow = 0;

  for (std::string line; std::getline(mazeFileStream, line);) {
    int currentCol = 0;

    std::string row;

    std::copy_if(std::begin(line), std::end(line), std::back_inserter(row), [&](decltype(row)::value_type c) {
      switch (c) {
        case 'i':
          m_playerPosition.row = currentRow;
          m_playerPosition.col = currentCol;
          break;
        case 'g':
          m_goalPosition.row = currentRow;
          m_goalPosition.col = currentCol;
          break;
        default:
          break;
      }

      ++currentCol;

      return true;
    });

    m_grid.emplace_back(std::move(row));

    ++currentRow;
  }
}

void Maze::display() const {
  std::copy(std::begin(m_grid), std::end(m_grid), std::ostream_iterator<std::string>(std::cout, "\n"));
}

void Maze::run() {
  bool running = true;

  char key;

  while (running) {
    display();

    MoveResult moveResult;

    std::cin >> key;

    switch (key) {
      case 'w':
        moveResult = movePlayer(MoveDirection::UP);
        break;
      case 'a':
        moveResult = movePlayer(MoveDirection::LEFT);
        break;
      case 's':
        moveResult = movePlayer(MoveDirection::DOWN);
        break;
      case 'd':
        moveResult = movePlayer(MoveDirection::RIGHT);
        break;
      default:
        std::cerr << "Please use WASD keys to move player" << std::endl;
        break;
    }

    switch (moveResult) {
      case MoveResult::OUT_OF_BOUNDS:
        running = false;
        std::cout << "failure (out of bounds) - game over" << std::endl;
        break;
      case MoveResult::COLLISION:
        running = false;
        std::cout << "failure (collision) - game over" << std::endl;
        break;
      case MoveResult::GOAL:
        running = false;
        std::cout << "success - game over" << std::endl;
        break;
      default:
        break;
    }
  }
}

Maze::MoveResult Maze::movePlayer(Maze::MoveDirection direction) {
  Position previousPlayerPosition = m_playerPosition;

  switch (direction) {
    case MoveDirection::UP:
      m_playerPosition.row -= 1;
      break;
    case MoveDirection::LEFT:
      m_playerPosition.col -= 1;
      break;
    case MoveDirection::DOWN:
      m_playerPosition.row += 1;
      break;
    case MoveDirection::RIGHT:
      m_playerPosition.col += 1;
  }

  //check bounds
  try {
    m_grid.at(m_playerPosition.row).at(m_playerPosition.col);
  }
  catch (const std::out_of_range exc) {
    return MoveResult::OUT_OF_BOUNDS;
  }

  //check collision
  if (m_grid[m_playerPosition.row][m_playerPosition.col] == 'x') {
    return MoveResult::COLLISION;
  }
  //check goal
  else if (m_grid[m_playerPosition.row][m_playerPosition.col] == 'g') {
    return MoveResult::GOAL;
  }

  m_grid[previousPlayerPosition.row][previousPlayerPosition.col] = ' ';
  m_grid[m_playerPosition.row][m_playerPosition.col] = 'i';

  return MoveResult::OK;
}

int main() {
  auto maze = std::unique_ptr<Maze>(new Maze);
  maze->load("maze1.txt");
  maze->run();
}

也许吧?你确定为OP编写一个完整的程序是个好主意吗?OP可以自行决定。是的,这是一个问答网站。我们可能对这个问题有不同的解释。标题是问如何在迷宫中移动角色,问题主体是问如何从控制台阅读角色。我相信我已经解决了这两个问题。
xxxgx
xx  x
xx xx
x  xx
xixxx