vector of blocks spawn x and y issue

Started by
2 comments, last by Elit3d 7 years, 8 months ago

So I am trying to display a vector of blocks that will be randomly generated x and y eg.

[media]https://i.gyazo.com/782325225d1020bb2a4dec987a534383.png[/media]

however my code is displaying:

[media]https://i.gyazo.com/ce0aa34a4822fa3440deebc28a92427c.gif[/media]

I know why it is doing this but I dont know a better way of dealing with this. Any ideas?

my code:


	for (int i = 0; i < block_vector.size(); i++)
	{
		if (block_vector[i].getPosition().x >= 320)
		{
			currentBlockPos.y = i + 32;
			block_vector[i].setPosition(0, currentBlockPos.y);
		}
		else
		{
			currentBlockPos.x = i * 32;
			block_vector[i].setPosition(currentBlockPos.x, currentBlockPos.y);
		}
	}
Advertisement

It looks like you're trying to set positions every frame (when you draw/update?).

Only set the positions once, e.g. at start-up (or tied to a button press or similar).

You said you wanted to randomly generate x and y, but you never do anything related to random. Here's a reference talking about a simple way to implement random. It also contains links to further reading (<random>).

http://www.cplusplus.com/reference/cstdlib/rand/

I'd start there, and try generating random coordinates.

Something you will run into very quickly is the same location being chosen multiple times (causing overlapping blocks), which you will have to deal with somehow.

This can be either by verifying after generating the coordinates that they aren't in use, or maybe having a separate list of coordinates and randomly flag them first before placing, or some other solution.

If you get stuck, post here -- but I would recommend trying to solve it on your own first.

Hello to all my stalkers.

It looks like you're trying to set positions every frame (when you draw/update?).

Only set the positions once, e.g. at start-up (or tied to a button press or similar).

You said you wanted to randomly generate x and y, but you never do anything related to random. Here's a reference talking about a simple way to implement random. It also contains links to further reading (<random>).

http://www.cplusplus.com/reference/cstdlib/rand/

I'd start there, and try generating random coordinates.

Something you will run into very quickly is the same location being chosen multiple times (causing overlapping blocks), which you will have to deal with somehow.

This can be either by verifying after generating the coordinates that they aren't in use, or maybe having a separate list of coordinates and randomly flag them first before placing, or some other solution.

If you get stuck, post here -- but I would recommend trying to solve it on your own first.

Im going for a downwell sort of thing with my map. My randomisation is handled like so:


bool Game::SpawnSolidBlock()
{
	int blockNum = rand() % 2;
	if (blockNum == 1)
	{
		return true;
	}
	return false;
}

void Game::Update()
{
	if (SpawnSolidBlock() == true)
	{
		block_vector.push_back(block_sprite);
	}

For anyone browsing this later on, I fixed it like so:

in the setup function:


for (int x = 0; x < 10; x++)
	{
		for (int y = 0; y < 10; y++)
		{
			block_sprite[x][y].setTexture(block_texture);

			if (SpawnSolidBlock() == true)
			{
				block_sprite[x][y].setPosition(x * 32, 32 + (y * 64));
				block_vector.push_back(block_sprite[x][y]);
			}
		}
	}

	for (int x = 0; x < 3; x++)
	{
		for (int y = 0; y < 3; y++)
		{
			block2_sprite[x][y].setTexture(block2_texture);

			if (SpawnSolidBlock() == false)
			{
				block2_sprite[x][y].setPosition(x * 32, 0 + (y * 32));
				block_vector.push_back(block2_sprite[x][y]);
			}
		}
	}

then just draw the vector in the draw function.

There are probably better ways to handle the blank tiles but this is my method.

This topic is closed to new replies.

Advertisement