def draw_drone(surface, radius, position, theta, parameter, direction, color, sides): """ Draw a party. :param surface: Surface to draw :type surface: Surface :param radius: Party radius :param position: Party position :type position: Point :param theta: Angle :param parameter: Animation parameter :param direction: Current moving direction :type direction: Point """ if direction.x == 0: angle = np.pi / 2 else: angle = np.arctan(direction.y / direction.x) if direction.x > 0: angle += np.pi / 6 else: angle -= np.pi / 6 theta_1 = angle theta_2 = angle + np.pi * 2 / 6 position_1 = position + Point(-direction.y, direction.x).limit_size(1).scale(radius / 2) position_2 = position + Point(direction.y, -direction.x).limit_size(1).scale(radius / 2) draw_ngon(surface, color, sides, radius / 2, position_1, theta_1) draw_ngon(surface, color, sides, radius / 2, position_2, theta_2)
def get_size(self): """ :return: Size :rtype: Point """ return Point( self.font.size(self.text)[0], self.font.size(self.text)[1]) + Point( self.options[self.active_option].get_size().x, 0)
def draw(self, surface): """ Draw menu centered on the screen. :param surface: Surface to draw :type surface: Surface """ v_size = self.get_vertical_size() center = Point(surface.get_size()[0], surface.get_size()[1]).scale(0.5) current_position = center - v_size.copy().scale(0.5) for item in self.menu_items: item.set_position(current_position.copy()) current_position = current_position + Point(0, item.get_size().y).copy() for item in self.menu_items: item.draw(surface)
def get_size(self): """ :return: Size :rtype: Point """ return Point( self.font.size(self.get_text())[0], self.font.size(self.get_text())[1])
def __init__(self, a_map, position, radius, destination, max_speed): """ Constructor :param a_map: Map which this object belongs to :type a_map: Map :param position: Object's position :type position: Point :param destination: Object's moving destination :type destination: GraphicColony :param max_speed: Object's max speed """ super(MovingObject, self).__init__(a_map, position, radius) self.destination = destination self.speed = Point(0, 0) self.acceleration = Point(0, 0) self.max_speed = max_speed self.external = Point(0, 0)
def select(self, position): """ Select given position and act according to current state. :param position: Position selected :type position: tuple """ position = Point(position[0], position[1]) self.state.select(position)
def create_points(): points = [] point = BearoffPoint( ((Measures.QUADRANTWIDTH.value * 2) + Measures.BARSIZE.value, Measures.BOTTOMHEIGHT.value - Measures.BORDERSIZE.value), 0, (Measures.BORDERSIZE.value, Measures.BOARDHEIGHT.value + Measures.BORDERSIZE.value)) points.append(point) x = Measures.BOARDWIDTH.value - Measures.BORDERSIZE.value - Measures.TRIANGLEWIDTH.value y = Measures.BOARDHEIGHT.value - Measures.BORDERSIZE.value - Measures.TRIANGLEHEIGHT.value for i in range(1, 7): point = Point((50, 200), i, (x, y)) points.append(point) x = x - Measures.TRIANGLEWIDTH.value x = Measures.BOARDWIDTH.value - Measures.BORDERSIZE.value - Measures.QUADRANTWIDTH.value \ - Measures.BARSIZE.value - Measures.TRIANGLEWIDTH.value y = Measures.BOARDHEIGHT.value - Measures.BORDERSIZE.value - Measures.TRIANGLEHEIGHT.value for i in range(7, 13): point = Point((50, 200), i, (x, y)) points.append(point) x = x - Measures.TRIANGLEWIDTH.value x = Measures.BORDERSIZE.value y = Measures.BORDERSIZE.value for i in range(13, 19): point = Point((50, 200), i, (x, y)) points.append(point) x = x + Measures.TRIANGLEWIDTH.value x = Measures.BORDERSIZE.value + Measures.QUADRANTWIDTH.value + Measures.BARSIZE.value y = Measures.BORDERSIZE.value for i in range(19, 25): point = Point((50, 200), i, (x, y)) points.append(point) x = x + Measures.TRIANGLEWIDTH.value point = BarPoint( (Measures.BARSIZE.value, Measures.QUADRANTHEIGHT.value), 25, (Measures.BORDERSIZE.value + Measures.QUADRANTWIDTH.value, Measures.BORDERSIZE.value)) points.append(point) return points
def update_mouse_position(self, position): """ Acknowledge current mouse position. :param position: Mouse position :type position: tuple """ position = Point(position[0], position[1]) self.state.update_mouse_position(position)
def hover(self, position): """ Hover at given position and act according to current state. :param position: Position to hover :type position: tuple """ position = Point(position[0], position[1]) self.state.hover(position)
def release_mouse(self, position): """ Release mouse at given position and act according to current state. :param position: Position where release occurs :type position: tuple """ position = Point(position[0], position[1]) self.state.release_mouse(position)
def __init__(self, driver): """ Constructor. :param driver: Current game driver. """ self.menu_items = [] self.active_selection = 0 self.mouse_position = Point(0, 0) self.driver = driver
def get_vertical_size(self): """ Get vertical size of all the menu items stacked together. :return: Vertical size :rtype: Point """ v_size = Point(0, 0) for item in self.menu_items: v_size.y += item.get_size().y return v_size
def draw(self, surface): """ Draw on given surface. :param surface: Surface to draw on :type surface: Surface """ draw_ngon(surface, self.colony.get_color(), 4, self.radius, self.position, self.theta) label = self.font.render(self.text, 1, GUISettings.COLONY_NUMBER_COLOR) font_point = Point(self.font.size(self.text)[0], self.font.size(self.text)[1]).scale(0.5) surface.blit(label, (self.position - font_point).to_tuple())
def draw(self, surface): """ Draw on given surface. :param surface: Surface to draw :type surface: Surface """ label = self.font.render(self.text, 1, GUI_TEXT_COLOR) font_point = self.get_size().scale(0.5) top_left_corner = self.position - font_point surface.blit(label, top_left_corner.to_tuple()) self.options[self.active_option].draw( surface, self.position + Point(self.font.size(self.text)[0] / 2, 0))
def __init__(self, menu, text, font_size=TITLE_FONT_SIZE, bold=False): """ Constructor. :param menu: Unterlying menu :param text: Label :param font_size: Font size :param bold: Flag to make text bold """ super(Label, self).__init__(menu, NullHandler(), font_size, bold) self.text = text self.size = Point( self.font.size(self.text)[0], self.font.size(self.text)[1])
def load_random(self, a_map, n_colonies, min_size, max_size, human_players, n_enemies, possible_races): a_map.empty() n_players = len(human_players) + n_enemies theta = np.random.rand(1)[0] * 2 * np.pi center = Point(a_map.width / 2, a_map.height / 2) for human in human_players: position = Point( np.cos(theta) * (a_map.width / 2 - INITIAL_COLONIES_RADIUS), np.sin(theta) * (a_map.height / 2 - INITIAL_COLONIES_RADIUS), ) + center GraphicColony(a_map, RegularColony(human.race, INITIAL_PLAYER_COLONY_SIZE), position, INITIAL_COLONIES_RADIUS) theta += (2 * np.pi) / n_players other_players = human_players.copy() for i in range(n_enemies): position = Point( np.cos(theta) * (a_map.width / 2 - INITIAL_COLONIES_RADIUS), np.sin(theta) * (a_map.height / 2 - INITIAL_COLONIES_RADIUS), ) + center enemy = RandomPlayer(other_players) enemy.random_race(other_players, possible_races) other_players.append(enemy) GraphicColony(a_map, RegularColony(enemy.race, INITIAL_PLAYER_COLONY_SIZE), position, INITIAL_COLONIES_RADIUS) theta += (2 * np.pi) / n_players self.players = other_players for i in range(n_colonies): while True: radius = np.random.randint(min_size, max_size) position = Point( np.random.randint(radius, a_map.width - radius), np.random.randint(radius, a_map.height - radius) ) if a_map.can_place_colony(radius, position): GraphicColony(a_map, RegularColony(NullRace(), INITIAL_NULL_COLONY_SIZE), position, radius) break else: print("Can't place random colony. Generating a new one.") pass
def __init__(self, menu, handler, size=DEFAULT_FONT_SIZE, bold=False): """ Constructor. :param menu: Underlying menu :param handler: Handler :param size: Font size :param bold: Flag to make bold :type bold: bool """ self.text = "" self.menu = menu self.handler = handler self.font = pygame.font.Font("res/Cabin-Regular.ttf", size) self.font.set_bold(bold) self.position = Point(0, 0)
def draw_eccentric(surface, radius, position, theta, parameter, direction, color, sides): """ Draw a party. :param surface: Surface to draw :type surface: Surface :param radius: Party radius :param position: Party position :type position: Point :param theta: Angle :param parameter: Animation parameter :param direction: Current moving direction :type direction: Point """ theta = theta * FERTILE_SPIN_SPEED new_position = position + Point( np.cos(parameter * FERTILE_ANIMATION_SPEED) * radius * FERTILE_ANIMATION_AMP, np.sin(parameter * FERTILE_ANIMATION_SPEED) * radius * FERTILE_ANIMATION_AMP) draw_ngon(surface, color, sides, radius * 2 / 3, new_position, theta)
def read_files(self, sub_folder): """ Loads race files. :param sub_folder: Sub folder where the data is :type sub_folder: str """ for file in os.listdir(os.getcwd() + sub_folder): if file == INSTRUCTION_FILENAME: continue f = open(os.getcwd() + sub_folder + file) map_data = dict() map_data[NAME_FIELD] = "" map_data[N_ENEMIES_FIELD] = 0 map_data[EMPTY_COL_FIELD] = [] map_data[ENEMY_COL_FIELD] = [] map_data[PLAYER_COL_FIELD] = [] for line in f: line = line.strip("\n") elements = line.split("=") if len(elements) == 2: if elements[0] == NAME_FIELD: map_data[NAME_FIELD] = elements[1] if elements[0] == N_ENEMIES_FIELD: map_data[N_ENEMIES_FIELD] = int(elements[1]) else: args = elements[1].split(",") field = "" if elements[0] == EMPTY_COL_FIELD: field = EMPTY_COL_FIELD if elements[0] == ENEMY_COL_FIELD: field = ENEMY_COL_FIELD if elements[0] == PLAYER_COL_FIELD: field = PLAYER_COL_FIELD if not field == "": map_data[field].append( (RegularColony(), int(args[0]), Point(float(args[1]), float(args[2])), float(args[3]))) self.maps[map_data[NAME_FIELD]] = map_data
def draw(self, surface): """ Draw on given surface :type surface: Surface """ label = self.font.render(self.time_as_text(), 1, CLOCK_TEXT_COLOR) text_max_size = Point( self.font.size("00:00")[0], self.font.size("00:00")[1]) text_size = Point( self.font.size(self.time_as_text())[0], self.font.size(self.time_as_text())[1]) top_left_corner = self.upper_center_point - Point( text_size.copy().scale(0.5).x, 0) p1 = (self.upper_center_point + Point(-text_max_size.copy().scale(0.5).x, 0)).to_tuple() p2 = (self.upper_center_point + Point(text_max_size.copy().scale(0.5).x, 0)).to_tuple() p3 = (self.upper_center_point + Point(text_max_size.copy().scale(0.5).x, text_max_size.copy().y)).to_tuple() p4 = (self.upper_center_point + Point(-text_max_size.copy().scale(0.5).x, text_max_size.copy().y)).to_tuple() pygame.draw.polygon(surface, CLOCK_RECT_COLOR, [p1, p2, p3, p4]) surface.blit(label, top_left_corner.to_tuple())
class MovingObject(AbstractGraphicObject): """ An object that moves towards a destination. """ def __init__(self, a_map, position, radius, destination, max_speed): """ Constructor :param a_map: Map which this object belongs to :type a_map: Map :param position: Object's position :type position: Point :param destination: Object's moving destination :type destination: GraphicColony :param max_speed: Object's max speed """ super(MovingObject, self).__init__(a_map, position, radius) self.destination = destination self.speed = Point(0, 0) self.acceleration = Point(0, 0) self.max_speed = max_speed self.external = Point(0, 0) def tick(self): """ Update object's position. """ self.move() def set_max_speed(self, new_max_speed): """ Set max speed :param new_max_speed: New max speed :type new_max_speed: float """ self.max_speed = new_max_speed return self def move(self): """ Update position """ # Set speed towards destination self.speed = (self.destination.position - self.position).limit_size(self.max_speed) # Add external speed and adjust to avoid getting stuck self.speed += self.external.limit_size(self.max_speed).copy().scale( Settings.GUISettings.PARTY_EXTERNAL_SPEED_ADJUST ) # Normalize speed self.speed.normalize(self.max_speed) # Effectively move self.position = self.position + self.speed @abc.abstractmethod def arrived(self): """ Handle arrival to destination. """ pass @abc.abstractmethod def collide(self, o): """ Collide with an object. :param o: Object to collide with """ pass @abc.abstractmethod def add_to_map(self): """ Add to its map. """ pass @abc.abstractmethod def avoid(self, o): """ Avoid given object. :param o: Object to avoid """ pass