Issues with a Maze game - Help me Im a newbie

So, Im testing things out to help out my students. We are doing loads of tutorials etc to try to make our own games.

I am trying to make a maze game and have managed to make a 3 level game all good!

Bit now IM trying to add enemies to it and having some issues I know have an easy fix but I don’t know how! Trying this to encourage my kids to add extra challenges like enemies:

  • Add enemies - They spawn randomly but sometimes they seem to do it on walls…?
  • Sometimes they spawn right in front of my sprite which makes it impossible to play
    -My game seems laggy Im not sure why
  • I have a 30 sec countdown - how can I make it so in Level 2 coutdown is 20 sec and level 3 countdown is 10 sec?

That’s for a starter…Once I figure that out I would like to add items to collect and a score - lives that you lose and possibly shooting and when you shoot the enemy they dissapear.

Alright, first let’s take a look at spawning enemies:

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:

image

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:

image

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:

jwunderl/arcade-sprite-util

Here’s a GIF showing how to add this extension to your project:

how-to-extension

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:

If you copy this code though, you might notice your game freezes. That’s because of how you’re initializing your tilemaps. Let’s tackle that in the next post.

3 Likes

Alright, let’s take a look at how you’re setting the current level:

image

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:

image

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:

image

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:

Inside of the startNextLevel function, I can use the “place on top of random tile” block to place the player on that starting point:

And while I’m at it, I can also destroy all of the enemies from the last level:

image

1 Like

Now let’s look at your game over code:

image

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:

image

Also, did you know you can control the speed of the following enemies? Click the plus button to set the speed:

set-follow-speed

2 Likes

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!

THANK YOU!