Skip to content
TheChatArchiveThe Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)Hugging Face OpenAssistant Conversations (oasst2)

Ich habe einen Text und möchte diesen in pygame darstellen. Wie erzeuge ich newlines so…

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    Ich habe einen Text und möchte diesen in pygame darstellen. Wie erzeuge ich newlines so das der Text in der Darstellung nie aus dem Bildschirm rausreicht?

  2. Assistant

    Dafür gibt es leider keine einfache Möglichkeit. Du musst also jedes Wort einzeln rendern und dann schauen, ob der Text über den Rand des Fensters hinausgeht. Hier ist ein Beispielcode, der dir helfen könnte:

    import pygame
    
    
    pygame.init()
    
    
    SIZE = WIDTH, HEIGHT = (1024, 720)
    FPS = 30
    screen = pygame.display.set_mode(SIZE, pygame.RESIZABLE)
    clock = pygame.time.Clock()
    
    
    def blit_text(surface, text, pos, font, color=pygame.Color('black')):
        words = [word.split(' ') for word in text.splitlines()]  # 2D array where each row is a list of words.
        space = font.size(' ')[0]  # The width of a space.
        max_width, max_height = surface.get_size()
        x, y = pos
        for line in words:
            for word in line:
                word_surface = font.render(word, 0, color)
                word_width, word_height = word_surface.get_size()
                if x + word_width >= max_width:
                    x = pos[0]  # Reset the x.
                    y += word_height  # Start on new row.
                surface.blit(word_surface, (x, y))
                x += word_width + space
            x = pos[0]  # Reset the x.
            y += word_height  # Start on new row.
    
    
    text = "Dies ist ein extrem langer Satz mit einigen Zeilenumbrüchen.\nManchmal bricht der Text auch " \
        "dann ab, wenn kein Zeilenumbruch im Satz ist, das liegt daran, dass der Text zu lang ist, um auf " \ 
        "den Bildschirm zu passen.\nManchmal kann das seltsam aussehen.\nDiese Funktion prüft allerdings " \
        "nicht, ob der Text zu hoch ist, um auf die Höhe der Oberfläche zu passen, sodass der untere Teil " \
        "des Textes manchmal verschwindet."
    
    font = pygame.font.SysFont('Arial', 64)
    
    while True:
    
        dt = clock.tick(FPS) / 1000
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
    
        screen.fill(pygame.Color('white'))
        blit_text(screen, text, (20, 20), font)
        pygame.display.update()
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.