5. Kill little martians

_images/scratch-p05-juego.png

In this practice we are going to program a game that consists of killing Martians that fall from the top of the screen with the laser of a spaceship.


  1. We start the Scratch editor.


  2. We delete the cat by clicking on it with the right mouse button and then pressing delete.

    borrar-gato


  3. We will change the background of the stage with stars.

    Click on the New Background button.

    cambia-escenario

    Then click on Space Theme.

    Then we select the stars background.

    The screen will be as follows.

    _images/scratch-p05-stars.png


  4. Added a new character, a spaceship.

    Press the new object button nuevo-objeto

    Then click on Space Theme.

    Then we select the Spaceship object.

    _images/scratch-spaceship.png


  5. Added a new character, a blue button that will be used to fire shots.

    Press the new object button nuevo-objeto

    Then click on Things Category.

    Then we select the Button2 object.

    _images/scratch-button2.png

    Now, by clicking on the i button of the object, we change its name to Shot

    _images/scratch-p05-disparo.png


  6. We added a starfish, which will act as a Martian.

    Press the new object button nuevo-objeto

    Then click on Animals Category.

    Then we select the Starfish object.

    _images/scratch-starfish.png


  7. We added the variable Points, which will count the number of points accumulated while playing.

    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


  8. Now we will go to the spaceship program

    _images/scratch-p05-spaceship.png

    and there we create the following program.

    _images/scratch-p05-spaceship-prog1.png

    These instructions start the program by deleting the points variable, resize the spaceship, and position it at the bottom of the screen.


  9. We continue to program the spaceship.

    _images/scratch-p05-spaceship-prog2.png

    This block moves the spaceship from left to right and ends the program when a Martian touches the spaceship.


  10. With another small function, the ship will be able to fire its laser.

    _images/scratch-p05-spaceship-prog3.png


  11. We will test that everything works correctly by pressing the green flag and moving the ship left and right with the arrow keys.

    The laser will work later when we program the Shot object.

Laser shot

  1. Now we will go to the program of the object Trigger

    _images/scratch-p05-disparo.png

    and there we will program its operation.

    _images/scratch-p05-disparo-prog1.png

    This program places the start of the laser at the bottom of the screen (y = -120),

    sets the button upright (address 0)

    and reduce the size so that the blue button looks like a shot (size to 20%)

    Each clone then appears at the spaceship's current position and moves up until it reaches the bottom of the screen, where it disappears.


  2. At this point you can already test the operation of the shots with the spaceship.

Martians

  1. Now we will go to the program of the object Starfish

    _images/scratch-p05-starfish.png

    And we are going to program the behavior of the Martians at the beginning of the program.

    _images/scratch-p05-starfish-prog1.png

    This program starts 10 Martians at the top of the screen and creates more Martians from time to time.


  2. The program that manages each one of the Martians will be the following.

    _images/scratch-p05-starfish-prog2.png

    When a new Martian is started, it appears at the top of the screen, in a random position.

    If a shot hits a Martian, it disappears and the point counter increases by one point.

    If the Martian touches the bottom edge, it disappears and the point counter decreases by 10 points.

    Every half second, the Martian moves down to the bottom of the screen.


  3. This is the time to try all the programs together and play the Martians for a while.



Exercises

  1. Change the parameters of the program to adjust its difficulty making more Martians appear per second and go down faster.
  2. Adds a double fire function for the spaceship when pressing the "up arrow" key
  3. Add a barrier that protects the spaceship for a second when pressing "down arrow".
  4. Invent another original modification for the program.