# Spawning enemies in random areas

**URL:** <https://forum.makecode.com/t/spawning-enemies-in-random-areas/1471>\
**Category:** Help\
**Created:** [March 21, 2020, 5:56am UTC](https://forum.makecode.com/t/spawning-enemies-in-random-areas/1471 "2020-03-21T05:56:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Icicles](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/icicles/32/734_2.png) [@Icicles](https://forum.makecode.com/u/Icicles)\
**Post date:** [March 21, 2020, 5:56am UTC](https://forum.makecode.com/t/spawning-enemies-in-random-areas/1471/1 "2020-03-21T05:56:41Z")

</div>

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

> **[Microsoft MakeCode Arcade](https://arcade.makecode.com/#editor)**
>
> Develop your programming skills by quickly creating and modding retro arcade games with Blocks and JavaScript in the MakeCode editor

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [March 23, 2020, 4:18pm UTC](https://forum.makecode.com/t/spawning-enemies-in-random-areas/1471/2 "2020-03-23T16:18:41Z")

</div>

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:

 ![2020-03-23 09.17.36](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/1X/b33ee0cc63a7c343c23e4b6dd8665405eae8f852.gif)

---

<div class="post-metadata">

**Author:** ![Icicles](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/icicles/32/734_2.png) [@Icicles](https://forum.makecode.com/u/Icicles)\
**Post date:** [March 23, 2020, 10:03pm UTC](https://forum.makecode.com/t/spawning-enemies-in-random-areas/1471/4 "2020-03-23T22:03:39Z")

</div>

> **[One Room Jam](https://arcade.makecode.com/31198-16429-38332-05792)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

  
@richard  
Like this?

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [March 24, 2020, 12:17am UTC](https://forum.makecode.com/t/spawning-enemies-in-random-areas/1471/5 "2020-03-24T00:17:50Z")

</div>

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](https://en.wikipedia.org/wiki/Euclidean_distance) formula:

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

```

And in blocks it ends up looking like this:

 ![Screen Shot 2020-03-23 at 5.15.09 PM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/1X/30c8a571e24160b11fc71bb0abf803102f24a7d9.png)

( 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!

---

<div class="post-metadata">

**Author:** ![Icicles](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/icicles/32/734_2.png) [@Icicles](https://forum.makecode.com/u/Icicles)\
**Post date:** [March 24, 2020, 4:36pm UTC](https://forum.makecode.com/t/spawning-enemies-in-random-areas/1471/6 "2020-03-24T16:36:24Z")

</div>

Thank you @richard very useful!
