-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
27 lines (24 loc) · 1.05 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
import pygame
#Class Button
class Button():
def __init__(self, x, y, image, scale):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.clicked = False
def draw(self, surface):
action = False
#lấy vị trí chuột
pos = pygame.mouse.get_pos()
#kiếm tra chuột và vị trí đã nhấn chuột
if self.rect.collidepoint(pos): #nếu chuột chạm vào button
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: #số 0 là chuột trái, 1 là chuột giữa, 2 là chuột phải # ==1 tức là được nhấn
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
#vẽ button lên màn hình
surface.blit(self.image, (self.rect.x, self.rect.y))
return action