Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a platform flash game which includes a level with just two backgrounds which are flipped side by side so that a looping background effect is created. In this I have to include Obstacles. Basically, when I designed the level, that was neat and good. But now I want to make the obstacles come randomly . Another factor is that since the player character in the platformer always keeps on running, if two obstacles come almost close together, it results in an impossible condition to win the game. So i need to avoid that too, without losing the factor of making the player think that the game is random.

share|improve this question
up vote 4 down vote accepted

What you are basically talking about is Procedural Content Generation. Roughly, you want to use a randomizing algorithm to make a "map" of upcoming content, and then run a check over that map to look for problems like "This gap is too small and makes the level impossible."

I found, of all things, a wiki dedicated to the topic, which also has some code samples:

You might also check out the roguelike development resources here:

The techniques applied to roguelikes can usually apply to most 2D games.

share|improve this answer
+1 for the pcg wiki – Vishnu Oct 18 '10 at 6:12

Here is one way to do it.

Set a minimum distance that things can spawn at, so things can always be winnable, but make this distance almost impossible to win.

Have a difficulty variable that that starts out high for easy, and lowers as it gets harder.

Set the position of the next object at the minimum distance + difficulty variable + a small random number.

This will ensure that stuff spawns randomly, but it just has some rules to follow before it spawns.

There are other more complex way to do it, but for what you are describing I think this is the best.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.