How to make an object move with in an intervals?

I created a moving car sample using graphics.h
The car moved correctly but how can I stop it rather than disappearing at a point?

Here is the source code:



#include<iostream>
#include <graphics.h>
using namespace std;
int main()
{
int gd=DETECT;
int gm; initgraph(&gd, &gm, "c:\\TC\\BGI");
int maxX = getmaxx();
for(int i=0,color=10;i<=maxX;i++,color++) {
if(color > 15) color=1;
setcolor(color);
bar(50+i*3,200,200+i*3,240);
bar(75+i*3,170,150+i*3,200);
circle(75+i*3,250,15);
circle(175+i*3,250,15);
line(0,265,maxX,265);
if(i==0)
{
setcolor(WHITE);
outtextxy(100,400,"PRESS ANY KEY TO START");
getch();
}
else if(i==maxX)
{
setcolor(WHITE);
outtextxy(200,200,"THE CAR IS GONE");
}
delay(0);
if(i<maxX)cleardevice();
}
getch();
closegraph();
}
this is pretty hard to read.
but presumably if you want to stop the car before it goes off the screen, you have to stop it. Three immediate ideas come to mind..
- you could stop the loop before it gets there, or make the loop condition contain information to stop it, eg for(whatever, carposition on screen && other cond, something++) type setup.

-you can check to see if it is going to go off the screen and not move it any more (same idea, but now check in the loop, not in the condition, and keep looping even if it would go off, maybe because you do other stuff that should not stop even if the car stops, like maybe a bird is flying around also and you want to animate that but stop the car, etc)

- you could use an imposter, so if you detect the car went off the screen you draw a stunt double in a fixed location. This is sometimes used in 3-d graphics where a scene is put in the background instead of 3-d objects for performance; the scene can be a movie/frames even, but its not doing all that 3-d computation stuff.




Thanks for your support


But still it is not working
you may want to post what you changed?
Topic archived. No new replies allowed.