3
\$\begingroup\$

I'm trying to make a game like Crossy Road:

Screenshot of Crossy Road

In this game, you control the character(the frog in this case) to go across the streets, and you have to prevent your character from being hit by a car.

There are also "footpaths". On the footpaths there are trees act as stationary obstacles and you can't go through them.

I tried to make a similar game but I have a problem:

Screenshot showing two lanes of obstacles completely blocking the player

The obstacles prevent the player from getting further. What can I do to make sure that the player always has at least one way to pass through?

\$\endgroup\$

3 Answers 3

4
\$\begingroup\$

For the footpaths, you want to ensure there's always at least one gap / clearing all the way through to the far side. Some ways you can do this:

  1. Guess-and-Check

    Generate the trees/rocks randomly, then try to flood-fill the passable space from the near side. If your flood fill reaches the far side, then there's at least one passable route. If not...

    • Try again from scratch! This approach is easy, but relies on your randomization routine already having a good probability of making a passable result by luck. If passable results are rare, you might spend a lot of time re-generating only to fail.

    • Fix it: carve out one or more solution paths somewhere, either picking a random column to bulldoze to passable space, or selectively deleting an obstacle (eg at the thinnest point)

  2. Solution First

    Start by picking one or more solution paths - say taking a few biased random walks from the passable tiles from the previous footpath, until they reach all the way to the far side of this footpath. Mark each tile these random paths touch as "reserved".

    Now, randomly fill some/all of the non-reserved tiles with obstacles. You're guaranteed that your reserved solutions still work, possibly in addition to some other solutions if you've left gaps in the non-reserved tiles.

  3. Remix

    Pre-author a bunch of "chunks" of footpath in various thicknesses with a solvable route through it.

    When generating a new footpath, randomly select from these chunks and snap them end-to-end like LEGO bricks.

    This approach gives you a lot of stylistic control, curating arrangements of obstacles/powerups that look good to you, but the repetition can be perceptible to players.

For the lanes of traffic/logs, it may suffice to ensure that there is eventually a way through:

  • Set the traffic generation in each lane to produce at least some gaps (say, add a randomized cooldown between obstacle spawning in a single lane, or deal obstacles from a "deck" with several "gap" cards in it)

  • Set adjacent lanes to travel in opposite directions or at different speeds, so that even if sometimes an obstacle in one lane blocks a gap in another, it won't always do so.

This way you'll get gaps stochastically, and you can tune the spawning frequencies/speeds to control how often they arise.

But the other approaches above work too, if you add time as a variable. For instance, a "remix" strategy could use pre-authored blocks of cars in adjacent lanes that move at the same speed. Or a "solution first" approach could periodically plan a path for the player between adjacent footpaths, recording the time window they'll be in each tile, then veto random spawning of any obstacle that's projected to overlap that tile in that time window, based on the lane's speed. Try the simple/stochastic route first though, and see if you still have unsolvable situations cropping up too often.

\$\endgroup\$
3
\$\begingroup\$

You could generate a random path or a few paths, and reserve this space. When you are placing objects, make sure they avoid the path area.

\$\endgroup\$
1
\$\begingroup\$

Additionally to what DMGregory said already you can check for potential paths after each obstacle generation.

You generate potential random position for obstacle and test if it's possible to find any way through.

If Yes - you spawn obstacle at this position.

If No - ignore this position and generate a new one.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .