how to create/use a simple map in a game

This is a discussion on how to create/use a simple map in a game within the Game Programming forums, part of the General Programming Boards category; Technically I grew up in Citrus Heights, in the last year I moved to Orangevale, I have never seen Norway ...

  1. #16
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Technically I grew up in Citrus Heights, in the last year I moved to Orangevale, I have never seen Norway Drive. But I give directions based on buildings and Carls Jr's instead of streets.
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  2. #17
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    yea and i lived in MI all my life (southeast part, like westland, redford, detroit, livonia, garden city, oak park, just to name a few places i lived...) now i travel around the US as a sound technician for this guy with no arms that sings. i'm starting college next year for programming. but anyway...
    so can you explain to me what the initializerooms() function does exactly? please? i really don't understand it.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  3. #18
    ---
    Join Date
    May 2004
    Posts
    1,379
    linucksrox, my code is not as advanced as the other guys code. My code is in fact using C apart from the bool variables. but that doesnt really matter since its either true or false (0 or 1) which you can implement yourself in C but i was too lazy. As for the atached code. namespace is only used in C++ so that you dont have to type std:: before cin and cout. apart from changing the booleans, and cout to printf etc you can change it to C quite easily.

    BTW: at the beginning of this thread was when i learned about structs. they are not that hard to understand.

  4. #19
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    well as far as BOOL goes there is nothing to really "implement" per say...

    Code:
    int boul=0;
    if(!boul) cout >> "false";
    Any non zero value is true, and zero is false...
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  5. #20
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    I am not sure what to explain with the function InitializeRooms()..
    the purpose of the function itself is to just handle the creation of the rooms without cluttering main(). You could stick all your dynamic room creation code as well in there if you wish.. I didn't, but you could access a txt file or a database to load up your rooms.

    I hate to jabber too much but here it goes...
    The structure and/or purpose of the multidimensional array of the variable "rooms" is to give your map XYZ coordinates... now I don't know if X is north/south or Y north/south is up, but I still utilize them in my code..
    In my code i made X=east/West; Y=north/south; Z=up/down;
    Code:
    rooms[east/west][north/south][up/down].DESC="A Room";
    Now to explain the properties of the ROOM struct...
    I gave each ROOM a number of directional properties that basically indicates that when they are in that room if it's SOUTHWEST property is "true"
    Code:
    rooms[0][1][0].SOUTHWEST=true;
    then they can enter the command for southwest and travel southwest.

    Now positionally what is the difference between [0][1][0] and [0][0][0] and technically that is up to you but in my code...

    Note:below isn't compilable code...
    Code:
     //ROOM 0
      rooms[1][0][0]
     //ROOM 1
     rooms[0][0][0]
     //ROOM 2
     rooms[1][1][0]
     //ROOM 3
     rooms[0][1][0]
    consult this Map Grid(sucky one at that)...
    Map Structure
    23
    01
    and it breaks down to being
    (rooms[1][1][0])(rooms[0][1][0])
    (rooms[1][0][0])(rooms[0][0][0])

    what this induces is the ability for people to use the command for "North" and (depending on how you set your grid up) and all they have to do is...
    Code:
    if(rooms[player_X][player_Y][player_Z].NORTH) player_Y++;
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  6. #21
    Banned
    Join Date
    May 2004
    Posts
    129
    if(!boul) cout >> "false";
    You're using the wrong operator for the stream class, lol.

    int processes faster than a bool, but a bool saves space...whenever I simply need to return just true or false in a program, without saving the result in memory, I use a BOOL which is an unsigned 32 bit integer. Otherwise there is extra processing cycles that needs to be done to process a bool (even though a bool datatype is only 1 byte, which is the smallest amount that can be put at a memory address, a 32bit processor cannot process anything other than 32bits at a time, regardless).

  7. #22
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    vNvNation
    I am not going to change it now just for that....

    each time I read my code I get more and more irritated because I made my variable plural... rooms[0][0][0] when it should be singular room[0][0][0]
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  8. #23
    Banned
    Join Date
    May 2004
    Posts
    129
    It's cool, it's not that big of a deal. I have been programming for several years, and I do stuff like that every now and again.

    i.e I still catch myself doing

    if(variable = true)
    {
    dostuff();
    }

    when it should be

    if(variable == true)
    {
    dostuff();
    }

  9. #24
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    i.e I still catch myself doing

    if(variable = true)
    {
    dostuff();
    }

    when it should be

    if(variable == true)
    {
    dostuff();
    }
    I am usually pretty good with stuff like that. However, ever since I have started programming in Java and C#, I have been doing stuff like:

    if ( pointer != NULL )
    { ...code... }

    instead of:

    if ( pointer ) { ...code... }

    That kind of makes me mad...Java is changing my coding habits...

    if ( pointer ) is so much better....Java and C# should conform to that specification and allow stuff such as that...

    Anyways...I am WAY off topic. Sorry
    My Website

    "Circular logic is good because it is."

  10. #25
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    DavidP,
    That is disgraceful...

    I program in VB as well, and I will either find myself adding semi colons to my VB code... or tacking on THEN's in my IF statements for C....

    or I will try to call a function in C like this...
    StartProcess Param1,Param2;
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  11. #26
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Haha, I know very little about other programming languages but I know what you guys are talking about and I am enjoying this conversation, even though it's way off topic. BillboeBaggins, I think I understand the initializerooms() function now, at least to the extent that what it does is it "sets up" the rooms, so that they exist. That way when you enter South, for example, when it checks for that room's existence, it returns true because now the room is there. Right? And as for the namespaces and bool and all that good stuff, i think I can handle changing that to C. I'm off to learn all about structs right now and then see if I can make a little more sense of that code.

  12. #27
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Hmm...linucksrox, are you making a 2D game or a 3D game? Will it be represented by graphics, whether the graphics be console or gui...or will it be a purely text based representation? I.e you ask for movement it moves within the code and doesn't show it?

    I don't particularely get why you are thinking xyz myself..if your doing 2D representations. Since when can you fly in a dungeon unless it's in water? New one on me....but regaurdless it's good to see that you have a bit more of an understanding of how this works.

    As for your questions about how and why...

    Heres the basic game model:
    Code:
    int main()
    {
      while(1)
      {
    /*do stuff here*/
    }
    if(game_success)
    {
    return 0;
    }
    else
    {
    cout << err(error_code);
    return 1;
    }
    I'll assume you know about the basic game loop yes?

    To make things a little bit more concrete for you..this works like this:

    1)Prepare game
    -Load Maps, Character, Inventory etc in memory.
    2)Display UI
    -Display Maps, Character, etc after load.
    3)Ask for Input.
    -Ask for where to move.
    4)Process Input
    -See if the input is correct and valid.
    5)Do Input
    -Carry out the movement.
    6)Restart Loop.
    -Go back to step 1 or 2.

    Just a recap if you didn't know how this stuff works.

    As far as structs go, just remember that you don't _need_ structs but they are always useful to have. I wouldn't use them in place of arrays though..that can get slightly confusing and harder to work with.

    Personally I wouldn't work with arrays for this. Why might you ask? Well because with something as simple as this you really don't need to unless you are mapping out player position and something else..maybe enemies to. If all your using the array for is mapping out player position and collision it's not needed. My suggestion is to define bounds in which the player can move, without reserving memory like that.

    In retrospect you would be doing this:
    1)Create a struct of X and Y variables to store where the player is in the world.
    2)Ask the player where to move.
    -He says "move south 2 and east 3".
    3)Check Whether movement is correct.
    -Check if "south 2 and east 3" is right. Lets say you have a dungeon of bounds "9 wide and 9 tall". In code this would look like:
    Code:
    if(!player_X > X_MAX)
    {
      /*Proceed In the game*/
    }
    else
    {
      /*Act accordingly as if input was not within correct bounds*/
    }
    You would do the same type of representation for player_Y and Y_MAX.

    In esscense your using less memory and around the same(if not less) processing power to do the same thing. Now this can change if you have more then one sprite to deal with..just a little bit of a thought for you there.

    Arrays can be easy..they can be hard....they can be a good representation in code for your game..but it's not needed in this program.

    Does this kind of answer some questions? Sorry to burst your bubble but in my opinion your doing to much work for so little needed.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  13. #28
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Dare I suggest you use pointers as a way to access other rooms?? This would eliminate the 2D array.

    Code:
    enum Directions {North,South,West,East,Northwest,Northeast,Southeast,Southwest};
    
    struct Room 
    {
      Room *neighbors;
    
    };
    
    ...
    ...
    Both methods work but my preference is using pointers to rooms since there is no need to check connectivity with other rooms. If a pointer is NULL...then there is no room in that direction.

  14. #29
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Tronic:
    Think levels of a house... for Z. Just because it is text doesn't mean everything is ground level.
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  15. #30
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I suppose it would make sense in that instance, guess I wasn't looking at that at the right angle ^^;
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

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

Similar Threads

  1. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  2. Looking for coder: Simple C++ Client / Server game
    By kocho in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 02-04-2006, 01:34 PM
  3. Simple driving game
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 07-06-2004, 09:34 AM
  4. Map for a game
    By GhOsT_DeStRoYeR in forum Game Programming
    Replies: 3
    Last Post: 05-13-2003, 12:52 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM

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