Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to make text change lines pygame

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 = "This is a really long sentence with a couple of breaks.
Sometimes it will break even if there isn't a break " 
       "in the sentence, but that's because the text is too long to fit the screen.
It can look strange sometimes.
" 
       "This function doesn't check if the text is too high to fit on the height of the surface though, so sometimes " 
       "text will disappear underneath the surface"
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()
Comment

PREVIOUS NEXT
Code Example
Python :: python get current class name 
Python :: install pythonjsonlogger 
Python :: Python get all keys from nested dictionary 
Python :: google-api-python-client python 3 
Python :: how to extract domain name from url python 
Python :: python merge list of lists 
Python :: spacy tokineze stream 
Python :: selenium set chrome executable path 
Python :: python web crawler 
Python :: soup itemprop 
Python :: how to make my discord bot shut down with a command 
Python :: django superuser 
Python :: import picturein colab 
Python :: python planet list 
Python :: pandas replace last cell 
Python :: Upper letter list 
Python :: enter selenium in python 
Python :: find an index of an item in a list python 
Python :: python window icon on task bar 
Python :: how to reindex columns in pandas 
Python :: Visualize Decision Tree 
Python :: check if two columns are equal pandas 
Python :: add to a list python 
Python :: python count character occurrences 
Python :: radiobuttons django 
Python :: loading in pyqt5 
Python :: make sns heatmap colorbar larger 
Python :: install chrome driver python 
Python :: count nan values 
Python :: async python 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =