-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.py
29 lines (25 loc) · 965 Bytes
/
Button.py
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
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (200, 200, 200)
class Button:
def __init__(self, x, y, width, height, text):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.font = pygame.font.Font(None, 28)
self.is_hovered = False
def draw(self, surface):
color = GRAY
if self.is_hovered:
color = WHITE
pygame.draw.rect(surface, color, self.rect)
text_surface = self.font.render(self.text, True, BLACK)
text_rect = text_surface.get_rect(center=self.rect.center)
surface.blit(text_surface, text_rect)
def handle_event(self, event):
if event.type == pygame.MOUSEMOTION:
self.is_hovered = self.rect.collidepoint(event.pos)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.is_hovered:
return True