-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttonRect.py
More file actions
78 lines (62 loc) · 2.47 KB
/
buttonRect.py
File metadata and controls
78 lines (62 loc) · 2.47 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
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
77
78
import pygame
class Button:
def __init__(self, text, pos, size, margin_x, margin_y, color, textcolor,
font, hovercolor=None, presscolor=None, border=None):
"""
Create a Button object
:param text: Text for the button
:param x: x-coordinate for the button
:param y: y-coordinate for the button
:param width: the width of the button
:param height: the height of the button
:param margin_x: the margin_x for the text
:param margin_y: the margin_y for the text
:param color: the color of the button
:param textcolor: the color of the text
:param font: the font used
"""
self.font = font
self.pos = list(pos)
self.size = list(size)
self.color = color
self.text = text
self.textcolor = textcolor
if not hovercolor:
self.hovercolor = self.textcolor
else:
self.hovercolor = hovercolor
if not presscolor:
self.presscolor = self.textcolor
else:
self.presscolor = presscolor
if not border:
self.border = 0
else:
self.border_color = border
self.marginx = margin_x
self.marginy = margin_y
self.clicked = False
def render(self, surface):
"""
Draw the button while return a bool to detect click
:param surface: the display to draw the button on
:return: a boolean for click
"""
action = False
pos = pygame.mouse.get_pos()
color = self.textcolor
rect = pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])
if rect.collidepoint(pos):
color = self.hovercolor
if pygame.mouse.get_pressed()[0] == 1 and not self.clicked:
color = self.presscolor
self.clicked = True
text_surf, text_rect = self.font.render(self.text, color)
pygame.draw.rect(surface, self.color, (self.pos[0], self.pos[1], self.size[0], self.size[1]))
pygame.draw.rect(surface, self.border_color, (self.pos[0], self.pos[1], self.size[0], self.size[1]), 3)
surface.blit(text_surf, (self.pos[0] + self.marginx + self.size[0]/2 - text_rect.w / 2,
self.pos[1] + self.marginy + self.size[1]/2 - text_rect.h / 2))
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
def update(self):
return self.clicked