-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.py
50 lines (36 loc) · 1.26 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
#!/usr/bin/python
import pygame
from pygame.locals import *
class Button():
def __init__(self, x, y, width, height, text):
self.screen = pygame.display.get_surface()
self.text = text
self.buttonRect = pygame.Rect(x, y, width, height)
self.color = pygame.Color(150, 150, 150)
if pygame.font:
font = pygame.font.Font(None, 22)
self.textDisp = font.render(self.text, 1, (100, 100, 100))
self.textRect = self.textDisp.get_rect(centerx = x + width/2, centery = y + height/2 + 1)
def update(self):
pygame.draw.rect(self.screen, self.color, self.buttonRect)
self.screen.blit(self.textDisp, self.textRect)
def onButton(self, (x, y)):
if x >= self.getX() and x <= (self.getX() + self.getWidth()) and y >= self.getY() and y <= (self.getY() + self.getHeight()):
return True
else:
return False
def getX(self):
return self.buttonRect.x
def getY(self):
return self.buttonRect.y
def getWidth(self):
return self.buttonRect.w
def getHeight(self):
return self.buttonRect.h
def getText(self):
return self.text
def setY(self, y):
self.buttonRect.y = y
self.textRect = self.textDisp.get_rect(centerx = self.buttonRect.x + self.buttonRect.w/2, centery = self.buttonRect.y + self.buttonRect.h/2 + 1)
def setColor(self, color):
self.color = color