-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
76 lines (68 loc) · 2.68 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pygame
pygame.init()
X = 1360
Y = 768
color1 = [250,250,250]
color2 = [50,100,100]
black = [0,0,0]
shift = False
padding = 40
fontName = "montserrat.ttf"
class Button():
"""
class defined to present a button to perform some function
"""
def __init__(self, title = "Button", position = (X//2,Y//2), width = 100, height = 50, fontname = fontName):
"""
Parameters : Title of Button
"""
self.title = title
self.position = position
self.width = width
self.is_clicked = False
self.is_hover = False
self.height = height
self.color = [240,0,40]
self.hover_color = [220,0,0]
self.click_color = [177,15,46]
self.font = pygame.font.Font(fontname, 25)
self.title_img = self.font.render(self.title, True, color1)
self.title_img_rect = self.title_img.get_rect()
self.title_img_rect.center = (self.position[0] + self.width //2, self.position[1] + self.height//2)
def set_color(self,color):
self.color = color
def set_hover_color(self,hover):
self.hover_color = hover
def set_click_color(self,click):
self.click_color = click
def check(self, mouse_pos, event):
"""
THe function checks whether the button was clicked or if the mouse is hovering on the button.
"""
if mouse_pos[0] >= self.position[0] and mouse_pos[0] <= self.position[0] + self.width and mouse_pos[1] >= self.position[1] and mouse_pos[1] <= self.position[1] + self.height:
self.is_hover = True
if event.type == pygame.MOUSEBUTTONDOWN:
self.is_hover = False
self.is_clicked = True
else:
self.is_clicked = False
else:
self.is_clicked = False
self.is_hover = False
def display(self, screen):
"""
Displays the button on the screen according to its position and the action on the button.
"""
if self.is_hover :
pygame.draw.rect(screen, self.hover_color , (self.position[0], self.position[1], self.width, self.height))
elif self.is_clicked :
pygame.draw.rect(screen, self.click_color, (self.position[0], self.position[1], self.width, self.height))
else:
pygame.draw.rect(screen, self.color, (self.position[0], self.position[1], self.width, self.height))
screen.blit(self.title_img, self.title_img_rect)
def set_center(self, pos):
"""
Sets the center for the button to the given parameters.
"""
self.title_img_rect.center = pos
self.position = (pos[0] - self.width // 2, pos[1] - self.height // 2)