Advertisement

map generation

Started by September 01, 2023 10:36 PM
12 comments, last by LorenzoGatti 1 year, 2 months ago

I am able to draw a 13 row x 19 cols map of grass what I want to do is draw randomly on the map Mountains, Trees, Castles, Knights, Grass and Deserts. here is some stubbed out code.

#include <iostream>
#include <time.h>

using namespace std;

int main()
{
	srand(time(NULL));
	int num = 0;
	for (int i = 1; i <= 19; i++)
	{
		for (int j = 1; j <= 13; j++)
		{
			num = rand()%6+1;
			if (num == 1)
			{
				cout << "M";
			}
			if (num == 2)
			{
				cout << "T";
			}
			if (num == 3)
			{
				cout << "C";
			}
			if (num == 4)
			{
				cout << "K";
			}
			if (num == 5)
			{
				cout << "G";
			}
			if (num == 6)
			{
				cout << "D";
			}
		}
		cout << endl;
	}






	return 0;
} 

Is there.a question?

Advertisement

yes, how do I generate a random map of sprites?

I don't understand the question. Can you show an example of the kind of output you are expecting, perhaps?

In the meantime, here's a version of your code which uses a modern-C++ approach to generating random numbers:

#include <iostream>
#include <random>

int main() {
  std::random_device rd;
  std::mt19937_64 generator(rd());
  std::uniform_int_distribution distribution(0,5);
  
  for (int i = 0; i < 19; ++i) {
    for (int j = 0; j < 13; ++j)
      std::cout << "MTCKGD"[distribution(generator)];
    std::cout << '\n';
  }
}

well, my stubbed-out code works but I want to incorporate it into my game. here is some of my code that draws the grass sprites.

void grass()
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[6]);
	for (float y = 100.0f; y >= -100.0f; y -= 20.0f)
	{
		for (float x = -140.0f; x <= 100.0f; x += 40.0f)
		{
			glBegin(GL_POLYGON);
			glTexCoord3f(0.0f, 0.0f, 0.0f);
			glVertex2f(x, y);
			glTexCoord3f(1.0f, 0.0f, 0.0f);
			glVertex2f(x + 40.0f, y);
			glTexCoord3f(1.0f, 1.0f, 0.0f);
			glVertex2f(x + 40.0f, y - 20.0f);
			glTexCoord3f(0.0f, 1.0f, 0.0f);
			glVertex2f(x, y - 20.0f);
			glEnd();
		}
	}
	glDisable(GL_TEXTURE_2D);
}

I suggest you start thinking in data structures, not just code that prints stuff on the screen.

Make a class that represents a level. Then you can have code to generate a random level, code that can write a level to a file or read it from a file, etc. And then code that can render the level.

Maybe start by writing a prototype for the Level class and go from there.

Advertisement

how do get better at developing data structures?

is this code better?

#include <iostream>
#include <time.h>

using namespace std;

class Level
{
public:
	void draw_Level();
private:
	int num=0;
};
void Level::draw_Level()
{
	srand(time(NULL));
	for (int i = 1; i <= 19; i++)
	{
		for (int j = 1; j <= 13; j++)
		{
			num = rand() % 6 + 1;
			if (num == 1)
			{
				cout << "M";
			}
			if (num == 2)
			{
				cout << "T";
			}
			if (num == 3)
			{
				cout << "C";
			}
			if (num == 4)
			{
				cout << "K";
			}
			if (num == 5)
			{
				cout << "G";
			}
			if (num == 6)
			{
				cout << "D";
			}
		}
		cout << endl;
	}
}

int main()
{
	Level lvl;
	lvl.draw_Level();

	return 0;
}

Not really. The Level class should have data members that describe the level. It shouldn't be the case that the same function creates the level and prints it out.

pbivens67 said:
how do get better at developing data structures?

pbivens67 said:
is this code better?

Your code uses random numbers to paint the fields of the level. That means that if you paint the level to the screen now, and one second later you do it again, most fields of the level will be different the second time (and the third time, and the fourth time and the …).

In a normal game, you will paint the entire screen several times a second. That means your current code will cause that all fields in the level constantly change, even faster than the player can see. For example, it's impossible to plan a path through the level because the fields change faster than a player can walk from one tile to the next.

EDIT: If you wan to see this for yourself, setup a game loop that repeatedly paints the tiles until you close the application (or press a key, or something else).

To fix, you need to always paint the same fields of the level at the same spot. This means you need to remember what field to paint at every tile in the level. To do that, you need to store what field needs to be drawn in a data structure, for example a 2-dimensionsal array. (You may want to start with a 1-dimensional array at first. The level will then be a single row wide, but it allows you to first deal with storing and using the tile information, and worry about more rows in a level at another time.)

The code should be split in an initialization step that creates the array and fills it with values for the fields (ie what tile to paint at each spot in the level). Once initialization is done, your paint code must use the data stored in the array to paint the fields of the level.

This topic is closed to new replies.

Advertisement