-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
34 lines (27 loc) · 996 Bytes
/
button.py
File metadata and controls
34 lines (27 loc) · 996 Bytes
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
import pygame
class Button:
def __init__(self, x, y, image):
self.image = image
self.x = x
self.y = y
self.clicked = False
self.cooldown = 0
self.waitframes = 45
def cool_down(self):
if self.cooldown >= self.waitframes:
self.cooldown = 0
elif self.cooldown > 0:
self.cooldown += 1
def draw(self, WIN, func=None, *args):
pos = pygame.mouse.get_pos()
if self.x <= pos[0] <= self.x + self.image.get_width() and self.y <= pos[1] <= self.y + self.image.get_height():
img = self.image.copy()
img.set_alpha(100)
WIN.blit(img, (self.x, self.y))
if pygame.mouse.get_pressed()[0] and self.clicked is False and self.cooldown == 0:
if func != None:
func(WIN, *args)
self.clicked = True
self.cooldown = 1
else:
WIN.blit(self.image, (self.x, self.y))