-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.py
30 lines (26 loc) · 1.21 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
#import pygame
class Button():
def __init__(self, pygame, win, onPressed=None, x=0, y=0, height=0, width=0, text = '', color=(255,255,255), textColor=(0, 0, 0)):
self.onPressed = onPressed
self.x = x
self.y = y
self.height = height
self.width = width
self.pygame = pygame
self.win = win
self.myFont = self.pygame.font.SysFont('Comic Sans MS', 18)
self.color = color
self.textColor = textColor
self.setText(text)
def setText(self, text):
self.text = self.myFont.render(text, False, self.textColor, self.color)
if(self.width < self.text.get_width()):
self.width = self.text.get_width()
def draw(self):
self.pygame.draw.rect(self.win, self.color, (self.x - self.width/2, self.y - self.height/2, self.width, self.height))
# self.win.blit(text,(self.x, self.y))
self.win.blit(self.text, (self.x - self.text.get_width()/2, self.y - self.text.get_height()/2))
def click(self, x, y):
if(x > self.x - self.width/2 and x < self.x + self.width/2 and y > self.y - self.height/2 and y < self.y + self.height/2):
if(self.onPressed != None):
self.onPressed()