Sign in to follow this  
Elit3d

vector of blocks spawn x and y issue

Recommended Posts

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);
		}
	}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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);
	}

Share this post


Link to post
Share on other sites

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.

Edited by Elit3d

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this