简体   繁体   English

无法获取pygame字体在图像上呈现

[英]Can't get pygame font to render on image

I've found lots of tutorials for using pygame, and its font library, but they all show the same exact thing. 我发现了很多关于使用pygame及其字体库的教程,但它们都显示了相同的东西。 They all show you how to write some plain text on to a background, which is great, but going beyond that there's not a lot of useful information that I've found. 他们都向您展示了如何在背景上写一些纯文本,这很棒,但除此之外,我找不到很多有用的信息。

With the project I'm working on, I have menu buttons that I've implemented as sprites. 通过我正在进行的项目,我有一些菜单按钮,我已经实现了精灵。 When you hover over them they change colors, and that all works pretty well. 当你将鼠标悬停在它们上面时,它们会改变颜色,而且一切都很好。 What I'd like to do is write text on these buttons, but I'm baffled on how to do that. 我想做的是在这些按钮上写文字,但我对如何做到这一点感到困惑。 From the docs font.render works as so: 从docs font.render的工作原理如下:

"This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render - draw text on a new Surface to create an image (Surface) of the text, then blit this image onto another Surface." “这会创建一个新的Surface,并在其上呈现指定的文本.Pygame无法直接在现有Surface上绘制文本:相反,您必须使用Font.render - 在新Surface上绘制文本以创建图像(Surface)文本,然后将此图像blit到另一个Surface上。“

So I've tried to take the image that's attached to my sprite and directly blit my text onto that. 所以我试图拍摄附在我的精灵上的图像,然后直接将文字加到上面。 That seems to mostly do absolutely nothing: 这似乎绝对没有什么:

    resume = self.button_font.render(
        'Resume Game',
        True,
        constants.WHITE,
        (23, 56, 245) # Main color of the button, tried without this as well
    )
    self.resume_button.image.blit(
        resume,
        self.resume_button.rect,
    )

I know that code runs, but you never see any text. 我知道代码运行,但你从来没有看到任何文字。 If I blit the text directly to my main screen surface, it'll just write over the top of my resume button (depending on the blit order of course). 如果我将文本直接显示到我的主屏幕表面,它只会写在我的简历按钮的顶部(当然取决于blit顺序)。 What am I doing wrong? 我究竟做错了什么? The docs seem to indicate that this is the right way to handle it, but I've yet to find anyone else doing this. 文档似乎表明这是处理它的正确方法,但我还没有找到其他人这样做。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Did you call convert on the surface of your Sprite ? 你有没有在Sprite的表面上调用convert If not, better do so. 如果没有,那就更好了。

Having different pixel formats on your surfaces will lead to all kind of annoying and hard to understand bugs. 在您的表面上使用不同的像素格式将导致各种烦人且难以理解的错误。


Example: 例:

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 400))
image = pygame.image.load('1.png')

sprite = pygame.sprite.Sprite()
sprite.image = image
sprite.rect = image.get_rect()

font = pygame.font.SysFont('Sans', 50)
text = font.render('This is a text', True, (255, 0, 0))

sprite.image.blit(text, sprite.rect)

group = pygame.sprite.Group()
group.add(sprite)
group.draw(screen)

pygame.display.flip()

print 'bits per pixel:'
print 'image', image.get_bitsize()
print 'screen', screen.get_bitsize()

This will lead to the following bug: 这将导致以下错误:

没有转换

Output: 输出:

bits per pixel
image 8
screen 32

Now change the line 现在换行

image = pygame.image.load('1.png')

to

image = pygame.image.load('1.png').convert()

and everything will be fine: 一切都会好的:

与转换

Output: 输出:

bits per pixel
image 32
screen 32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM