-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgets.py
More file actions
78 lines (64 loc) · 2.23 KB
/
Copy pathWidgets.py
File metadata and controls
78 lines (64 loc) · 2.23 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pygame
import sys
pygame.init()
width, height = 600, 400
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Personalized Message App")
font = pygame.font.Font(None, 36)
background_color = (255, 255, 255)
text_color = (0, 0, 0)
def display_message(name):
window.fill(background_color)
message = f"Hello, {name}!"
text_surface = font.render(message, True, text_color)
text_rect = text_surface.get_rect(center=(width // 2, height // 2))
window.blit(text_surface, text_rect)
pygame.display.flip()
def get_user_name():
input_box = pygame.Rect(150, height // 2, 300, 40)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = True
color = color_active
else:
active = False
color = color_inactive
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
return text
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
window.fill(background_color)
txt_surface = font.render(text, True, text_color)
width_input_box = max(200, txt_surface.get_width()+10)
input_box.w = width_input_box
window.blit(txt_surface, (input_box.x+5, input_box.y+5))
pygame.draw.rect(window, color, input_box, 2)
pygame.display.flip()
clock.tick(30)
def main():
user_name = get_user_name()
if user_name:
display_message(user_name)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if __name__ == "__main__":
main()