Jump to content
  • Advertisement
Sign in to follow this  
Cymbalistic1

Random enemies

This topic is 4145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm slowly making progress on my 2D side scroller. Now, I'm to a point where I want to have random enemies come onto the screen. With 3 different enemies, how do I make them come on the screen randomly?

Share this post


Link to post
Share on other sites
Advertisement
Are you asking 1) how to choose between one of the three enemies at random, or 2) how to randomly add an enemy to the screen, or 1+2) both?

1) To decide which enemy to add, it's as simply as:
switch (rand( ) % 3)
{
case 0:
// add enemy 1
break;
case 1:
// add enemy 2
break;
case 2:
default:
// add enemy 3
}

This is assuming C++, and without more information.

2) To decided when to add a new enemy, you have several options. You could add one every five seconds, but that wouldn't be very random. So, you could add/subtract some amount of time to/from this constant time to give it some randomness. Alternatively, you could do something along the lines of:
if (rand( ) % 500 < 10)
// add enemy
Varying the numbers would change the results, obviously, and could be tweaked to your needs.

Edit: I should clarify about the rand( ) function. Many languages have support for it, and it returns a "random" integer between 0 and some large number (don't worry about what it is for now). This number isn't truly random, which means that the sequence of numbers the function returns can be repeated. To keep this from happening, you must seed the random function once per program execution with a unique number. A common number used for this purpose is the current time. In C++, this would be accomplished with the following:
#include <ctime>

// somewhere in main
srand(time(NULL)); // seed the random function with the current time
// don't do this again in your program!

// whenever you want a random number:
int randomInteger = rand( );

// whenever you want a random number < 10
randomInteger = rand( ) % 10;

// whenever you want a random number such that 25 <= number < 35:
randomInteger = rand( ) % 10 + 25;

Random floats are another topic, but you won't need them for now. Also, I should note that there are times when you'd want to re-seed the random function, but that's for reproducing results, which is something you definitely don't want to happen in this case.

Share this post


Link to post
Share on other sites
I would use a function that takes an integer as a "enemy ID" and use that to display the monster according to the random number. The ID would probably an array number, like in a struct that describes the enemies.

addEnemy(rand() % 3);

This is better than a switch statement and you wouldn't have to rewrite the code in your addEnemy function when you decide to add a new enemy.

Share this post


Link to post
Share on other sites
Alrighty, thanks for the responses. All very useful information. But now I need to know one more thing. The actual act of adding the enemy is what I don't know how to do now. How do I keep track of exclusive variables for the enemies (energy, position, dead/alive)?

Share this post


Link to post
Share on other sites
An enemy is a good candidate for a class.

The simplest way to do it is to instantiate a random enemy object inside your addEnemy() method. Then, add the enemies to a vector so you can keep track of them. The vector needs to be accessible to all the methods that update enemy locations and render them, obviously, so it might be a good candidate for a global or member variable.

Share this post


Link to post
Share on other sites
(Assuming C++)

If you have an STL container holding a collection of (handles to) enemies then you can use random_sample to generate enemies at random from a list of possibilities. If the possible enemies needs to vary depending on some condition (like the player never meets ghosts in the day time or wolves only live in forests, something like that) then you would add or remove enemies from the source collection to reflect the generation rules.

L
-

Share this post


Link to post
Share on other sites
Ok, I think I understand now. Thanks for all the help. I think I'll try the object oriented approach and have each enemy be a struct. Thats just for now, though. Since its my first game I'll just keep it simple. Thanks a bunch.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!