Thread: GotoXY Problem

  1. #1
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38

    GotoXY Problem

    I'm having problem with using a gotoxy function to move the cursor around the screen (as part of game controls).

    Code:
    #define GET getche() 
    
    done = FALSE;
    while(!done)	
    {
            i = GET; 
            if (i == UPKEY)
            {
                  gotoxy(x, y--);
            }
            if (i == DOWNKEY)
            {
                  gotoxy(x, y++);
            }
            if (i == LEFTKEY)
            {
                  gotoxy(x--, y);
            }
            if (i == RIGHTKEY)
            {
                  gotoxy(x++, y);
            }
    I'm currently using getche() as I wanted to see if the console was picking up the correct input and it is. The problem is that when I hit a direction key, I get a delay in moving in the proper direction. The cursor will go one extra step before it changes. This is easier to explain using a screenshot and getche()

    http://img291.imageshack.us/img291/9655/consolepr2.jpg

    My brain is telling me that this will be caused by the incrementing/decrementing method I'm using with gotoxy, but the assignment says we HAVE to use gotoxy as the controls and I can't think of a better way to do it.

    Thanks for any help
    Last edited by Swordsman; 04-16-2007 at 02:23 AM. Reason: Messed up link

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Me thinks gotoxy(x, --y); etc

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Hi

    I guess zacs7 is correct.. try to use pre increment and pre decrement... rather than doing post increment and post decrement..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Good old side effects, gets 'em every time.
    Code:
            if (i == UPKEY)
            {
                  y--;
            }
            if (i == DOWNKEY)
            {
                  y++;
            }
            if (i == LEFTKEY)
            {
                  x--;
            }
            if (i == RIGHTKEY)
            {
                  x++;
            }
            gotoxy( x, y );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Get rid of that define as well. It's pretty much pointless.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    even better, use a switch

    Quote Originally Posted by Desolation View Post
    Get rid of that define as well. It's pretty much pointless.
    It's not pointless, it means he can easily change it if he goes to a compiler that doesn't support getche()...

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by zacs7 View Post
    It's not pointless, it means he can easily change it if he goes to a compiler that doesn't support getche()...
    I would imagine it would be better to define it to at least look like a function, rather than looking like some bland global const variable.

  8. #8
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Quote Originally Posted by MacGyver View Post
    I would imagine it would be better to define it to at least look like a function, rather than looking like some bland global const variable.
    As somebody mentioned, it's because I compile on both Linux and Windows, so there's a bunch of different ones - I only included getche() to demonstrate what GET was, and they won't be in the final program it's just a stopgap.
    Anyway, thanks for the help on this, I've been racking my brains for days

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM