-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
66 lines (55 loc) · 2 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
import pygame
from pygame.locals import *
from constants import *
# Button Object
class Button():
# Preset values for a button
button_col = WHITE
hover_col = GRAY
text_col = GRAY
click_col = LIGHT_GRAY
clicked = False
space = 0
# Initialization of button with location, size, and text
def __init__(self, x, y, size, text):
self.x = x
self.y = y
self.text = text
self.size = size
#end
# Draws button and returns true if button is pressed
def draw(self, screen):
action = False
pos = (pygame.mouse.get_pos()[0], HEIGHT - pygame.mouse.get_pos()[1])
# Creates rectangles for button, collision, and shaddow
button_rect = Rect((0,0), self.size)
collision = Rect(0,0, self.size[0], self.size[1] + self.space)
shaddow = Rect((0,0), self.size)
button_rect.center = (self.x, self.y + self.space)
collision.center = (self.x , self.y + self.space / 2)
shaddow.center = (self.x, self.y)
# Checks if button is hovered over or pressed
if collision.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1:
self.clicked = True
self.space = 4
elif pygame.mouse.get_pressed()[0] == 0 and self.clicked == True:
self.clicked = False
action = True
else:
self.space = 8
else:
self.space = 0
self.clicked = False
#end
# Draws button, shaddow, and text
pygame.draw.rect(screen, self.hover_col, shaddow)
pygame.draw.rect(screen, self.button_col, button_rect)
font = pygame.font.Font('fonts/MajorMonoDisplay.ttf', 35)
text_img = font.render(self.text, True, self.text_col)
text_img = pygame.transform.flip(text_img, False, True)
text_len = text_img.get_width()
screen.blit(text_img, (self.x - text_len / 2, self.y - 17 + self.space))
return action
#end
#end Button