-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
168 lines (138 loc) · 6.46 KB
/
gui.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pygame
import error_messages
import enum
import player_object
# the main menu class that contains all the elements in the menu and manages them
class Menu:
def __init__(self, game, level) -> None:
self.level = level
self.game = game
self.elements = []
# this is used for polymorphism within the menus
def interaction(self, event):
pass
# used to draw the elements in the menu on the screen
def draw(self, game):
for i in self.elements:
i.draw(self.game, self.level)
# enum to represent all the aligment options avalible
class AligPos(enum.Enum):
Left = 0
Right = 1
Center = 2
Top = 3
Bottom = 4
Middle = 5
# class with functions to allign anything to a certain location
class Alignment():
def Left(x, width):
return x
def Right(x, width):
return x - width
def Center(x, width):
return x - (width / 2)
def Top(y, height):
return y
def Bottom(y, height):
return y - height
def Middle(y, height):
return y - (height / 2)
# the basic element class, every element in the GUI must inherit from this
class GUI_element():
def __init__(self, game, level, x, y, tags=[]) -> None:
self.game = game
self._x = x
self._y = y
self.tags = tags
self.level = level
# used to draw the element on the screen
def draw(self, game, level):
try:
game.level_manager.loaded_levels[level].screen.blit(self.texture.get_texture(), (self._x, self._y))
except Exception as e:
print(e)
error_messages.polymorphism_rule_violation_made_an_issue(self)
# used to align the element
def AlignSelf(self, align, posx, posy, game, level, width, height):
self.level = level
posx = game.level_manager.loaded_levels[level].screen.get_width() * posx
posy = game.level_manager.loaded_levels[level].screen.get_height() * posy
if align[0] == AligPos.Left:
posx = Alignment.Left(posx, width)
if align[0] == AligPos.Right:
posx = Alignment.Right(posx, width)
if align[0] == AligPos.Center:
posx = Alignment.Center(posx, width)
if align[1] == AligPos.Top:
posy = Alignment.Top(posy, height)
if align[1] == AligPos.Bottom:
posy = Alignment.Bottom(posy, height)
if align[1] == AligPos.Middle:
posy = Alignment.Middle(posy, height)
return posx, posy
# Button element class, inherits from the GUI_element
class GUI_button(GUI_element):
def __init__(self, game, level, texture, posx, posy, align=(AligPos.Left, AligPos.Top), tags=[]) -> None:
self.texture = game.texture_manager.get_texture(texture)
posx, posy = self.AlignSelf(align, posx, posy, game, level, self.texture.get_texture().get_width(), self.texture.get_texture().get_height())
super().__init__(game, level, posx, posy, tags)
# Image element class, inherits from the GUI_element
class GUI_image(GUI_element):
def __init__(self, game, level, texture, posx, posy, align=(AligPos.Left, AligPos.Top), tags=[]) -> None:
self.texture = game.texture_manager.get_texture(texture)
posx, posy = self.AlignSelf(align, posx, posy, game, level, self.texture.get_texture().get_width(), self.texture.get_texture().get_height())
super().__init__(game, level, posx, posy, tags)
# Text element class, inherits from the GUI_element
class GUI_text(GUI_element):
def __init__(self, game, level, text, posx, posy, align=(AligPos.Left, AligPos.Top), font="JetBrainsMono-Bold.ttf", size=10, antialias=True, color=(255, 255, 255), tags=[]) -> None:
self.font = font
self.size = size
self.antialias = antialias
self.color = color
self.font = pygame.font.Font("textures/fonts/" + font, size)
self.text = self.font.render(text, antialias, color)
self.string_text = text
width = self.text.get_width()
height = self.text.get_height()
posx, posy = self.AlignSelf(align, posx, posy, game, level, width, height)
super().__init__(game, level, posx, posy, tags)
def update_text(self, text, font="JetBrainsMono-Bold.ttf", size=10, antialias=True, color=(255, 255, 255)):
self.font = font
self.size = size
self.antialias = antialias
self.color = color
self.font = pygame.font.Font("textures/fonts/" + font, size)
self.text = self.font.render(text, antialias, color)
self.string_text = text
def draw(self, game, level):
game.level_manager.loaded_levels[level].screen.blit(self.text, (self._x, self._y))
####################
# MENU DEFINITIONS #
####################
# the menu shown in the beginning of the game
class MainMenu(Menu):
def __init__(self, game, level) -> None:
super().__init__(game, level)
# start button
self.elements.append(GUI_button(game, level, "gui/button.png", 0.5, 0.4, align=(AligPos.Center, AligPos.Middle), tags=["Start"]))
# DUNGEON DELVE logo
self.elements.append(GUI_image(game, level, "gui/logo.png:{Relative_Scale:(0.8;0.8)}", 0.5, 0.2, align=(AligPos.Center, AligPos.Middle), tags=["Logo"]))
def interaction(self, event):
# detection if the mouse is pressed and if it is on a button
if event.type == pygame.MOUSEBUTTONUP:
for i in self.elements:
if event.pos[0] > i._x and event.pos[0] < i._x + i.texture.get_texture().get_width() and event.pos[1] > i._y and event.pos[1] < i._y + i.texture.get_texture().get_height():
# if its a "Start" button, load the first level
if "Start" in i.tags:
self.game.level_manager.set_level("level1.json")
self.game.level_manager.set_level("fps.json")
self.game.level_manager.unset_level("GUI_mainMenu.json")
player_object.player(game=self.game, texture="player/default.png:{Relative_Scale:(3;3)}", size_x=48, size_y=48, layer=50, x=117, y=116)
class FPS(Menu):
def __init__(self, game, level) -> None:
super().__init__(game, level)
self.game.event_manager.add_tick_listener(self)
self.elements.append(GUI_text(game, level, f"FPS: {self.game.__clock__.get_fps()//1}", 0, 0, align=(AligPos.Left, AligPos.Top)))
def tick(self):
for i in self.elements:
i.update_text(f"FPS: {self.game.__clock__.get_fps()//1}")