屏幕上协调对象
coordinating objects on the screen
,因此我想在屏幕上显示具有某些坐标的对象,并使其能够移动。例如,如果按下" 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的立方体;不管我按什么,它都眨眼都向我展示了同一位置的"播放器"。你们能告诉我什么问题?
首先,我建议使用字符文字而不是数值值:
char const keyup = 'u';
// ^ they are constants, aren't they???
0
不是可打印的字符,因此使用它是因为symbol
在屏幕上不会打印任何东西...您可能会使用e。G。'+'
而不是。
然后,您正在在功能中重复几乎重复代码。您实际需要的只是一个打印功能。类似:
void update()
{
system("cls");
for(int i = 0; i < y; ++i)
std::cout << 'n'; // navigate to appropriate line
for(int i = 0; i < x; ++i)
std::cout << ' '; // navigate to column
std::cout << symbol << std::endl; // endl flashes buffer, too
}
您可以使用它将符号打印为当前位置(x/y(。在输入上,您只需要修改坐标(我个人建议一个开关语句而不是if/else链(:
std::cin >> input;
switch(input)
{
case keyup: // NEEDS to be a constant to be used that way!
--y;
break;
case keydown:
++y;
break;
case keyleft:
--x;
break;
case keyright:
++x;
break;
default: // catch invalid user input
// if you have, appropriate error handling – or just ignore
break;
}
一个空的默认可以被排除在外 - 不过,这是一个好习惯,始终捕获无效的情况。
最后:屏幕的第二个清理(在您的if/else链之后(将在用户可能已经看到任何东西之前再次清空屏幕。只要把它留出来。全部合并:
for(;;)
{
update(); // first screen update and returning here in every loop
std::cin >> input;
switch(input)
{
// ...
case ExitKey: // recommend to add it!
// the only disadvantage of switch: cannot use 'break' to exit loop!
goto LOOP_END;
default:
break;
}
}
LOOP_END:
// clean up application
return 0; // exit main...
我尚未在上面省略的一件事,但是非常重要:范围检查!在增加或减少其中一个坐标之前,您需要检查是否可以:
if(x > 0)
--x;
或我个人喜欢的变体:
y += y < SCREEN_HEIGHT; // needs to be defined appropriately...
关于屏幕尺寸:这可能很感兴趣...
,因此改进的代码看起来像这样:
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <vector>
#include <iomanip>
using namespace std;
char input;
char player = 1;
const char keyup = 'u', keydown = 'd', keyright = 'r', keyleft = 'l', Exitkey = 'k';
int x = 0, y = 0;
void space()
{
for (int start = 0; start < 7; start++)
{
cout << "n";
}
}
void update()
{
system("cls");
for (int i = 0; i < y; ++i)
cout << 'n';
for (int i = 0; i < x; ++i)
cout << ' ';
cout << player << endl;
}
void inputfunc()
{
cin >> input;
switch (input)
{
case keyup:
if (y = 0)
{
y = 0;
}
else --y;
goto endloop;
case keydown:
++y;
goto endloop;
case keyleft:
if (x = 0)
{
x = 0;
}
else --x;
goto endloop;
case keyright:
++x;
goto endloop;
case Exitkey:
goto endloop;
default:
break;
}
endloop:;
}
int main()
{
for (;;)
{
update();
space();
inputfunc();
}
}
还有一件事:输入一个角色后,我不知道如何自动制作它。我需要输入任意数量的字符,然后按Enter部署播放器。我该怎么做?
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 屏幕上协调对象