-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.py
More file actions
34 lines (27 loc) · 1.05 KB
/
button.py
File metadata and controls
34 lines (27 loc) · 1.05 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
import pygame
class Button:
def __init__(self, x, y, width, height, color, text=""):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.text = text
def draw(self, win, outline=None):
if outline:
pygame.draw.rect(win, outline,
(self.x-2, self.y-2,
self.width + 4, self.height+4),
0)
pygame.draw.rect(win, self.color,
(self.x, self.y, self.width, self.height), 0)
if self.text != "":
font = pygame.font.SysFont("comicsans", 35)
text = font.render(self.text, 1, (0, 0, 0))
win.blit(text, (self.x + (self.width/2 - text.get_width()/2),
self.y + (self.height/2 - text.get_height()/2)))
def is_over(self, pos):
if self.x < pos[0] < self.x + self.width:
if self.y < pos[1] < self.y + self.height:
return True
return False