-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buttons.py
50 lines (45 loc) · 1.97 KB
/
Buttons.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
# Simon H. Larsen
# Buttons
# Project startet: d. 26. august 2012
import pygame
pygame.init()
class Button:
def create_button(self, surface, color, x, y, length, height, width, text, text_color):
surface = self.draw_button(surface, color, length, height, x, y, width)
surface = self.write_text(surface, text, text_color, length, height, x, y)
self.rect = pygame.Rect(x, y, length, height)
return surface
def write_text(self, surface, text, text_color, length, height, x, y):
font_size = int(length // len(text))
myFont = pygame.font.SysFont("Calibri", font_size)
myText = myFont.render(text, 1, text_color)
surface.blit(myText, ((x + length / 2) - myText.get_width() / 2, (y + height / 2) - myText.get_height() / 2))
return surface
def draw_button(self, surface, color, length, height, x, y, width):
for i in range(1, 10):
s = pygame.Surface((length + (i * 2), height + (i * 2)))
s.fill(color)
alpha = (255 / (i + 2))
if alpha <= 0:
alpha = 1
s.set_alpha(alpha)
pygame.draw.rect(s, color, (x - i, y - i, length + i, height + i), width)
surface.blit(s, (x - i, y - i))
pygame.draw.rect(surface, color, (x, y, length, height), 0)
pygame.draw.rect(surface, (190, 190, 190), (x, y, length, height), 1)
return surface
def pressed(self, mouse):
if mouse[0] > self.rect.topleft[0]:
if mouse[1] > self.rect.topleft[1]:
if mouse[0] < self.rect.bottomright[0]:
if mouse[1] < self.rect.bottomright[1]:
# print("Some button was pressed!")
return True
else:
return False
else:
return False
else:
return False
else:
return False