def __init__(self, app): super().__init__(app) self.planet = None self.name_font = pygame.font.Font(None, 18) filename = os.path.join("fonts", "OpenSansRegular.ttf") info_font = pygame.font.Font(filename, 16) text_renderer = UITextRenderer(info_font) self.tile_renderer = UITileRenderer(text_renderer) self.button_production = UIButton("Change", self.on_change_production_clicked) self.button_research = UIButton("Change", self.on_change_research_clicked) self.button_colonize = UIButton("Colonize", self.on_colonize_clicked) self.button_next_turn = UIButton("End Turn", self.on_next_turn_clicked) self.button_fleet = UIButton("Manage", self.on_fleet_manage_clicked) self.buttons = [ self.button_production, self.button_research, self.button_colonize, self.button_next_turn, self.button_fleet ] self.fleet_selection_popup = None self.fleet_info_rect = None self.selected_fleet = None
def __init__(self, app): super().__init__(app) self.selected_planet = None filename = os.path.join("fonts", "OpenSansRegular.ttf") font = pygame.font.Font(filename, 16) text_renderer = UITextRenderer(font) self.tile_renderer = UITileRenderer(text_renderer) self.tiles = {} self.back_button = UIButton("Cancel", self.on_back_button_clicked)
def __init__(self, app): super().__init__(app) filename = os.path.join("fonts", "OpenSansRegular.ttf") font = pygame.font.Font(filename, 16) self.text_renderer = UITextRenderer(font) self.tile_renderer = UITileRenderer(self.text_renderer) filename = os.path.join("images", "fleet.png") self.fleet_surface = pygame.image.load(filename) screen_rect = self._app._surface.get_rect() self.buttons = [] self.header_left = None self.header_right = None b = UIButton("< 1", self.on_left_1_clicked) b.rect.center = screen_rect.center b.rect.move_ip(-32, -32) self.buttons.append(b) b = UIButton("< 10", self.on_left_10_clicked) b.rect.center = screen_rect.center b.rect.move_ip(-32, 0) self.buttons.append(b) b = UIButton("< 100", self.on_left_100_clicked) b.rect.center = screen_rect.center b.rect.move_ip(-32, 32) self.buttons.append(b) b = UIButton("100 >", self.on_right_100_clicked) b.rect.center = screen_rect.center b.rect.move_ip(32, -32) self.buttons.append(b) b = UIButton("10 >", self.on_right_10_clicked) b.rect.center = screen_rect.center b.rect.move_ip(32, 0) self.buttons.append(b) b = UIButton("1 >", self.on_right_1_clicked) b.rect.center = screen_rect.center b.rect.move_ip(32, 32) self.buttons.append(b) self.disband_fleet_left = UIButton("Disband", self.on_disband_fleet_left_clicked) self.buttons.append(self.disband_fleet_left) self.disband_fleet_right = UIButton("Disband", self.on_disband_fleet_right_clicked) self.buttons.append(self.disband_fleet_right) b = UIButton("OK", self.on_ok_clicked) b.rect.bottomright = screen_rect.bottomright b.rect.move_ip(-16, -16) self.buttons.append(b) self.fleet_selection_popup = None
def __init__(self, app): super().__init__(app) self.star = None self.selected_planet = None self.selected_fleet = None self.show_fleet_info = False filename = os.path.join("images", "selection.png") self.selection_marker_surface = pygame.image.load(filename) filename = os.path.join("fonts", "OpenSansRegular.ttf") info_font = pygame.font.Font(filename, 16) text_renderer = UITextRenderer(info_font) self.tile_renderer = UITileRenderer(text_renderer) self.fleet_button = UIButton("Manage", self.on_manage_fleet_clicked) self.next_turn_button = UIButton("End Turn", self.on_next_turn_clicked) self.fleet_selection_popup = None
class ProductionScreen(ScreenBase): def __init__(self, app): super().__init__(app) self.selected_planet = None filename = os.path.join("fonts", "OpenSansRegular.ttf") font = pygame.font.Font(filename, 16) text_renderer = UITextRenderer(font) self.tile_renderer = UITileRenderer(text_renderer) self.tiles = {} self.back_button = UIButton("Cancel", self.on_back_button_clicked) def setup(self, planet): self.selected_planet = planet def on_event(self, event): if (event.type == pygame.KEYUP): if event.key == pygame.K_ESCAPE or event.key == pygame.K_c: self.on_back_button_clicked() elif event.type == pygame.MOUSEBUTTONUP: for project_name, tile_rect in self.tiles.items(): if tile_rect.collidepoint(event.pos): self.change_production(project_name) if self.back_button.rect.collidepoint(event.pos): self.back_button.on_click() def render(self, surface): self.back_button.rect.midbottom = surface.get_rect().midbottom self.back_button.rect.move_ip(0, -16) self.back_button.render(surface) project = self.selected_planet.production.projects["Farms"] farm_surface = self.render_tile(project) farm_rect = farm_surface.get_rect() farm_rect.move_ip(32,32) surface.blit(farm_surface, farm_rect) self.tiles["Farms"] = farm_rect project = self.selected_planet.production.projects["Mines"] mine_surface = self.render_tile(project, (255,255,200)) mine_rect = mine_surface.get_rect() mine_rect.topleft = farm_rect.bottomleft mine_rect.move_ip(0, 6) surface.blit(mine_surface, mine_rect) self.tiles["Mines"] = mine_rect project = self.selected_planet.production.projects["Laboratories"] lab_surface = self.render_tile(project, (200,200,255)) lab_rect = lab_surface.get_rect() lab_rect.topleft = mine_rect.bottomleft lab_rect.move_ip(0, 6) surface.blit(lab_surface, lab_rect) self.tiles["Laboratories"] = lab_rect project = self.selected_planet.production.projects["Defenses"] def_surface = self.render_tile(project, (255,200,200)) def_rect = def_surface.get_rect() def_rect.topleft = lab_rect.bottomleft def_rect.move_ip(0, 6) surface.blit(def_surface, def_rect) self.tiles["Defenses"] = def_rect anchor = surface.get_rect().topright anchor = anchor[0] - 32, anchor[1] + 32 if self.selected_planet.shipyard_level == 0: project = self.selected_planet.production.projects["Shipyard1"] sy_surface = self.render_tile(project, (255,200,200)) sy_rect = sy_surface.get_rect() sy_rect.topright = anchor surface.blit(sy_surface, sy_rect) self.tiles["Shipyard1"] = sy_rect anchor = sy_rect.bottomright anchor = anchor[0], anchor[1] + 6 if (self.selected_planet.player.tech_levels["Shipyard"] >= 2) \ and (self.selected_planet.shipyard_level == 1): project = self.selected_planet.production.projects["Shipyard2"] sy_surface = self.render_tile(project, (255,200,200)) sy_rect = sy_surface.get_rect() sy_rect.topright = anchor surface.blit(sy_surface, sy_rect) self.tiles["Shipyard2"] = sy_rect anchor = sy_rect.bottomright anchor = anchor[0], anchor[1] + 6 if (self.selected_planet.player.tech_levels["Shipyard"] >= 3) \ and (self.selected_planet.shipyard_level == 2): project = self.selected_planet.production.projects["Shipyard3"] sy_surface = self.render_tile(project, (255,200,200)) sy_rect = sy_surface.get_rect() sy_rect.topright = anchor surface.blit(sy_surface, sy_rect) self.tiles["Shipyard3"] = sy_rect anchor = sy_rect.bottomright anchor = anchor[0], anchor[1] + 6 if self.selected_planet.shipyard_level >= 1: project = self.selected_planet.production.projects["Scout"] scout_surface = self.render_tile(project, (255,200,200)) scout_rect = scout_surface.get_rect() scout_rect.topright = anchor surface.blit(scout_surface, scout_rect) self.tiles["Scout"] = scout_rect anchor = scout_rect.bottomright anchor = anchor[0], anchor[1] + 6 if self.selected_planet.shipyard_level >= 3 \ and self.selected_planet.player.tech_levels["Colony"] > 0: project = self.selected_planet.production.projects["Colony"] colony_surface = self.render_tile(project, (255,255,255)) colony_rect = colony_surface.get_rect() colony_rect.topright = anchor surface.blit(colony_surface, colony_rect) self.tiles["Colony"] = colony_rect anchor = colony_rect.bottomright anchor = anchor[0], anchor[1] + 6 if self.selected_planet.shipyard_level >= 2 \ and self.selected_planet.player.tech_levels["Frigate"] > 0: project = self.selected_planet.production.projects["Frigate"] frigate_surface = self.render_tile(project, (255,200,200)) frigate_rect = frigate_surface.get_rect() frigate_rect.topright = anchor surface.blit(frigate_surface, frigate_rect) self.tiles["Frigate"] = frigate_rect anchor = frigate_rect.bottomright anchor = anchor[0], anchor[1] + 6 if self.selected_planet.shipyard_level >= 3 \ and self.selected_planet.player.tech_levels["Destroyer"] > 0: project = self.selected_planet.production.projects["Destroyer"] destroyer_surface = self.render_tile(project, (255,200,200)) destroyer_rect = destroyer_surface.get_rect() destroyer_rect.topright = anchor surface.blit(destroyer_surface, destroyer_rect) self.tiles["Destroyer"] = destroyer_rect def render_tile(self, project, color=(255,255,255)): text = "" text += project.name + "\n" text += project.desc + "\n" text += "Cost: " + str(project.cost) return self.tile_renderer.render(text, color) def on_back_button_clicked(self): self._app.screens.change_to("Planet") def change_production(self, project): self.selected_planet.production.change_to(project) self._app.screens.change_to("Planet")
class PlanetScreen(ScreenBase): def __init__(self, app): super().__init__(app) self.planet = None self.name_font = pygame.font.Font(None, 18) filename = os.path.join("fonts", "OpenSansRegular.ttf") info_font = pygame.font.Font(filename, 16) text_renderer = UITextRenderer(info_font) self.tile_renderer = UITileRenderer(text_renderer) self.button_production = UIButton("Change", self.on_change_production_clicked) self.button_research = UIButton("Change", self.on_change_research_clicked) self.button_colonize = UIButton("Colonize", self.on_colonize_clicked) self.button_next_turn = UIButton("End Turn", self.on_next_turn_clicked) self.button_fleet = UIButton("Manage", self.on_fleet_manage_clicked) self.buttons = [ self.button_production, self.button_research, self.button_colonize, self.button_next_turn, self.button_fleet ] self.fleet_selection_popup = None self.fleet_info_rect = None self.selected_fleet = None def setup(self, planet): self.planet = planet self.centered_rect = planet.rect.copy() self.centered_rect.width *= 3 self.centered_rect.height *= 3 self.centered_surface = pygame.transform.smoothscale(planet.surface, self.centered_rect.size) self.centered_rect.center = self._app._surface.get_rect().center self.name_surf = self.name_font.render(self.planet.name, True, (255,255,255)) self.name_rect = self.name_surf.get_rect() if planet.player: surface = planet.player.icon_ownermarker self.ownermarker_rect = surface.get_rect() self.ownermarker_rect.width *= 3 self.ownermarker_rect.height *= 3 self.ownermarker = pygame.transform.smoothscale(surface, self.ownermarker_rect.size) surface = planet.player.icon_shipyard self.shipyard_rect = surface.get_rect() self.shipyard_rect.width *= 2 self.shipyard_rect.height *= 2 self.shipyard_surface = pygame.transform.smoothscale(surface, self.shipyard_rect.size) surface = planet.player.icon_defense self.defense_rect = surface.get_rect() self.defense_rect.width *= 2 self.defense_rect.height *= 2 self.defense_surface = pygame.transform.smoothscale(surface, self.defense_rect.size) self.player_fleets = [] for f in self.planet.fleets: if f.player == self._app.local_player: self.player_fleets.append(f) if self.player_fleets: self.selected_fleet = self.player_fleets[0] elif self.planet.fleets: self.selected_fleet = self.planet.fleets[0] else: self.selected_fleet = None self.fleet1, self.fleet2, self.fleet3 = self.fleet_picker(self.planet.fleets) if self.fleet1: self.fleet1_rect = self.fleet1.rect.copy() self.fleet1_rect.width *= 2 self.fleet1_rect.height *= 2 self.fleet1_surface = pygame.transform.smoothscale(self.fleet1.surface, self.fleet1_rect.size) self.fleet1_rect.midleft = self.centered_rect.topright if self.fleet2: self.fleet2_rect = self.fleet2.rect.copy() self.fleet2_rect.width *= 2 self.fleet2_rect.height *= 2 self.fleet2_surface = pygame.transform.smoothscale(self.fleet2.surface, self.fleet2_rect.size) self.fleet2_rect.midbottom = self.centered_rect.topright if self.fleet3: self.fleet3_rect = self.fleet3.rect.copy() self.fleet3_rect.width *= 2 self.fleet3_rect.height *= 2 self.fleet3_surface = pygame.transform.smoothscale(self.fleet3.surface, self.fleet3_rect.size) self.fleet3_rect.midbottom = self.centered_rect.topleft def on_event(self, event): if (event.type == pygame.KEYUP): if (event.key == pygame.K_q) or (event.key == pygame.K_ESCAPE): self._app.screens.change_to("Quit") elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER: self.on_next_turn_clicked() elif (event.key == pygame.K_g): self._app.screens.change_to("Galaxy") elif (event.key == pygame.K_s): self._app.screens.change_to("Star") elif (event.key == pygame.K_t): s = self._app.screens.change_to("Test") s.setup("Planet") elif (event.key == pygame.K_r): self.on_change_research_clicked() elif (event.key == pygame.K_p): self.on_change_production_clicked() elif (event.key == pygame.K_f): self.on_fleet_manage_clicked() elif (event.key == pygame.K_PERIOD): self.on_next_planet() elif (event.key == pygame.K_p): if (pygame.key.get_mods() & pygame.KMOD_LSHIFT): self.on_prev_planet() elif (pygame.key.get_mods() & pygame.KMOD_RSHIFT): self.on_prev_planet() else: self.on_next_planet() elif (event.key == pygame.K_COMMA): self.on_prev_planet() elif (event.type == pygame.MOUSEBUTTONUP): if self.fleet_selection_popup: self.selected_fleet = self.fleet_selection_popup.handle_click(event) self.fleet_selection_popup = None else: if self.centered_rect.collidepoint(event.pos): self.on_planet_clicked() else: if len(self.player_fleets) > 1: if self.fleet_info_rect.collidepoint(event.pos) \ or self.fleet1_rect.collidepoint(event.pos): self.fleet_selection_popup = UIPopup( self.player_fleets, event.pos ) for b in self.buttons: if b.rect.collidepoint(event.pos): b.on_click() def update(self, delta_time): pass def render(self, surface): surface.blit(self.centered_surface, self.centered_rect) if self.planet.player: self.ownermarker_rect.center = surface.get_rect().center surface.blit(self.ownermarker, self.ownermarker_rect) if self.fleet1: surface.blit(self.fleet1_surface, self.fleet1_rect) if self.fleet2: surface.blit(self.fleet2_surface, self.fleet2_rect) if self.fleet3: surface.blit(self.fleet3_surface, self.fleet3_rect) if self.planet.shipyard_level > 0: self.shipyard_rect.midleft = self.centered_rect.bottomright surface.blit(self.shipyard_surface, self.shipyard_rect) if self.planet.defense > 0: self.defense_rect.midright = self.centered_rect.bottomleft surface.blit(self.defense_surface, self.defense_rect) # Planet name self.name_rect.midtop = self.centered_rect.midbottom surface.blit(self.name_surf, self.name_rect) # Planet info info_surface = self.render_info_text() info_rect = info_surface.get_rect() info_rect.bottomright = self.centered_rect.topleft info_rect.move_ip(-32, -32) surface.blit(info_surface, info_rect) # Colonize button if self.planet.player: self.button_colonize.rect.bottomright = (0, 0) elif self.planet.fleets and self.selected_fleet.get_ship_counts()["Colony"] > 0: self.button_colonize.rect.midtop = info_rect.midbottom self.button_colonize.rect.move_ip(0, -1) self.button_colonize.render(surface) # Fleet info fleet_surface = self.render_fleet_text() self.fleet_info_rect = fleet_surface.get_rect() # self. because of the fleet selection popup self.fleet_info_rect.bottomleft = self.centered_rect.topright self.fleet_info_rect.move_ip(32, -32) surface.blit(fleet_surface, self.fleet_info_rect) if self.player_fleets: self.button_fleet.rect.midtop = self.fleet_info_rect.midbottom self.button_fleet.rect.move_ip(0, -1) self.button_fleet.render(surface) else: self.button_fleet.rect.topright = (0, 0) if self.planet.player: # Research info research_surface = self.render_research_text() research_rect = research_surface.get_rect() research_rect.topright = self.centered_rect.bottomleft research_rect.move_ip(-32, 32) surface.blit(research_surface, research_rect) if self.planet.player == self._app.local_player: self.button_research.rect.midtop = research_rect.midbottom self.button_research.rect.move_ip(0, -1) self.button_research.render(surface) else: self.button_research.rect.topright = (0, 0) # Production info production_surface = self.render_production_text() production_rect = production_surface.get_rect() production_rect.topleft = self.centered_rect.bottomright production_rect.move_ip(32, 32) surface.blit(production_surface, production_rect) if self.planet.player == self._app.local_player: self.button_production.rect.midtop = production_rect.midbottom self.button_production.rect.move_ip(0, -1) self.button_production.render(surface) else: self.button_production.rect.topright = (0, 0) self.button_next_turn.rect.topright = surface.get_rect().topright self.button_next_turn.rect.move_ip(-16, 16) self.button_next_turn.render(surface) if self.fleet_selection_popup: self.fleet_selection_popup.render(surface) # 3 fleets to choose # if local player has a fleet, it must be f1 # only choose the first fleet of every player # if more than 3 players, only choose the first 3 def fleet_picker(self, fleets): f1 = None f2 = None f3 = None player_fleets = {} # First fleet of every player for f in fleets: if f.player not in player_fleets: player_fleets[f.player] = f # if local player has a fleet, it is f1 if self._app.local_player in player_fleets: f1 = player_fleets[self._app.local_player] del player_fleets[self._app.local_player] for player, fleet in player_fleets.items(): if not f1: f1 = player_fleets[player] elif not f2: f2 = player_fleets[player] elif not f3: f3 = player_fleets[player] else: break return f1, f2, f3 def render_info_text(self): text = "" text += "Class: " + self.planet.size + " " + self.planet.type + "\n" text += "Population: " + str(self.planet.population) + "\n" text += "Industry: " + str(self.planet.industry) + "\n" text += "Science: " + str(self.planet.science) + "\n" text += "Defense: " + str(self.planet.defense) + "\n" if self.planet.shipyard_level == 0: text += "Shipyard: None" else: text += "Shipyard: Lvl." + str(self.planet.shipyard_level) return self.tile_renderer.render(text, (255,255,255)) def render_research_text(self): if self.planet.current_research_project == None: text = "" text += "Researching:\n" text += "(Nothing)\n" text += "Cost: N/A\n" text += "Progress: N/A" return self.tile_renderer.render(text, (200,200,255)) text = "" text += "Researching:\n" text += self.planet.current_research_project.name + "\n" text += "Cost: " + str(self.planet.current_research_project.cost) + "\n" if self.planet.current_research_project.progress < self.planet.current_research_project.cost: text += "Progress: " + str(self.planet.current_research_project.progress) + "\n" else: text += "Progress: COMPLETE!" return self.tile_renderer.render(text, (200,200,255)) def render_production_text(self): if self.planet.production.current_project == None: text = "" text += "Producing:\n" text += "(Nothing)\n" text += "Cost: N/A\n" text += "Progress: N/A\n" text += "ETA: N/A\n" return self.tile_renderer.render(text, (255,255,200)) if self.planet.industry == 0: remaining_turns = "N/A" else: remaining_turns = str(math.ceil( (self.planet.production.current_project.cost - self.planet.production.current_project.progress) / self.planet.industry )) + " turn(s)" text = "" text += "Producing:\n" text += self.planet.production.current_project.name + "\n" text += "Cost: " + str(self.planet.production.current_project.cost) + "\n" text += "Progress: " + str(self.planet.production.current_project.progress) + "\n" text += "ETA: " + remaining_turns + "\n" return self.tile_renderer.render(text, (255,255,200)) def render_fleet_text(self): if not self.planet.fleets: text = "No fleet" else: ship_counts = self.selected_fleet.get_ship_counts() text = "" text += self.selected_fleet.name + "\n" text += "Scout(s): " + str(ship_counts["Scout"]) + "\n" text += "Colony Ship(s): " + str(ship_counts["Colony"]) + "\n" text += "Frigate(s): " + str(ship_counts["Frigate"]) + "\n" text += "Destroyer(s): " + str(ship_counts["Destroyer"]) + "\n" text += "Total: " + str(ship_counts["Total"]) return self.tile_renderer.render(text, (255,200,200)) def on_next_turn_clicked(self): self._app.next_turn() self.setup(self.planet) def on_planet_clicked(self): self._app.screens.change_to("Star") def on_change_production_clicked(self): s = self._app.screens.change_to("Production") s.setup(self.planet) def on_change_research_clicked(self): s = self._app.screens.change_to("Research") s.setup(self.planet) def on_fleet_manage_clicked(self): if self.player_fleets: s = self._app.screens.change_to("Fleet") s.setup(self.selected_fleet, "Planet", self.on_back_from_fleet_manage) def on_back_from_fleet_manage(self, fleet_screen): if fleet_screen.fleet_left: self.selected_fleet = fleet_screen.fleet_left elif len(fleet_screen.fleets) > 0: self.selected_fleet = fleet_screen.fleets[0] else: self.selected_fleet = None def on_colonize_clicked(self): self.planet.player.found_colony(self.planet, self.selected_fleet) def on_next_planet(self): self.select_planet(self._app.local_player.next_planet(self.selected_planet).star) def on_prev_planet(self): self.select_planet(self._app.local_player.prev_planet(self.selected_planet).star)
class ResearchScreen(ScreenBase): def __init__(self, app): super().__init__(app) self.selected_planet = None filename = os.path.join("fonts", "OpenSansRegular.ttf") font = pygame.font.Font(filename, 16) text_renderer = UITextRenderer(font) self.tile_renderer = UITileRenderer(text_renderer) self.tiles = {} self.back_button = UIButton("Cancel", self.on_back_button_clicked) def setup(self, planet): self.selected_planet = planet def on_event(self, event): if (event.type == pygame.KEYUP): if event.key == pygame.K_ESCAPE or event.key == pygame.K_c: self.on_back_button_clicked() elif event.type == pygame.MOUSEBUTTONUP: for project_name, tile_rect in self.tiles.items(): if tile_rect.collidepoint(event.pos): self.change_project(project_name) if self.back_button.rect.collidepoint(event.pos): self.back_button.on_click() def render(self, surface): self.back_button.rect.midbottom = surface.get_rect().midbottom self.back_button.rect.move_ip(0, -16) self.back_button.render(surface) anchor = surface.get_rect().topleft anchor = anchor[0] + 32, anchor[1] + 32 if self.selected_planet.player.tech_levels["Shipyard"] < 3: project = self.selected_planet.player.research_projects["Shipyard"] sy_surface = self.render_tile(project) sy_rect = sy_surface.get_rect() sy_rect.topleft = anchor surface.blit(sy_surface, sy_rect) self.tiles["Shipyard"] = sy_rect anchor = surface.get_rect().topright anchor = anchor[0] - 32, anchor[1] + 32 project = self.selected_planet.player.research_projects["Scout"] scout_surface = self.render_tile(project, (255,200,200)) scout_rect = scout_surface.get_rect() scout_rect.topright = anchor surface.blit(scout_surface, scout_rect) self.tiles["Scout"] = scout_rect anchor = scout_rect.bottomright anchor = anchor[0], anchor[1] + 6 project = self.selected_planet.player.research_projects["Colony"] colony_surface = self.render_tile(project, (255,255,255)) colony_rect = colony_surface.get_rect() colony_rect.topright = anchor surface.blit(colony_surface, colony_rect) self.tiles["Colony"] = colony_rect anchor = colony_rect.bottomright anchor = anchor[0], anchor[1] + 6 project = self.selected_planet.player.research_projects["Frigate"] frigate_surface = self.render_tile(project, (255,200,200)) frigate_rect = frigate_surface.get_rect() frigate_rect.topright = anchor surface.blit(frigate_surface, frigate_rect) self.tiles["Frigate"] = frigate_rect anchor = frigate_rect.bottomright anchor = anchor[0], anchor[1] + 6 project = self.selected_planet.player.research_projects["Destroyer"] destroyer_surface = self.render_tile(project, (255,200,200)) destroyer_rect = destroyer_surface.get_rect() destroyer_rect.topright = anchor surface.blit(destroyer_surface, destroyer_rect) self.tiles["Destroyer"] = destroyer_rect def render_tile(self, project, color=(255,255,255)): text = "" text += project.name + "\n" text += project.desc + "\n" text += "Cost: " + str(project.cost) return self.tile_renderer.render(text, color) def on_back_button_clicked(self): self._app.screens.change_to("Planet") def change_project(self, project_name): self.selected_planet.set_research(project_name) self._app.screens.change_to("Planet")
class StarScreen(ScreenBase): def __init__(self, app): super().__init__(app) self.star = None self.selected_planet = None self.selected_fleet = None self.show_fleet_info = False filename = os.path.join("images", "selection.png") self.selection_marker_surface = pygame.image.load(filename) filename = os.path.join("fonts", "OpenSansRegular.ttf") info_font = pygame.font.Font(filename, 16) text_renderer = UITextRenderer(info_font) self.tile_renderer = UITileRenderer(text_renderer) self.fleet_button = UIButton("Manage", self.on_manage_fleet_clicked) self.next_turn_button = UIButton("End Turn", self.on_next_turn_clicked) self.fleet_selection_popup = None def setup(self, star): """Setup the screen around this star""" self.star = star self.selected_planet = None self.selected_fleet = None self.show_fleet_info = False self.centered_rect = star.rect.copy() self.centered_rect.width *= 3 self.centered_rect.height *= 3 self.centered_surface = pygame.transform.smoothscale(star.surface, self.centered_rect.size) self.name_rect = self.star.name_surf.get_rect() def on_event(self, event): if (event.type == pygame.KEYUP): if (event.key == pygame.K_q) or (event.key == pygame.K_ESCAPE): self._app.screens.change_to("Quit") elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER: self.on_next_turn_clicked() elif (event.key == pygame.K_g): self._app.screens.change_to("Galaxy") elif (event.key == pygame.K_PERIOD): self.on_next_planet() elif (event.key == pygame.K_p): if (pygame.key.get_mods() & pygame.KMOD_LSHIFT): self.on_prev_planet() elif (pygame.key.get_mods() & pygame.KMOD_RSHIFT): self.on_prev_planet() else: self.on_next_planet() elif (event.key == pygame.K_COMMA): self.on_prev_planet() elif (event.type == pygame.MOUSEBUTTONUP): if self.fleet_selection_popup: clicked_fleet = self.fleet_selection_popup.handle_click(event) self.fleet_selection_popup = None if clicked_fleet: self.on_fleet_clicked(clicked_fleet) else: if self.centered_rect.collidepoint(event.pos): self.on_star_clicked() elif self.next_turn_button.rect.collidepoint(event.pos): self.on_next_turn_clicked() elif self.show_fleet_info \ and self.selected_fleet.player == self._app.local_player \ and self.fleet_button.rect.collidepoint(event.pos): self.on_manage_fleet_clicked() else: for p in self.star.planets: if p.rect.collidepoint(event.pos): self.on_planet_clicked(p) return clicked_fleets = [] for f in self.star.fleets: # if f.player == self._app.local_player: if f.rect_s.collidepoint(event.pos): clicked_fleets.append(f) if len(clicked_fleets) == 1: self.on_fleet_clicked(clicked_fleets[0]) elif len(clicked_fleets) > 1: self.fleet_selection_popup = UIPopup( clicked_fleets, clicked_fleets[0].rect_s.center ) def update(self, delta_time): pass def render(self, surface): fleets_in_orbit_around_star = [] for f in self.star.fleets: if f.destination_star or f.destination_center_star_rect: pygame.draw.aaline( surface, (255,255,255), f.rect_s.center, self.centered_rect.center ) if f.destination_planet: if f.player != self._app.local_player \ and f.destination_planet.player == self._app.local_player: color = (255,0,0) else: color = (255,255,255) pygame.draw.aaline( surface, color, f.rect_s.center, f.destination_planet.rect.center ) if not f.planet: if f.destination_star \ or f.destination_center_star_rect \ or f.destination_planet: f.rect_s = f.rect.copy() f.rect_s.midright = self.centered_rect.topleft surface.blit(f.surface, f.rect_s) else: fleets_in_orbit_around_star.append(f) f1, f2, f3 = self.fleet_picker(fleets_in_orbit_around_star) if f1: f1.rect_s = f1.rect.copy() f1.rect_s.midleft = self.centered_rect.topright surface.blit(f1.surface, f1.rect_s) if f2: f2.rect_s = f2.rect.copy() f2.rect_s.midbottom = self.centered_rect.topright surface.blit(f2.surface, f2.rect_s) if f3: f3.rect_s = f3.rect.copy() f3.rect_s.midbottom = self.centered_rect.topleft f3.rect_s.move_ip(35, 0) surface.blit(f3.surface, f3.rect_s) for p in self.star.planets: surface.blit(p.surface, p.rect) if self.selected_planet: surface.blit(self.selection_marker_surface, self.selected_planet.rect) if p.player: surface.blit(p.player.icon_ownermarker, p.rect) if p.shipyard_level > 0: rect = p.rect.copy() rect.midleft = p.rect.bottomright surface.blit(p.player.icon_shipyard, rect) if p.defense > 0: rect = p.rect.copy() rect.midright = p.rect.bottomleft surface.blit(p.player.icon_defense, rect) if p.fleets: fleets_in_orbit = [] for f in p.fleets: if f.destination_star \ or f.destination_center_star_rect \ or f.destination_planet: rect = f.rect.copy() rect.midright = p.rect.topleft surface.blit(f.surface, f.rect_s) else: fleets_in_orbit.append(f) f1, f2, f3 = self.fleet_picker(fleets_in_orbit) if f1: rect = f1.rect.copy() rect.midleft = p.rect.topright surface.blit(f1.surface, rect) if f2: rect = f2.rect.copy() rect.midbottom = p.rect.topright surface.blit(f2.surface, rect) if f3: rect = f3.rect.copy() rect.midbottom = p.rect.topleft rect.move_ip(3, 0) surface.blit(f3.surface, rect) surface.blit(p.name_surf, p.name_rect) self.centered_rect.center = surface.get_rect().center surface.blit(self.centered_surface, self.centered_rect) self.name_rect.midtop = self.centered_rect.midbottom surface.blit(self.star.name_surf, self.name_rect) if self.selected_fleet: surface.blit(self.selection_marker_surface, self.selected_fleet.rect_s) if self.show_fleet_info: fleet_info_surface = self.render_fleet_text() fleet_info_rect = fleet_info_surface.get_rect() fleet_info_rect = self.move_fleet_rect(fleet_info_rect) surface.blit(fleet_info_surface, fleet_info_rect) if self.selected_fleet.player == self._app.local_player: self.fleet_button.rect.midtop = fleet_info_rect.midbottom self.fleet_button.render(surface) self.next_turn_button.rect.topright = surface.get_rect().topright self.next_turn_button.rect.move_ip(-16, 16) self.next_turn_button.render(surface) if self.fleet_selection_popup: self.fleet_selection_popup.render(surface) # 3 fleets to choose # if local player has a fleet, it must be f1 # only choose the first fleet of every player # if more than 3 players, only choose the first 3 def fleet_picker(self, fleets): f1 = None f2 = None f3 = None player_fleets = {} # First fleet of every player for f in fleets: if f.player not in player_fleets: player_fleets[f.player] = f # if local player has a fleet, it is f1 if self._app.local_player in player_fleets: f1 = player_fleets[self._app.local_player] del player_fleets[self._app.local_player] for player, fleet in player_fleets.items(): if not f1: f1 = player_fleets[player] elif not f2: f2 = player_fleets[player] elif not f3: f3 = player_fleets[player] else: break return f1, f2, f3 def render_fleet_text(self): ship_counts = self.selected_fleet.get_ship_counts() text = "" text += self.selected_fleet.name + "\n" text += "Scout(s): " + str(ship_counts["Scout"]) + "\n" text += "Colony Ship(s): " + str(ship_counts["Colony"]) + "\n" text += "Frigate(s): " + str(ship_counts["Frigate"]) + "\n" text += "Destroyer(s): " + str(ship_counts["Destroyer"]) + "\n" text += "Total: " + str(ship_counts["Total"]) return self.tile_renderer.render(text, (255,200,200), (0,0,0)) def move_fleet_rect(self, fleet_info_rect): center_x = self.centered_rect.centerx center_y = self.centered_rect.centery if self.selected_fleet.rect_s.centerx < center_x: if self.selected_fleet.rect_s.centery < center_y: fleet_info_rect.topleft = self.selected_fleet.rect_s.bottomright fleet_info_rect.move_ip(8, 8) else: fleet_info_rect.bottomleft = self.selected_fleet.rect_s.topright fleet_info_rect.move_ip(8, -8) else: if self.selected_fleet.rect_s.centery < center_y: fleet_info_rect.topright = self.selected_fleet.rect_s.bottomleft fleet_info_rect.move_ip(-8, 8) else: fleet_info_rect.bottomright = self.selected_fleet.rect_s.topleft fleet_info_rect.move_ip(-8, -8) return fleet_info_rect def select_planet(self, planet): if self.selected_fleet \ and self.selected_fleet.player == self._app.local_player: self.dispatch_fleet_to_planet(self.selected_fleet, planet) self.selected_fleet = None self.show_fleet_info = False else: if self.selected_planet == planet: screen = self._app.screens.change_to("Planet") screen.setup(planet) else: self.selected_planet = planet self.selected_fleet = None self.show_fleet_info = False def select_fleet(self, fleet): if fleet == self.selected_fleet: self.show_fleet_info = not self.show_fleet_info else: self.selected_fleet = fleet self.selected_planet = None if fleet.player != self._app.local_player: self.show_fleet_info = True def dispatch_fleet_to_planet(self, fleet, planet): # Cannot change destination while traveling if not fleet.star: return # Cancel departure if planet == fleet.planet: fleet.cancel_departure() fleet.rect_s.midleft = fleet.planet.rect.topright return fleet.set_destination_planet(planet) if fleet.planet: fleet.rect_s.midright = fleet.planet.rect.topleft else: fleet.rect_s.midright = self.centered_rect.topleft def select_star(self): if self.selected_fleet \ and self.selected_fleet.player == self._app.local_player: self.dispatch_fleet_to_star(self.selected_fleet, self.centered_rect) self.selected_fleet = None self.show_fleet_info = False else: self._app.screens.change_to("Galaxy") def dispatch_fleet_to_star(self, fleet, star_rect): # Cannot change destination while traveling if not fleet.star: return # Cancel departure if not fleet.planet: fleet.cancel_departure() fleet.rect_s.midleft = star_rect.topright return fleet.set_destination_center_star(star_rect) fleet.rect_s.midright = fleet.planet.rect.topleft def on_star_clicked(self): self.select_star() def on_planet_clicked(self, planet): self.select_planet(planet) def on_fleet_clicked(self, fleet): self.select_fleet(fleet) def on_next_turn_clicked(self): self._app.next_turn() def on_manage_fleet_clicked(self): s = self._app.screens.change_to("Fleet") s.setup(self.selected_fleet, "Star", self.on_back_from_fleet_screen) def on_back_from_fleet_screen(self, fleet_screen): if fleet_screen.fleet_left == None: if fleet_screen.fleet_right == None: self.selected_fleet = None self.show_fleet_info = False else: self.selected_fleet = fleet_screen.fleet_right else: self.selected_fleet = fleet_screen.fleet_left