def __init__(self, x, y, resource): # font setup font = pygame.font.Font(PortRect.font_type, PortRect.font_size) font_width = get_font_width(font) font_height = get_font_height(font) margin_multiplier = 0.15 margin_x = int(font_width * margin_multiplier) margin_y = int(font_height * margin_multiplier) rect_x = PortRect.num_chars * font_width + 2 * margin_x rect_y = font_height + 2 * margin_y super().__init__(PortRect.color, rect_y, rect_x, (x, y)) # setup 'exchange', i.e. the leftmost char of the rect which will # be colored based on the resource this port is associated with self.resource = resource if resource == Resource.ANY_RESOURCE: exchange_rate = '3' else: exchange_rate = '2' self.exchange = Text(font, exchange_rate, resource.value) self.exchange.rect.x = self.rect.x + margin_x self.exchange.rect.y = self.rect.y + margin_y # base text always the same ':1' after the exchange rate self.base_text = Text(font, ' :1', BLACK) margin_adjuster = 2 # to better center text self.base_text.rect.x = self.rect.x + margin_x + margin_adjuster self.base_text.rect.y = self.rect.y + margin_y
def __init__(self, x, y, resource, font): self.resource = resource self.font = font # counter number initialization self.num = SelectableText(font, '0', resource.value, 0, 0) font_height = get_font_height(font) font_width = get_font_width(font) # store this center location long-term for re-centering self.num_center = (int(x + ResourceCounter.font_x_offset * font_width), int(y + ResourceCounter.font_y_offset * font_height)) self.num.rect.center = self.num_center # arrow initialization arrow_size = int(font_height * ResourceCounter.arrow_size_modifier) arrow_offset = int(font_height / 2 + font_height * ResourceCounter.arrow_separation) self.up_arrow = Triangle(x, y - arrow_offset, arrow_size, ResourceCounter.arrow_color) self.down_arrow = Triangle(x, y + arrow_offset, arrow_size, ResourceCounter.arrow_color, pointed_up=False) # collision rects for detecting mouse / clicks self.up_arrow_collision = pygame.rect.Rect(0, 0, arrow_size, arrow_size) self.up_arrow_collision.center = (x, y - arrow_offset) self.down_arrow_collision = pygame.rect.Rect(0, 0, arrow_size, arrow_size) self.down_arrow_collision.center = (x, y + arrow_offset)
def __init__(self, x, y, prompt, allow_cancel): super().__init__(TBox.color, TBox.height, TBox.width, (x, y)) self.x = x self.font = pygame.font.Font(TBox.font_type, TBox.font_size) # setup prompt prompt_width = get_font_width(self.font) * len(prompt) self.prompt = TextRect(x, self.rect.y + TBox.prompt_box_offset, prompt_width, TBox.prompt_box_height, prompt, self.font) # setup response buttons if allow_cancel: self.ok = TextRect(self.rect.left + TBox.ok_box_x_offset, self.rect.top + TBox.response_box_y_offset, TBox.response_box_width, TBox.response_box_height, 'OK', self.font) self.cancel = TextRect(self.rect.left + TBox.cancel_box_x_offset, self.rect.top + TBox.response_box_y_offset, TBox.response_box_width, TBox.response_box_height, 'CANCEL', self.font) else: self.ok = TextRect(self.rect.left + TBox.lone_ok_box_x_offset, self.rect.top + TBox.response_box_y_offset, TBox.response_box_width, TBox.response_box_height, 'OK', self.font) self.cancel = None # text for if the user does something bad and should be scolded self.error_text = None
def __init__(self, x, y, font): font_width = get_font_width(font) self.counters = [] # have a counter for each of the five resource cur_x = x - 2 * font_width - 2 * ResourceSelector.counter_separation * font_width for resource in common.Resource: self.counters.append(ResourceCounter(cur_x, y, resource, font)) cur_x += font_width + ResourceSelector.counter_separation * font_width
def __init__(self, num_players): # font stuff self.large_font = pygame.font.Font(SideBar.font_type, SideBar.large_font_size) self.small_font = pygame.font.Font(SideBar.font_type, SideBar.small_font_size) self.large_font_height = get_font_height(self.large_font) self.small_font_height = get_font_height(self.small_font) # now begin adding Text items to the SideBar from the top to the bottom self.player_header = SelectableText(self.large_font, 'Players: ', SideBar.normal_header_text_color, SideBar.x, SideBar.y) self.cur_player_top = SideBar.y + self.large_font_height self.players = [] # players can't be added yet, so skip down and create everything else self.cur_top = self.cur_player_top + (self.large_font_height + 3 * self.small_font_height) \ * num_players # resources are the first thing after the players self.resources_header = SelectableText( self.large_font, 'Resources: ', SideBar.normal_header_text_color, SideBar.x, self.cur_top) self.cur_top += self.large_font_height self.resources = ResourceBlock(SideBar.x, self.cur_top) self.cur_top += 5 * self.large_font_height # 5 for the number of different resources # next come the development cards self.cards_header = SelectableText(self.large_font, 'Dev Cards: ', SideBar.normal_header_text_color, SideBar.x, self.cur_top) self.cur_top += self.large_font_height self.cards = self.create_cards() # then the actions a user can take when it is their turn self.action_header = SelectableText(self.large_font, 'Actions: ', SideBar.normal_header_text_color, SideBar.x, self.cur_top) self.cur_top += self.large_font_height self.actions = self.create_actions() # finally, create the box that will be in the background box_width = SideBar.chars_per_line * get_font_width(self.large_font) self.box = NonCenteredRect(SideBar.background, self.cur_top - SideBar.y, box_width, SideBar.x, SideBar.y)
def __init__(self, x, y, max_num_players, chars_per_line, large_font, small_font, background_color): self.x = x self.y = y self.max_num_players = max_num_players self.chars_per_line = chars_per_line self.large_font = large_font self.small_font = small_font self.large_font_height = get_font_height(large_font) self.small_font_height = get_font_height(small_font) large_font_width = get_font_width(large_font) box_height = self.large_font_height * ( max_num_players + 1) + self.small_font_height * max_num_players * 3 box_width = large_font_width * chars_per_line self.box = NonCenteredRect(background_color, box_height, box_width, x, y) # note: title_text is a SelectableText but will never actually be selected self.title_text = SelectableText(large_font, "Players: ", WHITE, self.x, self.y) self.current_top = y + self.large_font_height # where the next text item should go self.players = []