Hello, I’m Yue Chuang. Over the next 30 days, I’ll be updating the basic Game development tutorial for Python PyGame. Each article will correspond to a video tutorial, which will be published on the public account: AI Joychuang.

directory

This time, the goal of the article:

  1. Third-party libraries for Python
  2. A brief introduction to PyGame
  3. Create form, background

1. Third-party Python libraries

Python itself has some basic functions and functions, but obviously cannot cover all the functions we need, such as image processing, massive data calculation and analysis, and so on. In this case, third-party libraries are needed.

This third-party library uses PIP in Python to manage installation, upgrade, uninstall, and so on; The official Python library website is pypi.org/

The following article will use the PyGame library to develop Python games. We will need to install PyGame. To install PyGame, type the following command on the terminal:

pip install pygame
Copy the code

The installation requires a network connection. If “Done” “Success” is seen, the installation is successful. If the pyGame library cannot be found during subsequent coding, reinstall it.

2. Brief introduction to PyGame

Pygame is a popular library for Python games. It provides functions that can handle graphics, text, sound, etc. There are some commercial game projects that use PyGame, but in general it is not suitable for large game development. It’s going to be a lot of functions and things like that, and it’s going to be a little harder).

Example 1: Create a new file to test the pyGame installation and learn how to import the library.

# file:Lesson_01.py
import pygame Import the PyGame library
Then import the Aliens example from the PyGame library
import pygame.examples.aliens
Run the main entry in Aliens
pygame.examples.aliens.main()
Copy the code

Code parsing:

  • import pygame: To use additional libraries, import the corresponding library;
  • You can also use the “From library name import specific features” method if you only want to import certain features of the library.
  • This example comes with reference examples, an alien invasion small game, run no problem that pyGame installed successfully, examples lower level there are many other sample games, can be viewed by themselves.

Running result:

3. Create a form, background

Case 1: By writing a form interface, to experience pyGame initialization, color processing, event acquisition and other functions.

Description:

After starting the game to write a long code, the following will be given according to the steps, and explain, some will also have a simple flow chart, in addition to the code notes will also indicate the key tips.

# file:Lesson_02.py
import pygame Import the PyGame library
pygame.init() Call the initialization function
Set the window width and height
size = width, height = 600.400
# ↑ The above multivariate assignment is then assigned to size, which becomes a tuple
# Next create screen, also variable store can be
screen = pygame.display.set_mode(size)
After the first step, the program will have a window flash
Copy the code

Code parsing:

  • At size, the multivariate assignment is performed first, and the two values are assigned to width and height, and then to size, which becomes a tuple (similar to a list, but cannot be changed). At this time, the overall multiassignment is formed. If size is printed, the result will be (600, 400).
  • Screen receives objects created by PyGame, a concept we’ll learn more about later.

After running, you will see a black window as shown below.

Description:

  • Next, follow the first step to write, mainly complete continuous running, screen update and click the upper right corner x to exit the program, three functions.

  • To exit, you need to use the SYS library, which is a built-in library. You don’t need to download or install it. Just import sys in the header of the file.

To keep the program running, you need to use a loop

import sys
To keep the program running, you need to use a loop
while True:
    # In the loop, decide whether to exit after each loop
    for event in pygame.event.get():
        Use the for loop to get the current PyGame Form event
        if event.type == pygame.QUIT:
            # If the event type is QUIT
            sys.exit() # Then call system exit
    # Update the window screen after each judgment
    pygame.display.update() # update means to update
# -- After step 2, the window will not blink and can be closed with the mouse
Copy the code

Code parsing:

  • I parsed every line of code and wanted to read the comments;
  • Pay attention to indentation. Every time you encounter a loop, a judgment, a function, etc., pay attention to four Spaces to indent. This will show the hierarchy and make the program work as expected (Python strictly relies on indentation to distinguish different blocks of code).
  1. Next, add the window name and background color, both of which precede the loop.
# ↓ Can set the name of the window
pygame.display.set_caption("My Game")
# Define a list of stored background colors, represented by RGB colors
# Search RGB color table to select your favorite color value
bgcolor = [0.255.255]
The # background color needs to be filled with fill(), which we put in the loop
Copy the code
  1. The actual fill color code is written inside the loop.
screen.fill(bgcolor) # Fill the background color
Copy the code

Code parsing:

  • Bgcolor: is a list that holds the RGB information of the background color, but note that variables are only digital containers until they are actually used, and you cannot see the background effect after setting the variables.

  • The background color is set in a loop so that the background can be refreshed each time, and the values stored in the BGColor are actually used.

In order to let the small white also can understand, I put out the complete code of this stage.

import pygame Import the PyGame library
import sys
pygame.init() Call the initialization function
Set the window width and height
size = width, height = 600.400
# ↑ The above multivariate assignment is then assigned to size, which becomes a tuple
# Next create screen, also variable store can be
screen = pygame.display.set_mode(size)
After the first step, the program will have a window flash

# ↓ Can set the name of the window
pygame.display.set_caption("My Game")
# Define a list of stored background colors, represented by RGB colors
# Search RGB color table to select your favorite color value
bgcolor = [0.255.255]
The # background color needs to be filled with fill(), which we put in the loop

To keep the program running, you need to use a loop
while True:
    # In the loop, decide whether to exit after each loop
    for event in pygame.event.get():
        Use the for loop to get the current PyGame Form event
        if event.type == pygame.QUIT:
            # If the event type is QUIT
            sys.exit() # Then call system exit
    screen.fill(bgcolor) # Fill the background color
    # Update the window screen after each judgment
    pygame.display.update() # update means to update
# -- After step 2, the window will not blink and can be closed with the mouse
Copy the code

Run the program again, and the form name and background change.

AI joycreate · launched tutorials, including “Python language tutorials, C++ tutorials, algorithm/data structure tutorials, children’s programming, pygame game development”, all one-to-one teaching: one-to-one tutoring + one-to-one q&a + assignment + project practice, etc. QQ, wechat online, at any time to respond! V: Jiabcdefh