[Original God, activate! 】Python-pygame implements Genshin Impact startup interface

 

Table of contents

Preface

video module (expired)

Import pictures

alpha channel

Play BGM

Synchronous playback


Preface

This is a new attempt with zero basics (.・ω・)ノ゙

I have been learning C++ before, but this computer seems to focus on one thing. I have been unsuccessful in building environments for languages ​​​​such as Java and C++. Only Python can barely be used, so the current practical part... can only be left to Python ( ̄ ~ ̄;)

Letting the program produce a simple animation effect in the window seems to be more interesting than entering a password or the like?

video module (expired)

(The code block has omitted the import and initialization process)

The video function is the first relatively direct method that comes to my mind. After all, it’s not impossible to just use the original video to fool people... right?

screen = pygame.display.set_mode((640, 480))# 创建Pygame显示窗口
video = pygame.movie.Movie("video.mp4")#  加载文件
video.play()# 播放视频

while video.get_busy(): # 等待视频播放结束
    for event in pygame.event.get():    # 处理Pygame事件
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(video.get_surface(), (0, 0))    # 将视频帧绘制到屏幕上
    pygame.display.flip()
 

But video seems to have been removed in Python3, so... when I saw the yellow highlight of "Movie", I already felt that something was wrong.

Import pictures

When the special font is missing, the text animation effect cannot be said to be greatly reduced in viewing quality, at least it is unsightly...(*/ω\*)

So I decided to directly use image gradient display to achieve the effect.

Take a picture first ↓ (the watermark is added by the system and cannot be deleted)

import pygame
pygame.init()  # 初始化
window = pygame.display.set_mode((1080, 640))  # 创建自定义大小的窗口(1080*640)
pygame.display.set_caption('Genshin Impact') # 设定了一个标题
window.fill((255, 255, 255)) # 窗口背景颜色,注意是RGB格式(此处为白)

Before displaying pictures, you should initialize pygame and build the window, and then import the pictures;

image1 = pygame.image.load('Files/SPLASH.jpg') # 加载图片
window.blit(image1, (0, 0)) # 进行渲染
pygame.display.flip() # 顺带着刷新一下

If the image is imported successfully, it does not mean that it can be displayed directly. You need to use window.blit() to render the image. The format in brackets is (rendering object, (abscissa, ordinate)). The horizontal and vertical coordinates are relative to the upper left corner. The upper left corner is (0,0)

display.flip() is only used for the first time. If it is refreshed later, just replace flip with update.

w, h = image1.get_size()# 定义了w,h作为宽,高;

W and h are defined as width and height; in calculations involving coordinates, the defined width and height can also be used directly.

(eg: 1280-w, 720-h)

alpha channel

set_alpha can be used to adjust the transparency of the image. Use the for loop below to achieve the gradient effect of the image, and finally achieve the image fade-in.

for a in range(1000):
    clock.tick(30)
    window.fill((255, 255, 255))
    if v == 50:
        picture.set_alpha(a*2)
    else:
        picture.set_alpha(255-a*10)

Play BGM

The title of a certain two-word game will display the title when the music reaches the climax of a bar , so we should keep it in sync , but the first priority is to get the music to play properly first

pygame.mixer.pre_init() # 初始化
pygame.mixer.music.load('Files/启动背景音乐.mp3') # 导入
pygame.mixer.music.play(-1) # 单曲循环

pygame.mixer.music.play(), what is filled in the brackets is the number of times to repeat the playback after finishing playing once.

If it is -1, it is a single loop

Synchronous playback

pygame.time.delay() can be set to execute the next step after a certain delay (in milliseconds).

The audio file here will climax at the 47th second, so the delay time should be 47000ms, which is 47s.

clock = pygame.time.Clock()

time_passed = clock.tick(6000)
pygame.time.delay(47000)

Show results

(Actually, this is the original animation)

Guess you like

Origin blog.csdn.net/m0_54909552/article/details/132368127