Python PyGame Example 1

2d-rpg

Project Structure

First, create the game project and select the required folders, etc. let’s create it.

In the core folder of the game modules, the source files, such as images, maps or XML, will also remain in the data folder.
Inside folders __init__.py to see the files, folders as a Python package. The file we work with is from characters.player import player will be able to import.

  • The Game Module and Life Cycle

Main module of the game main.py it will. PyGame is installed on my system because I did not hear the need to use virtualenv if you want to look here.

Now main.py create a class as follows or similar to it.

In the game class, you can first create a Pygame in the __init__ function and specify the window title and the workspace (self.screen. Window variables come as parameters during the creation of the class.

The clock in the class will hold the game time, the number of images that will be played in fps, the keyboard keys that are pressed at that moment, __pressed.

The game revolver is in loop. Game events with events (click, key press, etc.) respectively.) with update, the player will determine the features of the game objects, such as enemies, render, and finally the clock will draw the images that need to be printed on the screen.tick will advance the clock of the game.

Events (pygame.event.get) we got the events that worked for us. The running variable, which controls the game life cycle when the game is closed, is false. If a key has been pressed (key down) or if a key has been pressed (key up), the key value in __pressed is true or False.

Finally, we created an instance of the game class-giving the necessary parameters – and started it with the start function. If there is no problem Python main.py when you do a black screen will come to you.

 

80%
Awesome
  • Design

Leave A Reply

Your email address will not be published.