# Game freezing on PyBadge

**URL:** <https://forum.makecode.com/t/game-freezing-on-pybadge/38550>\
**Category:** Help\
**Created:** [August 8, 2025, 2:38am UTC](https://forum.makecode.com/t/game-freezing-on-pybadge/38550 "2025-08-08T02:38:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobAJoeJoe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bobajoejoe/32/21776_2.png) [@BobAJoeJoe](https://forum.makecode.com/u/BobAJoeJoe)\
**Post date:** [August 8, 2025, 2:38am UTC](https://forum.makecode.com/t/game-freezing-on-pybadge/38550/1 "2025-08-08T02:38:36Z")

</div>

My Super Mario Maker like game is freezing on my PyBadge and I don’t know why. Here’s the link if you want to help: [https://arcade.makecode.com/S56169-18796-80738-18523](https://arcade.makecode.com/S56169-18796-80738-18523)

---

<div class="post-metadata">

**Author:** ![a012](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/a012/32/24099_2.png) [@a012](https://forum.makecode.com/u/a012)\
**Post date:** [August 27, 2025, 11:16pm UTC](https://forum.makecode.com/t/game-freezing-on-pybadge/38550/2 "2025-08-27T23:16:57Z")

</div>

Alright, I took a careful look at this code. From a MakeCode Arcade / TypeScript perspective, here are some **key things you or someone else might want to fix or watch out for** :

* * *

### 1️⃣ Array out-of-bounds in `make_level()`

```auto
for (let index = 0; index <= Xpos.length; index++) {
    tiles.setTileAt(tiles.getTileLocation(Xpos[index], Ypos[index]), blocks[Type[index]])
}

```

- `index <= Xpos.length` → This will **go one past the last index** , causing `undefined` errors.
- Fix:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/7/3/73a5e1b78e20366733be9b2dacad42eeaca65f91.png)

* * *

### 2️⃣ Global variables without initialization

You have a ton of global variables like `Xpos`, `Ypos`, `Type`, `blocks`, `Mario`, `making`, etc.

- Some are initialized at the bottom of the script, but **if functions run before initialization** , you’ll get errors.
- Good practice: Initialize all of the variabes at the top with defaults:

* * *

### 3️⃣ Repeated `forever` loops doing similar things

You have **dozens of `forever()` loops**, many of which check `controller.A.isPressed()` or `making` and then perform actions.

- **Problem:** This can lag the game. MakeCode executes each `forever()` independently, so heavy logic in multiple loops can slow the engine.
- **Better:** Combine related logic into fewer loops with `if` checks inside.

* * *

### 4️⃣ Sprite positions with offsets

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/optimized/3X/d/6/d64012f5f540ad512049a787fe7008ac22f655ee_2_690x24.png)

- Using decimals in tile positions is risky. `getTileLocation()` expects integers.
- Fix: Round the positions:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/optimized/3X/3/0/309205c3403973b7e8443ee2b57734724bf3c8ca_2_690x40.png)

* * *

### 5️⃣ `Xpos.insertAt(0, …)` logic

You’re **inserting at 0 every time** , meaning the arrays are in reverse order. If you care about placement order, consider using `.push()` instead.

* * *

### 6️⃣ `destroy_sprites()` vs `sprites.destroyAllSpritesOfKind`

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/2/3/230ceb7c87e819368ed5de2746aa562b99998517.png)

- That’s fine. But in `ded()`, you also destroy **all goombas** before resetting the player. ✅

* * *

### 7️⃣ Music/Sound Overlaps

- You have **multiple music tracks playing in parallel** in different `forever()` loops. This can conflict.
- Recommendation: Only play music when needed or use `music.stopAllSounds()` before playing a new track.

* * *

### 8️⃣ `onreset`, `oncurrentblock`, `onerase` logic

- These flags control if you can reset, erase, or place blocks.
- Seems okay, but check that `pauseUntil()` calls don’t block other logic you want running. Blocking in one loop can freeze animations.

* * *

### 9️⃣ `forever` loop doing nothing

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/b/a/ba57ce8e85668170d6460726833116e797a04f36.png)

- You can remove empty loops—they’re unnecessary.

* * *

### 🔟 Possible typo

```auto
if (onerase == 0 && onreset == 0 && oncurrentblock == 0)

```

- Later, you use `onerase` but earlier you wrote `onerase` with `e`? Make sure variable names are consistent.

* * *

✅ **Summary of main actionable fixes**

1. Change `<= Xpos.length` → `< Xpos.length`.
2. Round tile positions when using `tilemapLocation()`.
3. Combine redundant `forever()` loops to improve performance.
4. Ensure all global vars are initialized before use.
5. Review `.insertAt(0)` vs `.push()` logic for block placement order.
6. Avoid empty or overlapping `forever()` loops with sound/music.
