def __Draw(self, xxx_todo_changeme): (width_hint, height_hint) = xxx_todo_changeme surf = pygame.Surface((width_hint, height_hint)) bbox = Rect(0, 0, width_hint, height_hint) extra.Tile_Texture(surf, "006metal.jpg", surf.get_rect()) margin = 8 w = bbox.width - (margin * 2) th = None y = margin + bbox.top control_rects = [] max_width = 0 first_item = True for (num, name, hotkeys) in self.options: if (name == None): # a gap if (first_item): img = resource.Load_Image("header.jpg") img_r = img.get_rect() img_r.center = bbox.center img_r.top = y surf.blit(img, img_r.topleft) extra.Edge_Effect(surf, img_r) max_width = img_r.width + (margin * 2) y += img_r.height y += margin * 2 continue txt = render.Render(name, 18, (50, 200, 20), (200, 200, 0)) if (th == None): th = txt.get_rect().height + (margin * 2) tw = txt.get_rect().width + (margin * 2) if (tw > max_width): max_width = tw x = bbox.left + margin r = Rect(x, y, w, th) x += self.Justify(w, txt.get_rect().width) extra.Tile_Texture(surf, "greenrust.jpg", r) extra.Edge_Effect(surf, r) self.Enhancement_Interface(surf, num, r, margin) surf.blit(txt, (x, y + margin - 1)) y += th + margin control_rects.append((num, r)) first_item = False # Finalise drawing extra.Line_Edging(surf, bbox, True) return (surf, control_rects, (max_width, y))
def Special_Refresh(): extra.Tile_Texture( screen, "rivets.jpg", Rect(menu_margin, 0, menu_width, screen.get_rect().height)) edge = Rect(menu_margin, -10, menu_width + 10, screen.get_rect().height + 10) for r in [stats_rect, global_stats_rect, edge]: extra.Line_Edging(screen, r, False) r = picture.get_rect() r.center = picture_surf.get_rect().center extra.Line_Edging(picture_surf, r, False) picture_surf.blit(picture, r.topleft)
def Review(): g = game.game historian = game.game.historian (width, height) = screen.surface.get_rect().size extra.Tile_Texture(screen.surface, "006metal.jpg", screen.surface.get_rect()) def Text(str, size, (x, y), justify): img = stats.Get_Font(size).render(str, True, (255, 255, 255)) if (justify == 0): # centre x -= (img.get_rect().width) / 2 elif (justify < 0): # right x -= img.get_rect().width screen.surface.blit(img, (x, y)) y += img.get_rect().height return y + 5
def __Draw_H(self, title, text, height): width = self.width margin = 10 fs1 = 12 fs2 = 14 newline_gap = 12 surf = pygame.Surface((width, height)) bbox = surf.get_rect() extra.Tile_Texture(surf, "006metal.jpg", bbox) tsurf = stats.Get_Font(fs1).render(title, True, (250,250,200)) tsurf_r = tsurf.get_rect() tsurf_r.center = bbox.center tsurf_r.top = margin surf.blit(tsurf, tsurf_r.topleft) y = tsurf_r.bottom + margin # line edging for title extra.Line_Edging(surf, Rect(0,0,width,y), True) y += margin x = margin height = y while ( len(text) != 0 ): newline = False i = text.find(' ') j = text.find("\n") if (( j >= 0 ) and ( j < i )): i = j newline = True if ( i < 0 ): i = len(text) word = text[ : i ] + " " text = text[ i + 1 : ].lstrip() tsurf = stats.Get_Font(fs2).render(word, True, (250,200,250)) tsurf_r = tsurf.get_rect() tsurf_r.topleft = (x,y) if ( tsurf_r.right > ( width - 5 )): # Wrap. y += tsurf_r.height x = margin tsurf_r.topleft = (x,y) surf.blit(tsurf, tsurf_r.topleft) x = tsurf_r.right height = tsurf_r.bottom + margin if ( newline ): x = margin y = tsurf_r.bottom + newline_gap # line edging for rest of box extra.Line_Edging(surf, bbox, True) return (surf, height)
class Menu: def __init__(self, menu_options, force_width=0): self.options = menu_options self.control_rects = [] self.hover = None self.bbox = None self.selection = None self.update_required = True width_hint = height_hint = 10 if (force_width > 0): width_hint = force_width # Two attempts at drawing required. (discard1, discard2, (width_hint, height_hint)) = self.__Draw( (width_hint, height_hint)) if (width_hint < 150): width_hint = 150 if (force_width > 0): width_hint = force_width (self.surf_store, self.control_rects, (discard1, discard2)) = self.__Draw((width_hint, height_hint)) self.bbox = Rect(0, 0, width_hint, height_hint) def Get_Command(self): return self.selection def Select(self, snum): self.update_required = True self.selection = snum def Mouse_Move(self, spos): if ((spos == None) or (not self.bbox.collidepoint(spos))): self.hover = None return self.update_required = True (x, y) = spos old_sel = self.hover self.hover = None x -= self.bbox.left y -= self.bbox.top for (num, r) in self.control_rects: if (r.collidepoint(x, y)): self.hover = num if (old_sel != self.hover): sound.FX("click_s") return def Mouse_Down(self, spos): self.Mouse_Move(spos) if (self.hover != None): self.selection = self.hover sound.FX("click") def Key_Press(self, k): for (num, name, hotkeys) in self.options: if ((hotkeys != None) and (k in hotkeys)): self.selection = num self.update_required = True sound.FX("click") return def Draw(self, output, centre=None): if (self.update_required): self.update_required = False if (centre == None): self.bbox.center = output.get_rect().center else: self.bbox.center = centre self.bbox.clamp_ip(output.get_rect()) output.blit(self.surf_store, self.bbox.topleft) for (num, r) in self.control_rects: r = Rect(r) r.top += self.bbox.top r.left += self.bbox.left if (num == self.selection): pygame.draw.rect(output, (255, 255, 255), r, 1) elif (num == self.hover): pygame.draw.rect(output, (0, 180, 0), r, 1) def __Draw(self, (width_hint, height_hint)): surf = pygame.Surface((width_hint, height_hint)) bbox = Rect(0, 0, width_hint, height_hint) extra.Tile_Texture(surf, "006metal.jpg", surf.get_rect()) margin = 8 w = bbox.width - (margin * 2) th = None y = margin + bbox.top control_rects = [] max_width = 0 first_item = True for (num, name, hotkeys) in self.options: if (name == None): # a gap if (first_item): img = resource.Load_Image("header.jpg") img_r = img.get_rect() img_r.center = bbox.center img_r.top = y surf.blit(img, img_r.topleft) extra.Edge_Effect(surf, img_r) max_width = img_r.width + (margin * 2) y += img_r.height y += margin * 2 continue txt = render.Render(name, 18, (50, 200, 20), (200, 200, 0)) if (th == None): th = txt.get_rect().height + (margin * 2) tw = txt.get_rect().width + (margin * 2) if (tw > max_width): max_width = tw x = bbox.left + margin r = Rect(x, y, w, th) x += self.Justify(w, txt.get_rect().width) extra.Tile_Texture(surf, "greenrust.jpg", r) extra.Edge_Effect(surf, r) self.Enhancement_Interface(surf, num, r, margin) surf.blit(txt, (x, y + margin - 1)) y += th + margin control_rects.append((num, r)) first_item = False # Finalise drawing extra.Line_Edging(surf, bbox, True) return (surf, control_rects, (max_width, y))
hr.supply = g.net.hub.Get_Steam_Supply() hr.demand = g.net.hub.Get_Steam_Demand() hr.num_nodes = len(g.net.node_list) hr.num_pipes = len([ p for p in g.net.pipe_list if not p.Is_Destroyed() ]) hr.tech_level = g.net.hub.tech_level hr.work_units_used = g.work_units_used hr.work_units_avail = g.net.hub.Get_Avail_Work_Units() hr.city_pressure = g.net.hub.Get_Pressure() return hr # Called at the end of the game, to display statistics: def Review(screen, (width, height), game_object, historian): g = game_object extra.Tile_Texture(screen, "006metal.jpg", screen.get_rect()) def Text(str, size, (x,y), justify): img = stats.Get_Font(size).render(str, True, (255, 255, 255)) if ( justify == 0 ): # centre x -= ( img.get_rect().width ) / 2 elif ( justify < 0 ): # right x -= img.get_rect().width screen.blit(img, (x,y)) y += img.get_rect().height return y + 5 if ( g.win ):
def Review(screen, xxx_todo_changeme2, game_object, historian): (width, height) = xxx_todo_changeme2 g = game_object extra.Tile_Texture(screen, "006metal.jpg", screen.get_rect()) def Text(str, size, xxx_todo_changeme, justify): (x, y) = xxx_todo_changeme img = stats.Get_Font(size).render(str, True, (255, 255, 255)) if (justify == 0): # centre x -= (img.get_rect().width) / 2 elif (justify < 0): # right x -= img.get_rect().width screen.blit(img, (x, y)) y += img.get_rect().height return y + 5 if (g.win): y = Text("You have won the game!", 36, (width / 2, 10), 0) else: y = Text("You have lost the game!", 36, (width / 2, 10), 0) Text("Thankyou for playing!", 15, (width / 2, y), 0) y += height / 10 lev = dict() lev[MENU_TUTORIAL] = lev[MENU_BEGINNER] = "Beginner" lev[MENU_INTERMEDIATE] = "Intermediate" lev[MENU_EXPERT] = "Expert" lev[MENU_PEACEFUL] = "Peaceful" if (g.challenge not in lev): level = "??" else: level = lev[g.challenge] score = float(g.net.hub.total_steam) / float(g.game_time.Get_Day()) if (g.win): score *= 8 score = int(score) l = [("Game length", "%u days" % g.game_time.Get_Day()), ("Steam used", "%1.1f U" % g.net.hub.total_steam), ("Unused work units", "%u" % g.wu_integral), ("Game level", level), ("Your " + level + " Score", "%u" % score)] r = Rect(25, y, width / 2, 1) y = Text("Summary", 18, r.center, 0) for (key, data) in l: Text(key, 18, (r.left, y), 1) y = Text(data, 18, (r.right, y), -1) r.height = y - r.top r = r.inflate(10, 10) pygame.draw.rect(screen, (128, 128, 128), r, 2) y = r.bottom + (height / 10) graph_window = Rect(r.left, y, r.width, (height - y) - 25) available_graphs = [("Steam Supply", "supply", (0, 255, 0)), ("Steam Demand", "demand", (255, 0, 0)), ("Number of Nodes", "num_nodes", (128, 128, 0)), ("Number of Pipes", "num_pipes", (0, 128, 0)), ("City Technology Level", "tech_level", (255, 255, 0)), ("Work Unit Usage", "work_units_used", (255, 0, 255)), ("Work Unit Availability", "work_units_avail", (0, 255, 255)), ("City Steam Pressure", "city_pressure", (0, 0, 255))] def Regraph(xxx_todo_changeme1): (heading, attribute, colour) = xxx_todo_changeme1 pygame.draw.rect(screen, (0, 0, 0), graph_window) pygame.draw.rect(screen, (128, 128, 128), graph_window, 2) graph_subwin = graph_window.inflate(-25, -25) (x, y) = graph_subwin.center y = graph_window.top + 5 Text(heading, 18, (x, y), 0) text_margin = 30 graph_subwin.height -= text_margin graph_subwin.top += text_margin if (len(historian) == 0): print("Historian has no data - no graph available") return max_gt = max_gy = 0 values = [] for hr in historian: try: gy = getattr(hr, attribute) except Attribute_Error: print("Attribute", attribute, "not present") return if (gy < 0): gy = 0 # This should not happen gt = hr.day values.append((gt, gy)) if (gt > max_gt): max_gt = gt if (gy > max_gy): max_gy = gy if ((max_gt <= 0) or (max_gy <= 0)): print("Graph not available (/0)") return def Calc_Step_Max(maximum, number_of_steps): step = int((float(maximum) / float(number_of_steps)) + 1) if (step < 1): step = 1 return (step, int(step * number_of_steps)) (step_gt, max_gt) = Calc_Step_Max(max_gt, 20) (step_gy, max_gy) = Calc_Step_Max(max_gy, 10) t_scale = float(graph_subwin.width) / float(max_gt) y_scale = -1.0 * (float(graph_subwin.height) / float(max_gy)) # Vertical divisions for gt in range(0, max_gt, step_gt): x = int(gt * t_scale) + graph_subwin.left pygame.draw.line(screen, (55, 55, 55), (x, graph_subwin.bottom), (x, graph_subwin.top)) pygame.draw.line(screen, (255, 255, 255), (x, graph_subwin.bottom), (x, graph_subwin.bottom - 2)) # Horizontal divisions for gy in range(0, max_gy, step_gy): y = int(gy * y_scale) + graph_subwin.bottom pygame.draw.line(screen, (75, 75, 75), (graph_subwin.left, y), (graph_subwin.right, y)) pygame.draw.line(screen, (75, 75, 75), (graph_subwin.left, y), (graph_subwin.left + 2, y)) # Graph line (x1, y1) = graph_subwin.bottomleft for (gt, gy) in values: x = int(gt * t_scale) + graph_subwin.left y = int(gy * y_scale) + graph_subwin.bottom pygame.draw.line(screen, colour, (x1, y1), (x, y)) (x1, y1) = (x, y) # Graph border pygame.draw.line(screen, (255, 255, 255), graph_subwin.topleft, graph_subwin.bottomleft) pygame.draw.line(screen, (255, 255, 255), graph_subwin.bottomright, graph_subwin.bottomleft) Regraph(available_graphs[0]) graph_num = 0 # then... proceed = menu.Menu([(None, None, []), (MENU_PREV, "Previous Graph", []), (MENU_NEXT, "Next Graph", []), (None, None, []), (MENU_MENU, "Continue", [K_ESCAPE])]) quit = False while (not quit): (quit, cmd) = extra.Simple_Menu_Loop(screen, proceed, ((width * 3) / 4, height / 2)) if (cmd == MENU_MENU): quit = True elif (cmd == MENU_PREV): graph_num = ((graph_num + len(available_graphs) - 1) % len(available_graphs)) Regraph(available_graphs[graph_num]) elif (cmd == MENU_NEXT): graph_num = (graph_num + 1) % len(available_graphs) Regraph(available_graphs[graph_num]) return