issue173:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue173:python [2021/09/29 08:13] – d52fr | issue173:python [2021/10/03 17:09] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
**My adult son (35 years old) is a network administrator for the company he works for. He works with a huge amount of servers – handling all the network needs of his company and their employees. I started teaching him about computers when he was 10 years old. He (with a very little bit of help from me) built his first computer from a motherboard, | **My adult son (35 years old) is a network administrator for the company he works for. He works with a huge amount of servers – handling all the network needs of his company and their employees. I started teaching him about computers when he was 10 years old. He (with a very little bit of help from me) built his first computer from a motherboard, | ||
(Thanks to https:// | (Thanks to https:// | ||
+ | |||
+ | Mon fils adulte (35 ans) est administrateur de réseau pour la société pour laquelle il travaille. Il travaille avec un grand nombre de serveurs et répond à tous les besoins de son entreprise et des employés en matière de réseau. J'ai commencé à lui enseigner l' | ||
+ | (Merci à https:// | ||
**I admit it. When it comes to game controllers, | **I admit it. When it comes to game controllers, | ||
Ligne 7: | Ligne 10: | ||
Before we get into actually using and installing Arcade, we need to discuss virtual environments.** | Before we get into actually using and installing Arcade, we need to discuss virtual environments.** | ||
+ | |||
+ | Je l' | ||
+ | |||
+ | Cela fait longtemps que je voulais faire un article sur la création de jeux en Python et, il y a quelques mois (FCM168 partie 116 avril 2021), j'ai montré un jeu simple basé sur du texte lorsque j'ai parlé de la correspondance de motifs qui arrive dans Python 3.10. Les jeux en mode texte, je peux y jouer. Je me souviens aussi d' | ||
+ | |||
+ | Avant d' | ||
**Virtual Environments | **Virtual Environments | ||
Ligne 25: | Ligne 34: | ||
$ pyenv local 3.7.9** | $ pyenv local 3.7.9** | ||
+ | |||
+ | Environnements virtuels | ||
+ | |||
+ | Nous savons tous que Python utilise pip pour installer les bibliothèques (à moins que vous n' | ||
+ | |||
+ | Pyenv | ||
+ | |||
+ | Si vous utilisez pyenv, il est très facile de mettre en place un environnement virtuel et de le faire fonctionner. Voici les étapes à suivre. | ||
+ | |||
+ | Créez un dossier pour votre environnement virtuel et accédez-y. Par exemple : | ||
+ | |||
+ | $ mkdir Pyenv-Virt1 | ||
+ | |||
+ | $ cd Pyenv-Virt1 | ||
+ | |||
+ | Décidez de la version de Python que vous voulez utiliser pour l' | ||
+ | |||
+ | $ pyenv local 3.7.9 | ||
**Create the virtual environment and name it. You can name it pretty much anything you want. I like to preface the name with “venv” so I can easily remember it. | **Create the virtual environment and name it. You can name it pretty much anything you want. I like to preface the name with “venv” so I can easily remember it. | ||
Ligne 55: | Ligne 82: | ||
(venv379) greg@earth: | (venv379) greg@earth: | ||
+ | |||
+ | Créez l' | ||
+ | |||
+ | $ pyenv virtualenv 3.7.9 venv379 | ||
+ | |||
+ | Looking in links: / | ||
+ | |||
+ | Requirement already satisfied: setuptools in / | ||
+ | |||
+ | Requirement already satisfied: pip in / | ||
+ | |||
+ | greg@earth: | ||
+ | |||
+ | Activez l' | ||
+ | |||
+ | $ pyenv activate venv379 | ||
+ | |||
+ | pyenv-virtualenv: | ||
+ | |||
+ | (venv379) greg@earth: | ||
+ | |||
+ | (venv379) greg@earth: | ||
+ | |||
+ | Package | ||
+ | ---------- ------- | ||
+ | pip 21.2.4 | ||
+ | |||
+ | setuptools 47.1.0 | ||
+ | |||
+ | (venv379) greg@earth: | ||
**Notice that your terminal prompt has changed. This verifies that you are in a virtual environment. After this, you can install whatever packages you want. When you are done, deactivate the virtual environment with pyenv deactivate. Your prompt will return to normal. | **Notice that your terminal prompt has changed. This verifies that you are in a virtual environment. After this, you can install whatever packages you want. When you are done, deactivate the virtual environment with pyenv deactivate. Your prompt will return to normal. | ||
Ligne 73: | Ligne 130: | ||
greg@earth: | greg@earth: | ||
+ | |||
+ | Notez que l' | ||
+ | |||
+ | Python | ||
+ | |||
+ | Créez un nouveau dossier pour votre environnement virtuel et changez de répertoire. Appelez-le comme vous le souhaitez. | ||
+ | |||
+ | greg@earth: | ||
+ | |||
+ | greg@earth: | ||
+ | |||
+ | greg@earth: | ||
+ | |||
+ | greg@earth: | ||
+ | |||
+ | Assurez-vous que la bibliothèque d' | ||
+ | |||
+ | greg@earth: | ||
**Collecting virtualenv | **Collecting virtualenv | ||
- | < | + | |
- | | + | |
Requirement already satisfied: importlib-metadata> | Requirement already satisfied: importlib-metadata> | ||
Ligne 102: | Ligne 177: | ||
greg@earth: | greg@earth: | ||
+ | |||
+ | Collecting virtualenv | ||
+ | |||
+ | Downloading virtualenv-20.7.2-py2.py3-none-any.whl (5.3 MB) | ||
+ | | ||
+ | |||
+ | Requirement already satisfied: importlib-metadata> | ||
+ | ... | ||
+ | Successfully installed backports.entry-points-selectable-1.1.0 distlib-0.3.2 filelock-3.0.12 platformdirs-2.3.0 six-1.16.0 virtualenv-20.7.2 | ||
+ | |||
+ | greg@earth: | ||
+ | |||
+ | Créez votre environnement virtuel avec : | ||
+ | |||
+ | python -m venv env | ||
+ | |||
+ | Cela va créer un dossier env. Vous pouvez maintenant activer votre environnement virtuel. | ||
+ | |||
+ | greg@earth: | ||
+ | |||
+ | (env) greg@earth: | ||
+ | |||
+ | Tout comme pyenv change l' | ||
+ | |||
+ | Pour désactiver l' | ||
+ | |||
+ | (env) greg@earth: | ||
+ | |||
+ | greg@earth: | ||
**Installing Arcade | **Installing Arcade | ||
Ligne 114: | Ligne 218: | ||
You should see about 17 files in your folder. There is an entire tutorial that walks through most of these files at https:// | You should see about 17 files in your folder. There is an entire tutorial that walks through most of these files at https:// | ||
+ | |||
+ | Installation d' | ||
+ | |||
+ | Maintenant que notre environnement virtuel est configuré, nous pouvons enfin installer le paquet Arcade. N' | ||
+ | |||
+ | Utilisez pip pour installer Arcade dans votre environnement virtuel. | ||
+ | |||
+ | $ pip install arcade | ||
+ | |||
+ | Si tout se passe bien, votre prochaine étape sera de télécharger le tutoriel de plateforme. Vous pouvez le trouver à l' | ||
+ | |||
+ | Vous devriez voir environ 17 fichiers dans le dossier. Il existe un tutoriel complet qui traite de la plupart de ces fichiers à l' | ||
**This file is a skeleton that creates a simple window and sets the background color. It provides you a nice starting point for creating your own games. | **This file is a skeleton that creates a simple window and sets the background color. It provides you a nice starting point for creating your own games. | ||
Ligne 131: | Ligne 247: | ||
You will have to click the x in the title bar or do a < | You will have to click the x in the title bar or do a < | ||
+ | |||
+ | Ce fichier est un squelette qui crée une fenêtre simple et définit sa couleur de fond. Il vous fournit un bon point de départ pour créer vos propres jeux. | ||
+ | |||
+ | import arcade | ||
+ | |||
+ | # Constants | ||
+ | SCREEN_WIDTH = 1000 | ||
+ | SCREEN_HEIGHT = 650 | ||
+ | SCREEN_TITLE = " | ||
+ | |||
+ | Comme vous pouvez le voir, la fenêtre aura une largeur de 1 000 pixels et une hauteur de 650 pixels et portera le titre Platformer (Plateforme). C'est assez simple. La prochaine chose à faire est de créer une classe Python qui contiendra votre vrai jeu. Vous pouvez voir que la fonction __init__ contient le code qui définit le véritable écran et la couleur de fond et que la fonction setup démarre/ | ||
+ | |||
+ | Lorsque vous exécutez le programme squelette, vous verrez une fenêtre s' | ||
+ | |||
+ | $ python 01_open_window.py | ||
+ | |||
+ | Vous devrez cliquer sur le x dans la barre de titre ou faire un < | ||
**We will need to add some code in order to make the program show something other than just a pretty blue background. In your IDE or text editor, open the file 02_draw_sprites.py . This file is an expansion of the skeleton that we just ran. Between the constants at the top of the source code and the beginning of the class, you’ll see the following additional lines… | **We will need to add some code in order to make the program show something other than just a pretty blue background. In your IDE or text editor, open the file 02_draw_sprites.py . This file is an expansion of the skeleton that we just ran. Between the constants at the top of the source code and the beginning of the class, you’ll see the following additional lines… | ||
- | # Constants used to scale our sprites from their original size | + | # Constants used to scale our sprites from their original size |
CHARACTER_SCALING = 1 | CHARACTER_SCALING = 1 | ||
Ligne 143: | Ligne 276: | ||
These lines set the scaling for both the character sprite and the tiles. In the class, some code has been added to the class that sets up the character and some props. Between the super().__init__ line and the arcade.set_background_color line, you will see the following lines… | These lines set the scaling for both the character sprite and the tiles. In the class, some code has been added to the class that sets up the character and some props. Between the super().__init__ line and the arcade.set_background_color line, you will see the following lines… | ||
- | # These are ' | + | # These are ' |
- | # go into a list. | + | # go into a list. (entrer dans une liste) |
self.wall_list = None | self.wall_list = None | ||
self.player_list = None** | self.player_list = None** | ||
+ | |||
+ | Il faut ajouter un peu de code pour que le programme affiche autre chose qu'un joli fond bleu. Dans votre IDE ou votre éditeur de texte, ouvrez le fichier 02_draw_sprites.py. Ce fichier est une expansion du squelette que nous venons d' | ||
+ | |||
+ | # Constants used to scale our sprites from their original size (Les constantes utilisées pour redimensionner nos sprites à partir de leur taille originale) | ||
+ | |||
+ | |||
+ | CHARACTER_SCALING = 1 | ||
+ | |||
+ | TILE_SCALING = 0.5 | ||
+ | |||
+ | |||
+ | Ces lignes définissent la mise à l' | ||
+ | |||
+ | # These are ' | ||
+ | pour pouvoir suivre nos sprites. Chaque sprite devrait aller dans une liste.) | ||
+ | |||
+ | # go into a list. | ||
+ | |||
+ | self.wall_list = None | ||
+ | |||
+ | self.player_list = None | ||
Ligne 165: | Ligne 319: | ||
The next part of the code is very important in that it creates the player sprite. The actual sprite image can be anything you like. To help new users along, the good people that created Arcade, included a few sprite images that you might need in the actual distribution package. If you look in your virtual environment at the / | The next part of the code is very important in that it creates the player sprite. The actual sprite image can be anything you like. To help new users along, the good people that created Arcade, included a few sprite images that you might need in the actual distribution package. If you look in your virtual environment at the / | ||
+ | |||
+ | # Separate variable that holds the player sprite (Variable séparée qui tient le sprite du joueur) | ||
+ | |||
+ | self.player_sprite = None | ||
+ | |||
+ | Comme vous pouvez le voir dans les commentaires, | ||
+ | |||
+ | # Create the Sprite lists | ||
+ | |||
+ | self.player_list = arcade.SpriteList() | ||
+ | |||
+ | self.wall_list = arcade.SpriteList(use_spatial_hash=True) | ||
+ | |||
+ | La partie suivante du code est très importante, car elle crée le sprite du joueur. L' | ||
**Finally, the on_draw function of our class is modified by adding the following lines to the bottom of the function (after the start_render() call)... | **Finally, the on_draw function of our class is modified by adding the following lines to the bottom of the function (after the start_render() call)... | ||
Ligne 175: | Ligne 343: | ||
You can work through the rest of the tutorial which will get you a very basic, somewhat playable game in the file 17_views.py . When you run it, it should look something like this (bottom right) …** | You can work through the rest of the tutorial which will get you a very basic, somewhat playable game in the file 17_views.py . When you run it, it should look something like this (bottom right) …** | ||
+ | |||
+ | Enfin, la fonction on_draw de notre classe est modifiée en ajoutant les lignes suivantes au bas de la fonction (après l' | ||
+ | |||
+ | # Draw our sprites (Dessiner nos sprites) | ||
+ | self.wall_list.draw() | ||
+ | self.player_list.draw() | ||
+ | |||
+ | Bien que cela ne crée rien d' | ||
+ | |||
+ | Vous pouvez suivre le reste du tutoriel qui vous permettra d' | ||
**There is another program you should consider downloading, | **There is another program you should consider downloading, | ||
Ligne 181: | Ligne 359: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Il existe un autre programme que vous devriez envisager de télécharger, | ||
+ | |||
+ | Je fournirai sur mon dépôt un fichier zip pour les 17 fichiers sources du code du tutoriel dont vous avez besoin pour commencer, mais je vous suggère fortement de télécharger le dépôt d' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | |||
+ | //Textes en noir dans les encadrés :// | ||
+ | p. 16 : | ||
+ | **The main function instantiates the class, calls the setup function and then the game runs.** | ||
+ | La fonction principale instancie la classe, appelle la fonction setup, puis le jeu démarre. | ||
+ | |||
+ | p 17 : | ||
+ | **The ground that the character and any props are located is also set as a sprite.** | ||
+ | Le sol, où sont placés le personnage et tous les accessoires, | ||
+ | |||
+ | **Then some crates are placed on the ground and added to the game object.** | ||
+ | Puis quelques caisses sont placées sur le sol et ajoutées à l' | ||
+ | |||
issue173/python.1632895980.txt.gz · Dernière modification : 2021/09/29 08:13 de d52fr