所以我试图用c ++制作一个迷宫游戏。下面的代码是我到目前为止所做的,但我希望在游戏中使用箭头键移动角色。
我应该使用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();
}
答案 0 :(得分:1)
像一些流行的游戏一样使用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();
}
我使用的示例映射文件如下所示:
xxxgx
xx x
xx xx
x xx
xixxx