Character Movement

This is a discussion on Character Movement within the C Programming forums, part of the General Programming Boards category; Hey, I'm having a problem with my code. I am trying to move the character "#" left and right along ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    216

    Character Movement

    Hey, I'm having a problem with my code. I am trying to move the character "#" left and right along the x axis. However, it is only letting me move 1 left, and 1 right, as well as skipping spaces, and not going to the next coordinate. All help is appreciated! Thanks
    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
    
    
    #include <stdio.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);
                               
                               //x = x + 1;
                               
                               printf(" ");
                               x++;
                               
                               x = x - 2;
                               
                               gotoxy(x,y);
                               
                               printf("%c",player);
                               
                               break;
                               
                          case right:
                               
                               x--;
                               gotoxy(x,y);
                               
                               //x = x + 1;
                               
                               printf(" ");
                               
                               x++;
                               
                               //gotoxy(x,y);
                               
                               printf("%c",player);  
                               
                               break;                         
                               
                    }
          }                             
    }
    Some things have been commented out from my testing purposes. I'm stumped here. Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Are you ignoring warnings? You are missing several header files...

    You are treating the '#' as both a char and a string. Pick one.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I haven't gotten any warnings at all.

    I have fixed that now.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by binks View Post
    I have fixed that now.
    And?

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Nothing has changed, and what header files might I be missing?

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by binks View Post
    I haven't gotten any warnings at all.
    I think you need conio.h, no? If I don't #include it I get warnings.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Can you debug?

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I suspect kbhit() is requiring that?

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I'll try now.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    So I debugged it, and got nothing. Except that it doesn't print "#" this anymore.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Code:
    case left:
        x--;  <-- if you wish to move the char to the left, don't you want to erase
                   the current position first, then move left and draw the char?
        gotoxy(x,y);
        printf(" ");
        x++;                 <--- why add one, then take away two?
        x = x - 2;
        gotoxy(x,y);
        printf("%c",player);
    
        break;

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by binks View Post
    So I debugged it, and got nothing. Except that it doesn't print "#" this anymore.
    "Debugged it" ? Did you not learning anything by stepping through the code and watching what happens?

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Code:
    x--;  <-- if you wish to move the char to the left, don't you want to erase
                   the current position first, then move left and draw the char?
    How do I erase the current position first, if the I/O is like this "#_" (underscore is i/o) I have to move it back one, then replace with " ".

    Code:
    x++;                 <--- why add one, then take away two?
    I did this because x is a coordinate of the i/o pointer, so I wanted to move it forward to represent " ", and then back 2 to make the shift left of 1 on the x axis.

    Also, when I have the printf's using %c, it doesn't display a character, but when it's like %s, it displays "#"

    EDIT: I did, I stepped through everything after init, and then every other line in move() but it went through as normal, and no output.
    Last edited by binks; 04-25-2011 at 12:42 PM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    Code:
    case left:
        x--;  <-- if you wish to move the char to the left, don't you want to erase
                   the current position first, then move left and draw the char?
        gotoxy(x,y);
        printf(" ");
        x++;                 <--- why add one, then take away two?
        x = x - 2;
        gotoxy(x,y);
        printf("%c",player);
    
        break;
    Actually that code would not be unusual, mike.

    Think about where the cursor is after printing a character... he has to back up, then overwrite the character which leaves him where he was, then he has to go back 2 spaces so the cursor is to the left of the character to the left. Then when he reprints the octothorpe it will land in the right place.... one space to the left.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    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;

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

Similar Threads

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

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