C++ 有没有更快的方法清除控制台?
我正在开发一些简单的基于2D阵列的游戏。它们的主要特点是在盒子里有一个弹跳的“球”(简单地说是一个字符球)。结构球有两个变量:X和Y,表示其数组位置。每个时钟(睡眠时间(毫秒))一个过程都会更改其X和Y(例如+1),清除控制台的窗口,然后再次绘制所有阵列以模拟球的运动 以下是部分代码:C++ 有没有更快的方法清除控制台?,c++,arrays,drawing,C++,Arrays,Drawing,我正在开发一些简单的基于2D阵列的游戏。它们的主要特点是在盒子里有一个弹跳的“球”(简单地说是一个字符球)。结构球有两个变量:X和Y,表示其数组位置。每个时钟(睡眠时间(毫秒))一个过程都会更改其X和Y(例如+1),清除控制台的窗口,然后再次绘制所有阵列以模拟球的运动 以下是部分代码: #include <iostream.h> //cout #include <windows.h> //Sleep(ms) and clearscreen() #define H 51
#include <iostream.h> //cout
#include <windows.h> //Sleep(ms) and clearscreen()
#define H 51
#define W 51
char box[H][W];
struct ball
{
char ball_char;
int x, y, movx, movy;
}b;
void clearscreen() //MY TEACHER SUGGESTED ME THIS CODE TO "CLEAR" THE SCREEN
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}
void draw() //here comes the bad "graphic engine" lol
{
int i;
clearscreen();
for(i=0;i<H;i++)
{
for(int j=0;j<W;j++)
cout<<box[i][j];
cout<<"|"<<endl; //DRAWING EDGES <---this is on the right side
}
for(i=0;i<W;i++)
cout<<"-"; //this is on the bottom side
}
所以问题是:你知道有什么更快的方法来清洁控制台吗?我已经知道系统(“cls”)是要避免的。您可以检查不同的方法来清除屏幕,我建议写多个空行作为一个简单的解决方案,可能足以满足您的应用程序
main()
{
b.ball_char='O';
b.x=10;
b.y=0;
b.movx=1;
b.movy=1;
do //this is what I call "the clock"
{
move() //my procedure to simulate the ball movements (it simply adds movements force to their coordinates with some controls etc.)
draw();
Sleep(100);
}while(1);
}