-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
60 lines (44 loc) · 1.54 KB
/
interface.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
56
57
58
59
60
import pygame
import sys
pygame.init()
display = None
def init(screen):
global display
display = screen
class Button:
def __init__(self, x, y, w, h, action=None, colorNotActive=(189, 195, 199), colorActive=None):
self.x = x
self.y = y
self.w = w
self.h = h
self.colorActive = colorActive
self.colorNotActive = colorNotActive
self.action = action
self.font = None
self.text = None
self.text_pos = None
def add_text(self, text, size=20, font="Times New Roman", text_color=(0, 0, 0)):
self.font = pygame.font.Font(font, size)
self.text = self.font.render(text, True, text_color)
self.text_pos = self.text.get_rect()
self.text_pos.center = (self.x + self.w/2, self.y + self.h/2)
def draw(self):
if self.isActive():
if not self.colorActive == None:
pygame.draw.rect(display, self.colorActive,
(self.x, self.y, self.w, self.h))
else:
pygame.draw.rect(display, self.colorNotActive,
(self.x, self.y, self.w, self.h))
if self.text:
display.blit(self.text, self.text_pos)
def isActive(self):
pos = pygame.mouse.get_pos()
if (self.x < pos[0] < self.x + self.w) and (self.y < pos[1] < self.y + self.h):
return True
else:
return False
class Label(Button):
def draw(self):
if self.text:
display.blit(self.text, self.text_pos)