如何使角色在C 中移动
how to make a character move in c++
所以我正在尝试在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!nn";
cout << "In order to win this game, you must make yourn";
cout << " way through the maze and find the O. n";
cout << " Try to get the best time you can to be number 1!nn";
cout << " Warning! Don't touch the walls or you will n";
cout << " lose points!n ";
cout << " Goodluck and have fun!nnnn";
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();
}
如何使用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
相关文章:
- 使lambda不可复制/不可移动
- 我应该使我的局部变量常量还是可移动的
- 为什么字符串流只读取整数中的第一个数字?如何使它只在空白处移动?
- 移动构造函数是否使shared_from_this无效
- 为什么移动对象不会使其为空?
- 我可以在移动平台上有效地使用用C/C 编写的数字算法
- 您如何使形状沿着更BE的曲线SFML移动
- 我可以在 std::list 中移动元素而不会使迭代器或引用无效,但是如何移动呢?
- 改变对象的原点点使其以不同的轴移动
- 如何使角色在C 中移动
- 什么是使类不可移动的用例(如果有的话)
- 是否允许复制/移动省略使使用已删除函数的程序格式正确?
- 移动std::shared_ptr会使程序崩溃
- 我的 SFML/C++ 程序按预期工作,但如何使移动变慢
- 使对象在 C++-SFML 中平滑移动
- 使投射物移动与时间相关SFML C++
- 平台独立性:它与将源代码从一个操作系统移动到另一个操作系统有何不同
- 使用 CSplitterWindowImpl 处理移动并使子窗格无效
- 移动是否使对象处于可用状态
- C++11编译器何时会使RVO和NRVO优于移动语义和常量引用绑定