-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_text_display
60 lines (45 loc) · 1.88 KB
/
pygame_text_display
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## The following code is a code snippet at the Carla simulator research blog - https://carlasimblog.wordpress.com/2024/01/25/pygame-plotting-improving-driver-user-experience/.
## Feel free to show your support via requested, suggestions and interesting ideas for future research material.
## This code will display two texts, "Hello, Pygame!" and "Second line of text," on the top-left area of the screen for 5 seconds.
## After 5 seconds, the screen will be cleared. Adjust the texts, colors, positions, and duration as needed.
import pygame
import sys
import time
# Initialize Pygame
pygame.init()
# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Text Display Example")
# Set up the font
font = pygame.font.Font(None, 36)
# Function to display text on the screen
def display_text(text1, text2, color, duration):
text_surface1 = font.render(text1, True, color)
text_rect1 = text_surface1.get_rect(topleft=(10, 10))
text_surface2 = font.render(text2, True, color)
text_rect2 = text_surface2.get_rect(topleft=(10, 50)) # Adjust the vertical position as needed
screen.blit(text_surface1, text_rect1)
screen.blit(text_surface2, text_rect2)
pygame.display.flip()
start_time = time.time()
while time.time() - start_time < duration:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Clear the screen after the specified duration
screen.fill((0, 0, 0))
pygame.display.flip()
# Display two texts for 5 seconds
display_text("Hello, Pygame!", "Second line of text", (255, 255, 255), 5)
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
# Quit Pygame
pygame.quit()
sys.exit()