14. Walk mazes

In this practice we are going to program a game that consists of going through several mazes from the beginning to the end point before our character runs out of energy.

_images/scratch3-p14-screen.png


  1. We start the Scratch editor.


  2. Press the language button boton-idioma in the top bar and choose English.


  3. We delete the cat object by clicking on the trash can icon.

    borrar-gato


  4. Next we add a new character, a basketball.

    Press the button choose an object seleccionar-objeto.

    We look in the Sports section.

    and select the Basketball object.

    _images/scratch3-objeto-basketball.png


  5. Now we will create the variable Phase that will count the number of mazes that we are going through. When this variable exceeds five mazes, the program will terminate.

    Press the variables button boton-variables,

    click on create a variable boton-crear-variable.

    We change the name of the variable to Phase

    _images/scratch3-p14-variable-fase.png

    Finally, click on the OK button.


  6. We will also create the variable Energy that will count the energy we have to go through all the mazes. If the energy variable becomes empty, we will lose the game and the program will end.

    Press the variables button boton-variables,

    click on create a variable boton-crear-variable.

    We change the name of the variable to Energy

    _images/scratch3-p14-variable-energia.png

    Finally, click on the OK button.


  7. We create two new blocks called move_x and move_y that will move the ball around the screen.

    First click on the button my blocks boton-misbloques

    Then click on create a block boton-crear-bloque

    Then we change the name of the new block to move_x

    First click on the button my blocks boton-misbloques

    Then click on create a block boton-crear-bloque

    Then we change the name of the new block to move_y


  8. We make a program that moves the ball in all four directions, consuming energy and bouncing off the walls of the maze so that it cannot pass through.

    _images/scratch3-p14-basketball-3.png _images/scratch3-p14-basketball-4.png

    The ball still won't roll because we need another routine to call the functions we've created.


  9. Now we create a new block, Start, which will show the ball with small size at the beginning of the maze.

    _images/scratch3-p14-basketball-2.png


  10. To continue we will program the main routine that calls all the blocks that we have previously created.

    We won't be able to change the backdrop to "No_energy" yet because this backdrop hasn't been created yet. Remember to change it after creating the backdrop.

    _images/scratch3-p14-basketball-1.png


  11. To finish the ball program, we will program the behavior when it receives the Start and Finish messages.

    _images/scratch3-p14-basketball-5.png


  12. Now we will switch to the Scenario and inside the backdrops tab we will create the game messages.

    First we select in backdrops, paint a backdrop.

    _images/scratch3-p14-pinta-un-fondo.png

    Next, with the Text tool we write the following message on the screen.

    _images/scratch3-p14-fondo-perdido.png

    Change the name of the costume to "Out_Energy".

    _images/scratch3-p14-disfraz-sin-energia.png

    Remember to modify the basketball program now that this backdrop is already created.


  13. Now we will switch back to the Scenario and within the backdrops tab we will create more in-game messages.

    We select in funds, paint a backdrop.

    _images/scratch3-p14-pinta-un-fondo.png

    Next, with the Text tool we write the following message on the screen.

    _images/scratch3-p14-fondo-terminado.png

    Changed the name of the costume to "Finished".


  14. We select again to paint a backdrop.

    Next, with the Text tool we write the following message on the screen.

    _images/scratch3-p14-fondo-finalizado.png

    Changed the name of the costume to "Finished".


  15. To complete the backdrops, we are going to download the following compressed file that contains several mazes inside.

    Labyrinths to go through. ZIP format.

    Once downloaded, we open the Zip file and extract all the maze files inside it to a known folder.

    All files can be extracted by right-clicking on the Zip file and selecting the Extract All... option and then clicking the Extract button.


  16. Back in the Scenario, within the backdrops tab, we are going to import the different mazes.

    First we select in backdrops, load a backdrop.

    _images/scratch3-p14-carga-un-fondo.png

    We choose the folder where the previous Zip file has been downloaded and the folder where the mazes have been extracted. Ask your teacher if you don't know how to proceed in this step.

    Once in the mazes folder, click on the first mazes and press the Open button.

    _images/scratch3-p14-carga-de-archivos.png


  17. We repeat the previous step with the first five mazes.


  18. To continue, we are going to program the behavior of the Scenario in the Code tab.

    At the start of the program we set the value of the Phase variable to 1 (the number of the maze) and display the first maze.

    _images/scratch3-p14-escenario-1.png

    Every time you get the message to Start a new maze, we add one to the Phase variable and display the corresponding maze. Once the value 6 is reached, the mazes will be over and we can end the program with a winning message.

    _images/scratch3-p14-escenario-2.png


  19. At this point, it is only necessary to define when each maze ends. To achieve this we are going to add a new object, an arrow, which will send the maze finished message when it touches the basketball.

    Added a new character, an arrow.

    Press the button choose an object seleccionar-objeto.

    We look in the All section.

    and select the object Arrow1.

    _images/scratch3-objeto-arrow1.png


  20. We make a program that shows the arrow in the lower right corner at the start of the program. He must also always be detecting if he touches the basketball, to finish the maze and start a new one.

    _images/scratch3-p14-arrow-1.png

    In addition, we will program the arrow to hide from the end of a maze until the next maze begins.

    _images/scratch3-p14-arrow-2.png


  21. Press the green flag bandera-verde to test the operation of the program.



Challenges

  1. Add a light blue trail to the movement of the ball so that we know at all times what path the ball has taken.


  2. It measures how much energy the ball consumes to go through each maze and establishes at the beginning an energy value that is just the one necessary to pass the game.


  3. At the beginning of each maze, add small fruits scattered randomly. When the ball touches one of these fruits, the fruit must disappear and add energy to the ball.

    Fruits will be added as clones of a fruit that must remain hidden.

    Set the ball to a lower starting energy value, so you need to collect multiple fruits in order to finish all the mazes with enough energy.

    Adjust the number of fruits and the energy each fruit adds to make the game difficult, but still finishable.