7. Catch apples

_images/scratch-p03-escenario2.png

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


  1. We start the Scratch editor.


  2. We will change the background of the stage to a desert.

    Click on the New Background button.

    cambia-escenario

    Then click on Nature Theme.

    Then we select the background Desert.

    The screen will be as follows.

    _images/scratch-p03-escenario.png


  3. Now we must click on the cat to carry out its program. The cat icon must be selected.

    gato-seleccionado


  4. By clicking on the letter i inside the cat icon, we can change its name.

    _images/scratch-p03-cambiar-nombre-gato.png


  5. In the cat programs tab we will create a new function called Start

    First click on the button more blocks

    mas-bloques

    Then click on create a block crear-bloque

    Then we change the name of the new block to Home

    _images/scratch-bloque-inicio.png

    Finally we press the button OK


  6. Now we will create the variable Lives.

    Within the Data datos tab,

    press create a variable crear-variable

    We change the name of the variable to Lives

    _images/scratch-p03-variable-vidas.png

    Finally we press the button OK

    This variable will store the lives that the cat still has. When its value reaches zero, the cat will die.


  7. Now we will write the Start block.

    First click on the button more blocks

    mas-bloques

    Then click on create a block crear-bloque

    Then we change the name of the new block to Home

    We place the following blocks in Start.

    _images/scratch-p03-programa-inicio.png

    This program gives the cat three lives at start.

    Place the cat at the bottom of the screen, with left-right rotation mode.

    It also erases all images on the screen.

    Finally, wait for the space key to be pressed to continue.


  8. Now we will program a block that will move the cat left and right with the arrow keys. The new block will be called Move_cat

    _images/scratch-p03-programa-mover-gato.png


  9. To finish this section, we will carry out a small program that will test everything we have programmed so far.

    _images/scratch-p03-programa-gato1.png

    By pressing the green flag bandera-verde, the cat will first wait for us to press the space bar, then the cat will move left and right by pressing the left and right keys.

Apple program

Warning

From this point on, all instructions should be added to the Programs tab of the apple object.


  1. The first thing we will do is add a new character, an apple

    Press the new object button nuevo-objeto

    Then click on Things Category.

    Then we select the Apple object.

    _images/scratch-p03-manzana.png


  2. Now we will create a new variable called Points

    Within the Data datos tab,

    press create a variable crear-variable

    We change the name of the variable to Points

    _images/scratch-variable-puntos.png

    Finally we press the button OK

    This variable will count the points that the game character accumulates.

    The cat will earn one point each time it catches an apple and will lose two points if the apple falls to the ground.


  3. Now we add a new message called Start

    Click on the Events button.

    eventos

    and we move the block "when receiving message1" to programs.

    Clicking on the arrow, we select new message...

    nuevo-mensaje

    We name the new message as Start.

    _images/scratch-p03-mensaje-iniciar.png

    Finally we press the button OK

    This message will serve to warn the apples that they may start to fall after the space key is pressed.

    It also clears the points to start the game and shows them on the screen.


  4. Now we can program the necessary instructions to drop the apples when receiving the Start message.

    _images/scratch-p03-programa-manzana1.png

    The program hides the object so that it cannot be seen on the screen, resets the point counter to zero, and creates copies of the apple (clones of the apple) every few random times, as long as the cat has lives.


  5. Let's create another costume that represents the apple squashed on the ground on the ground.

    First we select the costumes tab disfraces of the Apple.

    Next we select with the right button of the mouse on the disguise of the apple to duplicate it.

    _images/scratch-p03-duplicar-manzana.png

    In the newly created Apple2 costume, we squash the image.

    _images/scratch-p03-aplastar-manzana.png


  6. In this section we will define how apple clones behave. We will create the following programs.

    _images/scratch-p03-programa-manzana2.png _images/scratch-p03-programa-manzana3.png

    The apple is placed at the top of the screen and falls little by little (change y by -10) until it touches the cat or until it falls to the ground.

    If the apple touches the cat, it increases the points and disappears.

    If the apple touches the ground, it will be crushed and a thump will sound.

    The program has been divided into several functions to simplify each of the blocks.


  7. Now let's check that the new instructions work. We modified the cat's program to add the message Start

    _images/scratch-p03-programa-gato2.png


  8. At this time, the cat will be able to play apple picking. We must move the cat with the left and right arrows so that it touches the apples before they fall to the ground.

Lightning program

In this section we are going to program several lightning bolts to fall from above and take the lives of the cat. In this way we give the game more difficulty and emotion.

Warning

From this point on, all instructions should be added to the Programs tab of the lightning object.


  1. The first thing we'll do is add a new character, a lightning.

    Press the new object button nuevo-objeto

    Then click on Things Category.

    Then we select the Lightning object.

    _images/scratch-p03-rayo.png


  2. Now we are going to program the lightning strike from the top of the screen. As with the apples, lightning will strike while the cat has lives.

    _images/scratch-p03-programa-rayo.png


  3. We are going to add a new sound to the lightning, so that the cat complains when struck by lightning.

    Select the tab Sounds sonidos of lightning.

    Click on selecting a new sound from the library sonido-nuevo.

    In the Animal Category we select the sound meow

    _images/scratch-p03-sonido-meow.png


  4. Now we must create a program for each lightning clone.

    _images/scratch-p03-programa-rayo-clon.png

    These programs will place each ray in a random position each time a clone is created. The ray will then fall (change y by -1) until it either hits the cat or hits the bottom edge.

    If the lightning strikes the cat, it will take a life and it is erased.

    If the beam hits the ground, it disappears.


  5. We will check by pressing the green flag bandera-verde that everything works fine.

Cat death

When the cat loses all its lives the game ends and apples and lightning stop falling, but the cat keeps moving. In this section we are going to add the necessary instructions so that the cat appears dead at the end of the game.

Warning

From this point on, all instructions should be added to the Programs tab of the cat object.


  1. Added the Cat_die function, which is executed when the cat loses all of its lives.

    _images/scratch-p03-programa-gato-muere.png


  2. And we modified the cat's program.

    _images/scratch-p03-programa-gato3.png


  3. At this point, to finish, we are going to add a background sound to the game.

    _images/scratch-p03-programa-sonido.png


  4. Test the game to verify that everything works correctly.


Exercises

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