-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
34 lines (28 loc) · 874 Bytes
/
button.py
File metadata and controls
34 lines (28 loc) · 874 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 as pg
class Button():
def __init__(self, x, y, image, single_click, name=None):
self.name=name
self.image = image
self.posX=x
self.posY=y
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.clicked = False
self.single_click = single_click
def draw(self, surface):
# draw button on screen
surface.blit(self.image, self.rect)
def action(self):
action = False
#get mouse position
pos = pg.mouse.get_pos()
#check mouseover and clicked conditions
if self.rect.collidepoint(pos):
if pg.mouse.get_pressed()[0] == 1 and self.clicked == False:
action = True
#if button is a single click type, then set clicked to True
if self.single_click:
self.clicked = True
if pg.mouse.get_pressed()[0] == 0:
self.clicked = False
return action