-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.py
31 lines (24 loc) · 932 Bytes
/
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
import pygame
from util import drawText
class Button:
def __init__(self, pos, width, height, font, text):
self.rect = pygame.Rect(pos, (width, height))
self.text = text
self.font = font
# text position
textWidth, textHeight = self.font.size(self.text)
self.textPos = pygame.Vector2(
self.rect.x + ((self.rect.w - textWidth) / 2),
self.rect.y + ((self.rect.h - textHeight) / 2)
)
self.isClicked = False
def render(self, screen):
# render button
pygame.draw.rect(screen, (255, 0, 0), self.rect)
# draw text
drawText(screen, self.text, self.font, self.textPos, (255,255,255))
def update(self, leftClick, mousePos):
self.isClicked = False
if self.rect.collidepoint(mousePos):
if leftClick:
self.isClicked = True