0% found this document useful (0 votes)
83 views2 pages

Timed Text and Image in Pygame

The document provides code examples for displaying timed text and images in a pygame window. The first section shows how to render text to a surface and display it for 3 seconds before ending the loop. The second section loads an image, displays it for a duration of 3 seconds in the center of the screen, then stops displaying it after the time elapsed. Both sections initialize pygame, set up a display window, track elapsed time, and end the loop after the set duration is reached.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views2 pages

Timed Text and Image in Pygame

The document provides code examples for displaying timed text and images in a pygame window. The first section shows how to render text to a surface and display it for 3 seconds before ending the loop. The second section loads an image, displays it for a duration of 3 seconds in the center of the screen, then stops displaying it after the time elapsed. Both sections initialize pygame, set up a display window, track elapsed time, and end the loop after the set duration is reached.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

TIMED WORDS ON WINDOW:

pygame.init()
window = pygame.display.set_mode((500, 500))
font = pygame.font.Font(None, 36)
text_surface = font.render('WORDS HERE', True, (255, 255, 255))
text_rect = text_surface.get_rect()
text_rect.center = (250, 250)
running = True
duration = 3000 # Duration in milliseconds (3 seconds)
start_time = pygame.time.get_ticks()

while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

current_time = pygame.time.get_ticks()
elapsed_time = current_time - start_time

if elapsed_time >= duration:


running = False

window.fill((0, 0, 0))
window.blit(text_surface, text_rect)
pygame.display.update()

ADDING A TIMED IMAGE ON WINDOW

# Initialize Pygame
pygame.init()
WIDTH, HEIGHT = 800, 600
FPS = 60
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

# Load image
image = pygame.image.load('image.png') # Replace 'image.png' with your image file
path
image_rect = image.get_rect()

# Set the duration of the image in seconds


duration = 3
show_image = True
start_time = pygame.time.get_ticks()

# Game loop
running = True
while running:
clock.tick(FPS)

# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Calculate elapsed time since the start


elapsed_time = (pygame.time.get_ticks() - start_time) / 1000 # Convert
milliseconds to seconds
# Drawing
screen.fill((255, 255, 255)) # Fill the screen with white color

# Display the image if elapsed time is less than the duration


if show_image and elapsed_time < duration:
screen.blit(image, (WIDTH//2 - image_rect.width//2, HEIGHT//2 -
image_rect.height//2))
else:
show_image = False

pygame.display.flip()

# Quit the game


pygame.quit()

You might also like