Thread: how to create/use a simple map in a game

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    how to create/use a simple map in a game

    I know this is very original so don't steal my idea... I'm trying to make a text based dungeon adventure game, my very first, but I was just having trouble trying to figure out a mapping system that the program can keep track of to display my coordinates and stuff. I have a very limited knowledge of C right now, I know as much as working with files, i know how to properly use multidimensional arrays, looping statements and that stuff, i just don't know a good way to keep track of my coordinates... i'd even appreciate a link to somewhere else that might explain a little bit, i don't mind having to think by the way i was thinking of trying a 2 dimensional array but then i was unsure of how to keep track of where i was in that array... what tells me where i am in that array? change one of the values in the array different from all the rest? have another variable pointing to a coordinate in the array? thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Do you know what structures are? I will assume you do, if you do not you can reply back and I can elaborate. Basically you can define a Room struct and store information that would be important for that room. For instance, you may want to know the X,Y coordinate for that room. You may also want to keep track of the exits for that room. You can also have a Player struct that will keep track of his current position. You initialize it to whatever you want and then if you have a function called MoveNorth then it could take as a parameter a pointer to a Player struct and update his position. Then you can have a multi-dimensional array of type Room. You must make sure you properly line up exits in the rooms though.

    If this is too advanced let me know. I'm not sure what your current level is as far as the C language. Just keep letting the questions flow and we'll keep helping.

    Good luck and don't get discouraged.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    I'm sort of understanding what you're saying, but I haven't learned all about structures yet, I don't even know how to use them. But I have seen a few, and it kind of makes sense. So ok, you have a struct for each room with information about each room, and then you have a function that can update any value for any room struct right? Then there's another struct with player info, with inventory and location and what not. And a function that can modify that info. right? or is it several functions that modify different parts of that struct? like a function that modifies inventory of player struct, and another function that updates location of player struct? I'd guess you can pass the whole struct or a pointer to it to the function so the function can modify the entire contents of the player struct, which would make things a little easier I think.
    Ok if what I said was right or partially right, then what about location checking? Or "collision"? Once I have my player location set, which will be a default room when the game starts, what happens? you display the room and info, exits, etc, and then wait for input... then check input, perform inputted task(s)... i guess i'll have to learn about structs.
    But I have a question, is there a way to just use an array of some sort just for a map? say I just want to create a very simple game for now that doesn't include inventory, just a base so i can understand the concept of the map system first, then move on to a struct later. i figured maybe use a 2 dimensional int array, but how exactly does that work? what data does the array contain, and how do you use it to tell your coordinates? is there another variable involved somehow? i looked at some game development sites and couldn't find any information about this kind of thing, it says beginner programming and then says "start out with tetris" then no information, then it says "now try doing breakout" it gives no explanations or even links to resources. and i sure couldn't find any. but i'm sure i'm not at that level yet to even think about trying to make a tetris clone...
    One final thing, I don't want to just copy someone else's source code and use that because i want to know how it actually works and be able to create it myself. otherwise i have no pride haha

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, what you were saying about the structures is partially right. I would advise studying up on them first, however. Do you know any C++ ?

    Let's just skip that stuff and concentrate on the second part of your reply. A very simple game. You can have a 2D array of ints for example. You can have #define's or whatever for MAP_SIZE_X , MAP_SIZE_Y or whatever and then declare your 2D array using those dimensions. You could just have 2 integers ( x and y ) to keep track of player position in that array. Then when the user wanted to move somewhere you would check to see if it's a valid location in the array. If the integer value stored at that location in the array is a 1 then that may indicate a wall. 0 may indicate passible tiles. Again, you can use #define's to make it easier.

    Code:
    #define WALKABLE  0
    #define WALL          1
    // Etc
    Then you could pass in to the function the player's desired x and y and it would look up in the array to see what the value was. Also you would need to check boundary conditions. For instance, you're on the left side of array and the player tries to move left. Either you wrap around to the other side of the array or you don't allow moving off the edge. Hopefully that can get you moving in the right direction.

    As far as structs, just start reading some tutorials and get comfortable with them first.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Hey thanks, that definitely helps, I wasn't sure what the point of even having an array was at first. I read where you said make an array of x, y dimensions, and then have a variable x and variable y to keep track of your coordinates. So then I'm thinking, "ok then what's the point of the array? why don't i just use the x and y variables and keep track of my position?" but now I understand the array lets you set up tiles or boundaries or whatever else you want to set up, and it probably makes things a whole lot easier to keep track of. I'll start of with that idea and see if I can get something out of it, then I'll read up on structs and figure them out (which by the way no i don't know any more of C++ programming than I do C, and right now I'd rather concentrate on just C until I at least finish this book). I'm on chapter 13 in my book and structures are in chapter 14 so i'm almost there anyway. just another week or two... So you think structs are better for this sort of thing? Anyway off I go to see if I can get this idea working in a program.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  6. #6
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Ok I give up... haha I can't figure out how to do this. My "simple" program turned into a complicated mess. I don't know what I need a function for and what I don't. I have a main menu that asks for 1) Start Game, 2) Options, and 3) Quit. 3 quits. 2 does nothing but display "options" right now, and 1 starts the game. i have a function called gamestart that will run the game once you select option 1. it's declared
    Code:
    void gamestart(void)
    and i'm not sure what to do with it. i'm lost totally... i have so many questions... ok my x and y coordinates, should they be global? or sould they be passed from some function that checks the change in location? where do i go from gamestart()? do i need a separate function to display the contents of each room, or just one that displays room content depending on what room you're in?
    here's what i'm thinking: an input function to retrieve your action, and verify it before passing the value back to the calling function, which is gamestart(). then gamestart can send that input right to another function that performs the action. this way the input has to be valid before it will be passed to the performaction() function. my only actions will be moving N, S, E, or W. so in the performaction() function, do i change my x or y coordinates, and then send them to another function to check them against the map array? or do i just change them and go back to gamestart() and then call from there the checklocationagainstmap() function? anyway after you check the map, what? call a function to display the room you're in if the location change was successful? and keep asking for input? this kinda sounds right but i just can't seem to get it into code... or am i thinking too hard? does anyone know of any text based dungeon game tutorials i could look at? for now i'm gonna take a little break from this and come back to it later. some psuedocode may be useful...
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  7. #7
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Okie... I know what you are trying to do and my code has a decent, not great, but decent application to what you are trying to do. It runs and compiles, but unfortunately it doesn't handle the direction commands you give very well yet. I know it is a rook mistake but at the moment I am drawing a blank... so review it look at it... twist it... and if you fix it so it works correctly please re post it...

    Snippet...
    Code:
    ROOM rooms[ROOMS_WIDE][ROOMS_TALL][ROOMS_ELEVATION];
    int player_X=0;     //Horizontal - Wide                 - FROM RIGHT
    int player_Y=0;     //Vertical - Tall                   - FROM BOTTOM
    int player_Z=0;     //Elevation (of sorts) - Elevation  - FROM GROUND
    Safe Codin.


    PS
    I know I prolly screwed up X Y Z and their meanings and the wording sucks.. buthey it is only a demo...
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by BillBoeBaggins
    Okie... I know what you are trying to do and my code has a decent, not great, but decent application to what you are trying to do. It runs and compiles, but unfortunately it doesn't handle the direction commands you give very well yet. I know it is a rook mistake but at the moment I am drawing a blank... so review it look at it... twist it... and if you fix it so it works correctly please re post it...

    Snippet...
    Code:
    ROOM rooms[ROOMS_WIDE][ROOMS_TALL][ROOMS_ELEVATION];
    int player_X=0;     //Horizontal - Wide                 - FROM RIGHT
    int player_Y=0;     //Vertical - Tall                   - FROM BOTTOM
    int player_Z=0;     //Elevation (of sorts) - Elevation  - FROM GROUND
    Safe Codin.


    PS
    I know I prolly screwed up X Y Z and their meanings and the wording sucks.. buthey it is only a demo...
    Just looking over your code I see a few things. You don't return values for InitializeRooms or GameStart.

    Also in your GameStart code you have 'n' case as quit? I thought it was 'q' ? And your if checks are bad because I think you copy and pasted the code and forgot to change it.

    Code:
    if(rooms[player_X+1][player_Y][player_Z].SOUTHWEST)
    Every case has that SOUTHWEST check. Probably not what you intended.

    There might be more errors but those are just the ones I initially saw while glancing through it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    I hate to turn this into a me thing, but thanks a lot MrWizard. Those are some dumb mistakes that I didn't intend as you said. Will fix and repost. The 'n' is because in my direction scheme 'q' turned into NorthWest.

    Safe Codin

    I attached the new version, the functions do return stuff now, it doesn't respond to what they return but it does return stuff. The directions work... such dumb mistakes... well have fun and I hope it is semi useful.
    Last edited by BillBoeBaggins; 06-02-2004 at 10:42 PM.
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    i got the basis for mine to work.
    Thanks MrWizard

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define WALL 1
    #define NOWALL 0
    
    void checkexits();
        int map[10][10] = {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
                           1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                           1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
        bool NORTH = true;
        bool SOUTH = true;
        bool EAST = true;
        bool WEST = true;
    
    struct
       {
            int x;
            int y;
            char name[10];
    
       } PERSON = {1,1};
    
    int main(void)
    {
        puts("What is your name?\n");
        scanf(" %s",PERSON.name);
        checkexits();
        if (NORTH == true)
            {printf("North ");}
        if (SOUTH == true)
            {printf("South ");}
        if (EAST == true)
            {printf("East ");}
        if (WEST == true)
            {printf("West ");}
        getch();
        return 0;
    }
    
    void checkexits()
    {
        if (map [PERSON.x-1][PERSON.y] == WALL)
            NORTH = false;
        else {true;}
    
        if (map [PERSON.x+1][PERSON.y] == WALL)
            SOUTH = false;
        else {true;}
    
        if (map [PERSON.x][PERSON.y-1] == WALL)
            WEST = false;
        else {true;}
    
        if (map [PERSON.x][PERSON.y+1] == WALL)
            EAST = false;
        else {true;}
    }

  11. #11
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    I'll look at the code and see if i can make any sense of it, but the problem i'm running into now is 1) this is C++ code and I'm working with C, and 2) I haven't learned how to use structs yet, but from all your responses it seems like I'm gonna have to learn about them before i even think about making a game that uses any kind of map. maybe i'll learn about structs and then change this code to c and see if i can get the same concept to work...
    and wait, why are you mixing <iostream> with <stdio.h>? shouldn't you choose one or the other? and if you're working in c++ shouldn't you choose <iostream>? plus there don't seem to be any functions from stdio.h so it's unnecessary
    Last edited by linucksrox; 06-03-2004 at 03:29 PM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  12. #12
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Your probably right on all accounts as far as include libraries go. I don't even attempt to remember which header includes prototype which functions... I only seem to worry about it when the compiler yells at me.

    Now I couldn't be quoted on the exact coding differences between straight C and C++... but from my code I think COUT and CIN are the only C++ keywords I am using. Feel free to alternate them or any of the code with their straight C equivalent.

    Safe Codin.
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  13. #13
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    haha, i'm with you as far as the headers, but actually as far as the differences between c and c++ there are more than just the cin and cout. there's namespace, c doesn't have namespaces. then there's the bool keyword that c doesn't support. i know it's not a big deal.
    what i wanna know is what exactly the program is looking for as far as input... i tried entering 'n' to go north and it went into an infinite loop. then i used numbers and it worked fine. also what exactly is going on in the initializerooms() function? i guess since i dont' understand structures yet i wouldn't understand what's happening.
    oh and thanks for all the help, i'm learning slowly but surely. i'm gonna take the basic concepts of what's happening and use that to do my own thing
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  14. #14
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Ok I attached it again for the 500000000th time... the function CIN fails when you pass it a character value, when it is expecting a numeric value. So I changed it...
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sorry to interrupt but:

    BillBoeBaggins
    Registered User
    Join Date: Oct 2003
    Location: Orangevale California
    Posts: 73
    I grew up in orangevale. On top of Norway Drive. I miss that place

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, 02: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