Here we're going to use a famous algorithm to solve a maze built in MakeCode Arcade.
This comes in two parts. You can build Part 1 on your own at home, but during club we'll start at Part 2.
Make a new tilemap and set the position of myself sprite to the correct starting location.
In your tilemap, make sure you paint the walls in! (Go into wall mode and make all the solid walls red).
myself sprite is found in the 3D Render menu.
Make a function called goLeft.
Inside that function:
If the tile to the left of the player is the kind you used for the walls, then:
Say "you can't go left!"
Otherwise, move the player left by 16 pixels (x - 16)
Don't forget to make functions for the other directions:
Right (x + 16)
Backwards (y + 16)
Forwards (y - 16)
When you press the B button, do the right combination of goRights, goLefts, goForwards, and goBackwards to get your player to the goal.
Now we're ready to move on to the part we'll be doing in code club.
When the B button is pressed:
We want to look to see if there is a floor tile to the left of my character.
If so, we should stick the location of that tile in a big list of tiles we need to look at.
(Make an empty list at the start of the game. If there is a floor tile to the left of the player we should put the location of that tile at the end of our list)
I should also check for floor tiles:
In front of my player
Downwards from my player
To the right of my player
If there is a floor tile in any of these locations, I should add the location of that tile to the end of my list.
After the sprite has looked all around for floor tiles, it should teleport to the first tile in the list. It's important that I delete that first tile in the list as well.
(place myself sprite on top of get and remove first item from list)
Once my sprite has teleported, I can put down a new tile on the spot my sprite is standing on.
This stops us from accidentally going back on ourselves.
Design your own maze and see if the algorithm still works.
Wrap the algorithm in a repeat block (with a pause) so you can see the journey.
Make the algorithm stop early if the player has found the exit.
Add collectibles to the maze. You can use the maze solver only if you have enough coins.
Add an enemy to the maze.
Generate a new maze every time the game begins! https://professor-l.github.io/mazes/