Repository navigation
Expand file tree
/
Copy pathbutton.py
More file actions
49 lines (49 loc) · 2.33 KB
/
Copy pathbutton.py
File metadata and controls
49 lines (49 loc) · 2.33 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
__author__ = 'mohammed'
import pygame
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0) #defining colours
RED = ( 255, 0, 0)
class Button(object): #button class
def __init__(self, text, left, top, width, height):
self.left = left
self.top = top
self.width = width
self.height = height
self.rect = pygame.Rect(self.left,self.top,self.width,self.height)
self.text = text #text for label
self.font_colour = (BLACK) #font colour
self.hover = False #testing for mouseover
self.colour = (GREEN) #base colour
self.hover_colour = (RED) #mouse over colour
self.green = 255
self.red = 0
self.obj = None
def label(self):
'''button label font'''
font = pygame.font.Font(None, 20)
return font.render(self.text, 1, self.font_colour)
def colourchooser(self): #checks mouseover state and returns appropriate colour
if self.hover and self.red <= 255 and self.green >=0:
colour = (self.red, self.green, 0)
self.red = self.red + 40
self.green = self.green - 40
return colour
elif self.red >= 255 and self.hover:
return (255,0,0)
else:
self.red = 0
self.green = 255
return self.colour
def draw(self, mouse, screen2): #draws the rectangle
rectcoords = (self.left, self.top, self.width, self.height) #puts it into pygame appropriate
self.check_hover(mouse) #check if moused over
pygame.draw.rect(screen2, self.colourchooser(), rectcoords) #draws the button
text_x = ((self.rect.right - self.rect.left)/2 + self.rect.left) - ((self.label().get_rect().right - self.label().get_rect().left)/2) #half of the text is on the left side of the midpoint of the rect, and half on the right side
text_y = ((self.rect.bottom - self.rect.top)/2 + self.rect.top) - ((self.label().get_rect().bottom - self.label().get_rect().top)/2) #half of the text is on the top side of the midpoint of the rect, and half on the bottom side
screen2.blit(self.label(), (text_x, text_y)) #midpoint of box - midpoint text
def check_hover(self, mouse): #checks for mouseover
if self.rect.collidepoint(mouse):
self.hover = True
else:
self.hover = False