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
Coder un jeu avec Scratch, quatrième épisode,
Coding a game with Scratch, episode four,
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
automatiquement vers un autre lutin.
to move automatically toward another sprite.
Par exemple ici, dans le jeu du chat et la souris,
For example, in this ‘cat and mouse’ game,
la souris se déplace automatiquement vers le chat.
the mouse moves automatically toward the cat.
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.
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
sorcière sur laquelle il doit tirer.
witch it must shoot.
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,
et nous y allons tout de suite.
and we’ll get started right away.
Alors nous y voici.
Alright, here we are.
Donc je reprends le jeu du chat et de la souris,
So I’m continuing with the ‘cat and mouse’ game,
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,
avec le script que nous avons écrit dans le premier épisode,
using the script we wrote in episode one,
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,
croquer. Alors j'ai déjà un script d'initialisation ici,
trying to catch it. I already have an initialization script here,
quand je clique sur le drapeau vert,
which runs when I click the green flag,
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,
parce que je trouvais qu'elle était trop.
because I thought it was too big before.
sans ça. Je l'envoie aux coordonnées- 200 145,
Without this, I send it to coordinates (-200, 145),
ce qui correspond au coin en haut à gauche de l'écran.
which corresponds to the top-left corner of the screen.
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,
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.
Donc c'est ce que je vais faire.
So that’s what I’ll do.
Donc ici, si je clique sur le drapeau vert, voilà,
So if I click the green flag now, look,
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,
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.
le chat. Alors pour cela, on va déjà commencer un nouveau script,
So first, let’s start a new script:
donc quand le drapeau vert est cliqué,
When the green flag is clicked,
nous avons un bloc dans les mouvements qui s'appelle" s'orienter vers",
there’s a block in the Motion category called ‘point toward…’,
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.
Je vais orienter la souris
I’ll orient the mouse toward
vers le lutin du petit chat.
the kitten sprite.
Alors si je clique sur le drapeau vert, je
So, if I click the green flag, I
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
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,
parce que le script a été lu une seule fois,
because this script runs only once,
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,
et puis le script est terminé.
and then the script ended.
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,
nous allons répéter cela.
we’ll add a loop.
va toujours s'orienter vers le chat.
That way, it will always face the kitten.
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—
voilà, la souris s'oriente vers le chat, elle fait face au chat.
look—the mouse immediately faces the kitten, pointing directly at it.
Il me reste plus qu'à la faire avancer.
Next, I just need to make it move forward.
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.
Et là, je vois qu'elle va très très vite.
Here, I see it’s moving extremely fast.
Le jeu est injouable à cette vitesse- là.
At this speed, the game is unplayable.
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,
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.
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.
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,
et elle va toujours vers le chat.
and it will keep heading toward the kitten.
Alors ici, si je laisse faire,
Now, if I let it run freely,
il se passe quelque chose d'un petit peu étrange. Vous voyez,
something slightly strange happens—you can see that
quand elle arrive sur le chat,
when it reaches the kitten,
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,
ça change de position.
so its position keeps changing.
Donc on va essayer de trouver un moyen d'éviter ça.
So, we’ll try to find a way to avoid this.
Donc ce que nous allons faire,
So what we’ll do is:
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
certaine distance du chat.
certain distance from the cat.
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
lutin, donc ici la souris, le pointeur de la souris, ou alors un lutin.
sprite—here, the mouse pointer—or another sprite.
Donc ce que nous allons faire,
So what we’ll do is:
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,
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.
Donc nous allons avoir à
So we’ll need to
vérifier si cette distance
check whether this distance
est plus grande que quelque chose.
is greater than some value.
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
donc j'ai pris le bloc ici plus grand que, donc si la distance entre
the mouse and the cat
la souris et le chat est plus grande que 5,
is greater than 5,
je vais pouvoir faire quelque chose.
I’ll be able to do something.
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.
Et voici ce que je vais faire.
And here’s what I’ll do.
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,
alors j'oriente la souris vers le chat et j'avance.
then I’ll orient the mouse toward the cat and move it forward.
Sinon, je ne fais rien.
Otherwise, I’ll do nothing.
Alors voyons voir ce que ça donne.
Let’s see how this works in practice.
Donc ici, la distance est plus grande que 5,
Here, the distance is greater than 5,
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,
vous voyez la distance de 5 est quasiment négligeable ici,
you’ll see that the 5-unit distance becomes almost negligible,
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
qui était un effet désagréable de graphique.
we saw earlier.
Ici, le script fonctionne.
Here, the script works.
Alors on peut rajouter encore quelque chose,
So we can add something else,
parce que la souris a deux costumes. Lorsque
since the mouse sprite has two costumes. When
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.
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,
a avancé. Voici.
and here it is.
Et si je la fais déplacer ici, voilà,
And if I make it move here, look,
on a l'impression qu'elle marche ou qu'elle court vers le chat,
it appears to walk or run toward the cat,
et elle s'arrête si elle a touché le chat.
and stops as soon as it touches the cat.
Voilà, le script est fonctionnel, il est complet, on peut certainement.
There we go—the script is fully functional and complete. We can certainly
l'améliorer, on verra ça plus tard,
improve it—we’ll cover that later,
mais nous avons quelque chose qui fonctionne. Cet épisode est terminé,
but for now, we have something that works. This episode is over,
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,
é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,
jouable. Ne ratez pas ça, à bientôt.
so stay tuned—see you next time!