Thread: drawing a 2d array

  1. #1
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    drawing a 2d array

    ok im makeing a game and the maps are 80 accross 15 down and the only way i can think of drawing it is with a lot of
    couts or with alot of loops but i cant get the loop idea to work any have some sample code from an old program basicly its like if it is 0 draw the floor if it is 1 draw the wall.
    +++
    ++
    + Sekti
    ++
    +++

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i don't have any sample programs, but it sounds like relatively easy iterative program, the idea lies in the logic, have a nested (double) loop , first one iterates 15 times and the inner loop iterates 80 times, and within the inner loop make a conditional check....

    Code:
       if(x == 0) cout << something;
       
      else if(x == 1) cout<< somthingelse;
    hope this can get you started.....

    try to start on your own if you get stuck then post some code and we'll try to help you..

    good luck...

    matheo917

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    i am sorry

    here is a tip..

    use a 2d array like map [5][5]
    Code:
    then say map[5][5] = { 1, 0, 1, 0, 0, 1, 0, 0, 1, , 0};
       for ( row =0; row <= 4; row++ )
       {     
             for ( col =0; col <= 4; col++ )
               if ( map[row][col] == 1 )
               //print *
               else
       }        //print " "
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    Those work, but as you've got a rather large map, consider putting it in a text file. You could make '-'s be grass and then '~' be water. Then, you could load the file, and based on the character it reads, it could draw a certain character. Here's some minimal code. It will load up a file, assign some values into a map[][] array based on the characters on the file, and then print the map:
    Code:
    #include <fstream.h>
    #include <stdio.h>
    
    int map[80][15];
    #define GRASS 1
    #define WATER 2
    
    ...
    
    void Map()
    {
         int i;
         int j;
         ifstream fmap("map.txt");
    
         char str[1200]; // That's a big array but it needs to go through the file
    
         for(i = 0; i < 15; i++)
         {
              fmap >> str;
              for(j = 0; j < 80; j++)
              {
                    switch((int)str[j])
                    {
                    case '-':
                         map[j][i] = GRASS;
                         break;
                    case '~':
                         map[j][i] = WATER;
                         break;
                    }
               }
         }
    
         fmap.close();
    
         // Now show the map
         char tile = (char)176;
         HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
    
         for (i = 0; i < 15; i++)
         {
              for (j = 0; j < 80; j++)
              {
                   switch(map[j][i])
                   {
                   case GRASS:
                        gotoxy(j,i);
                        SetConsoleTextAttribute(std, F_D_GREEN | B_GREEN); // For simplicty's sake, pretend I have all the colors pre-defined as these
                        printf("%c", tile);
                        SetConsoleTextAttribute(std, F_GREY | 0); // I like changing the text back to grey
                        break;
                   case WATER:
                        gotoxy(j,i);
                        SetConsoleTextAttribute(std, F_BLUE | F_GREEN | B_BLUE);
                        printf("%c", tile);
                        SetConsoleTextAttribute(std, F_GREY | 0);
                        break;
                   }
              }
         printf("\n");
         }
    }
    That's some basic code. Hope that helps a little.

    Brendan
    Last edited by harryP; 09-05-2002 at 07:36 PM.
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM