Python PyGame Example 1
Project Structure
First, create the game project and select the required folders, etc. let’s create it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | Project ├── core │ ├── characters │ │ ├── __init__.py │ │ └── player.py │ │ │ ├── managers │ │ ├── __init__.py │ │ ├── resourcemanager.py │ │ └── tilemanager.py │ │ │ ├── maps │ │ ├── __init__.py │ │ └── island.py │ │ │ ├── mobs │ │ └── __init__.py │ │ │ ├── tiles │ │ └── __init__.py │ │ │ └── __init__.py │ ├── data │ ├── maps │ ├── sprites │ └── tiles │ └── main.py |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #!/usr/bin/env python # -*- coding: utf-8 -*- import pygame class Game: def __init__(self, display, flags, depth): pygame.init() pygame.display.set_caption("2D RPG") self.screen = pygame.display.set_mode(display, flags, depth) self.running = True clock = pygame.time.Clock() fps = 30 __pressed = {} def start(self): self.load() self.loop() def load(self): pass def loop(self): while self.running: self.events() self.update() self.render() self.clock.tick(self.fps) def update(self): pass def render(self): self.screen.fill((255, 255, 255)) # render işleri pygame.display.update() def events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False pygame.quit() elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP: self.__pressed[str(event.key)] = event.type == pygame.KEYDOWN if __name__ == "__main__": game = Game((800, 600), 0, 32) game.start() |
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.
https://www.kalogroup.com.au/. https://stellproekt.ru купить самоходные штабелеры.