This code places the enemy at a random location on the screen, which includes inside walls. If you want to avoid walls, consider using the “place on top of random tile” block instead! This way, you can ensure it’s always spawning on a floor tile and not a wall tile:
Looks like you have three levels and they all have different floor tiles. Luckily, the “place on top of random tile” block doesn’t do anything if there are no instances of a tile in a map, so we can just call it three times:
This solves your first problem, but enemies still have a small chance of spawning on top of the player. To fix that, we’ll need to check the distance between the player sprite and the enemy sprite. To do this, I’m going to use a block from this extension:
Here’s a GIF showing how to add this extension to your project:
The reason we want this extension is so that we can get the “distance between sprite” block. Then, we can use it to move our sprite if it’s too close to the here sprite:
Alright, let’s take a look at how you’re setting the current level:
It looks like you are setting a level variable inside of “on start” and then setting the tilemap inside of a “forever” block. While this might work, it makes it difficult to customize the behavior for each level, like setting a different countdown for each one. Instead, let’s refactor this code into a function to make this a little easier to do:
Here, I’ve created a “start next level” function that changes the level variable by 1 and sets both the tilemap and the countdown. This way, I can set the countdown once without having to do it inside of the forever block. You can also call this function inside your tile overlap event to advance the level:
Now, let’s say I want to control where the player starts in each level. For that, I’ll make a “start” tile that I place somewhere in my tilemap:
First off, the “play sound until done” block will wait until the sound finishes to run the next code, that’s why you have a bit of a delay between when you overlap the enemy and when the game over actually happens. You might want to first destroy the player so that they can’t keep moving during that time.
Second, the “you lose” code you have there will never run because it’s after the “game over” block. You need to put it before the “game over” block for it to run correctly:
Also, did you know you can control the speed of the following enemies? Click the plus button to set the speed:
OMG! This is so thoughtful and kind, to spend time explaining and showing me how to do it! THANK YOU SO MUCH!
I am going to take my time to read and understand how this code works and do it. This is excellent, but a bit much for my elementary class I feel.
Is it ok to teach them how to make the maze and levels using the forever loop - and just omit all the other features like enemies etc?
Or will it still lag?
Cheers and again, thank you so much - I love makecode because it allows me to learn and has such a fantastic community that helps out! I made a game in the past following tutorials and with help from the community!