Я работаю над текстовой игрой, которая работает в качестве приложения консоли. Во время заглавных последовательностей и открывающих титров у меня есть несколько разных мест, которые у меня есть пользовательская функция, просящая, чтобы пользователь ударил ввести , чтобы продолжить (я изменяю формулировку нескольких подсказок, но функциональность одинакова. Игра пауза и ждет, пока пользователь нажимает введите до начала). Проблема, которая у меня есть, заключается в том, что, если пользователь попадает в любые ключи перед подсказкой (я экспериментировал и просто набрал кучу или случайную чушь), он отображается на экране всякий раз, когда игра достигает точки, чтобы сделать паузу для ввода. Я пытался использовать функцию, чтобы остановить входное эхо, промывание входа и блокирование входа. Кажется, ничего не работает. Мне нужно игнорировать все ввод от пользователя, пока я не спрошу об этом, и если по какой -то причине некоторые входные входные проскальзывания в игру мне нужно полностью его разбить (или, по крайней мере, не допустить его появления на экране) < /p>
Код: Выделить всё
#pragma once
#ifndef _FUNCTIONS_H
#define _FUNCTIONS_H
// FUNCTIONS.H
// Namespace
using namespace std;
// Void Functions Defined Elsewhere
void extern
// Accept Only Enter Key As Input
enter(),
// Flush Input
flush(),
// Ignore Previous User Input
showInput(bool toggle);
#endif // !_FUNCTIONS_H
< /code>
// FUNCTIONS.CPP
// Definitions
#define NOMINMAX
// Includes
#include "libraries.h"
// Show User Input
void showInput(bool toggle)
{
// Console Handle
HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
// LP Mode
DWORD mode;
// Get Console LP Mode
GetConsoleMode(ConsoleHandle, &mode);
// If True
if (toggle)
{
// Enable Input Echoing
mode |= ENABLE_ECHO_INPUT;
}
// If False
else
{
// Disable Input Echoing
mode &= ~ENABLE_ECHO_INPUT;
}
// Set Console LP Mode
SetConsoleMode(ConsoleHandle, mode);
}
// Accept Only Enter Key As Input
void enter()
{
// Clear Error Flags
cin.clear();
// Ignore Input Other Than Enter Key
cin.ignore(numeric_limits::max(), '\n');
}
// Flush Input
void flush()
{
// Console Handle
HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//Remove Input Currently In Buffer
FlushConsoleInputBuffer(ConsoleHandle);
}
< /code>
#pragma once
#ifndef _LIBRARIES_H
#define _LIBRARIES_H
// LIBRARIES.H
// Standard Libraries
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// Standard Header Libraries
#include
#include
#include
#include
#include
#include
// Game Header Files
#include "functions.h"
// Namespace
using namespace std;
#endif // !_LIBRARIES_H
< /code>
// MAIN.CPP
// Includes
#include "libraries.h"
int main()
{
// Hide Input Echo
showInput(false);
// Block Input (Attempted separately from showInput and flush functions)
BlockInput(true);
// Various functions acting as title sequences and opening credits
// Show Input Echo
showInput(true);
// Unblock Input (Attempted separately from showInput and flush functions)
BlockInput(false);
// Flush Unwanted Input
flush();
// Await User Input (ENTER KEY)
enter();
// Game continues after user presses ENTER
// ...
// Eventual Game End and Exit
exit(0);
}
< /code>
I learned that BlockInputПодробнее здесь: https://stackoverflow.com/questions/797 ... -asked-for
Мобильная версия