Making Moving Objects

Ok so i want to make something like a letter move upon user commands. i suppose everyone has been taught some form of this with the getch() function apparently, but a post by sum guy and a comment from Drasco(or something like that) discourages me from that. So i already think i know how to start its jus the part where i have to say:

Take this char and move it to this place or this place or this place or this place depending on what the user inputs(arrow keys or sumthing).

anyone know a good way of doing that?
Last edited on
Perhaps clear the screen, and re-print the character?
I can use that to help towards the end, so ty for that. but thats not exactly what i was getting at.
You have to use some platform-specific code to do it. There is no way around it.

If you are using Windows, the stuff in <conio.h> is a perfectly acceptable option, though I would still encourage you to use the Windows Console Functions directly
http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14522
(instead of the ancient conio stuff).

Either will suit your purpose. However, if you plan to do this for cross-platform code, check out
NCurses (POSIX) http://www.gnu.org/software/ncurses/ and
PDCurses (Win32) http://pdcurses.sourceforge.net/
A getting started guide:
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

It looks scary at first but is really very simple. The basic setup is to initialize the terminal, then do your drawing/reading, and finalize the terminal before quitting. A simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <curses.h>

void initialize()
  {
  initscr();                     // enable curses
  raw();                         // get direct keyboard input (not line-buffered)
  (void)noecho();                // don't echo keys to screen
  nonl();                        // don't translate newlines
  intrflush( stdscr, FALSE );    // don't process interrupt keys (like Ctrl-C)
  (void)keypad( stdscr, TRUE );  // read extended keys (like arrow keys)
  // start_color();              // (if you plan to use text attributes)
  }

void finalize()
  {
  endwin();
  }

int main()
  {
  int ch;

  initialize();

  // draw stuff
  wclear( stdscr );
  wmove( stdscr, /*row*/ 5, /*column*/ 10 );
  waddstr( stdscr, (char*)"Hello, world!" );

  wmove( stdscr, 6, 10 );
  wprintw( stdscr, (char*)"There are %d lines and %d columns.", LINES, COLS );

  wmove( stdscr, 10, 0 );
  waddch( stdscr, '?' );

  wrefresh( stdscr );  // this makes sure the stuff is displayed

  // get input
  ch = wgetch( stdscr );
  /* do what you will with ch here */

  finalize();
  return 0;
  }

Basic extended/special key codes are:

KEY_UP
KEY_DOWN
KEY_LEFT
KEY_RIGHT
KEY_HOME
KEY_END
KEY_DC // delete key
KEY_IC // insert key
KEY_NPAGE // pgdn/next key
KEY_PPAGE // pgup/prior key
KEY_BACKSPACE
KEY_ENTER
KEY_F( n ) // function keys. n = 1, 2, 3, ...

You can find others in the <curses.h> header.
The Esc key is not defined there, since a lot of keyboards use it to start multi-byte key sequences. You can
#define KEY_ESC 27
to use it.

A lot of the output commands (like waddch()) can be combined with the move commands: mvwaddch( y, x, c )

You can type "man 3x curses" and "man 3x addch" for documentation on the web.

Wow, bigger response than I meant....

Hope this helps.

[edit]
Oh, before I forget, those casts in initialize() get rid of some warning messages when using PDCurses, since some of those functions are actually macros that translate into sequenced statements (stuff like foo(), baz = 12, quux();)

[edit] Removed a call to a routine I wrote that got in there by accident. Also added the cast on line 31 to get rid of compiler warnings.
Last edited on
Topic archived. No new replies allowed.