diff options
Diffstat (limited to 'Saludame.activity/status_bars.py')
-rwxr-xr-x | Saludame.activity/status_bars.py | 340 |
1 files changed, 240 insertions, 100 deletions
diff --git a/Saludame.activity/status_bars.py b/Saludame.activity/status_bars.py index b160b8d..5c2ac98 100755 --- a/Saludame.activity/status_bars.py +++ b/Saludame.activity/status_bars.py @@ -6,46 +6,46 @@ import pygame ****************** VISUALES ****************** """ class BarsWindow(): - + """ + Clase que representa la ventana de las barras de estado del juego. + """ def __init__(self, position, frame_rate, background_color): - self.frame_rate = frame_rate - - #rect and surface: - self.rect = pygame.Rect(position, (400, 501)) + """ visual constant """ + self.max_bar_qty = 7 + self.px_per_bar = 26 + self.px_per_section = 50 + self.px_expanded = self.max_bar_qty * self.px_per_bar + 2 + self.qty_section = 50 + """ rect and surface: """ + self.rect = pygame.Rect(position, (400, 484)) self.surface = pygame.Surface(self.rect.size) self.background_color = background_color self.surface.fill(self.background_color) - #bars + """ game bars """ loader = BarsLoader() - self.bars = loader.create_bars(Bar("main_bar", "overall", None, [], 100, 50)) + self.bars = loader.create_bars(StatusBar("main_bar", "overall", None, [], 100, 50)) - #sections: - self.physica_section = BarSection("physica", self.bars[0], self.bars[0].children_list, (398, 68), (1, 90)) - self.fun_section = BarSection("fun", self.bars[3], self.bars[3].children_list, (398, 73), (1, 159)) - self.hygiene_section = BarSection("hygiene", self.bars[1], self.bars[1].children_list, (398, 94), (1, 233)) - self.nutrition_section = BarSection("nutrition", self.bars[2], self.bars[2].children_list, (398, 172), (1, 328)) - self.overall_section = BarSection("Estado general", self.bars[4], [] , (398, 37), (1, 52)) - self.score_section = ScoreSection(Bar("score_bar", "score", None, self.bars[4], 100, 15), (398, 50), (1, 1), 1) + """ sections """ + self.score_section = ScoreSection(StatusBar("score_bar", "score", None, self.bars[4], 100, 15), (398, 50), (1, 1), 1) + self.overall_section = BarSection("Estado general", self.bars[4], [] , (398, 37), (1, 52), self.px_expanded) - self.sections_list = [self.score_section, self.physica_section, self.fun_section, self.hygiene_section, self.nutrition_section, self.overall_section] + self.physica_section = BarSection("physica", self.bars[0], self.bars[0].children_list, (398, self.px_per_section), (1, 90), self.px_expanded) + self.fun_section = BarSection("fun", self.bars[3], self.bars[3].children_list, (398, self.px_per_section), (1, 142), self.px_expanded) + self.hygiene_section = BarSection("hygiene", self.bars[1], self.bars[1].children_list, (398, self.px_per_section), (1, 194), self.px_expanded) + self.nutrition_section = BarSection("nutrition", self.bars[2], self.bars[2].children_list, (398, self.px_per_section), (1, 246), self.px_expanded) + self.sections_list = [self.score_section, self.physica_section, self.fun_section, self.hygiene_section, self.nutrition_section, self.overall_section] + self.accordeon = Accordeon([self.physica_section, self.fun_section, self.hygiene_section, self.nutrition_section], self.px_per_bar, self.px_per_section, self.px_expanded) + """ """ - def draw(self, screen): self.surface.fill(self.background_color) - screen.blit(self.surface, self.rect) - changes = [] - changes += self.physica_section.draw(self.surface) - changes += self.hygiene_section.draw(self.surface) - changes += self.fun_section.draw(self.surface) - changes += self.nutrition_section.draw(self.surface) - changes += self.overall_section.draw(self.surface) - changes += self.score_section.draw(self.surface) - changes += [self.rect] - + for section in self.sections_list: + changes += section.draw(self.surface) + screen.blit(self.surface, self.rect) return changes @@ -57,109 +57,248 @@ class BarsWindow(): return def on_mouse_click(self, (x, y)): - for section in self.sections_list: + + for section in self.accordeon.sections_list: if(section.rect.collidepoint((x, y))): - section.on_mouse_click((x, y)) + if(not section.expanded): + self.accordeon.expand_section(section) + + def __relative_pos(self, (x, y)): + return (x + self.rect.left, y + self.rect.top) -class ScoreSection: +class Accordeon: + """ + Clase encargada de realizar los calculos para expandir y + contraer las secciones. + """ - def __init__(self, bar, size, position, level): - self.name = "score section" - self.score_bar = bar - self.score_bar_display = BarDisplay(26, (size[0] - 2), (1, (size[1] / 2) - 3), self.score_bar) - self.rect = pygame.Rect(position, size) - self.surface = pygame.Surface(self.rect.size) - self.surface.fill((2, 45, 126)) - #level: - self.level = level - self.font = pygame.font.Font(None, 20) - - - - def draw(self, screen): - #draw bar: - self.score_bar_display.draw(self.surface) - - #write actual level: - level_text = "Nivel: " + str(self.level) - - self.surface.blit(self.font.render(level_text, 1, (255, 0, 0)), (2, 5)) - - screen.blit(self.surface, self.rect) - return [self.rect] - + def __init__(self, sections_list, px_per_bar, px_per_section, px_expanded): + self.sections_list = sections_list #secciones sobre las que se realizarán los cambios + """ + Las secciones deben estar ordenadas de arriba abajo, según se muestren + en la pantalla. + """ + self.px_per_section = px_per_section #cantidad mínima de pixeles por sección + self.px_expanded = px_expanded #cantidad que se suma a la cantidad mínima de pixeles por sección + self.expand_section(self.sections_list[-1]) #inicialmente expande la última sección + + def expand_section(self, section): + """ + Espande la sección 'section' y re calcula la + posición del resto de las secciones. + """ + self.__compress_sections() + for i in range(0, len(self.sections_list)): + if(self.sections_list[i] == section): + if(i + 1 < len(self.sections_list)): + for j in range(i + 1, len(self.sections_list)): + self.sections_list[j].move_down() + self.sections_list[i].expand() + break + + def __compress_sections(self): + """ + Comprime todas las secciones y las localiza + en su posición inicial. + """ + for i in range(0, len(self.sections_list)): + if(self.sections_list[i].expanded): + self.sections_list[i].compress() + if(i + 1 < len(self.sections_list)): + for j in range(i + 1, len(self.sections_list)): #move up the displays under the expanded one + self.sections_list[j].move_up() + break class BarSection: + """ + Clase que contiene un conjunto de BarDisplay, y las + muestra por pantalla + """ - def __init__(self, name, root_bar, children_bar, size, position): + def __init__(self, name, root_bar, children_bar, size, position, max_expand): + """ section attributes """ self.name = name self.root_bar = root_bar - self.root_bar_display = BarDisplay(26, (size[0] - 2), (1, (size[1] / 2) - 13), self.root_bar) self.children_bar = children_bar self.rect = pygame.Rect(position, size) + + """ visuals """ self.surface = pygame.Surface(self.rect.size) - self.surface.fill((2, 45, 126)) - self.displays_list = self.__calculate(children_bar) - self.selected = False #Este flag se activa cuando se están mostrando las sub-barras + self.root_bar_display = BarDisplay(26, (size[0] - 2), (1, (size[1] / 2) - 13), self.root_bar, pygame.Color("blue")) + self.displays_list = self.__get_displays() #obtengo los displays para cada barra. + self.surface.fill((2, 45, 126)) #back ground color + + """ visuals constant """ + self.init_top = position[1] + self.init_height = size[1] + self.max_expand = max_expand + """ """ + self.expanded = False #Este flag se activa cuando se están mostrando las sub-barras + self.__calculate() #calculo la posición de cada barra en la sección + + def expand(self): + """ + Expande la sección, y calcula la posición de las barras + """ + self.expanded = True + self.rect.height = self.init_height + self.max_expand + self.__set_surface(self.rect.size) + self.__calculate() + + def compress(self): + """ + Comprime la sección, y re calcula la posición de las barras + """ + self.expanded = False + self.rect.height = self.init_height #vuelve al tamaño inicial + self.rect.top = self.init_top + self.__set_surface(self.rect.size) + self.__calculate() + + def move_up(self): + """ + Desplaza la sección a su posición original. + """ + self.rect.top = self.init_top + self.__calculate() + + def move_down(self): + """ + Desplaza la sección desde su posición original a + la posición original más el número de expanción + máxima de una sección. + """ + self.rect.top = self.init_top + self.max_expand + self.__calculate() def draw(self, screen): changes = [] - if (self.selected and len(self.children_bar) > 0): + if (self.expanded and len(self.children_bar) > 0): + changes += self.root_bar_display.draw(self.surface) for bar_display in self.displays_list: changes += bar_display.draw(self.surface) else: changes += self.root_bar_display.draw(self.surface) screen.blit(self.surface, self.rect) - return changes + return changes - def __calculate(self, children_bar): - qty = len(children_bar) - if(qty == 0): - qty = 1 - bar_height = int((self.rect.height / qty) - 2) - bar_width = int(self.rect.width - 2) + def __calculate(self): + """ + Calcula la posición de cada barra + dependiendo de si está expandida o no la sección. + """ + qty = len(self.displays_list) + if(self.expanded): + if(qty == 0): + qty = 1 + bar_height = 26 + + y = 50 + for display in self.displays_list: + display.rect.top = y + y += (bar_height + 1) + else: + for display in self.displays_list: + display.rect.top = 1 + + def __get_displays(self): + """ + Crea un BarDisplay para cada barra hija de la sección. + Y retorna una lista con los mismos. + """ display_list = [] - - y = int(self.rect.height / bar_height) # margen vertical - for status_bar in children_bar: - display = BarDisplay(bar_height, bar_width, (1, y), status_bar) + bar_height = 26 + color = pygame.Color(25, 255, 25, 1) #carga las barras hijas con el color verde por defecto. + for status_bar in self.children_bar: + display = BarDisplay(bar_height, 393, (1, 1), status_bar, color) display_list.append(display) - y += (bar_height + 1) - return display_list - def on_mouse_click(self, (x, y)): - self.selected = not self.selected + def __set_surface(self, size): + self.surface = pygame.Surface(size) + self.surface.fill((2, 45, 126)) + + + def __relative_pos(self, (x, y)): + return (x + self.rect.left, y + self.rect.top) class BarDisplay: + """ + Clase que se encarga de representar visualmente a una barra, manteniéndose + actualizada según los incrementos o decrementos de la barra representada. + """ - def __init__(self, height, width, position, status_bar): - self.label = status_bar.label + def __init__(self, height, width, position, status_bar, color): + """ attributes """ self.status_bar = status_bar + self.label = status_bar.label + self.color = color self.rect = pygame.Rect(position, (width, height)) self.position = position - self.color = pygame.Color(0, 255, 0, 1) + """ visuals """ self.surface = pygame.Surface(self.rect.size) self.font = pygame.font.Font(None, 20) + """ """ + self.last_value = self.status_bar.value #valor inicial + self.charge = pygame.Rect((1, 2), (((self.rect.width - 2) * self.last_value / self.status_bar.max, self.rect.height - 4))) def draw(self, screen): - - charge = pygame.Rect((1, 2), (self.rect.width - 2, self.rect.height - 4)) - charge.width = self.status_bar.value * self.rect.width / self.status_bar.max - charge_surface = pygame.Surface(charge.size) - charge_surface.fill(pygame.Color("blue")) + if(self.last_value != self.status_bar.value): + self.charge = pygame.Rect((1, 2), (self.rect.width - 2, self.rect.height - 4)) + self.charge.width = self.status_bar.value * self.rect.width / self.status_bar.max + + charge_surface = pygame.Surface(self.charge.size) + charge_surface.fill(self.color) self.surface.fill(pygame.Color("black")) - self.surface.blit(charge_surface, charge) - - self.surface.blit(self.font.render(self.label, 1, (255, 0, 0)), (2, 5)) + self.surface.blit(charge_surface, self.charge) + self.surface.blit(self.font.render(self.label, 1, (0, 0, 0)), (2, 5)) screen.blit(self.surface, self.rect) return [self.rect] + def __relative_pos(self, (x, y)): + #(x + self.rect.left, y + self.rect.top) + return (x + self.rect.left, y + self.rect.top) + + +class ScoreSection: + """ + Sección que muestra la barra de puntaje principal. + """ + def __init__(self, bar, size, position, level): + """ attributes """ + self.name = "score section" + self.score_bar = bar + self.level = level + self.rect = pygame.Rect(position, size) + """ visuals """ + self.score_bar_display = BarDisplay(26, (size[0] - 2), (1, (size[1] / 2) - 3), self.score_bar, pygame.Color("blue")) + self.surface = pygame.Surface(self.rect.size) + self.surface.fill((2, 45, 126)) + self.font = pygame.font.Font(None, 20) + + + + def draw(self, screen): + #draw bar: + self.score_bar_display.draw(self.surface) + + #write actual level: + level_text = "Nivel: " + str(self.level) + + self.surface.blit(self.font.render(level_text, 1, (255, 255, 255)), (2, 5)) + + screen.blit(self.surface, self.rect) + return [self.rect] + + def __relative_pos(self, (x, y)): + #(x + self.rect.left, y + self.rect.top) + return (x + self.rect.left, y + self.rect.top) + """ ****************** MODELOS ****************** """ @@ -170,7 +309,6 @@ class BarsController: a una barra especifica """ def __init__(self): - #self.main_bar = StatusBar("main_bar", None, [], 100, 50) self.second_level_bar = BarsLoader.create_bars() self.main_bar.children_list = self.second_level_bar third_level_bar = [] @@ -183,12 +321,13 @@ class BarsController: def decrease_bar(self, id, value): return -class Bar: +class StatusBar: """ Entity that represent the bar """ def __init__(self, id, label, parent_bar, children_list, max_value, init_value): + """ attributes """ self.id = id self.label = label self.max = max_value @@ -272,27 +411,27 @@ class BarsLoader: """'hard_level' para plasmar que la idea es que los valores por defecto de las barras se carguen según un nivel de dificultad""" """ physica """ - physica_children_id = ["energy", "resistencia", "fat"] - physica = Bar("physica", "Physica", main_bar, [], hard_level[0], hard_level[1]) - physica_children_bar = [Bar(id, id, physica, [], hard_level[0], hard_level[1]) for id in physica_children_id] + physica_children_id = ["Energy", "Resistencia", "Fat"] + physica = StatusBar("physica", "Physica", main_bar, [], hard_level[0], hard_level[1]) + physica_children_bar = [StatusBar(id, id, physica, [], hard_level[0], hard_level[1]) for id in physica_children_id] physica.children_list = physica_children_bar """ hygiene """ - hygiene_children_id = [("shower", "shower"), ("w_hands", "washing hands"), ("b_teeth", "brushing teeth"), ("toilet", "toilet")] - hygiene = Bar("hygiene", "Hygiene", main_bar, [], hard_level[0], hard_level[1]) - hygiene_children_bar = [Bar(id[0], id[1], hygiene, [], hard_level[0], hard_level[1]) for id in hygiene_children_id] + hygiene_children_id = [("shower", "Shower"), ("w_hands", "Washing hands"), ("b_teeth", "Brushing teeth"), ("toilet", "Toilet")] + hygiene = StatusBar("hygiene", "Hygiene", main_bar, [], hard_level[0], hard_level[1]) + hygiene_children_bar = [StatusBar(id[0], id[1], hygiene, [], hard_level[0], hard_level[1]) for id in hygiene_children_id] hygiene.children_list = hygiene_children_bar """ nutrition """ - nutrition_children_id = [("c_leguminosas", "cereales y leguminosas"), ("v_frutas", "verduras y frutas"), ("C_huevos", "carnes y huevos"), ("dulces", "dulces"), ("g_aceites", "grasas y aceites"), ("l_quesos", "leches y quesos"), ("agua", "agua")] - nutrition = Bar("nutrition", "Nutrition", main_bar, [], hard_level[0], hard_level[1]) - nutrition_children_bar = [Bar(id[0], id[1], nutrition, [], hard_level[0], hard_level[1]) for id in nutrition_children_id] + nutrition_children_id = [("c_leguminosas", "Cereales y leguminosas"), ("v_frutas", "Verduras y frutas"), ("C_huevos", "Carnes y huevos"), ("dulces", "Dulces"), ("g_aceites", "Grasas y aceites"), ("l_quesos", "Leches y quesos"), ("agua", "Agua")] + nutrition = StatusBar("nutrition", "Nutrition", main_bar, [], hard_level[0], hard_level[1]) + nutrition_children_bar = [StatusBar(id[0], id[1], nutrition, [], hard_level[0], hard_level[1]) for id in nutrition_children_id] nutrition.children_list = nutrition_children_bar """ fun """ - fun_children_id = ["sports", "playing", "relaxing"] - fun = Bar("fun", "Fun", main_bar, [], hard_level[0], hard_level[1]) - fun_children_bar = [Bar(id, id, fun, [], hard_level[0], hard_level[1]) for id in fun_children_id] + fun_children_id = ["Sports", "Playing", "Relaxing"] + fun = StatusBar("fun", "Fun", main_bar, [], hard_level[0], hard_level[1]) + fun_children_bar = [StatusBar(id, id, fun, [], hard_level[0], hard_level[1]) for id in fun_children_id] fun.children_list = fun_children_bar bars_list = [physica, hygiene, nutrition, fun, main_bar] @@ -301,3 +440,4 @@ class BarsLoader: return bars_list + |