Microsoft MakeCode

Spawning enemies in random areas

I need help coding, my game allows the snakes to spawn, basically anywhere, but I need it to spawn the snakes anywhere besides right next to the tree, like the spawn area box

The link you included doesn’t look like it points to a game. To share a link to your game, open it in the editor and press the “share” button in the top bar. Here’s a GIF:

Like this?


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

Thank you @richard very useful!