forked from prathimacode-hub/Awesome_Python_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonClass.py
43 lines (38 loc) · 1.35 KB
/
buttonClass.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pygame
class Button:
def __init__(self, x, y, width, height, text=None, colour=(73, 73, 73), highlightedColour=(189, 189, 189), function=None, params=None):
self.image = pygame.Surface((width, height))
self.pos = (x, y)
self.rect = self.image.get_rect()
self.rect.topleft = self.pos
self.text = text
self.colour = colour
self.highlightedColour = highlightedColour
self.function = function
self.params = params
self.highlighted = False
self.width = width
self.height = height
def update(self, mouse):
if self.rect.collidepoint(mouse):
self.highlighted = True
else:
self.highlighted = False
def draw(self, window):
self.image.fill(self.highlightedColour if self.highlighted else self.colour)
if self.text:
self.drawText(self.text)
window.blit(self.image, self.pos)
def click(self):
if self.params:
self.function(self.params)
else:
self.function()
# To add text
def drawText(self, text):
font = pygame.font.SysFont("arial", 20, bold=1)
text = font.render(text, False, (0,0,0))
width, height = text.get_size()
x = (self.width-width)//2
y = (self.height-height)//2
self.image.blit(text, (x, y))