-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutton.py
39 lines (32 loc) · 1.3 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
import pygame
pygame.font.init()
font = pygame.font.SysFont("Arial", 20)
class Button:
"""Create a button, then blit the surface in the while loop"""
def __init__(self, screen, text, pos, font=30, bg="black", feedback=""):
self.x, self.y = pos
self.screen = screen
self.font = pygame.font.SysFont("Arial", font)
if feedback == "":
self.feedback = "text"
else:
self.feedback = feedback
self.change_text(text, bg)
def change_text(self, text, bg="black"):
"""Change the text whe you click"""
self.text = self.font.render(text, 1, pygame.Color("White"))
self.size = self.text.get_size()
self.surface = pygame.Surface(self.size)
self.surface.fill(bg)
self.surface.blit(self.text, (0, 0))
self.rect = pygame.Rect(self.x, self.y, self.size[0], self.size[1])
def show(self):
self.screen.blit(self.surface, (self.x, self.y))
def click(self, event, regions):
x, y = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0]:
if self.rect.collidepoint(x, y):
#self.change_text(self.feedback, bg="red")
print(regions)
#pygame.quit()