-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathButton.py
42 lines (36 loc) · 1.23 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
import pygame
class Button:
def __init__(self, text):
self.text = text
self.is_hover = False
self.default_color = (100,100,100)
self.hover_color = (255,255,255)
self.font_color = (0,0,0)
self.obj = None
def setColor(self, button_color):
self.default_color = button_color
def setHoverColor(self,hover_color):
self.hover_color = hover_color
def setFontColor(self,font_color):
self.font_color = font_color
def label(self,font_size):
'''button label font'''
font = pygame.font.Font(None, font_size)
return font.render(self.text, 1, self.font_color)
def color(self):
'''change color when hovering'''
if self.is_hover:
return self.hover_color
else:
return self.default_color
def draw(self, DISPLAY_SURF, mouse, rectcoord, labelcoord,font_size=20):
'''create rect obj, draw, and change color based on input'''
self.obj = pygame.draw.rect(DISPLAY_SURF, self.color(), rectcoord)
DISPLAY_SURF.blit(self.label(font_size), labelcoord)
self.check_hover(mouse)
def check_hover(self, mouse):
'''adjust is_hover value based on mouse over button - to change hover color'''
if self.obj.collidepoint(mouse):
self.is_hover = True
else:
self.is_hover = False