简体   繁体   English

Pygame 同时blit图文

[英]Pygame blit image and text simultaneously

I'm new to pygame and now wanted to blit a background image with a text on top of it.我是 pygame 的新手,现在想在背景图像上添加文字。 Both together should appear on the screen for only half a second, then both should be gone again.两者一起应该只出现在屏幕上半秒钟,然后两者都应该再次消失。 The background image is meant to be full-screen.背景图像是全屏的。

What I'm stuck on now : every time, a black screen appears shortly before text and image are visible, they disappear quickly again.我现在坚持的是:每次,在文本和图像可见之前不久出现黑屏,它们又很快消失了。

    pg.init()
    info = pg.display.Info()
    window = pg.display.set_mode((info.current_w, info.current_h), pg.FULLSCREEN)
    
    font = pg.font.SysFont("Arial", 33, bold=True)
    text = font.render(text, True, (0, 0, 0))
    background = pg.image.load('temp.png')
    
    window.blit(background, [0, 0])
    window.blit(text, text.get_rect(center = window.get_rect().center))
    pg.display.flip()
    sleep(0.5)
    pg.quit()

I feel like this has to be possibly quite easily, still I haven't found out how to do it just yet.我觉得这可能很容易,但我还没有找到如何做到这一点。

I'm assuming you are using the time.sleep function in your code.我假设您在代码中使用time.sleep function。 This function isn't good for GUIs since it delays the program for a given amount of time without checking for events and updating the display.这个 function 不适用于 GUI,因为它会延迟程序一段给定的时间,而不检查事件和更新显示。 This is why a black screen appears.这就是出现黑屏的原因。 So to fix your issue, I wrote a function called wait that waits a given amount of time without freezing the game.因此,为了解决您的问题,我编写了一个名为wait的 function 等待给定的时间而不冻结游戏。

The wait Function: wait Function:

def wait(ms):
    start = pg.time.get_ticks()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit(0)

        now = pg.time.get_ticks()
        if now - start == ms:
            running = False

        blit_background()

Explanation解释

It takes one parameter: ms , which is the amount of time that you want the program to wait in milliseconds (1 second = 1000 milliseconds).它需要一个参数: ms ,这是您希望程序等待的时间量,以毫秒为单位(1 秒 = 1000 毫秒)。 In the function, I set a variable called start to pygame.time.get_ticks() , which returns the current time.在 function 中,我将一个名为 start 的变量设置为pygame.time.get_ticks() ,它返回当前时间。 Then, inside a while loop, I created an event loop which checks for the pygame.QUIT event so that if the user tries to close the game while the function is still running, the game will respond and quit the program.然后,在一个 while 循环中,我创建了一个事件循环来检查pygame.QUIT事件,这样如果用户在 function 仍在运行时尝试关闭游戏,游戏将响应并退出程序。 After the event loop, I set a variable called now to pygame.time.get_ticks() to get the current time.在事件循环之后,我将一个名为 now 的变量设置为pygame.time.get_ticks()以获取当前时间。 Then I checked if now (the current time) subtracted by start (the start time) is equal to ms (the given amount of waiting time in milliseconds).然后我检查了现在(当前时间)减去 start(开始时间)是否等于ms (给定的等待时间,以毫秒为单位)。 This checks if the given amount of time has passed.这将检查给定的时间是否已经过去。 If it has, the while loop ends.如果有,while 循环结束。 If not, the while loop keeps running until that condition is True.如果不是,while 循环将继续运行,直到该条件为 True。

I also wrote another function called blit_background , which displays the background and the text on the screen.我还写了另一个名为 blit_background 的blit_background ,它在屏幕上显示背景和文本。 I am calling this function inside the wait function so that the screen can display the background and the text while also waiting for the given amount of time to pass.我在wait function 中调用这个 function 以便屏幕可以显示背景和文本,同时等待给定的时间过去。

The blit_background Function: blit_background Function:

def blit_background():
    window.blit(background, [0, 0])
    window.blit(text, text.get_rect(center=window.get_rect().center))
    pg.display.flip()

Full Modified Code:完整修改代码:

import pygame as pg
import pygame.time
import sys

pg.init()
info = pg.display.Info()
WIDTH = info.current_w
HEIGHT = info.current_h
window = pg.display.set_mode((WIDTH, HEIGHT), pg.FULLSCREEN)

font = pg.font.SysFont("Arial", 33, bold=True)
text = font.render("text", True, (0, 0, 0))
background = pg.image.load('temp.png')
background = pg.transform.scale(background, (WIDTH, HEIGHT))


def blit_background():
    window.blit(background, [0, 0])
    window.blit(text, text.get_rect(center=window.get_rect().center))
    pg.display.flip()


def wait(ms):
    start = pg.time.get_ticks()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit(0)

        now = pg.time.get_ticks()
        if now - start == ms:
            running = False

        blit_background()

wait(500)
pg.quit()
sys.exit(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM