-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathinterface.py
More file actions
71 lines (52 loc) · 1.76 KB
/
Copy pathinterface.py
File metadata and controls
71 lines (52 loc) · 1.76 KB
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
61
62
63
64
65
66
67
68
69
70
71
'''
Game: Angry Birds
File: interface.py
Contents: Class Buttons and Class Labels for User Interface
Requirements: Pygame, sys
By: Jatin Kumar Mandav
Blog: https://www.jatinmandav.wordpress.com
Twitter: @jatinmandav
YouTube: https://www.youtube.com/mandav
'''
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)