-
Notifications
You must be signed in to change notification settings - Fork 1
/
button.py
55 lines (44 loc) · 1.94 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pygame as py
import random
import settings
class Button:
def __init__(self, text, action, shape, pos, colour=py.Color("black"), back_colour=py.Color("white"), back_img=None):
self.colour = colour
self.back_colour = back_colour
self.surface = py.Surface(shape)
self.surface.fill(back_colour)
if back_img is not None:
img = py.image.load(back_img)
img = py.transform.scale(img, shape).convert()
img.set_alpha(60)
self.surface.blit(img, (0, 0))
py.draw.rect(self.surface, colour, self.surface.get_rect(), 3)
self.text = settings.text_surface(text, surf_shape=shape, size=max(shape)-4)
self.surface.blit(self.text, (0, 0))
self.action = action
self.position = pos
def click(self, pos):
if self.surface.get_rect().move(self.position).collidepoint(pos):
return self.action()
def draw(self, display):
display.blit(self.surface, self.position)
class CustomButton(Button):
def action(self):
self.reversed = not self.reversed
colour = py.Color("white") if self.reversed else py.Color("gray")
self.surface.fill(colour)
py.draw.rect(self.surface, self.colour, self.surface.get_rect(), 3)
self.surface.blit(self.text, (0, 0))
def click(self, pos):
if self.surface.get_rect().move(self.position).collidepoint(pos):
self.action()
def __init__(self, text, shape, pos, colour=py.Color("blue"), back_colour=py.Color("gray")):
self.colour = colour
self.back_colour = back_colour
self.surface = py.Surface(shape)
self.surface.fill(back_colour)
py.draw.rect(self.surface, colour, self.surface.get_rect(), 3)
self.text = settings.text_surface(text, surf_shape=shape, size=max(shape)-4)
self.surface.blit(self.text, (0, 0))
self.position = pos
self.reversed = False