A2 · Episode 4 · 5:45

Making a Sprite Automatically Chase Another Sprite

This episode teaches how to program a sprite (e.g., the mouse) in Scratch to automatically orient itself toward and move continuously toward another sprite (e.g., the cat), using distance sensing, conditional logic, and animation.

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

Transcript

  1. Coder un jeu avec Scratch, quatrième épisode,

    Coding a game with Scratch, episode four,

  2. dans lequel nous allons voir comment programmer un lutin pour qu'il se déplace

    in which we’ll learn how to program a sprite

  3. automatiquement vers un autre lutin.

    to move automatically toward another sprite.

  4. Par exemple ici, dans le jeu du chat et la souris,

    For example, in this ‘cat and mouse’ game,

  5. la souris se déplace automatiquement vers le chat.

    the mouse moves automatically toward the cat.

  6. Le chat est joué par le joueur, mais la souris est jouée par l'ordinateur.

    The cat is controlled by the player, but the mouse is controlled by the computer.

  7. Et dans le jeu à côté, le vaisseau spatial s'oriente automatiquement vers la

    And in the game next to it, the spaceship automatically turns toward the

  8. sorcière sur laquelle il doit tirer.

    witch it must shoot.

  9. Voilà, donc c'est ce que nous allons apprendre à coder avec Scratch,

    So that’s what we’re going to learn to code with Scratch,

  10. et nous y allons tout de suite.

    and we’ll get started right away.

  11. Alors nous y voici.

    Alright, here we are.

  12. Donc je reprends le jeu du chat et de la souris,

    So I’m continuing with the ‘cat and mouse’ game,

  13. j'ai donc le petit chat qui se dirige avec les flèches du clavier,

    and the little cat is currently steered using the keyboard arrow keys,

  14. avec le script que nous avons écrit dans le premier épisode,

    using the script we wrote in episode one,

  15. et je veux que la souris se dirige toute seule vers le chat pour essayer de le

    and now I want the mouse to move toward the cat on its own,

  16. croquer. Alors j'ai déjà un script d'initialisation ici,

    trying to catch it. I already have an initialization script here,

  17. quand je clique sur le drapeau vert,

    which runs when I click the green flag,

  18. je réduis la taille de la souris à 75% de sa taille initiale,

    I reduce the mouse’s size to 75% of its original size,

  19. parce que je trouvais qu'elle était trop.

    because I thought it was too big before.

  20. sans ça. Je l'envoie aux coordonnées- 200 145,

    Without this, I send it to coordinates (-200, 145),

  21. ce qui correspond au coin en haut à gauche de l'écran.

    which corresponds to the top-left corner of the screen.

  22. Je la fais s'orienter vers la droite pour ne pas qu'elle tourne le dos à l'écran,

    I make it face right, so it doesn’t turn its back to the screen,

  23. et je la montre parce que à la fin du jeu, j'ai un script qui cache les lutins.

    and I show it, because at the end of the game, there’s a script that hides all sprites.

  24. Donc c'est ce que je vais faire.

    So that’s what I’ll do.

  25. Donc ici, si je clique sur le drapeau vert, voilà,

    So if I click the green flag now, look,

  26. la souris est allée se mettre en place, je peux diriger le lutin,

    the mouse has moved into position—I can steer the cat sprite,

  27. et je veux maintenant écrire quelque chose qui me permette d'orienter la souris vers

    and now I want to write something that lets me orient the mouse toward the cat.

  28. le chat. Alors pour cela, on va déjà commencer un nouveau script,

    So first, let’s start a new script:

  29. donc quand le drapeau vert est cliqué,

    When the green flag is clicked,

  30. nous avons un bloc dans les mouvements qui s'appelle" s'orienter vers",

    there’s a block in the Motion category called ‘point toward…’,

  31. et on peut s'orienter vers le pointeur de la souris ou vers un lutin.

    and we can point toward either the mouse pointer or toward a sprite.

  32. Je vais orienter la souris

    I’ll orient the mouse toward

  33. vers le lutin du petit chat.

    the kitten sprite.

  34. Alors si je clique sur le drapeau vert, je

    So, if I click the green flag, I

  35. vois que les lutins sont mis en position initiale et la souris s'est orientée vers

    see all sprites reset to their starting positions, and the mouse has oriented itself toward

  36. le chat effectivement, mais si je bouge le chat, il ne se passe rien,

    the kitten—indeed! But if I move the kitten, nothing happens,

  37. parce que le script a été lu une seule fois,

    because this script runs only once,

  38. quand j'ai cliqué sur le drapeau vert, elle s'est orientée vers le chat,

    specifically when I clicked the green flag—it oriented toward the kitten,

  39. et puis le script est terminé.

    and then the script ended.

  40. Donc nous allons lui dire de répéter cela indéfiniment pendant tout le jeu,

    So we’ll tell it to repeat this action indefinitely throughout the game,

  41. nous allons répéter cela.

    we’ll add a loop.

  42. va toujours s'orienter vers le chat.

    That way, it will always face the kitten.

  43. Et maintenant, si je recommence le script, si je clique sur le drapeau vert,

    Now, if I restart the script—by clicking the green flag again—

  44. voilà, la souris s'oriente vers le chat, elle fait face au chat.

    look—the mouse immediately faces the kitten, pointing directly at it.

  45. Il me reste plus qu'à la faire avancer.

    Next, I just need to make it move forward.

  46. Donc je veux qu'elle soit face au chat et je vais la faire avancer.

    So I want it facing the kitten first, then I’ll make it advance.

  47. Et là, je vois qu'elle va très très vite.

    Here, I see it’s moving extremely fast.

  48. Le jeu est injouable à cette vitesse- là.

    At this speed, the game is unplayable.

  49. Donc pour régler la vitesse, au lieu de la faire avancer de 10,

    So to adjust the speed—instead of moving 10 steps at once,

  50. on va la faire avancer par exemple de 2 ou de 3, ce qui est plus raisonnable.

    we’ll move it just 2 or 3 steps instead—which feels much more reasonable.

  51. Donc si je recommence la partie, voilà, la souris se dirige vers le chat.

    So if I restart the game—look—the mouse heads straight toward the kitten.

  52. Alors là, c'est peut- être un petit peu lent, on pourrait augmenter la vitesse,

    But here, it might be a bit slow—we could increase the speed a little,

  53. et elle va toujours vers le chat.

    and it will keep heading toward the kitten.

  54. Alors ici, si je laisse faire,

    Now, if I let it run freely,

  55. il se passe quelque chose d'un petit peu étrange. Vous voyez,

    something slightly strange happens—you can see that

  56. quand elle arrive sur le chat,

    when it reaches the kitten,

  57. il y a un effet graphique en fait qui fait que la souris se retourne en permanence,

    a visual effect kicks in, causing the mouse to flip back and forth continuously,

  58. ça change de position.

    so its position keeps changing.

  59. Donc on va essayer de trouver un moyen d'éviter ça.

    So, we’ll try to find a way to avoid this.

  60. Donc ce que nous allons faire,

    So what we’ll do is:

  61. c'est que nous allons faire dérouler ce script uniquement si la souris est à une

    we’ll run this script only if the mouse is at a

  62. certaine distance du chat.

    certain distance from the cat.

  63. Alors dans les capteurs, nous avons un capteur qui détecte la distance entre le

    In the Sensors category, we have a sensor that detects the distance between the

  64. lutin, donc ici la souris, le pointeur de la souris, ou alors un lutin.

    sprite—here, the mouse pointer—or another sprite.

  65. Donc ce que nous allons faire,

    So what we’ll do is:

  66. c'est que si la distance entre le chat et la souris est plus grande que 2,

    if the distance between the cat and the mouse is greater than 2,

  67. 3, 4, 5, alors on va faire diriger la souris vers le chat.

    3, 4, or 5, then we’ll make the mouse turn toward the cat.

  68. Donc nous allons avoir à

    So we’ll need to

  69. vérifier si cette distance

    check whether this distance

  70. est plus grande que quelque chose.

    is greater than some value.

  71. Donc ici dans les opérateurs, je peux comparer,

    In the Operators category, I can compare values—so I’ve selected the ‘greater than’ block here. Thus, if the distance between

  72. donc j'ai pris le bloc ici plus grand que, donc si la distance entre

    the mouse and the cat

  73. la souris et le chat est plus grande que 5,

    is greater than 5,

  74. je vais pouvoir faire quelque chose.

    I’ll be able to do something.

  75. Et donc c'est une condition, si alors, donc je prends un bloc ici si alors.

    This is an ‘if-then’ conditional, so I’ll use the ‘if-then’ block here.

  76. Et voici ce que je vais faire.

    And here’s what I’ll do.

  77. Donc si la distance de la souris au chat est plus grande que 5,

    If the distance from the mouse to the cat is greater than 5,

  78. alors j'oriente la souris vers le chat et j'avance.

    then I’ll orient the mouse toward the cat and move it forward.

  79. Sinon, je ne fais rien.

    Otherwise, I’ll do nothing.

  80. Alors voyons voir ce que ça donne.

    Let’s see how this works in practice.

  81. Donc ici, la distance est plus grande que 5,

    Here, the distance is greater than 5,

  82. donc la souris se dirige vers le chat. Et si la souris arrive sur le chat,

    so the mouse moves toward the cat. And when the mouse reaches the cat,

  83. vous voyez la distance de 5 est quasiment négligeable ici,

    you’ll see that the 5-unit distance becomes almost negligible,

  84. et il n'y a pas l'effet qu'il y avait tout à l'heure,

    and there’s no more of that unpleasant graphical flickering effect

  85. qui était un effet désagréable de graphique.

    we saw earlier.

  86. Ici, le script fonctionne.

    Here, the script works.

  87. Alors on peut rajouter encore quelque chose,

    So we can add something else,

  88. parce que la souris a deux costumes. Lorsque

    since the mouse sprite has two costumes. When

  89. l'on enchaîne les deux costumes, on a l'impression que la souris marche.

    we switch between them rapidly, it looks like the mouse is walking.

  90. Donc dans Apparence, on va la faire passer au costume suivant à chaque fois qu'elle

    So in the ‘Looks’ category, we’ll make it switch to the next costume each time it moves forward,

  91. a avancé. Voici.

    and here it is.

  92. Et si je la fais déplacer ici, voilà,

    And if I make it move here, look,

  93. on a l'impression qu'elle marche ou qu'elle court vers le chat,

    it appears to walk or run toward the cat,

  94. et elle s'arrête si elle a touché le chat.

    and stops as soon as it touches the cat.

  95. Voilà, le script est fonctionnel, il est complet, on peut certainement.

    There we go—the script is fully functional and complete. We can certainly

  96. l'améliorer, on verra ça plus tard,

    improve it—we’ll cover that later,

  97. mais nous avons quelque chose qui fonctionne. Cet épisode est terminé,

    but for now, we have something that works. This episode is over,

  98. ne ratez pas le prochain épisode dans lequel nous reprendrons le script du premier

    don’t miss the next episode, where we’ll revisit the script from Episode 1,

  99. épisode pour obtenir des mouvements un peu plus fluides et donc un jeu bien plus

    to achieve smoother movements—and thus a much more playable game,

  100. jouable. Ne ratez pas ça, à bientôt.

    so stay tuned—see you next time!