Spawning enemies in random areas

One way to do it is to calculate the distance. If the snake is too close, we just keep moving it to random positions until it’s far enough away.

If we use the euclidean distance formula:

distance = Math.sqrt((player.x - snake.x)^2 + (player.y - snake.y)^2)

And in blocks it ends up looking like this:

( the ** operator in blocks is for exponents)

In that screenshot, I put 30 as the minimum distance. I hope that helps! And thank you very much for the note, it made our day!

1 Like