animating ansii text using for loops and the gotoxy function?

Started by
5 comments, last by Zahlman 18 years, 5 months ago
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.
Advertisement
Quote:Original post by dk32321
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.


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.
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.
Quote:Original post by dk32321
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.

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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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.
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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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 << 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).

This topic is closed to new replies.

Advertisement