Thread: Moving ASCII Character

  1. #1
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Moving ASCII Character

    I am trying to make an ASCII character move across a map.

    Example:

    ..........
    ...!......
    ..........
    ..........
    ..........
    ..........

    I want the '!' to move Up, Down, Left and Right depending on the key you enter. Not sure at all how to do this, help or an example would be appriciated.

    -Thanks, Ranedhel
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What have you tried so far?

    How about a simple 2-d array, like
    char a[10][10];
    to hold the map.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Re:

    yes, I thought of that - but I'm not totally sure how to put everything together. If it's not to much trouble, could you
    make me an example code?
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This creates a 10x10 array, and populates all elements with a dot.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char a[10][10];
      int i, j;
      
      for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
          a[i][j] = '.';
      
      return(0);
    }
    Now see if you can make it work for you
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Re:

    Heh, I'm sorta new to arrays, I'm not exactly sure how to display the elements.
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to read up on it then

    printf ("%c", a[i][j]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Re:

    Heh, Good Idea. Thanks.
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or (and I should have said this in the first place):

    >>putchar(a[i][j]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    1
    um.. what about just saving the position of this special character and when you want to move you redraw the map (10x10) and put the special character on its position like this:

    Code:
    for(int y=0;y<MAX_Y;y++) //MAX_X is the width of you charmap
    { for(int x=0;x<MAX_X;x++) //guess what
        putch('.');
      putch('\n');
    }
    gotoxy(posx,posy);
    putch('!');
    greets,
    corpse

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    #define EMPTY 0
    #define PLAYER 1
    
    #define TRUE 1
    #define FALSE 0
    
    void DrawMap(void);
    void MovePlayer(int xmove,int ymove);
    void Init(void);
    void GetInput(void);
    void ClearScreen(void);
    
    void Init(void)
    {
    }
    
    void GetInput(void)
    {
    }
    
    void ClearScreen(void)
    {
    }
    
    
    void DrawMap(void)
    {
      for (int i=0;i<10;i++)
      {
        for (int j=0;j<10;j+)
        {
           switch (map[i][j])
           {
             case EMPTY: printf(".");break; 
             case PLAYER: printf("!");break;
           }
         }
         printf("\n");
      }
    }
    
    void MovePlayer(int xmove,int ymove)
    {
       if (xmove!=0)
       {
          int tempx=playerx+xmove;
          if (tempx>0 && tempx<10) playerx=tempx;
       }
    
       if (ymove!=0)
       {
          int tempy=playery+ymove;
          if (tempy>0 && tempy<10) playery=tempy;
          
       }
    }
    
    int main(void)
    {
       Init();
       while (Playing)
       {
         ClearScreen();
         DrawMap();
         GetInput();
       }
    return(0);
    }
    Just one example of how to draw the map and a small example of your main game loop - GetInput() does not return until the user does something so this is not a good real-time example. Also in real time you would not want to ClearScreen() every frame as this would cause a lot of flicker. To explain more I would have to go into details that might be beyond the scope of this thread.

    MovePlayer() does not check for map boundaries but it needs to. I'll leave this up to you. If you do not check the boundaries, this program will crash hard if you move off of the map. The emtpy functions are for you to figure out plus I'm too lazy to code them for you.




  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    17

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character to Ascii
    By tdep in forum C Programming
    Replies: 6
    Last Post: 07-10-2006, 03:07 PM
  2. Extended character ASCII in LINUX/WINDOWS
    By intmail in forum Linux Programming
    Replies: 2
    Last Post: 01-26-2006, 03:24 PM
  3. Replies: 3
    Last Post: 01-25-2006, 08:04 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. Replies: 1
    Last Post: 07-31-2002, 10:49 AM