Ejemplo n.º 1
0
 def textwrap(string: str, font: pygame.font.Font, limit, margin):
     words = string.split()
     i = 0
     before = 0
     newline = ''
     out = []
     while i < len(words):
         word = words[i]
         after = before + font.size(word)[0]
         if after < margin:
             newline += word + ' '
             before = after + font.size(' ')[0]
         else:
             if before > limit:
                 i -= 1
             else:
                 for j in range(len(word)):
                     if before + font.size(word[:j + 1] + '-')[0] > margin:
                         if j < 3:
                             i -= 1
                             break
                         newline += word[:j - 1] + '-'
                         words.insert(i + 1, '-' + word[j - 1:])
                         break
             out.append(newline)
             before = 0
             newline = ''
         i += 1
     out.append(newline)
     return out
Ejemplo n.º 2
0
    def drawText(self,
                 text,
                 color,
                 shadow_color,
                 rect,
                 font: pygame.font.Font,
                 scale=False):
        rect = pygame.Rect(rect)
        y = rect.top
        lineSpacing = -2

        fontHeight = font.size("Tg")[1]

        while text:
            i = 1

            if y + fontHeight > rect.bottom:
                break

            while font.size(text[:i])[0] < rect.width and i < len(text):
                i += 1

            if i < len(text):
                i = text.rfind(" ", 0, i) + 1

            image = font.render(text[:i], False, color).convert_alpha()
            shadow = font.render(text[:i], False, shadow_color).convert_alpha()

            if scale:
                image = pygame.transform.smoothscale(image, (int(
                    image.get_rect().size[0] * 0.9), image.get_rect().size[1]))
                shadow = pygame.transform.smoothscale(
                    shadow, (int(shadow.get_rect().size[0] * 0.9),
                             shadow.get_rect().size[1]))

            self.screen.blit(shadow,
                             (rect.left + (self.window_size[0] / 240) *
                              (fontHeight / 36), y +
                              (self.window_size[1] / 160) * (fontHeight / 36)))
            self.screen.blit(image, (rect.left, y))
            y += fontHeight + lineSpacing

            text = text[i:]

        return text
Ejemplo n.º 3
0
 def draw_score(self, window: pygame.Surface, font: pygame.font.Font):
     transparent_rect = pygame.Surface((95, 25), pygame.SRCALPHA)
     transparent_rect.fill((50, 50, 50, 80))
     message = f"Score: {self.score} "
     w, h = font.size(message)
     window.blit(pygame.transform.scale(transparent_rect, (w, h)),
                 (8, Application.WINDOW_HEIGHT - 30))
     window.blit(font.render(message, True, (255, 255, 255)),
                 (10, Application.WINDOW_HEIGHT - 30))
Ejemplo n.º 4
0
def word_length(text: str, font: pg.font.Font) -> pg.Rect:
    """

    Args:
        text:
        font:

    Returns:

    """
    return font.size(text)
Ejemplo n.º 5
0
 def render_multiline_text(self,
                           position,
                           text: str,
                           font: pygame.font.Font,
                           color=(255, 255, 255),
                           antialias=0):
     lines = text.splitlines()
     for x in lines:
         text = x.replace('\t', '    ')
         width, height = font.size(text)
         label = font.render(text, antialias, color)
         self.display.blit(label, position)
         position[1] += height
Ejemplo n.º 6
0
    def draw_text(self,
                  font: pygame.font.Font,
                  word: str,
                  colour: Tuple[int, int],
                  x_frac: Tuple[int, int],
                  y_frac: Tuple[int, int],
                  sub_x_pixels: int = None,
                  sub_y_pixels: int = None,
                  sub_coordinates: Tuple[int, int] = [0, 0]) -> pygame.Rect:
        """Draw text onto the screen and return the corresponding pygame.Rect.

        x_frac and y_frac are tuples where the first element is the numerator and
        the second element is the denominator in a fraction that splits the
        screen lengthwise and widthwise, respectively.

        Normally, the entire screen is used for partitioning. However, the
        coordinates and resolution for a subwindow can be given to be used
        instead.
        """
        if sub_x_pixels and sub_y_pixels:
            x_pixels = sub_x_pixels
            y_pixels = sub_y_pixels
        else:
            x_pixels = self.x_pixels
            y_pixels = self.y_pixels

        word_surface = font.render(word, True, colour)
        x_coord = (((x_pixels / x_frac[1] - font.size(word)[0]) / 2) +
                   ((x_frac[0] - 1) / x_frac[1]) * x_pixels)
        y_coord = (((y_pixels / y_frac[1] - font.size(word)[1]) / 2) +
                   ((y_frac[0] - 1) / y_frac[1]) * y_pixels)
        self.screen.blit(
            word_surface,
            (x_coord + sub_coordinates[0], y_coord + sub_coordinates[1]))
        return pygame.Rect(
            (x_coord + sub_coordinates[0], y_coord + sub_coordinates[1]),
            (font.size(word)[0], font.size(word)[1]))
Ejemplo n.º 7
0
def _print_instructions(screen: pygame.Surface,
                        font: pygame.font.Font, height: int) -> \
        pygame.Surface:
    text_height = font.size("Test")[1]
    image = screen.subsurface(((750, 0), (250, height)))

    # Setup the initial position
    x_pos = 10
    y_pos = 5

    y_pos = _print_human_instructions(x_pos, y_pos, text_height, font, image)

    y_pos += text_height + Y_FONT_PADDING
    y_pos = _print_ai_instructions(x_pos, y_pos, text_height, font, image)

    y_pos += text_height + Y_FONT_PADDING
    _print_colours(x_pos, y_pos, text_height, font, image)

    return image
Ejemplo n.º 8
0
    def __init__(self, text: str, font: pygame.font.Font, duration: float,
                 *groups: pygame.font.Font):
        super().__init__(*groups)
        w, h = font.size(text)
        self.rect = pygame.Rect(0, 0, w + self.padding, h + self.padding)
        self.rect.center = SCREEN_SIZE[0] // 2, SCREEN_SIZE[1] // 2
        self.text = text
        self.font = font
        self.duration = duration

        self.image = pygame.surface.Surface(self.rect.size)
        pygame.draw.rect(self.image, (0, 0, 0),
                         pygame.Rect(0, 0, *self.rect.size))
        pygame.draw.rect(self.image, (255, 255, 255),
                         pygame.Rect(0, 0, *self.rect.size),
                         self.border_thickness)
        w, h = self.rect.size
        tw, th = self.font.size(self.text)
        text_rect = pygame.Rect((w - tw) / 2, (h - th) / 2, tw, th)
        self.image.blit(self.font.render(self.text, True, (255, 255, 255)),
                        text_rect)
Ejemplo n.º 9
0
 def __init__(self, text, x, y, font: pygame.font.Font):
     self.text = text
     self.x, self.y = x, y
     self.w, self.h = font.size(self.text)