0

I am trying to make a program where I can use the arrow keys or the WASD keys to move a character across the screen (rpg style). I dont have any clue how to redraw the board in an easy way since there would be so many possible positions. I was thinking about creating a 2d array which would hold the positions. I am not asking for you all to write the code, I am asking simply. Is it possible to make the text character move along positions across the array?

user3150762
  • 57
  • 1
  • 6
  • _'I am not asking for you all to write the code'_ But we're asking you to show some. – πάντα ῥεῖ Jan 05 '14 at 14:27
  • What O/S are you using? This is highly O/S dependant – bobobobo Jan 05 '14 at 14:33
  • I am using microsoft visual c++ 2010 with windows 7. @bobobobo – user3150762 Jan 05 '14 at 14:39
  • So I'd use `SetConsoleCursorPosition` then. It's fairly easy to use. You can [change the colors too](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047(v=vs.85).aspx). [see here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes) – bobobobo Jan 05 '14 at 14:42

2 Answers2

2

One way would be to clear the "screen" and redraw the "map" at every change. Another might be to only redraw the lines that have changed. Yet another might be to position the cursor after the character you want to "move", print a backspace followed by e.g. a space, then reposition the cursor to after where you want the new character to be, print another backspace and then the character.

Or simply use a library such as ncurses.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If the OS supports [ConsoleScreenBuffer](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx) type functions, you're best off using them. `cls`/full reprint won't perform well on many systems. – bobobobo Jan 05 '14 at 15:04
0

On Windows, you can use the SetConsoleCursorPosition function to move the cursor to any arbitrary XY coordinates that you like.

TO do this, you need a HANDLE to the console, which is fairly easy to get

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

More Windows Console functions.

See here for an example

Community
  • 1
  • 1
bobobobo
  • 64,917
  • 62
  • 258
  • 363