Pygame implements the Matrix screen effect

The screen effect of The Matrix is ​​shown in Figure 1.

Figure 1 The Matrix screen effect

To achieve the above function, you actually create two Surfaces in Pygame. One Surface has a pure black screen background and is used to cover the previous screen to achieve the effect of the numbers gradually disappearing; the other Surface is used to display the numbers to achieve the effect of the numbers moving down. Effect.

1 Create a Surface that displays numbers

Use the following code to create a full-screen Surface that displays numbers and has the effect of the numbers moving down.

screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)

Among them, pygame.FULLSCREEN means that the Surface is full screen.

2 Create a pure black screen background Surface

Use the pygame.Surface() method to create a pure black screen background Surface. The code is as follows.

screenwidth = screen.get_width()
screenheight = screen.get_height()
surface = pygame.Surface((screenwidth, screenheight), pygame.SRCALPHA)

The size of the Surface is the same as the Surface created in "1 Create a Surface to Display Numbers", so the Surface size specified by the first parameter of the pygame.Surface() method is the width and height of the screen; the second parameter pygame. SRCALPHA indicates that the Surface's pixel format has an alpha value, that is, transparency. Because we want to create a digital fade-out effect, the background Surface needs to be transparent.

3 Set the transparency of Surface

Set the transparency of Surface through the following code.

surface.fill((0, 0, 0, 10))

Among them, the first three 0s indicate that the background color of the Surface is (0,0,0), which is black, and the last digit indicates transparency.

Related Links 1 The transparency range is 0-255, 0 means completely transparent, and 255 means completely opaque.

The greater the value of the Surface's transparency, the "thicker" the Surface is on each layer, and the fewer layers it takes to completely cover the numbers. In other words, the larger the transparency value, the fewer numbers will be displayed in each column because less Surface is covered.

4 Create the display content

The content displayed on the screen is only "0" and "1", and the content to be displayed is created through list comprehension.

font = pygame.font.SysFont('宋体', 25)
texts = [font.render(i, True, (0, 255, 0)) for i in ['0','1']]

Among them, the function of the render() method is to create a new Surface to display "0" and "1". At this time, the two created Surfaces are saved in texts in the form of lists.

Related Links 2  For how to use font.render(), please refer to Pygame Displaying Text_Mianhou’s Blog-CSDN Blog

5 Cover screen with surface

Afterwards, in the while True loop, use a surface with transparency to cover the screen.

pygame.time.delay(50)
screen.blit(surface, (0, 0))

Because the displayed "0" and "1" need to have a fade-out effect, a delay needs to be added through pygame.time.delay. 50 means covering the surface on the screen every 50 milliseconds.

6 Display content in multiple rows and columns

In the while True loop, use a for loop to display "0" and "1" in multiple rows and columns.

for i in range(len(lst)):
        text = random.choice(texts)
        screen.blit(text, (i * 20, lst[i] * 20))
        lst[i] += 1
        if random.random() < 0.05:
            lst[i] = 0

Among them, lst is defined as

lst = list(range(99))

In the above code, i represents the number of rows displayed, and lst[i] represents the number of columns displayed. Randomly select the displayed content in texts through random.choice(); then display the content on the screen through the blit() method. The position of the displayed number is determined by i and lst[i]; after a certain row of numbers is displayed , it is necessary to add 1 to lst[i], so that the number will move downward; because the numbers in each column are not displayed until the last row before they are re-displayed from the first row, but are randomly displayed after a certain row. It will be displayed again from row 1, so add a random probability at the end of the code to make the value of lst[i] be 0, that is, a certain column will be displayed again from row 1.

At the end of the code, the pygame.display.flip() method is called to display the contents of the Surface on the screen.

pygame.display.flip()

7 complete code

import pygame
import random

pygame.init()
font = pygame.font.SysFont('宋体', 25)
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
screenwidth = screen.get_width()
screenheight = screen.get_height()
surface = pygame.Surface((screenwidth, screenheight), pygame.SRCALPHA)
surface.fill((0, 0, 0, 10))

texts = [font.render(i, True, (0, 255, 0)) for i in ['0','1']]
lst = list(range(99))
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT :
            exit()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                pygame.display.set_mode((400, 400))
            if event.key == pygame.K_f:
                pygame.display.set_mode((0,0), pygame.FULLSCREEN)
                #pygame.display.toggle_fullscreen()
    pygame.time.delay(50)
    screen.blit(surface, (0, 0))
    for i in range(len(lst)):
        text = random.choice(texts)
        screen.blit(text, (i * 20, lst[i] * 20))
        lst[i] += 1
        if random.random() < 0.05:
            lst[i] = 0
    pygame.display.flip()

Guess you like

Origin blog.csdn.net/hou09tian/article/details/133436382