Rocks Need Personal Space

  1. If you remember, I've been worked on the three major issues in my Ferb game, Today I address the last one.
  1. Here are the issues:
  1. DONE - place more rocks (this means creating an array to track the rocks and changing existing code to use the array)
  2. DONE - put an upper limit to the number of rocks placed.
  3. space the rocks (which means spacing the meteors)

Today, I want to deal with the 3rd point. The meteors are generated at random locations in the sky and, on occasion, they land and create rocks that are too close together for Ferb to hop over. I want to alter the meteor generation code so that the meteors and rocks don't end up being close to each other.


Here is the current code:

Screen Shot 2021-05-19 at 8.26.02 AM


Line 238 is the problem. I'll change it so it calls a new function which returns a good location instead of the random number. I'll rename this function to give it a clearer name.

Here's the new structure:

Screen Shot 2021-05-19 at 8.32.58 AM


The newgetmeteorxpos function needs to go through the rock array and generate a new position that isn't close to other rocks. How to do this is the question. The easy thing to do is to get a random location randomly between two rocks. If there is only one rock on the screen, then find a position between the rock and the edge of the screen. If we add a safety buffer to the rock's on the ground that should solve our problem. The size of the safety buffer is something I'll to play with. I could just sit down and calculate it, but where's the fun in that?

This is what I'm trying to do

ROCK.....| < in here somewhere> |…..ROCK



First, let's see if we have more than one rock. If we do, choose two rocks. If we don't, choose a random edge and the one rock.

Here's the code so far.


Screen Shot 2021-05-19 at 9.07.05 AM

I set the bounds of our possible location to the left and right edges of the screen. In lines 302/303. I then, see if I only have one rock. Then, I choose a a physical edge of the screen at random. If it's the left edge, the rock has to be to the right. If it's the left edge, the rock has to be to the left. I then set the bounds to the rock's x position appropriately. I'll want to refactor all of this into its own function later.

Now for the code if we have more than a single rock. First, I need to choose two different rocks at random.

Screen Shot 2021-05-19 at 9.16.20 AM



Lines 319-320 choose two rocks at random. I then make sure I haven't chosen the same rock twice in the loop 321-322. Yes, I could do this more efficiently, but this is good enough for now.

There's one condition I'm not checking, and should. What if we don't have any rocks? In that case I just want to create a rock randomly.


Screen Shot 2021-05-19 at 9.23.41 AM


Lines 303-305 should handle that.


Back to my problem. Now that I have the indexes of the two rocks, I need to adjust the bounds of the new rock to the two rocks.

Screen Shot 2021-05-19 at 9.25.40 AM

I get the two rocks' x positions based on the indexes in lines 331-332 I then need to get the minimum and maximums of the positions because the second rock can be to the left of the first one (because the rocks were chosen randomly). Now I can return a random x position between these two.


This site does not track your information.