【发布时间】:2019-10-11 19:41:26
【问题描述】:
所以我想在屏幕上显示一个具有特定坐标的对象并使其能够移动。例如,如果按下“u”,它会上升等等。所以我尝试使用变量 X 和 Y(初始化为 0)并在“清除屏幕”后使用 for 循环部署对象。所以看看这个:
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <vector>
#include <iomanip>
using namespace std;
char input;
char player = 1;
char keyup = 0x48, keydown = 0x50, keyright = 0x4d, keyleft = 0x4b;
int x = 0, y = 0;
void position()
{
for (int start = 0; start <= y; start++)
{
cout << "\n";
}
for (int start = 0; start <= x; start++)
{
cout << " ";
}
cout << player;
}
void moveup()
{
x - 1;
system("cls");
position();
}
void movedown()
{
y + 1;
system("cls");
position();
}
void moveright()
{
x + 1;
system("cls");
position();
}
void moveleft()
{
y - 1;
system("cls");
position();
}
int main()
{
while (1)
{
position();
cin >> input;
if (input == keyup) moveup();
if (input == keydown) movedown();
if (input == keyright) moveright();
if (input == keyleft) moveleft();
system("cls");
}
}
所以当我运行它时,它只会显示 ASCII 键 = 1 的立方体;无论我按什么,它都会闪烁,向我显示同一位置的“玩家”。 大佬能告诉我是什么问题吗?
【问题讨论】:
-
可能将“坐标”初始化为 0 是问题
-
我猜
cout << symbol;和char symbol = 0;是问题所在。字符'\0'通常用作字符串结束标记。我不会怀疑'\0'的输出是否影响最小。对于垂直进纸的输出,我会推荐'\n',对于水平 -' '(一个空格),当然,你必须按照这个顺序。 -
所以 Scheff 你的意思是没有变量
-
我该怎么做。我不推荐手动输出
-
你的问题不是变量——而是它的值。 ;-)
标签: c++ visual-studio visual-c++