-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.py
More file actions
51 lines (43 loc) · 1.52 KB
/
Button.py
File metadata and controls
51 lines (43 loc) · 1.52 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
39
40
41
42
43
44
45
46
47
48
49
50
51
import pygame
class Button:
def __init__(self, text,width,height,pos,elevation):
gui_font = pygame.font.Font(None, 30)
#Core attributes
self.pressed = False
self.elevation = elevation
self.dynamic_elecation = elevation
self.original_y_pos = pos[1]
# top rectangle
self.top_rect = pygame.Rect(pos,(width,height))
self.top_color = '#475F77'
# bottom rectangle
self.bottom_rect = pygame.Rect(pos,(width,height))
self.bottom_color = '#354B5E'
#text
self.text_surf = gui_font.render(text,True,'#FFFFFF')
self.text_rect = self.text_surf.get_rect(center = self.top_rect.center)
def draw(self, window):
# elevation logic
self.top_rect.y = self.original_y_pos - self.dynamic_elecation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation
pygame.draw.rect(window, self.bottom_color, self.bottom_rect, border_radius = 12)
pygame.draw.rect(window,self.top_color, self.top_rect,border_radius = 12)
window.blit(self.text_surf, self.text_rect)
self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_pos):
self.top_color = '#D74B4B'
if pygame.mouse.get_pressed()[0]:
self.dynamic_elecation = 0
self.pressed = True
else:
self.dynamic_elecation = self.elevation
if self.pressed == True:
print('click')
self.pressed = False
else:
self.dynamic_elecation = self.elevation
self.top_color = '#475F77'