如何在pygame中显示文本?

use*_*817 21 python text pygame

我无法想象在pygame中显示文本.
我知道我不能像常规python IDLE一样使用print但我不知道怎么做

import pygame, sys
from pygame.locals import *

BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = ( 255, 0, 0)

pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)

DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('P.Earth')
while 1: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:           
            pygame.display.update() 

import time

direction = ''
print('Welcome to Earth')
pygame.draw.rect(screen, RED, [55,500,10,5], 0)
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

这只是整个计划的开始部分.
如果有一种格式允许我显示我在pygame窗口中键入的文本,那就太棒了.因此,不使用打印,我会使用其他东西.但我不知道那是什么东西:/当我在pygame中运行我的程序时,它没有显示任何东西.
我希望程序在pygame窗口中运行,而不是只在空闲时运行.

Bar*_*ski 30

您可以在其上创建包含文本的曲面.为此,请看一下这个简短的例子:

pygame.font.init() # you have to call this at the start, 
                   # if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS', 30)
Run Code Online (Sandbox Code Playgroud)

这将创建一个可以调用该render方法的新对象.

textsurface = myfont.render('Some Text', False, (0, 0, 0))
Run Code Online (Sandbox Code Playgroud)

这将创建一个新表面,其中已经绘制了文本.最后,您只需将文本表面blit到主屏幕上即可.

screen.blit(textsurface,(0,0))
Run Code Online (Sandbox Code Playgroud)

请记住,每次文本更改时,您都必须重新创建曲面,以查看新文本.

  • 如果你已经在程序中调用了`pygame.init()`,那么你也不必调用`pygame.font.init()`.如果你想从游戏目录中的文件加载字体,还有[`pygame.font.Font`](http://www.pygame.org/docs/ref/font.html#pygame.font.Font) . (6认同)
  • 如果您使用的是最近的 pygame 版本,请使用 `freetype` 模块而不是 `font` 模块。在各方面都更好。 (2认同)

Gho*_*ag1 6

显示时我有时会创建一个名为Funk的新文件.这将具有字体,大小等.这是类的代码:

import pygame

def text_to_screen(screen, text, x, y, size = 50,
            color = (200, 000, 000), font_type = 'data/fonts/orecrusherexpand.ttf'):
    try:

        text = str(text)
        font = pygame.font.Font(font_type, size)
        text = font.render(text, True, color)
        screen.blit(text, (x, y))

    except Exception, e:
        print 'Font Error, saw it coming'
        raise e
Run Code Online (Sandbox Code Playgroud)

然后,当我想要显示文本taht更新EG分数时,我已执行以下操作:

Funk.text_to_screen(screen, 'Text {0}'.format(score), xpos, ypos)
Run Code Online (Sandbox Code Playgroud)

如果只是普通文本没有更新:

Funk.text_to_screen(screen, 'Text', xpos, ypos)
Run Code Online (Sandbox Code Playgroud)

您可能会在第一个示例中看到{0}.那是因为当使用.format(无论如何)时会更新.如果你有像Score这样的目标,那你得分{0}得分,然后得{1}得目标得分.format(得分,targetscore)


skr*_*krx 5

还有pygame.freetype一个更现代的模块,可以使用更多字体并提供其他功能。

使用pygame.freetype.SysFont()pygame.freetype.Font如果字体在游戏目录中,则创建一个字体对象。

您可以使用render类似于旧的方法来渲染文本,也可以使用来pygame.font.Font.render直接将文本渲染到目标表面上render_to

import pygame
import pygame.freetype  # Import the freetype module.


pygame.init()
screen = pygame.display.set_mode((800, 600))
GAME_FONT = pygame.freetype.Font("your_font.ttf", 24)
running =  True

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

    screen.fill((255,255,255))
    # You can use `render` and then blit the text surface ...
    text_surface, rect = GAME_FONT.render("Hello World!", (0, 0, 0))
    screen.blit(text_surface, (40, 250))
    # or just `render_to` the target surface.
    GAME_FONT.render_to(screen, (40, 350), "Hello World!", (0, 0, 0))

    pygame.display.flip()

pygame.quit()
Run Code Online (Sandbox Code Playgroud)


Shi*_*hah 5

这是一种更独立于操作系统的方式:

# do this init somewhere
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
font = pygame.font.Font(pygame.font.get_default_font(), 36)

# now print the text
text_surface = font.render('Hello world', antialias=True, color=(0, 0, 0))
screen.blit(text_surface, dest=(0,0))
Run Code Online (Sandbox Code Playgroud)