Reputation: 51
I'm new to pygame and now wanted to blit a background image with a text on top of it. 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.
Upvotes: 0
Views: 578
Reputation: 649
I'm assuming you are using the time.sleep
function in your code. 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. 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: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()
It takes one parameter: ms
, which is the amount of time that you want the program to wait in milliseconds (1 second = 1000 milliseconds). In the function, I set a variable called start to pygame.time.get_ticks()
, which returns the current time. 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. After the event loop, I set a variable called now to pygame.time.get_ticks()
to get the current time. 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). This checks if the given amount of time has passed. If it has, the while loop ends. If not, the while loop keeps running until that condition is True.
I also wrote another function called blit_background
, which displays the background and the text on the screen. 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.
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()
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)
Upvotes: 1