9. Catch apples

In this practice we are going to program a game that consists of catching apples with a cat and avoiding the lightning that kills. The cat will move with the right and left keys. When the cat loses its three lives, the game will be over.

_images/scratch3-p08-screen.png


  1. We start the Scratch editor.


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


  3. Now we choose a suitable backdrop for our game. We changed the backdrop to a desert.

    Press the button choose a backdrop seleccionar-fondo.

    We look in the Exteriors section.

    and select the Desert backdrop.

    _images/scratch3-fondo-desert.png


  4. We change the name of the object to Cat.

    _images/scratch3-objeto-nombre-gato.png


  5. We create the variable lives that will store the number of lives that the Cat has. When this variable is equal to zero, 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 lives

    _images/scratch3-crear-variable-vidas.en.png

    Finally, click on the OK button.


  6. Now we program the initialization instructions for the Cat object. This program will give the cat three lives, display the value on the screen, place the cat under the screen, and rotate left and right style.

    _images/scratch3-p08-gato-01.png


  7. We continue giving the cat movement to one side and to the other. The following program checks whether a left arrow or right arrow key has been pressed and if so, moves the cat in one direction or the other.

    _images/scratch3-p08-gato-02.png


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


  9. Now we create the variable points that will store the number of points that we have obtained by catching the apples.

    Press the variables button boton-variables,

    click on create a variable boton-crear-variable.

    We change the name of the variable to points

    _images/scratch3-crear-variable-puntos.en.png

    Finally, click on the OK button.


  10. Next we add a new character, an apple.

    Press the button choose an object seleccionar-objeto.

    We look in the Food section.

    and select the Apple object.

    _images/scratch3-objeto-apple.png


  11. Let's create another costume for the apple, a squashed apple. First we go to the costumes tab pestania-disfraces

    Next we duplicate the apple costume.

    _images/scratch3-p08-duplicar-disfraz-manzana.png


    Now we select the duplicate costume and crush it.

    _images/scratch3-p08-seleccionar-disfraz-manzana.png _images/scratch3-p08-aplastar-disfraz-manzana.png


  12. Now we can carry out the apple program within the code tab pestania-codigo of the Apple.

    First we are going to hide the apple, we assign zero to the points and we are creating clones of the apple so that they appear on the screen, while the cat has lives.

    _images/scratch3-p08-manzana-01.png
  13. Before executing the following program, the block "fall ground" must be defined. Within the section My blocks boton-misbloques press "Create a block" boton-crear-bloque and in the name of the block we write "fall ground".

  14. The following program will make each clone of the apple appear on top in a random position, then fall to the ground.

    _images/scratch3-p08-manzana-02.png
  15. Finally we program the block that makes the apple fall to the ground. In case of touching the cat, it will increase one point and the clone of the apple disappears. If the apple clone touches the ground, it will be crushed.

    _images/scratch3-p08-manzana-03.png


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


  17. Added a new item, a lightning.

    Press the button choose an object seleccionar-objeto.

    We look in the All section.

    and select the Lightning object.

    _images/scratch3-objeto-lightning.png


  18. Now we carry out the program so that lightning clones are generated every few seconds.

    _images/scratch3-p08-rayo-01.png


  19. Every time a clone is generated, its behavior will be as follows.

    It will drop down from the top of the screen. If you touch the cat, the lives are reduced by one. If you touch the bottom edge, the beam disappears.

    _images/scratch3-p08-rayo-02.png


  20. Finally we program the cat to die when the lives reach zero. First we select the cat object.

    _images/scratch3-p08-seleccionar-gato.png

    Next we add the program.

    _images/scratch3-p08-gato-03.png


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



Challenges

  1. Add a new character that takes lives just like lightning. This new character should appear after 10 seconds of gameplay.


  2. Modify the program so that the number of lightning bolts increases over time, making the game increasingly difficult.