是否有更快的方法来清除控制台?

时间:2015-01-28 16:09:57

标签: c++ arrays drawing

我正在开发一些简单的基于2D阵列的游戏。它们的主要特征是在一个盒子里弹跳“球”(简称char ball_char ='O')。结构BALL有两个变量:X和Y,它们表示阵列位置。每个时钟(睡眠(毫秒))一个程序改变其X和Y(例如+1),清除控制台的窗口,然后再次绘制所有数组,模拟球的运动。

以下是代码的一部分:

#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
}

如您所见,框越大,再次绘制整个数组所需的时间越长。正如我评论的那样,我向我的教授提到了这个项目,它向我建议清除控制台的代码。但它不是真正的清洁,但据我所知,它只将光标放在开头。

主要代码:

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);
}

所以问题是:你知道更快的方法来清理控制台吗?我已经知道要避免系统(“cls”)。

1 个答案:

答案 0 :(得分:0)

Here你可以检查清除屏幕的不同方法,我建议写多个空行作为一个简单的解决方案,可能已经足够你的应用了。