how to display a moving object

i have a question. Iwant to ask how to cout or display something that has been displayed and change it without cout it again. And the value go to and change that already been displayed.

Example : (cmd) being displayed:
10.

so basically if i change this integer value in the program, it wont change the displayed item. Instead i have to cout it again. But i want my program to do this..
x = 10; display x as '10', and i change the value of x, and the displayed item automatically changed too.

Another example:

===============================
H


i want to move this 'H' to another edge. How do i do this? even i change the value position of 'H', it wont move. Instead i cout it in a recursion, so it looks like:

===============================
H..............................

===============================
......H........................

===============================
............H..................

===============================
.................H.............

===============================
.......................H.......

===============================
..............................H

but i want it to work like this :

===============================
H..............................


===============================
H....H....H....H....H....H....H

so the point is, i want to move the displayed item/object, from the current position. And when it's move to another position, its position before it stayed is removed.
THanks

NB: *important : the user can watch the object moving
Last edited on
Don't use the console. Consoles are different from computer to computer and there are even multiple consoles per system if the user installs more. Not only that, but C and C++ have no concept of a "console" and only understand input streams and output streams. Your program might not even be run in a console.

Instead, learn to use a graphics library like SDL or SFML. I recommend SFML.
i haven't learn about that. What i was trying to say is something like mazetraverse, or race.

i have write a maze traverse program, the goal is for the object to walk through the maze to reach the goal and search for the exit. My maze is working, but everytime (X= object walk thourgh the maze), the 'X' walk or move to another position in the program but when the program is running it won't move. So what i did is, to printout the maze and the 'X' every time 'X' move into another position. I just don't know the concept or writing a program like that. I tried to write the program to display 'X' position either it stay in one position or move to another position in the same one maze and not print out the whole maze with 'X' moved to another position. I tried using pointer and reference, but it just didn't work. thanks for the answer though
The standard does not provide a way to do this with the console. You will either need to use a 3rd party lib (like ncurses) or you will have to use platform specific APIs (like WinAPI).

The former requires you download/install a library. The latter will make your code non-portable.


What LB is suggesting... is that if you're going to go through the trouble of using another lib to do this kind of thing... you might as well just do it right and get a graphic lib and draw actual graphics instead of using the console. I tend to agree with that. The console is not really designed for this and doing this kind of thing is very clunky. It's generally easier to just do proper graphics.


That said... if you really want to stay in the console for whatever reason... and you are on Windows.... you can use SetConsoleCursorPosition() to move the cursor position, so the next thing you print will be placed at a certain X,Y coordinate:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx
https://www.youtube.com/watch?v=rmw8FnVa-JA

how does that guy write a program like that? the maze, and status below the maze is changing, but the program don't need to print out the new changed maze or status instead of changing the current maze and status. I need a simple program of this concept as my sample and i need references. Thanks
He either uses a 3rd party lib like Ncurses, or he uses a platform specific lib like WinAPI (which I linked to in my previous post).

But again... this is extremely clunky to do in the console. You'll have to shift the cursor position all over the screen and print over old characters. It's usually easier to just have an actual window with graphics.
then what about this?
https://www.youtube.com/watch?v=cWSJXVPKZFk

my maze is exactly like his. But mine always print the maze everytime it moved, i want the program to work like his. The node is walking and moving inside the maze, while the maze stay the same. And don't need to printout the new maze with the node inside has moved. That way the user can watch the node walk through the maze, not watching the program printing the node moved to other position in the maze with 100 different maze and different node position (if the node walked and moved as many as 100 times) so the program has to print out 100 maze. Thanks again for the answer
Disch already told you twice how it is done. :P You need to print a space or something at the old position then print the character at the new position.
Ok thanks, i could write a statement for that and change its value for positioning. But i can't display it in the maze without printing another maze. So now i have two maze with different node position if the maze walked twice when i run the program
Maybe considering redesigning your program than if you have to redraw the whole thing when it[character] moves. I would consider a function move character.
1
2
3
4
void moveCharacter(short x, short y)
{
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {x,y});
}
Last edited on
On Windows, when you output something with cout, it will print it to the console wherever the current cursor position is.

The cursor position moves forward with each output character.

To print over old characters, you need to reposition the cursor. That way, when you print something new, you will overwrite what is already on the screen.

So basically to do this you need to move the cursor, print, move the cursor, print again, move, print, move print, etc, etc.

And again... there is no way in the standard lib to move the cursor. This must be done either with a platform specific lib or with a 3rd party lib.

If you are on Windows, you can use the SetConsoleCursorPosition. Details and examples of how to use are here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx




But like I said. This is probably more complicated and more difficult to do it this way than it would be to just use a graphic lib and draw actual graphics.
then how to let the program work buffering to watch the node moving, or something like that with move function? thanks
I don't understand that question. Can you rephrase?
the program working, so the user can watch an object moving. Like a maze traverse, snake, horse race, or something like that. Because as far as i have learn, i only can write a program to change a value, and print the whole thing everytime.

For easy question. "How to write a function that make an object moving"
Last edited on
I've answered that question 4 times.

You move the cursor.
But how? I am not talking about other third party, but using only a very basic c++ coding
Then the simple answer is no. If you don't want to use non-standard or platform specific things.
But how?


With SetConsoleCursorPosition (assuming you're on Windows)

The link again (for documentation so you can see how the function works. This is not a download link):
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx

giblit provided an example for you earlier in the thread of how to move the cursor to a specific X,Y coordinate.

I am not talking about other third party, but using only a very basic c++ coding


C++ is a language. The language itself allows you to create variables and move information around. It does not have any facilities for any kind of input/output to the user. For that... you need libraries.

Libraries extend the language by giving you functions/objects which can be used to input/output.

C++ comes bundled with a standard library. This library includes things like cout, cin, string, vector, fstream, etc, etc. Any time you #include some kind of header, you are probably adding some part of the standard library to your program.

The standard library has no way to move the console cursor. It is not available anywhere in it. If you are using the standard library alone... moving the cursor is impossible.


Apart from the standard library... other libraries come bundled with the compiler when you installed it. If you are on Windows, your compiler almost certainly came bundled with WinAPI (ie: the <Windows.h> header). So this library is likely available to you without you having to do any extra work.

Windows.h does have a way to move the cursor (with SetConsoleCursorPosition). So if you are on Windows, you can use that function.


Other libraries (like SFML, Ncurses, SDL, Allegro, wxWidgets, Qt, boost, etc) can be downloaded from the internet and installed. Unlike the standard library and WinAPI, these other libraries are not available to you immediately after you installed your compiler. You will need to install them separately if you want to use them.




If you do not want to use a downloaded lib... and if you are on Windows... you can use WinAPI.

If you do not want to use a downloaded lib and you do not want to use WinAPI, then it is impossible to do what you want to do with C++.
Last edited on
Topic archived. No new replies allowed.