0

I'm designing a game in Scratch. The game is suppose to have a Spaceship travel through space, avoiding asteroids. These asteroids start at a fixed X position on the right side of the screen and go to the left, horizontally until they hit a fixed X position and they'll disappear. The asteroids will start in groups between 2-6 (it's a random number generated), and each set is about 1 second apart.

Assuming the game throws out up to 6 asteroids at once, I want to make sure each asteroid is distant from the next. I tried using two variables and comparing the distance, but this did not work. I can put the group of asteroids Y spawning position into a list. So say for instance in my list, I have:

0, 100, 5, 30, -20

As you can see, there are two items in that list that are close together. What I'm trying to do, is prevent this, so the third item would be something else, like -50, for instance, and then if a six item is generated, ensure it's also distant.

Can someone pseudocode how to achieve something like this? It doesn't matter what programming language it's in, I can probably translate the general idea into Scratch.

Josh
  • 13
  • 3

2 Answers2

1

There is a way to do this without a trial-and-error loop. Instead of picking random positions, pick random distances, then scale them to fit the screen.

Roughly, the approach is as follows. The lists below represent the distances between neighboring asteroids (ordered by Y coordinate), as well as distances between the outermost asteroids and the edges of the screen. For example, if a group contains 6 asteroids, then you need lists of 7 elements each.

  1. Create a list L1 of minimal distances. Obviously, these are all fixed values.
  2. Create a list L2 of random numbers. Take them from some arbitrary, fixed range with a positive lower bound, e.g. [1..100].
  3. Calculate the total 'slack' = height of screen minus sum(L1).
  4. Calculate a multiplication factor = slack divided by sum(L2).
  5. Multiply every element of L2 with the multiplication factor.
  6. Add every value from L1 to the value in L2 at the same index.

L2 now contains a list of distances that:

  • obey the minimal distances specified in L1
  • together equal the height of the screen

The final step is to position every asteroid relative to its neightbor, based on the distances in L2.

Note: if step 3 gives a negative number, then obviously there is not enough room on screen for all asteroids. What's worse, a naive 'trial-and-error' algorithm would then result in an infinite loop. The solution is of course to fix your parameters; you cannot fit 6 asteroids in 360 pixels with a minimal distance of 100.

Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45
0

To do this, you need to do through each previous entry in the array, compare that value to the new value, and if any element is too close change the value. This process needs to repeat until a suitable number is found. If this number is less then some minimum distance, then a variable tooClose is set to yes and the value will be reset. At the begining of the loop tooClose is set to yes so that at least one random number will be generated. Then, at the beginning of the loop, the value is randomized, and tooClose is set to no, then, I loop through all the previous entries with the value i, comparing each element and setting tooClose to yes if it is too close. The comparison between numbers is done with a subtraction, followed by an absolute value, which will ensure the result is positive, giving the difference between the two numbers as a positive value.

Here is a screenshot of the code:

Code

And here is the project: https://scratch.mit.edu/projects/408196031/

BEN1JEN
  • 71
  • 1
  • 7