Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm creating a simple top-scrolling racing game. The concept is quite simple - the player controls the car, which goes on a dynamically generated field with square blocks. Once the car hits a car, the game ends.

My first implementation was simply to put a few random blocks in each row being generated on the top of the screen, but this does lead to maps where going through is impossible.

What is the best algorithm for generating such type maps? My platform of choice is XNA and C#, if that's important.

EDIT: Here's an illustration: (pic)

In this case, there's no possible way for the player to pass.

share|improve this question

5 Answers 5

up vote 4 down vote accepted

You will have to do some pathfinding. I'd suggest something like this: Choose grace distance, the shorter, the harder the game may be. When you add a new row fist find out in what places the player may be after travelling the grace distance. You can use an A* search algorithm to find the group of points efficiently, then you remove any point that doesn't have a path through the currently generated rows from the group. Now you generate a new row and check that all the points in the group have a path all the way through the new row, if some of them doesn't you will have to regenerate the row.

The pathfinding is very simple in this case, as the car can only occupy one of 8 different positions per row, so your pathfinding will never have to branch beyond 8 positions. You simply incrementally check what positions the car can reach in the next row, from the positions it may occupy in the previous row.

Strike the A* reference, I better just refer to Dijkstra, almost the same but seems easier to understand.

share|improve this answer
pathfinding..? doesn't that sound awfully overkill? –  falstro Nov 22 '10 at 10:09
Depending on the game it may not have to be very complicated, but by definition this is a pathfinding problem. –  eBusiness Nov 22 '10 at 10:43
Please see the image in the update - the game is incredibly simple, so there aren't any physics really. –  mcm69 Nov 22 '10 at 12:10
I agree with eBusiness. Plus, if this is a "learning" game, may as well start learning pathfinding. –  alphadogg Nov 22 '10 at 19:49
I agree on the learning stuff, and maybe I'm confused here. So let me ask you this, would you consider maze generation a "path finding problem"? If so, I agree. The point is, if you're free to define the layout, rather than finding a way through an existing one, the problems can be vastly simplified. See Richards solution, which is an excellent demonstration of this. –  falstro Nov 23 '10 at 18:49

Rather than try to generate a field to play in that you need to check to keep valid for play, instead generate a path the vehicle can follow, then randomly add obstructions to the parts of the map not filled in by the vehicle path generated.

1D Brownian motion: (http://classes.yale.edu/fractals/randfrac/brownian/Brownian3.html)

If you use low frequency brownian motion for the car path, filling in cells as not-obstructable, you can then simply have a low probability function for selecting whether each non filled cell is occupied/obstructed.

This will always lead to a game that can be played, but it can also be tuned to have sparse or dense obstructions patterns. You could even have the density function be brownian too ;)

share|improve this answer
This is definitely the best solution, +1. –  falstro Nov 23 '10 at 18:55
Yes, but You would need some more controll over the signal produced by this motion ... +1 –  user712092 Jul 4 '11 at 15:03

Just use the last 'top-row' as reference, and generate a valid row from there (for each block, pick one at random, if it's invalid, do it again). Now valid is something you'll have to define, but my guess is 'valid' would be reachable from any of the three neighboring blocks from the reference row.

share|improve this answer

Quick idea written during compile-time(curse you c++):

When placing a block make sure that the top half of the map has three columns next to each other which are free. Do that by summing up the number of black squares in each column. Then iterate through the column sums three at a time.

First check will sum column 0 1 2.

If sum equals 0 tag chunk 0 as free.

Next check will sum 1 2 3.

If sum equals 0 tag chunk 1 as free

After that 2 3 4

If sum equals 0 tag chunk 2 as free.


After placing a wall check that at least one of your previously tagged chunks are empty. Redo procedure until at least one is left empty.

This method leaves you with an option for easy and hard mode where easy mode checks entire map for free spaces instead of just top half. Also, by setting an upper limit of wanted empty chunks you can increase the difficulty. Hard diff would mean that one or two chunks are allowed to be free at any time.

Hope you understand what I mean.

share|improve this answer

i know im really really REALLY late but wouldnt the simplest way to do this be to change the car so that its only 1 block thick? leaving only an incredibly small chance that the game will give you something impassable?

share|improve this answer
You suggest to change the gameplay rather than simply solving the problem at hand. Also, if the game has to stay challenging you'd have to use more blocks, and thus increase the chance of running into a dead end. –  eBusiness Jan 28 '12 at 11:37

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.