好吧,我已经知道有些人会生气我会问这个问题因为已经有很多了,但我似乎对此有任何意义;如何在C / C ++中获得即时输入(在单个字符上)?我慢慢走向一所古老的学校“塞尔达的传说”,就像游戏一样,到目前为止这个运动似乎很好,但是即时输入会让它变得更好。
我也确定我对char和他们旁边的数字做错了,如果有人可以帮助我,或提供教程或链接,我将非常感激。
同样在研究'goto'功能时,我释放了许多人说这是混乱和混乱。是的,它有点耗费时间,但至少对我来说并不凌乱或混乱。任何人都能解释对它的所有仇恨吗?
无论如何,对不起笨拙地说,这是我到目前为止的代码,如果我犯了很多新手的错误,请随意扯掉我。
// movement test
#include <iostream>
#include <stdio.h>
int main(void){
char move[101];
one:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n O=== \n ==== \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto one;
}
if (*move == 'a') {
goto one;
}
if (*move == 's') {
goto five;
}
if (*move == 'd') {
goto two;
}
two:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n =O== \n ==== \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto two;
}
if (*move == 'a') {
goto one;
}
if (*move == 's') {
goto six;
}
if (*move == 'd') {
goto three;
}
three:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==O= \n ==== \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto three;
}
if (*move == 'a') {
goto two;
}
if (*move == 's') {
goto seven;
}
if (*move == 'd') {
goto four;
}
four:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ===O \n ==== \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto four;
}
if (*move == 'a') {
goto three;
}
if (*move == 's') {
goto eight;
}
if (*move == 'd') {
goto four;
}
five:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n O=== \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto one;
}
if (*move == 'a') {
goto five;
}
if (*move == 's') {
goto nine;
}
if (*move == 'd') {
goto six;
}
six:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n =O== \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto two;
}
if (*move == 'a') {
goto five;
}
if (*move == 's') {
goto ten;
}
if (*move == 'd') {
goto seven;
}
seven:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==O= \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto three;
}
if (*move == 'a') {
goto six;
}
if (*move == 's') {
goto eleven;
}
if (*move == 'd') {
goto eight;
}
eight:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ===O \n ==== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto four;
}
if (*move == 'a') {
goto seven;
}
if (*move == 's') {
goto twelve;
}
if (*move == 'd') {
goto eight;
}
nine:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n O=== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto five;
}
if (*move == 'a') {
goto nine;
}
if (*move == 's') {
goto thirteen;
}
if (*move == 'd') {
goto ten;
}
ten:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n =O== \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto six;
}
if (*move == 'a') {
goto nine;
}
if (*move == 's') {
goto fourteen;
}
if (*move == 'd') {
goto eleven;
}
eleven:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==O= \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto seven;
}
if (*move == 'a') {
goto ten;
}
if (*move == 's') {
goto fifteen;
}
if (*move == 'd') {
goto twelve;
}
twelve:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ===O \n ==== \n";
std::cin >> move;
if (*move == 'w' ) {
goto eight;
}
if (*move == 'a') {
goto eleven;
}
if (*move == 's') {
goto sixteen;
}
if (*move == 'd') {
goto twelve;
}
thirteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n O=== \n";
std::cin >> move;
if (*move == 'w' ) {
goto nine;
}
if (*move == 'a') {
goto thirteen;
}
if (*move == 's') {
goto thirteen;
}
if (*move == 'd') {
goto fourteen;
}
fourteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n =O== \n";
std::cin >> move;
if (*move == 'w' ) {
goto ten;
}
if (*move == 'a') {
goto thirteen;
}
if (*move == 's') {
goto fourteen;
}
if (*move == 'd') {
goto fifteen;
}
fifteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n ==O= \n";
std::cin >> move;
if (*move == 'w' ) {
goto eleven;
}
if (*move == 'a') {
goto fourteen;
}
if (*move == 's') {
goto fifteen;
}
if (*move == 'd') {
}goto sixteen;
sixteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n ===O \n";
std::cin >> move;
if (*move == 'w' ) {
goto twelve;
}
if (*move == 'a') {
goto fifteen;
}
if (*move == 's') {
goto sixteen;
}
if (*move == 'd') {
goto sixteen;
}
return 0;
}
答案 0 :(得分:1)
首先,你会为玩家可以做的每个动作制作一个条件吗?
然后,如果你想在标准输入上只有一个字符,你可以使用 Getchar 这个在c或c ++中相同的函数。
如果您想避免按“Enter”,您可以进入RAW模式
system ("/bin/stty raw");
==&GT;这将直接在stdin中发送所有char
[C]示例:
#include <stdio.h>
int main ()
{
int c;
puts ("Enter text. Include a dot ('.') in a sentence to exit:");
system ("/bin/stty raw");
do {
c=getchar();
putchar (c);
} while (c != '.');
system ("/bin/stty cooked");
return 0;
}