Writing text in pygame

2019-09-02 20:47发布

问题:

I know how to display text in pygame, but what I really need is to be able to write a text when the pygame window is already running.

this is my pygame window code: import pygame

def highscore():
    pygame.font.init()
    background_colour = (255,255,255)
    (width, height) = (600, 600)

    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption('Tutorial 1')
    screen.fill(background_colour)

    pygame.display.flip()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False



highscore()

回答1:

http://www.daniweb.com/software-development/python/code/244474/pygame-text-python

Here's a link to an example... the code you're kinda looking for is this I think...

# pick a font you have and set its size
myfont = pg.font.SysFont("Comic Sans MS", 30)
# apply it to text on a label
label = myfont.render("Python and Pygame are Fun!", 1, yellow)
# put the label object on the screen at point x=100, y=100
screen.blit(label, (100, 100))


回答2:

A slow but simple way to do it is:

import pygame

def text(surface, fontFace, size, x, y, text, colour):
    font = pygame.font.Font(fontFace, size)
    text = font.render(text, 1, colour)
    surface.blit(text, (x, y)0

screen = pygame.display.set_mode(500, 100)
screen.fill((255, 255, 255))
text(screen, 'font.ttf', 32, 5, 5, 'This is text', (0, 0, 0)

But this is slow as you have to load the font every time so I use this:

import pygame

fonts = {}

def text(surface, fontFace, size, x, y, text, colour):
    if size in fonts:
        font = fonts[size]
    else:
        font = pygame.font.Font(fontFace, size)
        fonts[size] = font
    text = font.render(text, 1, colour)
    surface.blit(text, (x, y)0

screen = pygame.display.set_mode(500, 100)
screen.fill((255, 255, 255))
text(screen, 'font.ttf', 32, 5, 5, 'This is text', (0, 0, 0)

This way, it keeps track of every font size used and if it has already used it, I loads it from a dictionary, otherwise it loads the font and saves it to the dictionary. The only problem is that this only works if you only use one font face, but for different font faces used, you could just have different dictionaries.

To loop just do:

screen = pygame.display.set_mode(500, 100)
running = True
while running:
    screen.fill((255, 255, 255))
    text(screen, 'font.ttf', 32, 5, 5, 'This is text', (0, 0, 0)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False

Also, just a side note, you need a file called 'font.ttf' in the CWD for this example to work.



回答3:

First you need to make a font object there is 2 ways

Way 1:

fontname = 'freesansbold.ttf'
fontsize = 30
font = pygame.font.Font(fontname, fontsize)

Way 2:

fontname = 'comicsansbold'
fontsize = 30
font = pygame.font.SysFont(fontname, fontsize)

Then you need to render a surface of your text from your font object

text = 'test'
antialias = True
colour = 0,0,0
textSurf = font.render(text, antialias, colour

Now you have your text surface and you can blit it to the screen and stuff.