A2 · Episode 9 · 6:28
Inter-sprite messaging in Scratch
This episode teaches how sprites in Scratch—such as the cat, mouse, dog, and 'Game Over' text—can communicate with each other by broadcasting and receiving messages to coordinate actions at key moments, like ending a game.
Source:Montagne Vincent / tube-numerique-educatif.apps.education.fr · License:CC BY 4.0
Transcript
Coder un jeu avec Scratch, huitième épisode dans lequel nous allons voir que les
Coding a game with Scratch—Episode 8, in which we’ll see that
lutins peuvent communiquer entre eux.
sprites can communicate with each other.
Alors par exemple, ici, dans le jeu du chat et de la souris,
For example, in this ‘cat-and-mouse’ game,
il y a deux lutins que vous ne voyez pas à l'écran et qui attendent que le chat ou
there are two sprites you don’t see on screen, waiting for the cat or
la souris leur envoie un message leur disant que la partie est terminée.
the mouse to send them a message saying the game is over.
Lorsque le chat va être touché par la souris, il envoie un message à ce lutin,
When the cat gets touched by the mouse, it sends a message to this sprite—the
le chien, et au lutin game over que vous voyez à l'écran ici.
blue dog—and to the ‘game over’ sprite you see on screen here.
Il envoie un message comme quoi la partie est terminée et que c'est le moment pour
It sends a message indicating the game is over, and that it’s time for
eux de rentrer en scène.
them to appear on stage.
Donc nous allons voir comment ajouter ce genre de communication entre lutins avec
So we’ll learn how to add this kind of communication between sprites using
Scratch et nous y allons tout de suite.
Scratch—and we’ll start right away!
Alors nous y voici.
Here we are.
J'ai toujours le chat et la souris, mais j'ai rajouté donc deux lutins,
I still have the cat and the mouse, but I’ve added two more sprites:
le chien bleu ici et le texte game over qui est aussi un lutin.
this blue dog, and the text ‘game over’, which is also a sprite.
Au niveau des scripts, j'ai toujours le script ici, je suis sur le chat,
In terms of scripts, I still have these scripts—I’m currently on the cat,
donc j'ai son script de déplacement et puis j'ai son initialisation et ce qui se
so there’s its movement script, its initialization script,
passe. de jeu, lorsque la souris touche le chat ou lorsque le chat touche la souris,
and what happens during gameplay—when the mouse touches the cat, or the cat touches the mouse,
avec l'effet graphique.
with a visual effect.
Pour la souris, pareil, j'ai toujours les mêmes scripts avec son initialisation,
For the mouse, likewise, I still have the same scripts: initialization,
ses déplacements, la souris se déplace toute seule vers le chat,
movement—the mouse moves automatically toward the cat,
on a déjà vu ça dans les épisodes précédents.
which we’ve already covered in earlier episodes.
Et qu'est- ce qui se passe quand elle touche le chat?
And what happens when it touches the cat?
C'est la fin du jeu, donc il y a un effet graphique ici aussi.
The game ends, so there’s a visual effect here too.
Ensuite, si je passe au chien, j'ai un script ici avec une initialisation,
Now let’s look at the dog—I’ve written a script for it, including initialization,
donc un déplacement qui donne l'impression que le chien marche jusqu'au milieu de
so a movement that makes it appear to walk toward the center of the screen,
l'écran, et puis il bascule sur le costume qui lui donne l'air songeur.
and then it switches to a costume that makes it look thoughtful.
Alors je peux cliquer sur le script pour voir ce que ça donne.
So, I can click on the script to see what happens.
Voilà, il avance, et quand il arrive au centre,
Here it goes forward, and when it reaches the center,
il s'arrête et il prend l'air songeur.
it stops and looks thoughtful.
Et ça, je veux que ça se passe à la fin du jeu.
And I want this to happen at the end of the game.
Donc vous voyez ici, j'ai pas mis quand est- ce que ce script va se dérouler,
So as you can see here, I haven’t specified when this script should run,
donc là si je lance le jeu, le script ne va pas se dérouler.
so if I start the game now, the script won’t run.
Pareil pour le lutin Game Over, alors une petite initialisation.
The same applies to the Game Over sprite—let’s do a quick initialization.
en réduisant la taille, et un effet de zoom en répétant 20 fois
First, shrink its size, then zoom in by repeating 20 times,
l'agrandissement de la taille de 3.
each time increasing the size by 3.
Et pareil, ce script- là ne sera jamais lancé tel quel,
Likewise, this script won’t run automatically as it is,
puisque je n'ai pas mis à quelles conditions le script va se dérouler.
since I haven’t set any conditions for it to trigger.
Alors première chose, c'est quand je commence le jeu ici,
First, when I start the game here,
si je clique sur le drapeau vert, le chien et le game over sont au milieu du jeu,
clicking the green flag makes both the dog and the Game Over sprite appear in the center,
et ça je ne veux pas, je veux qu'ils apparaissent une fois que la partie est
but that’s not what I want—I want them to appear only after the game ends.
terminée. Donc ce qu'on va commencer, c'est quand le drapeau vert est
So first, when the green flag is clicked,
cliqué, on va cacher le lutin game over, et on va faire pareil avec le chien.
we’ll hide the Game Over sprite—and do the same for the dog sprite.
Donc vous voyez, quand j'ai un script que je veux recopier vers un autre lutin,
As you can see, when I want to copy a script to another sprite,
j'ai juste à le prendre et à le faire glisser ici sur l'autre lutin.
I just drag and drop it onto that other sprite.
Et maintenant, pour le chien, j'ai le même script.
Now the dog sprite has the same script too.
Donc maintenant, quand je clique sur le drapeau vert, ils disparaissent.
So now, when I click the green flag, they disappear.
Bon maintenant, je veux qu'ils réapparaissent et que les scripts se passent à un
Now, I want them to reappear,
moment qui est la fin du jeu.
and have their scripts run precisely at the end of the game.
Quand est- ce que la fin du jeu apparaît, devra se lancer,
So, when exactly should the end of the game be triggered?
c'est quand le chat touche la souris et quand la souris touche le chat.
It’s when the cat touches the mouse—and the mouse touches the cat.
Donc ce qu'on va faire, c'est que nous allons créer un événement,
So we’ll create an event,
si je vais dans menu événement ici.
by going to the Events menu here.
à partir de messages.
from messages.
Vous voyez les conditions auxquelles on peut faire démarrer les scripts.
You see the conditions under which scripts can be started.
Donc quand le drapeau vert est cliqué, ça on l'utilise tout le temps.
So when the green flag is clicked, we use this one all the time.
Quand une touche du clavier est cliquée, ça on l'a déjà utilisé.
When a key on the keyboard is pressed, we’ve already used this one.
Quand un lutin est cliqué, quand on change d'arrière- plan par exemple,
When a sprite is clicked—such as when changing the background,
quand le volume sonore dépasse un certain niveau, ou quand on reçoit un message.
when the sound volume exceeds a certain level, or when a message is received.
Et ça c'est très bien, c'est ce qu'on va utiliser.
And that’s great—that’s exactly what we’ll use.
Les lutins peuvent envoyer des messages aux autres lutins.
Sprites can send messages to other sprites.
Alors ici, je vais faire envoyer un message par le petit chat,
So here, I’ll make the kitten send a message:
une fois qu'il aura été touché par la souris, il
once it’s clicked by the mouse,
va attendre une seconde, l'effet graphique va se dérouler,
it will wait one second, and the visual effect will play,
on attend une seconde et on va envoyer un message.
then wait another second, and send the message.
On va faire un nouveau message, je vais l'appeler"
We’ll create a new message—I’ll name it
game over" ce message.
'game over'.
Donc il va envoyer" game over" à tous les autres lutins.
So it will send 'game over' to all other sprites.
Alors, maintenant, si je retourne sur le script du chien,
Now, if I go back to the dog’s script,
quand il reçoit.
when it receives...
Le message game over, on
the 'game over' message,
va dérouler ce script.
we’ll run this script.
Pareil pour le texte game over, quand il reçoit le message game over,
The 'game over' text works the same way: when it receives the 'game over' message,
on va dérouler ce texte.
we’ll display this text.
Donc si je regarde ce qui se passe maintenant,
So now, let’s see what happens:
donc je vais faire perdre, voilà.
I’ll make it lose—there, just like that.
Donc le chat envoie le message au bout d'une seconde.
The kitten sends the message after one second.
Qu'est- ce qui se passe ici?
What’s happening here?
Si vous avez observé, là le lutin ici,
If you noticed, this sprite here,
je voyais qu'il bougeait.
I saw it moving.
J'ai caché le lutin quand on a cliqué sur le drapeau vert,
I hid the sprite when the green flag was clicked,
mais je ne l'ai pas montré une
but I didn’t show it once it received the 'game over' message,
fois qu'il reçoit le message game over.
not even once.
Donc ici, je dois montrer le lutin,
So here, I need to show the sprite,
alors on va le montrer après l'avoir mis à la bonne taille.
so we’ll show it after resizing it correctly.
Et ici, pour le chien, pareil,
And for the dog sprite, likewise,
on va montrer le lutin une fois qu'il sera à sa bonne position.
we’ll show the sprite once it reaches its correct position.
Alors je recommence.
So I’ll restart.
On va perdre la partie, voilà, le chien apparaît
We’ve lost the game—look, the dog appears,
et le texte Game Over aussi apparaît.
and the 'Game Over' text appears too.
Alors je vais juste rajouter une petite chose,
Now I’ll just add a small feature:
c'est que je veux cacher la souris et le chat quand la partie est finie.
When the game ends, I want to hide both the mouse and the cat.
Donc je vais cacher ici.
So I’ll hide them here.
Une fois qu'il aura envoyé le message Game Over.
Once it has sent the 'Game Over' message.
Mais si je le cache ici, il ne faut pas oublier dans l'initialisation de le montrer,
But if I hide it here, don’t forget to show it again during initialization,
ce qui était déjà présent ici.
which was already set up here.
Et pour la souris, pareil, je vais la cacher
For the mouse, same thing—I’ll hide it,
une fois qu'elle aura.
once it finishes…
On va la cacher une fois qu'elle recevra le message Game Over elle aussi,
We’ll hide the mouse once it receives the 'Game Over' message too,
donc quand elle reçoit le message Game Over, on cache la souris.
so when it receives that message, we hide the mouse.
Donc si je lance maintenant le jeu, voilà, le chat s'est fait croquer,
Now let’s run the game—look, the cat got eaten,
il disparaît, la souris disparaît, le chien et le lutin Game Over apparaissent.
it disappears; the mouse disappears; and the dog and 'Game Over' sprite appear.
Donc voilà comment faire communiquer entre des lutins,
So this is how sprites communicate with each other:
on envoie un message que les autres lutins reçoivent sous la forme d'un événement.
One sprite sends a message, and others receive it as an event.
Ce script est maintenant terminé.
This script is now complete.
Cet épisode est terminé, ne manquez pas le prochain épisode dans lequel nous verrons
This episode is now over—
comment utiliser le chronomètre qu'il y a dans Scratch.
don’t miss the next episode, where we’ll learn how to use the timer in Scratch.
À bientôt.
See you soon!