示例#1
0
	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
						)
示例#2
0
	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()
示例#3
0
	def header_right_clicked(self):
		fleet_list = self.fleets[:]
		if self.fleet_left:
			fleet_list.remove(self.fleet_left)
		fleet_list.append(NewFleetOption())
		if len(fleet_list) > 0:
			self.popup_right = UIPopup(fleet_list, self.header_right.rect.center)
示例#4
0
    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_PERIOD):
                self.on_next_planet()
            elif (event.key == pygame.K_COMMA):
                self.on_prev_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.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:
                for s in self._app.world.stars:
                    if s.rect.collidepoint(event.pos):
                        self.on_star_clicked(s)

                clicked_fleets = []
                for fleet in self._app.local_player.fleets:
                    if fleet.rect.collidepoint(event.pos):
                        clicked_fleets.append(fleet)
                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.center)

            if self.next_turn_button.rect.collidepoint(event.pos):
                self.on_next_turn_clicked()
示例#5
0
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)
示例#6
0
class GalaxyScreen(ScreenBase):
    def __init__(self, app):
        super().__init__(app)
        self.selected_star = None
        self.selected_fleet = None

        filename = os.path.join("images", "selection.png")
        self.selection_marker_surface = pygame.image.load(filename)

        self.next_turn_button = UIButton("End Turn", self.on_next_turn_clicked)

        self.fleet_selection_popup = None

    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_PERIOD):
                self.on_next_planet()
            elif (event.key == pygame.K_COMMA):
                self.on_prev_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.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:
                for s in self._app.world.stars:
                    if s.rect.collidepoint(event.pos):
                        self.on_star_clicked(s)

                clicked_fleets = []
                for fleet in self._app.local_player.fleets:
                    if fleet.rect.collidepoint(event.pos):
                        clicked_fleets.append(fleet)
                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.center)

            if self.next_turn_button.rect.collidepoint(event.pos):
                self.on_next_turn_clicked()

    def update(self, delta_time):
        pass

    def render(self, surface):
        for s in self._app.world.stars:
            surface.blit(s.surface, s.rect)

            for p in s.planets:
                if p.player:
                    surface.blit(p.player.icon_ownermarker, s.rect)

                if p.shipyard_level > 0:
                    rect = s.rect.copy()
                    rect.midleft = s.rect.bottomright
                    surface.blit(p.player.icon_shipyard, rect)

            if s.fleets:
                rect = s.rect.copy()
                orbiting_fleets = [
                    f for f in s.fleets if not f.destination_star
                ]
                f1, f2, f3 = self.fleet_picker(orbiting_fleets)
                if f1:
                    rect.midleft = s.rect.topright
                    surface.blit(f1.surface, rect)
                if f2:
                    rect.midbottom = s.rect.topright
                    surface.blit(f2.surface, rect)
                if f3:
                    rect.midbottom = s.rect.topleft
                    rect.move_ip(3, 0)
                    surface.blit(f3.surface, rect)

            surface.blit(s.name_surf, s.name_rect)

        for player in self._app.players + self._app.ais:
            for f in player.fleets:
                if f.destination_star:
                    # TODO: red for incoming enemy fleet
                    pygame.draw.aaline(surface, player.color, f.rect.center,
                                       f.destination_star.rect.center)
                    surface.blit(f.surface, f.rect)

        if self.selected_star:
            surface.blit(self.selection_marker_surface,
                         self.selected_star.rect)

        if self.selected_fleet:
            surface.blit(self.selection_marker_surface,
                         self.selected_fleet.rect)

        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 select_star(self, star):
        if self.selected_fleet:
            self.dispatch_fleet(self.selected_fleet, star)
            self.selected_fleet = None
        else:
            if self.selected_star == star:
                screen = self._app.screens.change_to("Star")
                screen.setup(star)
            else:
                self.selected_star = star
                self.selected_fleet = None

    def select_fleet(self, fleet):
        self.selected_fleet = fleet
        self.selected_star = None

    def dispatch_fleet(self, fleet, star):
        # Cannot change destination while traveling
        if not fleet.star:
            return

        # Cancel departure
        if star == fleet.star:
            fleet.cancel_departure()
            fleet.rect.midleft = fleet.star.rect.topright
            fleet = None
            return

        fleet.set_destination_star(star)
        fleet.rect.midright = fleet.star.rect.topleft

    def on_star_clicked(self, star):
        self.select_star(star)

    def on_fleet_clicked(self, fleet):
        self.select_fleet(fleet)

    def on_next_turn_clicked(self):
        self._app.next_turn()
示例#7
0
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