Character Movement

This is a discussion on Character Movement within the C Programming forums, part of the General Programming Boards category; Originally Posted by mike65535 OK, I'll have to think about this one a bit (not familiar with moving cursors around) ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    OK, I'll have to think about this one a bit (not familiar with moving cursors around)

    But this:
    Code:
        x++;                 <--- why add one, then take away two?
        x = x - 2;
    Yeah, that's probably part of his problem... I'm guessing that's a guess trying to get it to work. A code cleanup might be his best bet at this point.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Quote Originally Posted by mike65535 View Post
    OK, I'll have to think about this one a bit (not familiar with moving cursors around)

    But this:
    Code:
        x++;                 <--- why add one, then take away two?
        x = x - 2;
    Well, I did that just so it would be easier to understand, and try and fix it. Unfortunately, I still have no clue why the old "#" is not being erased, and why only movement 1 place to the left and right can be made.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Code:
    #ifdef WIN32
    #include <windows.h>
    void gotoxy(int x, int y)
    {  
         COORD cur = {x, y};  
         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
    }
    #else
    void gotoxy(int x, int y)
    {  
         printf("\033[%dG\033[%dd", x+1, y+1);
    }
    #endif
    // End of Giberish
    
    #include <stdio.h>
    #include <conio.h>
    
    #define length 77
    #define width 20
    
    #define left 'k'
    #define right 'l'
    
    #define pause_length 150000
    
    char player[] = "#";
    
    void setup();
    void init();
    void move();
    
    int main()
    {
        int j;
        
        setup();
        init();
        for(;;)
        {
              // for(j = 0; j < 14*pause_length; j++)
               //      j = 1 + j;
               move();
        }
        
        getch();
        return(0);
    }
    
    void setup()
    {
         int across;
         int down;
         
         gotoxy(1, 3);
    
         for(across = 0; across < length; across++)  
                    printf("=");         
         
         for(down = 0; down < width; down++)
         {
                  gotoxy(1,down + 4);
                  
                  printf("|");
                  
                  gotoxy(length, down + 4);
                  
                  printf("|");
         }
         
         gotoxy(1, 24);
         
         for(across = 0; across < length; across++)
                    printf("=");
                    
         gotoxy(1,1);
         
         printf("~~ Game ~~");
    }
                    
    void init()
    {
         gotoxy(37,23);
         printf("%s",player);
    }
    
    void move()
    {
         char keypress;
         char direction;
         
         int x = 38;
         int y = 23;
         
         if(kbhit())
         {
                    keypress = (char)getch();
                    
                    direction = keypress;
    
         
                    switch(direction)
                    {
                          case left:
                               
                               x--;
                               gotoxy(x,y);
                               
                               printf(" ");
                               
                               x = x - 2;
                               
                               gotoxy(x,y);
                               
                               printf("%s",player);
                               
                               break;
                               
                          case right:
                               
                               x--;
                               gotoxy(x,y);
                               
                               printf(" ");
                               
                               //x++; bug - pushes cursor 1 ahead of " _"
                               
                               gotoxy(x,y);
                               
                               printf("%s",player);  
                               
                               break;                         
                               
                    }
          }                             
    }
    Ok, I've cleaned that up, but it's still hasn't changed much on the visual side. It looks the same. I'm wondering, because in my setup function, I don't create spaces for all the blanks, so by replacing nothing with a blank, is that messing everything up. Or is it something to do with the coordinate x, because I've checked that 4 times, and surely it's correct now.
    Last edited by binks; 04-25-2011 at 12:02 PM. Reason: Fixed Bug

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I just noticed that player is a single character but you're treating it as a string...
    Code:
    char player = '#';
    
    printf("%c",player);
    
    // or much easier.
    printf("#");
    You might also want to print something visual for testing purposes... like maybe a colon or an asterisk so you can see where everything is going.
    Last edited by CommonTater; 04-25-2011 at 12:18 PM.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I had that before, but # wasn't even displayed. For some odd reason, it's only displayed when %s is used. But when it's like this
    Code:
    printf("#"); // without using variable
    it works. The left function works 1 to the left, but the right one only works when left is hit first, and doesn't erase the old # sign. Anyone know why it's only moving 1 space over (NOT per click, in total) i want it per click one space, but all the way across the screen if possible. ( I think it's because there aren't any spaces, I'll try adding them to setup)

  6. #21
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    PO is setting x and y up at the start of move(), but they aren't static so they get reset to the same value each time into move. Debugging and stepping through the code (as I suggested) makes this fairly obvious.

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by binks View Post
    I had that before, but # wasn't even displayed. For some odd reason, it's only displayed when %s is used. But when it's like this
    Code:
    printf("#"); // without using variable
    it works. The left function works 1 to the left, but the right one only works when left is hit first, and doesn't erase the old # sign. Anyone know why it's only moving 1 space over (NOT per click, in total) i want it per click one space, but all the way across the screen if possible. ( I think it's because there aren't any spaces, I'll try adding them to setup)
    The reason it didn't work with "%c" before is that you have defined player as an array of characters and preset it to the string of #\0 meaning that it is actually two characters. Of you use apostrophes (single quotes) as I showed you, you would not have that problem.

    Moving right should be easier than moving left... back up one space, printf(" #");
    Code:
    x--;
    gotoxy(x,y);
    printf(" #");
    x += 2;

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    I think you guys have gotoxy and the cursor wrong... maybe I misunderstood...

    This works:

    Code:
    char player = '#';
    
    #define START_X 37
    #define START_Y 23
    
    void move()
    {
         char keypress;
         char direction;
    
         static int x = START_X;
         static int y = START_Y;
    
         if(kbhit())
         {
                    keypress = (char)getch();
                    direction = keypress;
    
                    switch(direction)
                    {
                          case left:
                               gotoxy(x,y);
                               printf(" ");
                               x--;
                               gotoxy(x,y);
                               printf("%c",player);
                               break;
    
                          case right:
    
                               gotoxy(x,y);
                               printf(" ");
                               x++;
                               printf("%c",player);
                               break;
    
                        default:
                               break;
    
                    }
          }
    }

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hey mike... it's been a LOOOOONG time since I played with conio.h so you could be right...

  10. #25
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ahh! Thank you! I see it now, and I can't believe I totally missed it, each time it loops, it reintializes x and y as the default.
    Code:
    #ifdef WIN32
    #include <windows.h>
    void gotoxy(int x, int y)
    {  
         COORD cur = {x, y};  
         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
    }
    #else
    void gotoxy(int x, int y)
    {  
         printf("\033[%dG\033[%dd", x+1, y+1);
    }
    #endif
    // End of Giberish
    
    #include <stdio.h>
    #include <conio.h>
    
    #define length 77
    #define width 20
    
    #define left 'k'
    #define right 'l'
    
    #define pause_length 150000
    
    void setup();
    void init();
    void move();
    
    int main()
    {
        int j;
        
        setup();
        init();
    
              // for(j = 0; j < 14*pause_length; j++)
               //      j = 1 + j;
        move();
    
        
        getch();
        return(0);
    }
    
    void setup()
    {
         int across;
         int down;
         
         gotoxy(1, 3);
    
         for(across = 0; across < length; across++)  
                    printf("=");         
         
         for(down = 0; down < width; down++)
         {
                  gotoxy(1,down + 4);
                  
                  printf("|");
                  
                  gotoxy(length, down + 4);
                  
                  printf("|");
         }
         
         gotoxy(2,4);
         
         for(down = 0; down < width - 1; down++)
         {
                  gotoxy(2,down + 5);
                  
                  for(across = 0; across < length - 2; across++)
                             printf(" ");
         }
         
         gotoxy(1, 24);
         
         for(across = 0; across < length; across++)
                    printf("=");
                    
         gotoxy(1,1);
         
         printf("~~ Game ~~");
    }
                    
    void init()
    {
         gotoxy(37,23);
         printf("#");
    }
    
    void move()
    {
         char keypress;
         char direction;
         
         int x = 38;
         int y = 23;
         
         for(;;)
         {
                
         
                if(kbhit())
                {
                    keypress = (char)getch();
                    
                    direction = keypress;
    
         
                    switch(direction)
                    {
                          case left:
                               
                               x--;
                               gotoxy(x,y);
                               
                               printf(" ");
                               
                               x = x - 2;
                               
                               gotoxy(x,y);
                               
                               printf("#");
                               
                               break;
                               
                          case right:
                               
                               x--;
                               gotoxy(x,y);
                               
                               printf(" #");  
                               
                               break;                         
                               
                    }
                }
         }                             
    }
    Moving right seems to work the first time, but then after, it goes left. And left jumps two spaces. I'll try some more cleanups, and see. But is there anything either of you helpful people can spot easily that is messing it up?

    EDIT: I'll mess around with it more, what your saying makes sense. Thanks
    Last edited by binks; 04-25-2011 at 12:41 PM. Reason: Above 2 posts

  11. #26
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by CommonTater View Post
    Hey mike... it's been a LOOOOONG time since I played with conio.h so you could be right...
    Never touch the stuff (embedded c guy here).

  12. #27
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    In my design, x and y hold the current place that you wish to edit.

    So at first x is equal to where you originally drew the '#'. It remains there because you WILL erase it (print space) next. Then you will redraw the '#' (move x left or right) where you want to. Seems quite simple, no?

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You know you could do this and save yourself from some of the convolution...
    Code:
    // instead of...
              if(kbhit())
                {
                    keypress = (char)getch();
                    direction = keypress;
                    switch(direction)
     
    // use...
    switch(getch())
    This is one reason I suggested a code cleanup... you've a few of these and it would simplify debugging considerably.

    Also you should include a way out of the loop ....
    Code:
    switch(direction)
      {
        case left:
          //....
       case right:
         //.... 
       case 27:  // ascii ESCape key value
        return;
    Last edited by CommonTater; 04-25-2011 at 01:02 PM.

  14. #29
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Hey mike, that works smoothly, except I'm not using static int's, just int's (static's aren't suppose to change, so how would that work..?). Thank you.

    CommonTater, I will look into your code optimization, it looks much simpler and uses 2 less variables. Thanks.

    EDIT: Those optimizations are great, it's more clean now!
    Last edited by binks; 04-25-2011 at 03:28 PM. Reason: Result

  15. #30
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by binks View Post
    Hey mike, that works smoothly, except I'm not using static int's, just int's (static's aren't suppose to change, so how would that work..?). Thank you.
    Static variables can be changed... they just don't go out of scope.
    It's const (as in constant) variables that don't change.

    Glad we could help...

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 08-24-2010, 04:13 AM
  2. Replies: 13
    Last Post: 08-22-2010, 12:37 PM
  3. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  4. BMP Movement
    By Vicious in forum Game Programming
    Replies: 11
    Last Post: 06-06-2002, 11:19 AM
  5. Movement
    By got matt? in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2002, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21