A2 · Episode 6 · 4:56
Character Interactions in Scratch
This episode teaches how to program interactions between sprites in Scratch, such as triggering a pixelation effect when the mouse touches the cat.
Source:Montagne Vincent / tube-numerique-educatif.apps.education.fr · License:CC BY 4.0
Transcript
Coder un jeu avec Scratch, sixième épisode,
Creating a game with Scratch, episode six,
dans lequel nous allons voir que les lutins peuvent avoir des interactions entre
in which we’ll learn how sprites can interact with each other.
eux. Comme par exemple ici dans le jeu du chat et de la souris,
For example, in this ‘cat and mouse’ game,
lorsque la souris touche le chat, il se passe quelque chose,
when the mouse touches the cat, something happens—
il y a une interaction, la souris grossit, le chat est pixelisé.
an interaction occurs—the mouse grows larger, and the cat becomes pixelated.
Et dans l'autre jeu à côté, lorsque les tirs du vaisseau spatial,
And in the other game beside it, when the spaceship’s shots—also sprites—
qui sont des lutins, lorsque les tirs touchent la sorcière également,
touch the witch,
il y a une interaction qui apparaît entre le tir
an interaction appears between the shot
et la sorcière, la sorcière est pixelisée et le tir disparaît.
and the witch—the witch becomes pixelated, and the shot disappears.
Donc c'est ce genre d'interaction que nous allons apprendre à coder avec Scratch,
So, we’re going to learn how to code exactly this kind of interaction using Scratch.
et nous y allons tout de suite.
Let’s get started right away!
Alors, nous y voici.
Here we are.
Vous reconnaissez donc, je suis avec le jeu du chat et la souris,
You’ll recognize it—I’m working with the ‘cat and mouse’ game,
vous reconnaissez le script que nous avons écrit à l'épisode précédent pour déplacer
and you’ll recall the script we wrote in the previous episode,
le chat avec les touches du clavier.
to move the cat using keyboard keys.
Nous avons ici le script que nous allons continuer,
Here is the script we’ll continue building—
c'est le script d'initialisation que nous avons écrit à l'épisode 3.
the initialization script we wrote back in episode three.
Donc ça c'est l'initialisation du chat.
This initializes the cat.
Pour la souris, j'ai aussi l'initialisation que nous avons vue à l'épisode 3,
For the mouse, I’ve also kept the initialization script we saw in episode three,
et son mouvement que nous avons vu à l'épisode 4 pour que la souris se dirige toute
and its movement script from episode four,
seule vers le chat.
so the mouse moves toward the cat on its own.
Alors si je clique sur le drapeau vert ici, la souris poursuit le chat,
When I click the green flag, the mouse chases the cat,
mais il ne se passe rien quand elle l'attrape,
but nothing happens when it catches the cat.
et c'est ce que nous allons essayer de faire maintenant qu'il se passe quelque chose
So now, we’ll make something happen
quand la souris attrape le chat.
when the mouse catches the cat.
Alors pour cela, nous allons donc compléter le script du chat,
So for this, we’ll complete the cat’s script.
on va écrire à la suite du script d'initialisation,
We’ll write it right after the initialization script.
et nous allons avoir une condition, c'est si la souris touche le chat,
We’ll add a condition: if the mouse touches the cat,
alors il va se passer quelque chose.
then something will happen.
Donc je mets un bloc conditionnel si alors,
So I’ll place an ‘if-then’ conditional block.
et comment je sais si la souris a touché le chat?
How do I know whether the mouse has touched the cat?
Je vais aller dans les capteurs,
I’ll go to the ‘Sensing’ category.
et ici j'ai un bloc qui s'appelle toucher avec un point d'interrogation.
Here I have a block called ‘touching’, with a question mark.
Et on peut savoir si le lutin a touché le pointeur de la souris,
It can detect whether the sprite has touched the mouse pointer,
ou bien s'il a touché le bord de l'écran,
or whether it has touched the edge of the stage,
nous verrons ça dans le prochain épisode,
We’ll cover that in the next episode.
ou bien s'il a touché un des autres lutins.
or whether it has touched another sprite.
Donc je vais vérifier ici, est- ce que le chat a touché la souris?
So I’ll check here: has the cat touched the mouse?
Et là, je veux qu'il se passe quelque chose.
And here, I want something to happen.
Alors ce que je voudrais moi, c'est un effet de pixelisation
What I’d like is a pixelation effect on the cat,
sur le chat, donc je vais dans Apparence,
so I’ll go to the ‘Looks’ category,
et nous allons utiliser les effets de pixelisation, donc on va ajouter.
and use the pixelation graphic effect—so I’ll click ‘add’.
Par exemple 50 à l'effet graphique de pixelisation.
For example, set the pixelation effect to 50.
Alors on a plusieurs effets
There are several graphic effects here,
graphiques ici, vous pouvez les essayer un par un pour voir ce qu'ils donnent.
and you can try them one by one to see what they do.
Alors si je regarde maintenant qu'est- ce qui se passe.
Now let’s see what happens.
Alors j'aimerais bien vous poser la question, qu'est- ce qui va se passer?
I’d like to ask you: what will happen next?
Il se passe rien, et on va essayer de savoir pourquoi.
Nothing happens—and we’ll figure out why.
Il se passe rien parce que quand je clique sur le drapeau vert,
Nothing happens because when I click the green flag,
le script va être lu.
the script runs just once.
Donc il y a l'initialisation, le lutin est allé à sa place,
So first, initialization: the sprite moves to its position,
il s'est mis à la bonne taille, il est sur le bon costume,
resizes to the correct scale and switches to the right costume,
l'orientation est la bonne,
and faces the correct direction,
et la boucle, si la souris est touchée, on ajoute 50
then enters a loop: if the mouse touches the sprite,
à l'effet pixelisé.
add 50 to the 'pixelate' effect.
Cela ne s'est pas fait parce que quand j'ai cliqué sur le drapeau vert,
This didn’t work because when I clicked the green flag,
le script s'est lu très rapidement,
the script ran extremely quickly,
c'est quasiment instantané la lecture de ce script,
nearly instantaneously,
et ça n'a été lu qu'une seule fois.
and only ran once.
Donc ici, quand j'ai cliqué sur le drapeau vert, la
So just now, when I clicked the green flag,
souris n'est pas sur le chat.
the mouse wasn’t over the cat sprite.
Tiens, là on verra après ce qui s'est passé.
Look—what happened will become clear in a moment.
Donc ça n'a pas fonctionné.
So it didn’t work.
Donc ce que nous allons faire,
So what we’ll do next is,
c'est que nous allons dire au jeu de répéter ça indéfiniment durant tout le jeu.
tell the game to repeat this action indefinitely throughout gameplay.
Donc je vais rajouter ici une boucle" répéter indéfiniment".
So I’ll add a 'forever' loop here.
Et là, ça devrait marcher, j'espère.
Now it should work—I hope so.
Voilà, il y a un effet de pixellisation qui s'est mis.
There—we can see the pixelation effect has been applied.
Alors, il se passe des choses, vous voyez,
Hmm, something’s happening—you can see it,
on va régler ce problème tout de suite. Quand j'ai cliqué sur le drapeau vert,
and we’ll fix this issue right away. When I clicked the green flag just now,
On va recommencer, voilà, donc là le chat a disparu,
let’s restart—look, the cat disappeared,
donc l'effet de pixellisation, il a été fait un petit peu trop,
meaning the pixelation effect was applied too strongly,
on va voir pourquoi tout à l'heure.
and we’ll examine why shortly.
Donc si maintenant je clique sur le drapeau vert, regardez ce qui se passe,
Now, if I click the green flag again, watch what happens:
le lutin apparaît pixellisé.
the sprite appears already pixelated.
Si je recommence, tiens, ça l'a pas fait, c'est bizarre.
If I restart, look, it didn’t happen this time—that’s strange.
Je recommence.
I’ll try again.
Je clique sur le drapeau vert, le lutin apparaît pixelisé.
I click the green flag, and the sprite appears pixelated.
Alors pourquoi cela?
So why is that?
Parce que mon lutin est
Because my sprite
dans la position initiale ici, je vais faire aller la souris jusqu'à lui.
is at its starting position—I’ll move the mouse over it.
Voilà, donc la souris est positionnée ici,
There—we’ve positioned the mouse right here,
le lutin l'effet pixelisé s'est répété un petit peu trop de fois,
but the sprite’s pixelation effect has repeated too many times,
parce que en fait le test" si la souris touchée, je fais l'effet graphique,
since the condition checks ‘if the mouse touches the sprite, apply the graphic effect’,
j'ajoute 50 à l'effet graphique",
and each time adds 50 to the graphic effect value,
ça va se répéter ça tant que la souris touche le chat.
so this keeps repeating as long as the mouse stays touching the sprite.
Et ici c'est le cas puisque la souris ne bouge plus, elle est sur le chat,
And that’s exactly what’s happening now—the mouse isn’t moving; it’s staying on the sprite,
donc ça se répète beaucoup de fois.
so the effect repeats over and over.
Donc on va limiter ça.
So we’ll need to limit it.