A2 · Episode 8 · 4:56
Mouse and Muffin Game: Sprite Interaction in Scratch
This episode teaches how to program sprite interactions in Scratch: making a muffin disappear and play a sound when the mouse touches it, and then reappear at a random position on screen.
Source:ericgrisperez / tube-numerique-educatif.apps.education.fr · License:CC BY 4.0
Transcript
Bonjour, dans cette vidéo, nous allons continuer de travailler sur notre jeu de chat
Hello, in this video, we’ll continue working on our cat-and-mouse game.
et de souris. Pour rendre le jeu plus intéressant,
To make the game more interesting,
j'ai ajouté des éléments que la souris doit récupérer tout en ne se faisant pas
I’ve added items that the mouse must collect,
attraper par le chat.
while avoiding being caught by the cat.
Donc je lance mon jeu et je vais voir qu'au bout d'un temps,
So I’ll launch my game and, after a while,
je vais avoir un petit muffin à aller récupérer.
a small muffin will appear for me to collect.
Donc quand la souris se déplace, elle peut manger le muffin,
So when the mouse moves, it can eat the muffin,
elle récupère.
and it collects points.
Je dois me dépêcher de faire le tour sans que le chat m'attrape.
I need to hurry and circle around without getting caught by the cat!
Si le chat m'attrape, évidemment.
If the cat catches me, of course—
La partie s'arrête.
the game ends.
Nous allons voir comment faire en sorte qu'au contact, le muffin disparaisse.
We’ll now see how to make the muffin disappear upon contact.
Nous voici dans la partie code du lutin Muffin.
Here we are in the Muffin sprite’s coding section.
Donc ici, ton travail sera double.
So your task has two parts.
Avec les blocs que tu as au dessus,
Using the blocks above you,
cela va te permettre de gérer l'emplacement du muffin.
you’ll manage where the muffin appears.
Tu as remarqué tout à l'heure, à chaque fois que la souris mangeait un muffin,
You may have noticed earlier: each time the mouse eats a muffin,
un autre réapparaissait
another one reappears,
ailleurs sur l'écran.
but at a random location elsewhere on the screen.
Donc c'est ici où ce bloc position aléatoire va te servir.
That’s where this ‘go to random position’ block comes in handy.
La position aléatoire, c'est en fait l'ordinateur qui choisit un point X et un point
‘Random position’ means the computer picks an X-coordinate and a Y-coordinate,
Y tout à fait au hasard sur l'ensemble du repère que l'on a ici dans Scratch.
completely at random, across the entire Scratch stage.
Donc tu devras avancer ces premiers blocs pour gérer l'emplacement du muffin à
So you’ll need to drag these first blocks forward,
chaque fois que la souris l'attrape.
so the muffin’s position updates each time the mouse touches it.
Deuxième travail, ce sera de générer un son à chaque fois que la souris attrape un
The second task is to play a sound each time the mouse grabs a muffin.
muffin. Donc à chaque fois que la souris est en contact avec le muffin,
So whenever the mouse touches the muffin,
un son doit se déclencher.
a sound must trigger instantly.
Le muffin a déjà une taille de cinquante et une position de départ que tu retrouves
The muffin is already size 51, and its starting position is shown here.
ici. Donc deux programmes à faire.
So we need to write two programs.
Tu peux mettre pause sur la vidéo pour tenter de résoudre ce défi.
You can pause the video to try solving this challenge yourself.
Voyons le corriger ensemble.
Let’s correct it together.
Donc nous allons commencer par la partie haute sur la position du muffin.
So we’ll start with the top part—specifically, the muffin’s position.
Donc bien évidemment, je commence par un drapeau vert en tant qu'événement
Of course, I start with the green flag as the program’s trigger event.
déclencheur du programme.
It starts the entire program.
Puis ensuite, je me souviens un petit peu comment réagissait le jeu,
Then, I recall how the game behaved originally:
le muffin n'était pas visible à l'écran au moment de lancer mon jeu.
At the moment the game starts, the muffin isn’t visible on screen.
Je peux déjà dire qu'au moment de lancer le jeu, le muffin est donc caché.
So at game launch, the muffin must be hidden.
Une fois que le muffin est caché,
Once the muffin is hidden,
on avait un petit temps avant que le premier muffin apparaisse,
there’s a short delay before the first muffin appears,
donc on va attendre trois secondes.
so we’ll wait three seconds.
Donc le muffin est caché, on attend trois secondes,
The muffin is hidden, and we wait three seconds,
on va donc ensuite lui demander de se montrer.
then ask it to show itself.
Donc le muffin apparaît à l'écran.
The muffin then appears on screen.
Puis nous allons demander à l'ordinateur de systématiquement placer
Next, we’ll ask the computer to place the muffin
le muffin à un endroit aléatoire.
at a random location each time.
Donc si je prends la boucle infinie et une position aléatoire,
If I use an infinite loop and a random-position block,
je peux lancer le jeu, voir ce que ça donne.
I can run the game and see how it works.
Donc ici on attend les trois secondes.
Here, we’re waiting for those three seconds.
Et là, on voit que la
And now you see that
souris est partout sur l'écran et n'arrête pas de bouger.
the mouse cursor moves all over the screen nonstop.
Ici, parce qu'il est obligé de remettre à chaque fois le muffin quelque part.
That’s because it keeps repositioning the muffin somewhere new every time.
Donc ici, on va demander au lutin muffin d'attendre que la souris vienne l'attraper.
So here, we’ll tell the muffin sprite to wait until the mouse clicks it.
C'est- à- dire qu'il conservera cette
That is, it will keep this
position tant que la souris ne l'aura pas attrapé.
position until the mouse catches it.
Donc ici, on va dire qu'on va placer le muffin à un endroit au hasard sur l'écran,
So here, we’ll place the muffin at a random spot on the screen,
puis l'ordinateur va devoir attendre
and then the computer must wait
que la souris vienne le manger.
until the mouse comes to eat it.
Je relance mon jeu.
I restart my game.
je vois le buffle apparaître
I see the buffalo appear,
elle est effectivement et restera
and it really stays still,
si je me déplace avec ma souris pour l'attraper
if I move my mouse to catch it.
Le muffin va bien se positionner ailleurs.
The muffin will then move to another spot.
Donc là, hop, je l'attrape, il va encore ailleurs, etc.
So, there—once I catch it, it jumps elsewhere again, and so on.
Donc là, le muffin nous attend.
Right now, the muffin is waiting for us.
Pour changer de position.
To change its position.
Passons maintenant à la partie son.
Now let’s move on to the sound part.
Donc je souhaiterais qu'à chaque fois que la souris attrape un muffin,
I want a clear sound to play each time the mouse catches a muffin,
il y a un bruit significatif pour m'indiquer que j'ai bien réussi.
to clearly signal that I’ve succeeded.
Donc ici, de même manière, je commence par un drapeau événement pour déclencher mon
Here again, I start with an event flag to trigger my program. Then, thinking it through,
programme. Et là ensuite, si je réfléchis,
what should happen next?
ça va être si la souris touche le muffin,
It should be: when the mouse touches the muffin,
donc si muffin est touché par souris,
that is, when the condition ‘muffin is touched by mouse’ is true,
alors je dois jouer le son chomp jusqu'au bout.
then play the ‘chomp’ sound all the way to the end.
Ici on sait bien que cette surveillance doit être faite tout le temps,
We know this detection must run continuously,
donc je n'oublie pas d'intégrer ma condition dans cette boucle et de rattacher mon
so I mustn’t forget to put my condition inside this loop,
bloc d'événements.
and attach my event block.
Je lance mon jeu.
I launch my game.
le muffin est là, je l'attrape,
The muffin is there; I grab it,
et j'ai bien le son qui se déclenche quand je l'attrape.
and the sound effect plays correctly when I grab it.
Donc là nous avons réussi.
So, we’ve succeeded.
Donc voilà déjà une première progression dans notre jeu.
Here’s our first milestone in the game.