# How to prevent two sprites from overlapping after random repositioning?

**URL:** <https://forum.makecode.com/t/how-to-prevent-two-sprites-from-overlapping-after-random-repositioning/37398>\
**Category:** Help\
**Created:** [June 18, 2025, 4:52pm UTC](https://forum.makecode.com/t/how-to-prevent-two-sprites-from-overlapping-after-random-repositioning/37398 "2025-06-18T16:52:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TK015](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@TK015](https://forum.makecode.com/u/TK015)\
**Post date:** [June 18, 2025, 4:52pm UTC](https://forum.makecode.com/t/how-to-prevent-two-sprites-from-overlapping-after-random-repositioning/37398/1 "2025-06-18T16:52:03Z")

</div>

I’m creating a game in MakeCode Arcade where the player gains points by touching a food sprite and loses lives by touching an enemy sprite.  
After the player touches either, both the food and enemy are moved to random positions on the screen.  
The problem is that sometimes they end up on top of each other, which makes it too easy to lose a life unintentionally.  
I tried solving it with a `while` loop and `overlaps_with()` in Python, but MakeCode says the project can’t be converted back to blocks.  
What’s the best way to solve this using only blocks?

Hereby attached a screenshot of the blocks so far:

 ![makecodearcade](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/c/4/c4c2315baf5ee441ab7175c62699c6622a79babd.png)

---

<div class="post-metadata">

**Author:** ![WoofWoof](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/woofwoof/32/32523_2.png) [@WoofWoof](https://forum.makecode.com/u/WoofWoof)\
**Post date:** [June 18, 2025, 5:24pm UTC](https://forum.makecode.com/t/how-to-prevent-two-sprites-from-overlapping-after-random-repositioning/37398/2 "2025-06-18T17:24:28Z")

</div>

I usually use a While loop that checks for overlapping. For example, a While loop that looks something like this:

```auto
While < (sprite1 is overlapping sprite2) > {
//randomly set the sprite position
}

```

---

<div class="post-metadata">

**Author:** ![TK015](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@TK015](https://forum.makecode.com/u/TK015)\
**Post date:** [June 19, 2025, 3:59pm UTC](https://forum.makecode.com/t/how-to-prevent-two-sprites-from-overlapping-after-random-repositioning/37398/3 "2025-06-19T15:59:04Z")

</div>

Thanks for your reply.  
I’ve fixed it this way at the moment.

```python
def on_on_overlap(sprite2, otherSprite2):
    info.change_life_by(-1)
    safe_place_sprites()
sprites.on_overlap(SpriteKind.player, SpriteKind.enemy, on_on_overlap)

def on_on_overlap2(sprite, otherSprite):
    info.change_score_by(1)
    info.start_countdown(3)
    safe_place_sprites()
sprites.on_overlap(SpriteKind.player, SpriteKind.food, on_on_overlap2)

def safe_place_sprites():
    global max_attempts
    max_attempts = 50
    for index in range(max_attempts):
        pizza.set_position(randint(0, scene.screen_width() - pizza.width),
            randint(0, scene.screen_height() - pizza.height))
        eten_vis.set_position(randint(0, scene.screen_width() - eten_vis.width),
            randint(0, scene.screen_height() - eten_vis.height))
        if not (pizza.overlaps_with(eten_vis)) and not (pizza.overlaps_with(mySprite)) and not (eten_vis.overlaps_with(mySprite)):
            break

```
