I creating a 2d computer game, and I want to put enemies that in that appear at random coordinates. The problem is i'm not sure entirely how to do it. I have the "enemy" in a array of 30,and then i want to put these 30 enemies on a map, with separate random x y coordinates,the map also has a set x y coordinates already, 350 x, 20000 y so i want the random coordinates between the map range e.g. 200 x,4000 y. Also if possible to refresh them when they are destroyed or, moved off screen.
This is what i i have tried already, only the enemies or at least one of them are "destroyed" before they seem to be blitted.

(aEnemy[m].posX2)= rand() % 350 + 1; (rand()%350)+1;
(aEnemy[m].posY2)= rand() % 20000 + 1; (rand()%20000)+1


Please help me.

Recommended Answers

All 10 Replies

Why are you calling rand() twice on each line? That makes no sense.
Besides, the code has nothing to do with your problem.

And you might want to formulate an actual question, because I'm not sure what your problem exactly is.

Why are you calling rand() twice on each line? That makes no sense.
Besides, the code has nothing to do with your problem.

And you might want to formulate an actual question, because I'm not sure what your problem exactly is.

If you look closely it has 2 rand because one is for x coordinate and one for y coordinate.

That is only a small part other code I am working on and i would rather not disclose 2 much for personal reasons. How is this for a question for you. "How you would place an array of objects , at separate random coordinates, between a set range?"

You have 4 rand()s, not 2. Hence my question.
Your approach is pretty much correct - as long as the code is in a for loop.

for (int m=0;m<30;m++)
{
  aEnemy[m].posX2= rand() % 350 + 1;
  aEnemy[m].posY2= rand() % 20000 + 1;
}

You have 4 rand()s, not 2. Hence my question.
Your approach is pretty much correct - as long as the code is in a for loop.

for (int m=0;m<30;m++)
{
  aEnemy[m].posX2= rand() % 350 + 1;
  aEnemy[m].posY2= rand() % 20000 + 1;
}

Oh sorry i see what you mean about the the two rands now, sorry that was left over from something else i was trying, and forgot 2 delete it.

I also tried that particular piece of code and the enemy still gets destroyed before i even saw it all i hear is the explosion sound. And before you ask I tried it with two set coordinates, the enemy is placed and gets destroyed correctly but there is only 1.

I also tried that particular piece of code and the enemy still gets destroyed before i even saw it all i hear is the explosion sound.

That has nothing to do with setting the enemy coordinates - it's an entirely different problem.

the enemy is placed and gets destroyed correctly but there is only 1.

How do you know there's just one when you never get to see any enemies?

That has nothing to do with setting the enemy coordinates - it's an entirely different problem.


How do you know there's just one when you never get to see any enemies?

I do see "one" when I when I use set coordinates, but i need to either do random coordinates for each enemy or be able to do different set coordinates for each enemy.

Well, apparently you're drawing only one enemy or perhaps 30 times the same enemy...
we can't tell if you don't show any code that is related to your problem.

@aranath

In the code sample that you have given, it is possible that more than 1 enemy may be present in the sample spot. Also you want to use srand to randomize the order in which the random numbers are generated :)

@GamerXXX is it fine if 2 enemies appear at the same time

In the code sample that you have given, it is possible that more than 1 enemy may be present in the sample spot. Also you want to use srand to randomize the order in which the random numbers are generated :)

It doesn't matter - the chance that all enemies are put on the same spot is zero for all intents and purposes. This can't be the reason he's just seeing one enemy.
Yes, srand should be called once at the beginning of the program.

I have found all thirty enemies,but i had to use set coordinates, the problem is now is that they are all in a straight line, yes i was using srand before as you can see in the code below. So how do i make all 30 of this little in random places. (Also if this helps i'm not quite sure but when i set the score up,being 10 points for a destroyed enemy i finally thought i had the score right but, the guys total seem to add up to 10 whether i kill 1 or 30 its still ten points. Is it a possibility that they all seem to be 1 enemy?

srand(GetTickCount());
	(aEnemy[m].posX2)=(300,100);
	(aEnemy[m].posY2)=(m*10);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.