a problem after using cleardevice and sleep functions in graphics.h

I have a college project which is a car racing game using C++ and the old-school graphics library BGI. After I draw the map and placed the objects(Car,obstacles,road's borders etc..)
I added Sleep(); function to the function named Obstacles(); but the problem is, I can't move the car with the right&left arrows.
another problem,If I added a cleardevice(); command all objects disappears only the obstacles function keeps working.
the Code is here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
char c; 
do{ 
    c = (char)getch();
    if (c == KEY_LEFT)
{
        x = x - 10, x1 = x1 - 10;
    }
    if (c == KEY_RIGHT)
    {
        x = x + 10, x1 = x1 + 10;
    }
    line(300, 15, 300, 600);//left Border
    line(500, 15, 500, 600);//right Border
    bar(x, y, x1, y1);//the moving body of the car
        Obstacles();//Calling the obstacles function.
        cleardevice();
void Obstacles()//Obstacles function
{
    int q = 320, w = 0, e = 350, r = 30;
    for (int i = 0; i < 60; i++)
    {
        w = w + 10;
        r += 10;
        rectangle(q, w, e, r);
        Sleep(100);//this stops the moving controls to move the object's car
        cleardevice();//when I write this the other objects disappears
    }
}while(true);

note: this is not the whole code, it's only a small portion of it, not a debugging question only need a hint how to fix it. I googled a lot but no useful thing found. Thanks in advance.
Last edited on
cleardevice() does exactly that -- it clears everything on the screen.

Your program should initialize something like this:
int activepage = 1;
setactivepage( activepage );

And your main drawing function should work something like this:
begin draw
cleardevice();
draw everything

setvisualpage( activepage );
activepage ^= 1;
setactivepage( activepage );
end draw

Hope this helps.
didn't work :( .. the car's object didn't move. Also, when I attached the activepage lines didn't work ? the whole screen disappeared :O the main problem is that the sleep(); function is avoiding the object to be moved ,, how to find a way to fix it? :(
thanks a lot for your help my friend , if is it okay I will send the whole code to you and see maybe i put the lines in a wrong place. :)
Objects only move if you paint/draw them in a different spot.
Topic archived. No new replies allowed.