How to implement a MOVING CHARACTER/STRING in one line of the screen using C++? Is this possible? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to implement a MOVING CHARACTER/STRING in one line of the screen using C++? Is this possible?

Moving Character

27th Jun 2022, 4:12 PM
Liza Tolentino
4 Answers
+ 1
Thanks for immediate response... I will try this solution 😊 i appreciate every help from anyone about my question..
29th Jun 2022, 12:17 PM
Liza Tolentino
0
If you mean in a console window, there used to be a function gotoxy() in C, if you google that, you will find an example of how to reimplement it in Windows using SetConsoleCursorPosition(): #include <windows.h> void gotoxy(int x, int y) { COORD coord = { x, y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } This will allow you to position a text in the console IN WINDOWS. It only works on Windows. If this isn't what you were looking for, tell me and I will delete my answer so that your question will show up as unanswered so that others will perhaps look at it.
29th Jun 2022, 12:45 AM
lion
lion - avatar
0
Oh, there is another way: \r moves the current displaying position at the beginning of the current line, allowing you to overwrite whatever there already is on that line. Say you want to animate an arrow: cout << "==>\r"; Sleep(200); cout << " ==>\r"; Sleep(200); cout << " ==>\r"; Sleep(200); cout << " ==>\r"; Instead of Sleep() you can use whatever means to insert a delay so that it looks like an animation, Sleep() is what I used on Windows. I don't think it works on Sololearn playground. Perhaps it's also worth noting that like \r moves the cursor at the start of the current screen line, \b moves it one character to the left. These can be used to animate text on the current line, can't be used for multiple lines. For multiple lines I guess the other solution, with SetConsoleCursorPosition(), works.
30th Jun 2022, 8:31 AM
lion
lion - avatar
0
I'll try this. Thank you so much for additional knowledge! 🙏🙏🙏
2nd Jul 2022, 4:05 PM
Liza Tolentino