Thread: Moving a printed character

  1. #1
    george7378
    Guest

    Moving a printed character

    Hi all,

    Is there a function in Visual C++ which will allow me to move a letter or number I have printed on the screen left and right, for example, if I start with '0' printed in the top centre of the screen, is there any way in which I can use the left arrow key to move it to the left?

    Thanks.

  2. #2
    george7378
    Guest
    EDIT - Failing this, how can I destroy what I previously wrote on the screen to replace it with something new, so that I don't get loads of zeros on the screen?

  3. #3
    george7378
    Guest
    Is there a function to delete a char?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I've never done anything like this so I dont know exactly what APIs may exist. I know there is "ncurses" which is for creating nice text-based interfaces, and I imagine that has capability of doing what you want ("moving" characters, etc). However, I dont think this is available for Windows. You can try searching for a Windows port or similar library for Windows.

    Alternatively, you can simulate a move by printing a character at position X, clearing the screen, and printing the character again at position (X+1). Search around on MSDN and for examples.
    - clear screen: How To Performing Clear Screen (CLS) in a Console Application and How to clear the Console window with Visual C++ .NET or Visual C++ 2005
    - set cursor position to print: SetConsoleCursorPosition Function (Windows)
    - etc, etc.

    If you refresh the screen very often, you'll probably notice a "flickering", so you may want to investigate a different method.

  5. #5
    george7378
    Guest
    Thanks for the info - could I use the gotoxy () function to move the cursor position and then print a character with it?

  6. #6
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    Try this (its commented so it will explain itself!):

    Code:
    /* Load Libraries */
    #include <conio.h>
    #include <windows.h>
    #include <stdio.h>
    
    /* Declare Goto Function */
    void gotoxy(int, int);
    
    /* Init X and Y plus the Variables which hold 
     previous X and Y. */
    int x, y, lastx, lasty;
    
    /* Make a CHAR variable to get Key Input */
    char a;
    
    /* Main Function */
    int main()
    {
     /* Give a value to all integer variables we declared */
     x = 1; y = 1;
     lastx = x; lasty = y;
     
     /* start an Infinite Loop */
     while(1 == 1)
     {
     /* Head to LastX and LastY and clear old O*/
     gotoxy(lastx, lasty);
     printf(" ");
     /* Draw New O */
     gotoxy(x,y);
     printf("O");
     /* Get Input */
     a = getch();
     /* Remember Old X and Y */
     lastx = x; lasty = y;
    /* If Down Arrow is Pressed, Y is increased */
     if (a == 80) y = y + 1;
    /* If Up Arrow is Pressed, Y is decreased */
     if (a == 72) y = y - 1;
    /* If Right Arrow is Pressed, X is increased */
     if (a == 77) x = x + 1;
    /* If Left Arrow is Pressed, X is decreased */ 
     if (a == 75) x = x - 1;
     }
    }
    
    /* Make GOTOXY function */
    void gotoxy(int eex, int eey)
    {
      COORD coord;
      coord.X = eex;
      coord.Y = eey;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }

  7. #7
    george7378
    Guest
    Great - it works - thanks.

    I have another question - how do I get a character to appear at a random coordinate every time I start the program?
    Which operator do I use?

    Thanks.

    EDIT - Let me rephrase:

    How do I get a character to appear at pre-defined co-ordinates, for example, X and Y?
    Last edited by george7378; 02-27-2010 at 05:54 AM.

  8. #8
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    updated Code, (this code will explain itself as well).

    Code:
    /* Load Libraries */
    #include <conio.h>
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    /* Declare Goto Function */
    void gotoxy(int, int);
    
    /* Init X and Y plus the Variables which hold 
     previous X and Y. */
    int x, y, lastx, lasty;
    
    /* Make a CHAR variable to get Key Input */
    char a;
    
    /* Main Function */
    int main()
    {
     /* if we dont use SRAND, RAND returns the same number over and over */
     srand((unsigned) time(NULL));
     /* a typical Console screen is 80x40, since a Console Screen is
        made of only text. 80 is the number of columns and 40 is the
        number of rows. so we randomize X and Y in those areas */
    
     x = (rand() % 80) + 1; y = (rand() % 40) + 1;
     /* Store the previous X and Y */
     lastx = x; lasty = y;
     
     /* start an Infinite Loop */
     while(1 == 1)
     {
     /* Head to LastX and LastY and clear old O*/
     gotoxy(lastx, lasty);
     printf(" ");
     /* Draw New O */
     gotoxy(x,y);
     printf("O");
     /* Get Input */
     a = getch();
     /* Remember Old X and Y */
     lastx = x; lasty = y;
    /* If Down Arrow is Pressed, Y is increased */
     if (a == 80) y = y + 1;
    /* If Up Arrow is Pressed, Y is decreased */
     if (a == 72) y = y - 1;
    /* If Right Arrow is Pressed, X is increased */
     if (a == 77) x = x + 1;
    /* If Left Arrow is Pressed, X is decreased */ 
     if (a == 75) x = x - 1;
     }
    }
    
    /* Make GOTOXY function */
    void gotoxy(int eex, int eey)
    {
      COORD coord;
      coord.X = eex;
      coord.Y = eey;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }

  9. #9
    george7378
    Guest
    Thanks for the help. Is there a way to make a timer run alongside the program, for example, to time 10 seconds and then display a message? i can get a timer to run before the program starts, but not in parallel.

    Thanks.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Quote Originally Posted by george7378 View Post
    Thanks for the help. Is there a way to make a timer run alongside the program, for example, to time 10 seconds and then display a message? i can get a timer to run before the program starts, but not in parallel.

    Thanks.
    You could look at http://linux.die.net/man/3/timer_create for linux, or http://msdn.microsoft.com/en-us/libr...06(VS.85).aspx for Windows.

  11. #11
    george7378
    Guest
    Thanks - is that for GUI based code, or for command based code - I am using command based.

  12. #12
    george7378
    Guest
    I found this code:

    Code:
    void wait(double val)
    {
    	int i;
    
    	for (i = 0; i <= val; i++)
    	{
    		continue;
    	}
    }
    
    wait(4000000); //etc
    Will it do what I need?

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Quote Originally Posted by george7378 View Post
    I found this code:

    Code:
    void wait(double val)
    {
    	int i;
    
    	for (i = 0; i <= val; i++)
    	{
    		continue;
    	}
    }
    
    wait(4000000); //etc
    Will it do what I need?
    The functions I provided will work in either a console or GUI application. Although, they will take quite a bit of research on your part to get working. I suggest you try and find examples or tutorials on them.

    As for this function, absolutely not. Look what you're doing here. You've created a simple for loop that increments an integer a rediculous amount of times. The fact that the number is so large is the only reason you may get a pause; also keep in mind that this loop you've created is CPU-intensive, will block, and will also pause for a different amount of time on everyone's computer (depending on the computer's speed).
    The fact that you asked for a timer that runs "parallel" with your program is why I offered you the timer options.

    However, if all you want is a blocking call that will wait for a certain amount of time, I suggest you research sleep.

  14. #14
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    a loop is not good for delay. E.G if the function delays 5 seconds with your
    processor, a faster computer will make the function to delay 2 seconds while
    a slower one will make the function to delay 10 seconds. and so on. anyway,
    here is a working alternative which delays the same no matter how much
    fast your Pc is:

    Code:
    /* Load Libraries */
    #include <conio.h>
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    /* Load also Stdlib.h because it has the sleep function */
    #include <stdlib.h>
    
    /* Declare Goto Function */
    void gotoxy(int, int);
    
    /* Init X and Y plus the Variables which hold 
     previous X and Y. */
    int x, y, lastx, lasty;
    
    /* Make a CHAR variable to get Key Input */
    char a;
    
    /* Main Function */
    int main()
    {
     /* type some message for the user */
     printf("My Moving Char program!\n\n");
     /* sleep waits in miliseconds, so 5 * 1000 = 5 seconds */
     sleep(5 * 1000);
     /* Clear the screen completely */
     system("cls");
     /* if we dont use SRAND, RAND returns the same number over and over */
     srand((unsigned) time(NULL));
     /* a typical Console screen is 80x40, since a Console Screen is
        made of only text. 80 is the number of columns and 40 is the
        number of rows. so we randomize X and Y in those areas */
    
     x = (rand() % 80) + 1; y = (rand() % 40) + 1;
     /* Store the previous X and Y */
     lastx = x; lasty = y;
     
     /* start an Infinite Loop */
     while(1 == 1)
     {
     /* Head to LastX and LastY and clear old O*/
     gotoxy(lastx, lasty);
     printf(" ");
     /* Draw New O */
     gotoxy(x,y);
     printf("O");
     /* Get Input */
     a = getch();
     /* Remember Old X and Y */
     lastx = x; lasty = y;
    /* If Down Arrow is Pressed, Y is increased */
     if (a == 80) y = y + 1;
    /* If Up Arrow is Pressed, Y is decreased */
     if (a == 72) y = y - 1;
    /* If Right Arrow is Pressed, X is increased */
     if (a == 77) x = x + 1;
    /* If Left Arrow is Pressed, X is decreased */ 
     if (a == 75) x = x - 1;
     }
    }
    
    /* Make GOTOXY function */
    void gotoxy(int eex, int eey)
    {
      COORD coord;
      coord.X = eex;
      coord.Y = eey;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  3. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  4. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  5. Moving a character to stdin?
    By Geolingo in forum C Programming
    Replies: 2
    Last Post: 09-27-2003, 06:37 PM