issue173:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue173:python [2021/09/26 11:03] – créée auntiee | 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:// |
- | I admit it. When it comes to game controllers, | + | 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’ve wanted to do an article on creating games in Python for a long time, and, a few months ago (FCM168 part 116 April 2021), I showed a simple text based game when I talked about Pattern Matching coming up in Python 3.10. Text based games, I can pretty much play. I also remember talking about PyGame many, many years ago. FINALLY I get to start talking about (as my son would say) REAL games, with graphics and everything. I’m talking about the Arcade library for Python. | I’ve wanted to do an article on creating games in Python for a long time, and, a few months ago (FCM168 part 116 April 2021), I showed a simple text based game when I talked about Pattern Matching coming up in Python 3.10. Text based games, I can pretty much play. I also remember talking about PyGame many, many years ago. FINALLY I get to start talking about (as my son would say) REAL games, with graphics and everything. I’m talking about the Arcade library for Python. | ||
- | 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.** |
- | 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 | ||
We all know that Python uses pip to install libraries (unless you are using Anaconda and then you usually use Conda). However, many times a package you want to try out or develop with will have dependencies that conflict with packages that you already have installed. Often you won’t know that there are problems until something fails. Python, pyenv and Anaconda all provide virtual environments to get around this issue. Basically, when you create a virtual environment, | We all know that Python uses pip to install libraries (unless you are using Anaconda and then you usually use Conda). However, many times a package you want to try out or develop with will have dependencies that conflict with packages that you already have installed. Often you won’t know that there are problems until something fails. Python, pyenv and Anaconda all provide virtual environments to get around this issue. Basically, when you create a virtual environment, | ||
Ligne 23: | Ligne 32: | ||
Decide what version of Python you want to use for the virtual environment. For this project, I’m going to use 3.7.9 . Set the version for this folder. | Decide what version of Python you want to use for the virtual environment. For this project, I’m going to use 3.7.9 . Set the version for this folder. | ||
+ | |||
+ | $ 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 | $ 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. |
$ pyenv virtualenv 3.7.9 venv379 | $ pyenv virtualenv 3.7.9 venv379 | ||
Ligne 39: | Ligne 66: | ||
Activate the virtual environment and do a pip list to verify it’s brand new. | Activate the virtual environment and do a pip list to verify it’s brand new. | ||
+ | |||
+ | $ pyenv activate venv379 | ||
+ | |||
+ | pyenv-virtualenv: | ||
+ | |||
+ | (venv379) greg@earth: | ||
+ | |||
+ | (venv379) greg@earth: | ||
+ | |||
+ | Package | ||
+ | ---------- ------- | ||
+ | pip 21.2.4 | ||
+ | |||
+ | setuptools 47.1.0 | ||
+ | |||
+ | (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 activate venv379 | ||
Ligne 56: | Ligne 113: | ||
(venv379) greg@earth: | (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. |
Python | Python | ||
Ligne 71: | Ligne 128: | ||
Make sure that your python installation has the virtual environment library installed. Do a pip install virtualenv . | Make sure that your python installation has the virtual environment library installed. Do a pip install virtualenv . | ||
+ | |||
+ | 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: | greg@earth: | ||
- | Collecting virtualenv | + | **Collecting virtualenv |
Downloading virtualenv-20.7.2-py2.py3-none-any.whl (5.3 MB) | Downloading virtualenv-20.7.2-py2.py3-none-any.whl (5.3 MB) | ||
- | | + | |
Requirement already satisfied: importlib-metadata> | Requirement already satisfied: importlib-metadata> | ||
Ligne 100: | Ligne 175: | ||
(env) greg@earth: | (env) 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: | greg@earth: | ||
- | Installing Arcade | + | 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 | ||
Now that we have our virtual environment set up, we can finally install the Arcade package. Remember to activate the environment. For the purposes of this article, I’ll be using the “standard” Python virtual environment rather than the pyenv version. | Now that we have our virtual environment set up, we can finally install the Arcade package. Remember to activate the environment. For the purposes of this article, I’ll be using the “standard” Python virtual environment rather than the pyenv version. | ||
Ligne 113: | Ligne 217: | ||
If everything goes well, your next step will be to download the platformer tutorial. You can find it at https:// | If everything goes well, your next step will be to download the platformer tutorial. You can find it 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:// | + | You should see about 17 files in your folder. There is an entire tutorial that walks through most of these files at https:// |
- | 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. | + | 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. | ||
import arcade | import arcade | ||
Ligne 130: | Ligne 246: | ||
$ python 01_open_window.py | $ python 01_open_window.py | ||
- | 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 < |
- | 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… | + | 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. |
- | # Constants used to scale our sprites from their original size | + | 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… | ||
+ | |||
+ | # 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. (entrer dans une liste) | ||
+ | |||
+ | self.wall_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. | # go into a list. | ||
Ligne 152: | Ligne 306: | ||
- | # Separate variable that holds the player sprite | + | **# Separate variable that holds the player sprite |
self.player_sprite = None | self.player_sprite = None | ||
Ligne 164: | Ligne 318: | ||
self.wall_list = arcade.SpriteList(use_spatial_hash=True) | self.wall_list = arcade.SpriteList(use_spatial_hash=True) | ||
- | 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 / |
- | 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)... | + | # 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)... | ||
# Draw our sprites | # Draw our sprites | ||
Ligne 174: | Ligne 342: | ||
While this doesn’t create anything but a static image, you can see that, with a bit of planning and forethought, | While this doesn’t create anything but a static image, you can see that, with a bit of planning and forethought, | ||
- | 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) …** |
- | There is another program you should consider downloading, | + | 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, | ||
I will provide a zip file for all 17 source files of the tutorial code that you need to get started on my repository, but I strongly suggest that you download the arcade repository. There is a tremendous amount of sample files that will not only give you a large amount of information, | I will provide a zip file for all 17 source files of the tutorial code that you need to get started on my repository, but I strongly suggest that you download the arcade repository. There is a tremendous amount of sample files that will not only give you a large amount of information, | ||
- | 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.1632646982.txt.gz · Dernière modification : 2021/09/26 11:03 de auntiee