B1 · Episode 27 · 5:11
Introduction to Game Development with Pyxel
This episode explains the roles of the update and draw functions in Pyxel, how to use btnp to detect key presses (e.g., 'Q' to quit), cls to clear the screen with a color, and rect to draw shapes like rectangles — all within a simple Python game loop.
Source:jmbarbier / peertube.solidev.net · License:CC BY-SA 4.0
Transcript
Donc c'est pour ça, c'est décomposé en deux parties.
That’s why—it’s split into two parts.
Logiquement, les opérations un peu lourdes, on va les faire dans update.
Logically, the heavier operations go into the update function.
Et puis draw, c'est juste vraiment des appels de fonctions de dessin pour dessiner
Then draw is just about calling drawing functions to
ce qui a été décidé dans update.
draw what was decided in update.
PixelRun, on lance le jeu et on lui fournit comme argument,
PixelRun: When we launch the game, we pass it arguments—
donc l'argument de la fonction run ici,
specifically, the arguments of the run function here—
c'est la fonction update qui est là et la fonction draw qui est là.
namely, the update function and the draw function shown here.
Qu'est- ce qui se passe dans ces fonctions?
So, what actually happens inside these functions?
Le programme tourne.
The program runs.
Et il fait une boucle update draw, update draw,
It repeatedly loops through update and draw,
à plusieurs dizaines de fois par seconde.
dozens of times per second.
Donc update draw, update draw, update draw,
update, draw; update, draw; update, draw…
et c'est cette boucle de update draw qui fait que votre jeu se lance et que vous
It’s precisely this update-draw loop that
allez pouvoir jouer à l'intérieur.
makes your game launch and lets you play inside it.
programmer un comportement de jeu à l'intérieur.
You can also program game behavior inside it.
Donc si on regarde qu'est- ce qui se passe, le update, on a juste ici un
So let’s see what happens in update: there’s just one line here—
pixel btnpa. Alors qu'est- ce que c'est que btnp?
pixel.btnp(). So what is btnp?
Vous pouvez les regarder dans la documentation, BNP, ça fait partie des.
You can look it up in the documentation—BNP is part of…
chose qui permet de regarder si un bouton a été pressé.
functions that check whether a button has been pressed.
Je peux aussi regarder dans la doc de Pixel.
I can also check Pixel’s documentation.
BTNP. renvoie vrai si la touche qui, donc la touche qui est
btnp returns true if, at this instant, the key
donnée en premier argument
given as the first argument
est enfoncé à cet instant sinon ça renvoie false et
is being pressed; otherwise, it returns false.
après il y a des arguments quand on a un argument comme ça entre crochets c'est
Also, when an argument appears in square brackets,
qu'il est optionnel vous pouvez ne pas le mettre si vous le précisez hold et repeat
it means it’s optional—you may omit it. If you specify hold and repeat,
c'est pendant que la touche a été enfoncée pendant plus de n images
it means: while the key has been held down for more than n frames,
et Dans ce cas- là, pendant un certain nombre d'images,
then, for a certain number of frames,
on va renvoyer que vrai ou pas.
the function will keep returning true or false.
Si vous voulez avoir la liste des touches,
If you want to see the list of keys,
voilà la liste des touches.
here is the list of keys.
Donc pixel point, si vous voulez tester par exemple si c'est la page F12 qui a été
So pixel.point — if you want to test, for example, whether the F12 key was pressed,
faite, pixel point qui F12.
use pixel.point('F12').
Là ici, on va tester.
Here, we’ll run a test.
Q. Donc, qu'est- ce qui se passe dans le update?
The Q key. So what happens inside the update function?
Ici on a le if, le pixel bouton, donc on vérifie si le bouton,
Here we have an if statement and the pixel.button function, so we check whether a button,
la touche du clavier est enfoncée, quelle touche?
i.e., a key on the keyboard, is pressed.
Q. Dans ce cas- là, si la touche Q est enfoncée, pixel quit,
The Q key. In this case, if the Q key is pressed, pixel.quit is called,
on va quitter le jeu.
and the game will quit.
De l'autre côté, on a la fonction draw.
On the other side, we have the draw function.
Qu'est- ce qui va se passer dans la fonction draw?
What happens inside the draw function?
Pixel CLS. Qu'est- ce que ça veut dire CLS?
Pixel.CLS — what does CLS mean?
C'est pareil, on va regarder la documentation.
Same as before — let’s check the documentation.
efface l'écran avec la couleur col.
clears the screen with color 'col'.
Donc on va effacer l'écran avec la couleur.
So we’ll clear the screen using that color.
Alors quelles sont les couleurs de pixel?
So what colors does pixel support?
Vous avez ici la liste des couleurs: 0, 1, 2, 3, 4, 5, 6, 7,
Here’s the list of color numbers: 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10. Donc imaginez par exemple que je veux avoir un arrière- plan qui est
8, 9, 10. For instance, if I want a slightly violet background,
légèrement violet, je vais mettre 1.
I’ll choose number 1.
Donc pixel CLS, zéro c'était noir, je vais mettre un.
So in pixel.CLS, zero meant black — I’ll change it to one.
Et ensuite après on a toutes les fonctions de dessin,
Then, after that, we call all the drawing functions,
rect, donc ici rectangle pour créer un rectangle,
like rect — for drawing rectangles —
vous avez ça dans la documentation ici, rect.
you’ll find it documented here, under 'rect'.
Il est là, la position X, la position Y, donc le
It’s right here: the X position, the Y position —
coin en haut à gauche du rectangle, with la largeur, height la hauteur,
that is, the top-left corner of the rectangle; then 'width', the width; 'height', the height;
et col la couleur.
and finally 'col', the color.
Donc vous pouvez avoir un rectangle, vous pouvez faire un cercle,
So you can draw a rectangle, or you can draw a circle,
vous pouvez faire une ellipse, vous pouvez
you can also draw an ellipse,
colorier une partie d'écran.
and you can color part of the screen.
Voilà, donc il y a toutes ces
Here they are—these are all
possibilités- là pour faire des choses de côté graphique,
the available options for graphical operations,
ça c'est vraiment l'anti- sèche.
and this is truly the ultimate cheat sheet!
Et si vous voulez avoir le détail,
And if you want the details,
vous avez ici le très très grand readme qui va tout expliquer,
you’ll find here an extremely detailed README file that explains everything,
comment ça se passe, etc.
such as how it works, etc.
Vous avez la possibilité de faire aussi du son, de
You can also add sound and play music,
jouer de la musique, ça on le verra un peu plus tard.
and we’ll cover that a bit later.
Donc si je reviens ici, qu'est- ce qui se passe?
So, if I go back here—what happens?
Je suis en train de faire un rectangle à la position 10, 10,
I’m drawing a rectangle at position (10, 10),
de taille 20, 20 et de couleur 11.
with size 20 by 20, and color number 11.
La couleur 11, c'est...
Color 11 is…
Du vert bizarre.
a strange shade of green.
On peut, une fois qu'on a écrit le code,
Once you’ve written the code,
lancer le programme en appuyant sur Play. ça prend un certain temps surtout sur les
you can launch the program by clicking Play—it takes some time to compile, especially on a Raspberry Pi,
Raspberry Pi à compiler et vous avez ici votre espace de jeu et on a bien ici un
and then you’ll see your game area—with a purple background,
arrière plan dans les violets avec un carré qui a été mis ici.
and a square placed right here.
Il n'y a rien qui bouge, on a.
Nothing moves right now.
Pas de contrôle de jeu si ce n'est d'appuyer sur la touche Q.
There’s no game control except pressing the Q key to quit.
Et là, appuyer sur la touche Q, vu qu'on est en mode
Pressing Q now won’t exit much,
web, ça ne quittera pas grand- chose.
since we’re in web mode.
D'accord? Donc là, ici, l'appui sur la touche,
Understood? So here, pressing the key,
le quit ne sert pas à grand- chose puisqu'on n'est pas sur un jeu,
the ‘quit’ function isn’t very useful, since this isn’t a standalone game or application running on your computer,
une application sur l'ordinateur, mais on est dans le web,
but rather running in a web browser,
sur la version web.
i.e., the web version.