-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
38 lines (31 loc) · 1.34 KB
/
button.py
File metadata and controls
38 lines (31 loc) · 1.34 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
import pygame
from constants import color_inactive, BLACK
class Button:
def __init__(self, x, y, width, height, text='', color=color_inactive, text_color=BLACK, radius=5, font_size=28,
enabled=True):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.color = color
self.text_color = text_color
self.radius = radius
self.enabled = enabled
self.font = pygame.font.Font(pygame.font.match_font('arial'), font_size)
def draw(self, screen):
if not self.enabled:
disabled_color = (200, 200, 200)
pygame.draw.rect(screen, disabled_color, self.rect, border_radius=self.radius)
else:
pygame.draw.rect(screen, self.color, self.rect, border_radius=self.radius)
if self.text != '':
text_surface = self.font.render(self.text, True, self.text_color)
text_rect = text_surface.get_rect(center=self.rect.center)
screen.blit(text_surface, text_rect)
def is_pressed(self, event):
if event.type == pygame.MOUSEBUTTONDOWN and self.enabled:
if self.rect.collidepoint(event.pos):
return True
return False
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False