#A 2d game map
113 messages · Page 1 of 1 (latest)
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
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
you mean like you close the exe and open it again?
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.)
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
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
class Tile
{
int x;
int y;
void onPlayerInterraction() {}
}
class GameLevel
{
private:
std::vector<Tile> tiles;
};
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
Exactly, it's what l would like to do
well, ok, so what's the problem with doing it?
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
ignoring the offstreen stuff, do you know how you would make one screen game like your screenshot above?
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
just moving a little character around the console doing stuff?
a zoom on that specific part
since it is just a simple text game, you can just have a big two dimensional array containing your map and print to the console the part that you are in.
or you can store it in "chunks" similar to minecraft
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
#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;
}
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;
}
It should be something like this right?
Ok, it make sense
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
and that's where I start to get difficulty
Because on a fixed one I'm kinda fine, but the dynamically growing is ununderstandable for me
the easiest way to do that is something like mario or captain comic
then you want to do this
or change std::vector<Tile> to the array you had
so now you have an infinate numbere of arrays
I don't understand this :x
have you been introduced to classes yet, or have some understanding of objects?
Well, a bit yes. But I don't understand what does these two classes do
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
Okay perfect,but practically it didn't change anything right?
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
Yes yes, fine it will make the code way more clear and understandable
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
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
those are just examples of how you could do it, do you know what pointers are?
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
Ok so that logically fits with my mind
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?
Ok, you mean for example to creates obstacles, enemies etc before going in these "rooms" right?
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
this is probably the way to do it for now
cos that seemed to vibe with you the most
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
maybe, classes are used for everything, everything down to attoms, individual characters can be abstracted down to classes and objects
And also the use of vector, l understand it theorically but no on how to use it
class Character
{
char c{};
public:
void setToRandomCharacter() {}
}
for example xD
its just like your array you where using, but it has the ability to change size. get bigger and smaller at runtime
that is all its for, storing an arbitary length of things
like a list of enemies, or photos for your photo album or whatever
Ok fine
So the practical part to switch my array into this vector? How is it possible?
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....
here is an example of them:
https://www.geeksforgeeks.org/2d-vector-in-cpp-with-user-defined-size/
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
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
So that's it right?
sorry, i need to go to bed now, hope that has been enough food for thought to get you going
what i showed in the example above with the ints? yes
ta, night
@sweet mantle Has your question been resolved? If so, run !solved :)
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.
This question thread is being automatically marked as stale.
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
I think you mean to serialize
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
yeah you need to save the instance of the room when you leave and then load it when you come back. checkout serializing
Uh, and I can't do it in another way?
f5
how do you make stuff like that
When they leave, why don’t you update the array
I really don’t know what I’m taking about but yeah