animating ansii text using for loops and the gotoxy function?

Recommended Posts

dk32321    142
hello i am trying to get a simple line to animate across the console screen here is the code i got so far. // includes and function protocols and such in here int main() { linemaker(); return 0; } void linemaker() { char x = 179; gotoxy(1,1); cout << x; gotoxy(1,2); cout << x; gotoxy(1,3); cout << x; gotoxy(1,4); cout << x; gotoxy(1,5); cout << x; gotoxy(1,6); cout << x; } it displays the the line but show do i get the line to move across the screen.

Share on other sites
Deranged    668
Quote:
 Original post by dk32321hello i am trying to get a simple line to animate across the console screen here is the code i got so far.// includes and function protocols and such in hereint main(){ linemaker(); return 0;}void linemaker(){ char x = 179; gotoxy(1,1); cout << x; gotoxy(1,2); cout << x; gotoxy(1,3); cout << x; gotoxy(1,4); cout << x; gotoxy(1,5); cout << x; gotoxy(1,6); cout << x;}it displays the the line but show do i get the line to move across the screen.

Think about the code logically, it is moving the curosr and then drawing a line, here's how I would do it:

int main(){    char x = 179;    for(int i = 1; i <= 6; i++)    {        system("cls");        gotoxy(1, i);        cout << x;        Sleep(100);    }    system("pause");}

Note the Sleep for 100ms, if you don't sleep the line will move so fast you won't notice it.

EDIT: Sleep is in windows.h, when time comes it isn't too hard to write a version of it with ctime.

Share on other sites
dk32321    142
That would work but i cant use system("cls"); cause that would get rid of everything else i got on my console at that time of the program? anyother way.

Share on other sites
stylin    758
Quote:
 Original post by dk32321That would work but i cant use system("cls"); cause that would get rid of everything else i got on my console at that time of the program? anyother way.

Do you want to have a line drawn across the screen, or do you want a single character to move across the screen? If it's the latter, you can just erase the character using cout << " " before you cout << x.

Share on other sites
dk32321    142
see i want a line drawn with char x = 179; but the thing is i want to line to be longer in the yaxis so i made a function called line drawer which uses the gotoxy function to connect the lines together, then i want to move that function which contains a bunch of lines, how would i do that.

Share on other sites
stylin    758
Well, keeping it simple, you'd draw the line, then you'd erase the line. Then you'd draw it again from somewhere else. You can make a functions that will ease this process for you:

void draw_verticle_line( int x_pos, int y_pos, int length, char character );
void draw_horizontal_line( int x_pos, int y_pos, int length, char character );

First draw the line with char #179, then draw over the same line with char #32 (space). Then you can increment your x_pos or y_pos to move the start of the line over a character. Repeat.

Share on other sites
Zahlman    1682
I would honestly say, forget about manually specifying everything with gotoxy(). Instead, make a 'back buffer' char[][] that represents the whole set of things you want to draw; then in your main game loop you behave like
while (true) {  update(backbuffer); // actually make changes to the buffer contents according  // to what happens in game  gotoxy(0,0);  for (int i = 0; i < BACKBUFFER_HEIGHT; ++i) {    cout << backbuffer[i] << endl;  }}

In general this is a cleaner way of managing the complexity here, assuming your game is going to be anything like I imaging.

Note that this is one case where std::string objects would *not* be appropriate: the backbuffer data is really an "array of characters", and doesn't really represent textual data. However, you might want to write a function to "blit" the contents of a std::string at a specified location within the buffer.

This sort of exercise, BTW, is probably a good way to make the transition towards 'real' graphical programming (where back-buffer techniques are normally used all the time, too).