This article is participating in Python Theme Month. See the link to the event for more details
Pygame is a powerful and interesting set of modules for managing graphics, animation, and even sound, making it easy to develop complex games. By using Pygame to handle tasks such as drawing images on the screen, you don’t have to worry about a lot of tedious hard coding, but focus on the high-level logic of the program.
Install Python
Website address: www.python.org/downloads/
This series of blog posts uses Python3.6.8
For 64-bit systems, you can download the Windows x86-64 Executable Installer, double-click the Python installation package, install it through the GRAPHICAL interface, and then set the Python installation path. After that, configure the Python3 and Python3 Scripts directories to environment variables.
Install Pygame
Address: www.lfd.uci.edu/~gohlke/pyt…
Or use PIP install PyGame directly to install, convenient and fast.
Know pygame
From sys import exit # Import the exit function from sys library import PyGame # Import the PyGame library from PyGame.locals import * # Import some constants from PyGame library # Define the resolution of the window SCREEN_WIDTH = 480 SCREEN_HEIGHT = 320 def run_game(): # Initialize the game and create a screen object pygame.init() # Initialize a window for display # Object Screen is a surface, in PyGame, surface is part of the screen, Screen = pygame.display.set_mode(); screen = pygame.display.set_mode(); screen = pygame.display.set_mode(); Pygame.display.set_caption ("This is my first Pygame-program ") For event in Pygame.event.get (): if event.type == pygame.quit: sys.exit() run_game() for event.event.get (): if event.type == pygame.quit: sys.exit() run_game()Copy the code
As you can see from the comments above, this is a game that uses PyGame and uses the game to exit the game. We set up our own game name: This is my First PyGame-Program.
Of course we can also load the background image and use the game interface to have the background.
background = pygame.image.load('resources/image/background.jpg') while True: Pygame.display.update () pygame.display.update() pygame.display.update() pygame.display.update() pygame.display.update()Copy the code
There is a logic to this. Whenever we play a game, we need a while True. It’s an infinite loop. If the game is not over, you need to use this loop to update the game logic.
Overall, the game is not difficult to get started with and is easy to learn. The above idea is still said relatively clear. There is a need to take the complete game source code, please move to the public number: like the code poem. Now that I’m in, it’s not easy to be original. Let’s go with a “like”.