#A 2d game map

113 messages · Page 1 of 1 (latest)

sweet mantle
#

Hello guys, l need an advice on how to do a 2D map game like this that while going on doesn't get cancelled (so later on for example u can go back and find everything as it was). I thought about arrays but not sure on how to do it

hallow belfryBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#

@sweet mantle

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

urban herald
#

you mean like you close the exe and open it again?

sweet mantle
#

No no, while the game is open, u go like to the right going on in the level. But maybe after a bit u want to turn back for some reason and find the map perfectly as it was (for example the obstacles etc.)

urban herald
#

you can create some objects like "GameLevel".
each GameLevel object has the coordernates of each tile you want to display on the screen

#

some of these tiles can be "doors" which when the player walks over, can load a different game level

#

same in the new level, going back to the old one

sweet mantle
#

Well l thought that l wanted to do something that creates the map dynamically in the moment. Like a level that while keeping going gets always harder, never changing/loading other levels

urban herald
#
class Tile
{
  int x;
  int y;
  void onPlayerInterraction() {}
}
class GameLevel
{
private:
std::vector<Tile> tiles;
};
livid kettle
#

well, you will have to keep the contents of your map in memory somehow

#

unless the map is generated procedurally and so you have a way to re-generate parts of it

#

even then, you'd probably have to store information about changes the player has caused to the map

sweet mantle
livid kettle
#

well, ok, so what's the problem with doing it?

sweet mantle
#

My biggest problem (as a newbie) is to understand at first how to create this map, should l use an array that graphically create smtn like this one in the screen?

#

I don't even talk about the game itself, just the graphical part of the map

#

And then how to keep like the gone part of map so going back refind everything as it was

urban herald
#

ignoring the offstreen stuff, do you know how you would make one screen game like your screenshot above?

sweet mantle
#

Like the screen that l posted, the map in my mind should just scroll without being cancelled and the visible part should be only the one where I am, but just because (I would call it) a zoom on that specific part

urban herald
#

just moving a little character around the console doing stuff?

urban herald
#

or, like the example i gave above, but that was intended for a game like captain comic, where when you scroll off the screen, it loads a different screen

#

or, you can remember your position on a virtual board, have a std::vector<Things>
and these things know their x and y coordernates, if they are within the screen on your board, then you display them

sweet mantle
#


#include <conio.h>
#include <windows.h>
#include <iostream>

const int WIDTH = 20;
const int HEIGHT = 20;

char board[WIDTH][HEIGHT];

int main() {
    for (int i = 0; i < WIDTH; i++) {
        for (int j = 0; j < HEIGHT; j++) {
            board[i][j] = '.';
        }
    }

    int playerX = WIDTH / 2;
    int playerY = HEIGHT / 2;
    board[playerX][playerY] = 'P';

    while (true) {
        system("cls");

        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                std::cout << board[i][j];
            }
            std::cout << std::endl;
        }

        if (_kbhit()) {
            char input = _getch();
            if (input == 'a') {
                playerX--;
            }
            else if (input == 'd') {
                playerX++;
            }
            else if (input == 'w') {
                playerY--;
            }
            else if (input == 's') {
                playerY++;
            }
            board[playerY][playerX] = 'P';
        }

        Sleep(100);
    }

    return 0;
}

urban herald
#
cpp

#include <conio.h>
#include <windows.h>
#include <iostream>

const int WIDTH = 20;
const int HEIGHT = 20;

char board[WIDTH][HEIGHT];

int main() {
    for (int i = 0; i < WIDTH; i++) {
        for (int j = 0; j < HEIGHT; j++) {
            board[i][j] = '.';
        }
    }

    int playerX = WIDTH / 2;
    int playerY = HEIGHT / 2;
    board[playerX][playerY] = 'P';

    while (true) {
        system("cls");

        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                std::cout << board[i][j];
            }
            std::cout << std::endl;
        }

        if (_kbhit()) {
            char input = _getch();
            if (input == 'a') {
                playerX--;
            }
            else if (input == 'd') {
                playerX++;
            }
            else if (input == 'w') {
                playerY--;
            }
            else if (input == 's') {
                playerY++;
            }
            board[playerY][playerX] = 'P';
        }

        Sleep(100);
    }

    return 0;
}
sweet mantle
urban herald
#

that would be one way to do it yes, but this board is a fixed size, it will never grow dynamically or procedually, but you can just change it to a vector which is easy enough if you ever wanted to

sweet mantle
#

Because on a fixed one I'm kinda fine, but the dynamically growing is ununderstandable for me

urban herald
#

the easiest way to do that is something like mario or captain comic

sweet mantle
#

Exactly, it should be like Mario

#

The perfect example

urban herald
#

or change std::vector<Tile> to the array you had

#

so now you have an infinate numbere of arrays

sweet mantle
urban herald
#

have you been introduced to classes yet, or have some understanding of objects?

sweet mantle
#

Well, a bit yes. But I don't understand what does these two classes do

urban herald
#

let me change it a bit to fit your code a bit

#
#include <conio.h>
#include <windows.h>
#include <iostream>

const int WIDTH = 20;
const int HEIGHT = 20;

struct GameScreen
{
    char board[WIDTH][HEIGHT];
};

