-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
37 lines (29 loc) · 1.19 KB
/
Copy pathbutton.py
File metadata and controls
37 lines (29 loc) · 1.19 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
import pygame.font
class Button():
"""Clase para botones"""
def __init__(self, pantalla, msg, bottom_y=0):
"""Inicializa atributos de botón"""
self.pantalla = pantalla
self.pantalla_rect = pantalla.get_rect()
# Establece las dimensiones y propiedades del botón
self.width, self.height = 200, 50
self.button_color = (0, 255, 0)
self.text_color = (255, 255, 255)
self.font = pygame.font.SysFont(None, 48)
# Construye el objeto rect del botón y lo centra
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = self.pantalla_rect.center
if bottom_y:
self.rect.bottom = self.pantalla_rect.bottom - bottom_y
# El mensaje del botón debe prepararse solo una vez
self.prep_msg(msg)
def prep_msg(self, msg):
"""Convierte el msg en una imagen renderizada y centra el texto en el botón"""
self.msg_image = self.font.render(
msg, True, self.text_color, self.button_color)
self.msg_image_rect = self.msg_image.get_rect()
self.msg_image_rect.center = self.rect.center
def draw_button(self):
# Dibuja el botón en blanco y luego dibuja el mensaje
self.pantalla.fill(self.button_color, self.rect)
self.pantalla.blit(self.msg_image, self.msg_image_rect)