class DialogBox(MouseListener, KeyListener): def __init__(self, orientation, position, dimensions, dialog_tree, speaker_image): self._current_node = dialog_tree self._orientation = orientation self._position = position self._dimensions = dimensions self._speaker = pygame.image.load(utilities.relative_path(speaker_image, __file__)) background_file = utilities.relative_path('control_assets/dialogBox.png', __file__) self._background = pygame.image.load(background_file) padded_position = (position[0] + constants.DIALOG_BOX_TEXT_PADDING, position[1] + constants.DIALOG_BOX_TEXT_PADDING) self._text_box = Label(padded_position, (constants.DIALOG_BOX_DIMENSIONS[0] * .75 - (2 * constants.DIALOG_BOX_TEXT_PADDING), constants.DIALOG_BOX_DIMENSIONS[1] * .75 - (2 * constants.DIALOG_BOX_TEXT_PADDING)), self._current_node.get_text(), font_size=35, multiline=True) def _next_string(self): self._current_node = self._current_node.follow_link(0) self._text_box.set_string(self._current_node.get_text()) def handle_key_down(self, key): if key == K_SPACE: self._next_string() def handle_mouse_motion(self, coords): pass def handle_mouse_button_up(self, coords): pass def handle_mouse_button_down(self, coords): pass def _draw_background(self, screen): self._background = pygame.transform.scale(self._background, self._dimensions) screen.blit(self._background, self._position) def _draw_speaker(self, screen): speaker_dimensions = (int(constants.DIALOG_BOX_DIMENSIONS[0] * .25), int(constants.DIALOG_BOX_DIMENSIONS[0] * .25)) self._speaker = pygame.transform.scale(self._speaker, speaker_dimensions) speaker_position = (constants.DIALOG_BOX_COORDS[0] + int(0.75 * constants.DIALOG_BOX_DIMENSIONS[0]), constants.DIALOG_BOX_COORDS[1]) screen.blit(self._speaker, speaker_position) def draw(self, screen): self._draw_background(screen) self._draw_speaker(screen) self._text_box.draw(screen)
class Button(MouseListener): def __init__(self, text, position, dimensions): self._hovered = False self._mouse_downed = False self._label = Label(position, dimensions, text, font_size=dimensions[1], padding=(constants.BUTTON_TEXT_PADDING, constants.BUTTON_TEXT_PADDING)) self._hit_box = Rect(position, dimensions) self._position = position self._dimensions = dimensions super().__init__() def handle_mouse_motion(self, coords): self._hovered = self._hit_box.collidepoint(coords) def handle_mouse_button_up(self, coords): clicked = False if self._hovered and self._mouse_downed: clicked = True self._mouse_downed = False self._hovered = False return clicked def handle_mouse_button_down(self, coords): if self._hovered: self._mouse_downed = True def draw(self, screen): if self._hovered: border = constants.GREY background = constants.WHITE else: border = constants.WHITE background = constants.GREY screen.fill(border, self._hit_box) screen.fill( background, self._hit_box.inflate(-constants.BUTTON_BORDER_WEIGHT, -constants.BUTTON_BORDER_WEIGHT)) self._label.draw(screen)
class Interface: def __init__(self, size, img_dict, options): self.size = size self.img_dict = img_dict self.step = img_dict[Img.block_b].get_rect().h self.FIELD_START_WIDTH = int(options[Opt.width]/5 + (options[Opt.width]/5*4 - self.step*size)/2) self.FIELD_START_HEIGHT = int(options[Opt.height]/10) self.animation = [] self.up_information = None self.down_information = None def set_information(self, points, person, players): up_information = "Black :" + str(points[-1]) + "; White : " +\ str(points[1]) + "." self.up_information = Label(up_information, int(self.FIELD_START_WIDTH/10), (self.FIELD_START_WIDTH, self.FIELD_START_HEIGHT/5)) down_information = "" if players[-1] != Player.man or players[1] != Player.man: down_information = "You " + \ ("black. " if players[-1] == Player.man else "white. ") down_information += "Turn " + \ ("black. " if person == -1 else "white. ") self.down_information = Label(down_information, int(self.FIELD_START_WIDTH/10), (self.FIELD_START_WIDTH, self.FIELD_START_HEIGHT/5*45)) def draw(self, display, field, path={}): iRow = iColumn = 0 for row in field: for column in row: block_name = Img.block_w if (iColumn + iRow % 2) % 2 == 0 else\ Img.block_b display.blit(self.img_dict[block_name], (self.FIELD_START_WIDTH+iColumn*self.step, self.FIELD_START_HEIGHT+iRow*self.step)) if column in (-1, 1, 2): if column == 1: block_name = Img.point_w elif column == -1: block_name = Img.point_b elif column == 2: block_name = Img.black_hall display.blit(self.img_dict[block_name], (self.FIELD_START_WIDTH+iColumn*self.step, self.FIELD_START_HEIGHT+iRow*self.step)) iColumn += 1 iColumn = 0 iRow += 1 for (keyX, keyY) in path: display.blit(self.img_dict[Img.point], (self.FIELD_START_WIDTH+keyY*self.step, self.FIELD_START_HEIGHT+keyX*self.step)) self.up_information.draw(display) self.down_information.draw(display) def event(self): mouse = pygame.mouse.get_pos() mouse = (mouse[0] - self.FIELD_START_WIDTH, mouse[1] - self.FIELD_START_HEIGHT) if mouse_in(mouse, (0, 0), (self.step * self.size, self.step * self.size)): return mouse[1]//self.step, mouse[0]//self.step else: return -1, -1