int main() {
    GameScreen screen{};
    for (int i = 0; i < WIDTH; i++) {
        for (int j = 0; j < HEIGHT; j++) {
            screen.board[i][j] = '.';
        }
    }

    int playerX = WIDTH / 2;
    int playerY = HEIGHT / 2;
    screen.board[playerX][playerY] = 'P';

    while (true) {
        system("cls");

        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                std::cout << screen.board[i][j];
            }
            std::cout << std::endl;
        }

        if (_kbhit()) {
            char input = _getch();
            if (input == 'a') {
                playerX--;
            }
            else if (input == 'd') {
                playerX++;
            }
            else if (input == 'w') {
                playerY--;
            }
            else if (input == 's') {
                playerY++;
            }
            screen.board[playerY][playerX] = 'P';
        }

        Sleep(100);
    }

    return 0;
}
#

ok, with that slight modification, you see how the board, is now within a class

#

there is only one board still like before, 20*20 cells that are unchanging

sweet mantle
#

Okay perfect,but practically it didn't change anything right?

urban herald
#

your right

#

but now you have a class or struct, you can give it methods, which work specifically for that board and no others if you have multiple

#

for example:

struct GameScreen
{
    char board[WIDTH][HEIGHT];
    void drawToScreen() {}
    void playerWalkedOffScreen() {}
};
#

that for loop that draws the board, can be put in the drawToScreen method

#

if the player walks off the screen to the left, you can run the playerWalkedOffTheScreen() method

sweet mantle
#

Yes yes, fine it will make the code way more clear and understandable

urban herald
#

now, you can have this:

int main() {
    std::vector<GameScreen> screens{};
 ...
#

and you can have each screen know which is to the left, right, up down of them:

struct GameScreen
{
  GameScreen* theLeftScreen{};
  GameScreen* theRightScreen{};
  ...etc
}
#

so when your player "walks off the screen"
you can select the correct one to use and display

#

this also means, if there is no screen left for example yet, you can make one on the fly

#

and you can tell this screen, that to the right, is the GameScreen they just came from

sweet mantle
#

Uhh

#

So

#

Like I have this vector and to keep having in monitored I use the "theLeftScreen{}" and the right one

#

No nvm I don't get it

urban herald
#

those are just examples of how you could do it, do you know what pointers are?

sweet mantle
#

why I can't just move through this vector

#

Yes

urban herald
#

you can, if you want you dont have to have the GameScreen contain pointers to the screen to the left, right, up etc. you can just go up/down/left/right in a vector of GameScreens if you want

sweet mantle
#

Ok so that logically fits with my mind

urban herald
#

the benefit of having the gamescreen have pointers to what is left or right of them is methods in these objects know about them

#

maybe "rooms" is a better thing to call these GameScreens

#

they have a "map of the room" (which is your array[20][20]
and they have "doors" which are your pointers to other rooms

#

does that make sence?

sweet mantle
#

Ok, you mean for example to creates obstacles, enemies etc before going in these "rooms" right?

urban herald
#

ye, if you tell a room you want to go through the left door, you can have the room your in create that room

#
leftRoom = new Room(this);
#

for example

#

there are lots of ways to do what you want to do with different paradims.
the rooms with functions on them is how i do things.
you seem to be very procedual at the moment, not using classes, so perhaps this is confusing your understanding of it

urban herald
#

cos that seemed to vibe with you the most

sweet mantle
#

Well I mean maybe because I feel classes like a use for different things. I mean feel classes like "enemies" "weapons" "characters" etc (for example in this game) so using classes also to create the map is getting me confused 😄

#

But probably because I have no idea on how to do it

urban herald
#

maybe, classes are used for everything, everything down to attoms, individual characters can be abstracted down to classes and objects

sweet mantle
#

And also the use of vector, l understand it theorically but no on how to use it

urban herald
#
class Character
{
  char c{};
public:
  void setToRandomCharacter() {}
}
#

for example xD

urban herald
#

that is all its for, storing an arbitary length of things

#

like a list of enemies, or photos for your photo album or whatever

sweet mantle
#

Ok fine

#

So the practical part to switch my array into this vector? How is it possible?

urban herald
#

ah, it would be a 2 dimensional array, while the same idea, looks harder

#

perhaps stick with your board size for now, but use the vector for storing the Room or GameBoard objects like we discussed

#

oh, but you would have a screen up, down left right either way, so you still need to learn about 2d vectors....

#
std::vector<std::vector<int>> vect
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
#

you still access it the same way as you did with your array:

vec[0][0]; //would be the top left for example
urban herald
#

sorry, i need to go to bed now, hope that has been enough food for thought to get you going

urban herald
sweet mantle
#

Thank you, you get me a lot of stuff to learn about

#

Good night 🙂

urban herald
#

ta, night

hallow belfryBOT
#

@sweet mantle Has your question been resolved? If so, run !solved :)

hallow belfryBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

hallow belfryBOT
#

This question thread is being automatically marked as stale.

sweet mantle
#

I tried a bit, watched some tutorials and finally arrived to the final point that the best thing to do is work with pointers and prevRoom and nextRoom
I mean l realized it in my mind and it's perfect

#

But now have no idea on how create these 2 istances "prevRoom" and "nextRoom" and how to keep them in memory, I don't know if you get what I mean
Because I have to keep these rooms in memory, like a chain but can't make it in practice :p

fallow canopy
#

I think you mean to serialize

sweet mantle
#

Well I don't know, the idea is to have a pointer to these "rooms" like you go on in the game but if you want to come back to the room before to reload it

#

I don't know if I get you the idea

fallow canopy
#

yeah you need to save the instance of the room when you leave and then load it when you come back. checkout serializing

sweet mantle
#

Uh, and I can't do it in another way?

sweet mantle
#

f5

warped dirge
warped dirge
#

I really don’t know what I’m taking about but yeah