Moment For Technology

[Python-PyGame] Develop your own 2D action game out of nothing (1) Project creation | August more text challenges

Posted on Dec. 3, 2022, 8:39 a.m. by Samiha Gokhale
Category: The back-end Tag: python The back-end

Install the Python and PyGame libraries

Download the Python installation package from and install it.

(I am using Python3.7, which has been installed before. No screenshots are available.)

Use PIP Install PyGame to install the PyGame library with one click.

2, new window

Before using PyGame, you need to initialize the display module, otherwise the screen will not be displayed.

import pygame
Copy the code

Next, you need to create a window to display the screen.

Screen = pygame.display.set_mode((width, height))Copy the code

Use Screen to receive the created window. Screen will be used frequently in the future.

We get the window we want by setting display. In this case, we pass in the tuple of the width and height to set the size of the generated window.

At this point, we will run the program, we will find that there is a window flashed, the terminal shows that the program has completed.

To be able to persist a window, you need to use a loop to keep the state of the program.

Also, use PyGame's event listener to capture control names and respond accordingly.

The complete code above is as follows:

Import PyGame # main program, # Build screen (800*600) width = 800 height = 600 screen = pygame.display.set_mode((width, The window created above will be closed when the program finishes executing, and we need to use a loop to maintain while True: For event in Pygame.event.get (): if event.type == Pygame.quit: # Exit pygame.quit()Copy the code

Running the above code, we get a black window that does nothing.

Three, background generation

  • Some words:
  • Since the current plan is to make a 2D action game, we need to use scroll maps, and the length should change dynamically with the parameters of each level. We can use the way of map Mosaic to realize the infinite extension of the map, and the reuse of the map block can also reduce the time spent on art, I recommend it.

Create the resourse directory in the project directory to store resource files, and create the background subdirectory to store background blocks.

First try inserting an image:

# Make the background white, Convenient to show the from pygame. Color import THECOLORS screen. The fill (THECOLORS [' white ']) filename = '. / resourse/background/tree. PNG ' Picture = pygame.image.load(filename) # add.convert_alpha() to keep opacity 0] # Pygame.display.flip ()Copy the code

Load the image using Pygame.image.load, insert it into the window using screen.blit, and the array behind it represents the vertex coordinates [left, top], and the top left is [0, 0].

Always use it at the endpygame.display.flip()Refresh the screen, otherwise the screen is still black!!

  • The realization principle of screen refresh:
  1. We do the screen modification in the cache (equivalent to the back of the artboard)

  2. This is the front display that stays the same.

  3. After the modification is completed, we need to move the picture to the front desk for display (equivalent to flipping the artboard over).

The image is displayed correctly in the window (the material was pulled from the previous project).

The demo image has a resolution of 150 * 400, and the background is extended using a simple for loop.

for i in range(6):
    screen.blit(picture, [150 * i, 0])
Copy the code

This is what you end up with, and you can expand the scope as needed (you'll see it in a post or two if you look outside the window).

Four, trailer

Next, the structure will be combed and a simple package will be made to facilitate subsequent expansion.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.