A2 · Episode 8 · 6:40

Adding Interactions in a Scratch Game

This episode teaches how to code interactions between a sprite and the background in Scratch, including bouncing off screen edges, detecting collisions with other sprites, and triggering game events based on background color detection.

Source:Montagne Vincent / tube-numerique-educatif.apps.education.fr · License:CC BY 4.0

Transcript

  1. Coder un jeu avec Scratch, septième épisode dans lequel nous allons voir comment

    Coding a game with Scratch—Episode 7, in which we’ll learn how to

  2. ajouter des interactions entre un lutin et l'arrière- plan.

    add interactions between a sprite and the background.

  3. L'arrière- plan, c'est le décor de notre jeu.

    The background is the scene of our game.

  4. Alors ici, par exemple, dans le jeu de pong,

    For example, in this Pong game,

  5. la balle rebondit sur la raquette, ça c'est de l'interaction entre deux lutins,

    the ball bounces off the paddle—that’s an interaction between two sprites;

  6. mais elle rebondit également sur les bords de l'écran, en haut,

    but it also bounces off the top edge of the screen,

  7. à gauche et à droite, ça c'est une interaction avec le décor,

    and off the left and right edges—that’s an interaction with the scene,

  8. c'est- à- dire avec l'arrière- plan,

    that is, with the background;

  9. mais également la balle s'arrête lorsqu'elle touche la partie grise de l'arrière-

    also, the ball stops when it touches the gray part of the background—

  10. plan, donc nous avons ici une autre interaction.

    so here we have another type of interaction.

  11. Donc c'est ce genre de choses que nous allons voir comment coder avec Scratch,

    So we’ll now learn how to code exactly these kinds of interactions using Scratch,

  12. et nous y allons tout de suite.

    and we’ll get started right away!

  13. Alors nous y voici, j'ai donc deux lutins, la balle et la raquette.

    Here we are: I have two sprites—the ball and the paddle.

  14. Donc la raquette, c'est juste un rectangle noir que j'ai dessiné avec les outils de

    The paddle is simply a black rectangle,

  15. dessin ici dans les costumes.

    which I drew using the costume-drawing tools here.

  16. Si je regarde son script, il est très simple, j'ai une initialisation,

    If I look at its script, it’s very simple—I start with initialization,

  17. je le mets à sa position initiale, donc x égal 0, y moins 80.

    placing it at its starting position: x = 0, y = -80.

  18. Et j'ai les déplacements avec les flèches du clavier vers la gauche ou vers la

    Then come the movements—left and right—controlled by keyboard arrow keys,

  19. droite. Donc on peut regarder ce que ça donne.

    so let’s see how it works.

  20. Voilà, vers la gauche, vers la droite.

    There we go—left, right.

  21. Et on va donc s'intéresser au script de

    Now, let’s turn our attention to the ball’s script.

  22. la balle. Alors on va aussi avoir une initialisation,

    It also has an initialization step,

  23. on va la mettre par exemple par ici au départ.

    for instance, placing it here at the start.

  24. Donc je vais la faire aller. . .

    Now I’ll make it move…

  25. Alors vous voyez que quand je prends le bloc ici" aller à", les

    You’ll notice that when I select the “go to” block here,

  26. coordonnées qui sont déjà affichées,

    the coordinates shown,

  27. ce sont celles que le lutin occupe actuellement.

    are exactly the ones the sprite currently occupies.

  28. Donc là, la balle, elle est actuellement à- 808, alors on va arrondir,

    So right now, the ball is at -808; let’s round it.

  29. on va mettre 0, 110 par exemple.

    For example, we’ll set it to 0.110.

  30. Voilà, et la balle va avancer droit devant elle,

    There we go—the ball will move straight forward.

  31. donc on va l'orienter dans une direction

    So we’ll orient it in a specific direction.

  32. donnée. Alors pour commencer, on va l'orienter par exemple à 45 degrés.

    To start, let’s set it to 45 degrees.

  33. On verra après ce qu'on peut faire pour avoir quelque chose qui augmente la

    Later, we’ll see what we can do to increase the game’s difficulty.

  34. difficulté du jeu.

    —the game’s difficulty.

  35. Et maintenant, donc on va faire avancer de 10 la balle, et ça,

    Now, we’ll make the ball advance by 10 units—and keep doing so.

  36. on va le faire indéfiniment.

    —repeatedly, without stopping.

  37. Tant que le jeu n'est pas fini, la balle doit avancer de 10.

    As long as the game isn’t over, the ball must keep advancing by 10.

  38. Et la première chose qu'on va faire,

    The first thing we’ll do now is:

  39. c'est d'ajouter une interaction avec les bords du jeu.

    add interaction between the ball and the game boundaries.

  40. Nous avons un bloc qui nous dit que la balle peut rebondir si le bord est atteint.

    We have a block that makes the ball bounce when it hits a boundary.

  41. Alors on peut tout de suite regarder ce que ça donne.

    Let’s immediately check how this works.

  42. Voilà, si la balle atteint le bord, elle rebondit sur le bord.

    There you go—if the ball hits a boundary, it bounces off it.

  43. Alors pour le moment, je n'ai pas mis d'interaction avec la raquette,

    For now, I haven’t added interaction with the paddle,

  44. donc elle passe à travers la raquette, c'est normal,

    so it passes right through the paddle—that’s expected.

  45. mais je vois que la balle rebondit sur

    But I can see the ball bounces off any boundary it touches,

  46. tous les bords qu'elle touche.

    —immediately.

  47. Alors on va rajouter tout de suite l'interaction avec la raquette.

    Next, we’ll add interaction with the paddle right away.

  48. Donc si. La raquette est touchée.

    If the paddle is touched…

  49. Donc ici, je choisis si la raquette

    Here, I choose: if the paddle is touched,

  50. est touchée, alors la balle, elle doit rebondir.

    then the ball must bounce.

  51. Alors il n'y a pas de bloc

    However, there’s no built-in block in Scratch that lets us make one sprite bounce off another,

  52. qui nous permette de faire rebondir un lutin sur un autre lutin,

    —a sprite bouncing off another sprite.

  53. on peut rebondir seulement sur les bords.

    We can only make it bounce off the edges.

  54. Alors ici, il y a quelque chose d'un petit peu compliqué,

    Here, things get a bit complicated,

  55. on va devoir orienter la balle dans une nouvelle direction, et en fait,

    we’ll need to redirect the ball, and in fact,

  56. on va avoir un angle qui est symétrique par rapport à la verticale,

    the ball’s direction will form an angle symmetric to the vertical line,

  57. l'orientation de.

    that is, this direction.

  58. de la balle, la nouvelle orientation.

    Bullets, the new direction.

  59. Alors je vous donne le calcul ici, c'est 180 degrés

    So I give you the calculation here: 180 degrees

  60. moins l'angle que faisait la balle, c'est- à- dire la direction ici.

    minus the angle formed by the ball—that is, its direction here.

  61. Vous voyez le bloc direction tout en bas, que je peux prendre ici.

    You see the 'Direction' block at the very bottom, which I can select here.

  62. Donc si la raquette est touchée,

    So, if the racket is hit,

  63. alors la balle va s'orienter à une nouvelle direction qui est 180 moins la direction

    then the ball will travel in a new direction—

  64. qu'elle avait auparavant.

    180 degrees minus its previous direction.

  65. Alors on peut essayer de voir ce que ça donne.

    So we can try to see what it produces.

  66. Voilà, donc là j'ai l'impression que la balle rebondit,

    There we go—right now, I feel like the ball is bouncing,

  67. enfin c'est pas une impression, la balle rebondit sur

    no, it’s not just a feeling—the ball really is

  68. la raquette tout à fait correctement,

    bouncing perfectly off the racket,

  69. mais là elle peut passer derrière la raquette, c'est pas ce que je veux.

    yet right now it can pass behind the racket—which isn’t what I want.

  70. Moi ce que je veux, c'est que si elle passe derrière la raquette,

    What I want is that if she passes behind the racket,

  71. la partie s'arrête.

    the point ends.

  72. Alors ce qu'on va faire, c'est qu'on va rajouter une interaction avec la couleur

    So, what we’ll do is add an interaction with the gray color.

  73. grise ici qui est derrière la raquette,

    This gray is behind the paddle.

  74. qui est la couleur de l'arrière- plan gris. C'est pas un lutin ici,

    It’s the gray background color. There’s no sprite here.

  75. c'est l'arrière- plan qui est gris.

    Instead, the background itself is gray.

  76. Donc on va avoir si.

    So we’ll have…

  77. Et cette fois, le capteur qu'on va utiliser, c'est si la couleur. . .

    And this time, the sensor we’ll be using is the color sensor.

  78. Alors vous voyez ici c'est couleur verte, mais je veux la couleur grise,

    So you see, this is displayed in green, but I want gray.

  79. donc ce que je fais, c'est que je clique dans le carré là où il y a la couleur

    So I click on the square showing green,

  80. verte, et je vais aller chercher la couleur grise sur l'écran.

    and then select gray from the screen.

  81. Voilà, et maintenant c'est la couleur grise.

    Here we go—now it’s gray.

  82. Donc si la couleur grise est touchée.

    So, if the gray color is touched,

  83. Le jeu doit s'arrêter, donc on va stopper tout le jeu.

    the game must stop—so we’ll halt the entire game.

  84. Alors voyons voir ce que ça donne.

    Let’s see how this works.

  85. Donc ici, je rattrape la balle, si je la rattrape pas, le jeu est arrêté.

    Here, I catch the ball—if I miss it, the game ends.

  86. Alors on peut rajouter un petit effet avant que le

    We can add a small visual effect,

  87. jeu soit arrêté.

    just before the game stops.

  88. On va rajouter un petit effet, donc on va répéter par exemple 20

    We’ll add a color effect—repeating it 20 times, for example.

  89. fois un effet de couleur sur la balle.

    That is, apply the color effect to the ball 20 times.

  90. Alors on peut choisir ce que l'on veut, c'est juste pour.

    We can choose whatever we like—

  91. pour voir quelque chose, donc on va ajouter 5 par exemple,

    it’s just to make something visible.

  92. 20 fois à l'effet couleur, on va voir ce que ça donne.

    For instance, let’s try 5 or 20 color effects and see what happens.

  93. Donc faut pas hésiter, voilà, la balle change de couleur plusieurs fois,

    So don’t hesitate—look: the ball changes color several times,

  94. le jeu est terminé.

    and then the game ends.

  95. Donc ici j'ai un jeu qui est déjà

    Right now, I’ve built a playable game.

  96. jouable, on peut rajouter évidemment beaucoup de choses,

    Of course, we can add many more features.

  97. mais surtout ce qu'on peut rajouter c'est au départ que la balle s'oriente dans une

    But especially, we can add this: at the start,

  98. direction aléatoire, parce que là en fait on a toujours le même angle au départ et

    the ball should head off in a random direction—

  99. donc en fait on a toujours le même jeu.

    because right now it always starts at the same angle, so the gameplay is always identical.

  100. Donc ce qu'on va faire ici c'est qu'on va remplacer" s'orienter à 45 degrés",

    So here, we’ll replace ‘point in direction 45°’,

  101. on va générer un nombre aléatoire, donc ici je suis dans les opérateurs,

    and instead generate a random number.

  102. nombre aléatoire, alors entre 1 et 10, on va changer ça,

    In the Operators category, pick ‘pick random’, initially set from 1 to 10—we’ll adjust that.

  103. sur Scratch les angles sont entre- 180 degrés et+ 180 degrés.

    In Scratch, angles range from −180° to +180°.

  104. Et là si je recommence la partie, plusieurs fois,

    Now, if I restart the game several times,

  105. la balle à chaque fois part dans une direction différente,

    the ball heads off in a different direction each time.

  106. vous voyez si je recommence.

    You see—I’ll restart again.

  107. Voilà, donc là on a un jeu qui est opérationnel,

    Here we have a working game.

  108. alors bien sûr on peut rajouter beaucoup de choses, le comptage des points,

    Of course, we can add many features—like scoring,

  109. etc, augmenter la difficulté, rajouter des bonus, etc, mais nous avons un jeu.

    etc. We can also increase difficulty, add bonuses, etc.—but we already have a game.

  110. qui est déjà opérationnel comme ceci.

    It’s already operational like this.

  111. Voilà, le script est terminé.

    And that’s it—the script is complete.

  112. Cet épisode est terminé, ne manquez pas le prochain épisode dans lequel nous verrons

    This episode ends here—don’t miss the next one,

  113. que les lutins peuvent communiquer entre eux.

    in which we’ll see how sprites can communicate with each other.

  114. À bientôt!

    See you soon!