在字符串大胆中键入一个单词

发布于 2025-02-01 18:49:36 字数 443 浏览 4 评论 0 原文

给定以下代码(从此答案中:如何在pygame中显示文本? ):

pygame.font.init() # you have to call this at the start, 
                   # if you want to use this module.
my_font = pygame.font.SysFont('Comic Sans MS', 30)
text_surface = my_font.render('Some Text', False, (0, 0, 0))

是否可以在保留“某个”一词的同时使“文本”一词大胆?

(没有多次渲染)

Given the following code (from this answer: How to display text in pygame?):

pygame.font.init() # you have to call this at the start, 
                   # if you want to use this module.
my_font = pygame.font.SysFont('Comic Sans MS', 30)
text_surface = my_font.render('Some Text', False, (0, 0, 0))

Is it possible to make the word 'Text' bold while keeping the word 'Some ' regular?

(without rendering multiple times)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2025-02-08 18:49:36

不,这是不可能的。您必须使用字体的“粗体”版本。 “ Bold”不是标志或属性,而是一种不同的字体。因此,您需要用一个字体和另一种字体渲染单词“文本”和“ some”一词。
您可以使用 pygame.font.font.match_font() 找到特定字体文件的路径。

最小示例:

“”

import pygame

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()

courer_regular = pygame.font.match_font("Courier", bold = False)
courer_bold = pygame.font.match_font("Courier", bold = True)

font = pygame.font.Font(courer_regular, 50)
font_b = pygame.font.Font(courer_bold, 50)
text1 = font.render("Some ", True, (255, 255, 255))
text2 = font_b.render("Text", True, (255, 255, 255))

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

    window.fill(0)
    window.blit(text1, (50, 75))
    window.blit(text2, (50 + text1.get_width(), 75))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

使用 Modle文本可以用不同的样式呈现,例如 style_default style_strong 。但是,文本只能一次使用一种样式渲染。因此,您仍然必须分别渲染每个单词:

import pygame
import pygame.freetype

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()

ft_font = pygame.freetype.SysFont('Courier', 50)
text1, rect1 = ft_font.render("Some ", 
    fgcolor = (255, 255, 255), style = pygame.freetype.STYLE_DEFAULT)
text2, rect2 = ft_font.render("Text", 
    fgcolor = (255, 255, 255), style = pygame.freetype.STYLE_STRONG)

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

    window.fill(0)
    window.blit(text1, (50, 75))
    window.blit(text2, (50 + rect1.width, 75))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

See also

No it is not possible. You must use the "bold" version of the font. "bold" isn't a flag or attribute, it's just a different font. So you need to render the word "Text" with one font and the word "Some" with another font.
You can use pygame.font.match_font() to find the path to a specific font file.

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()

courer_regular = pygame.font.match_font("Courier", bold = False)
courer_bold = pygame.font.match_font("Courier", bold = True)

font = pygame.font.Font(courer_regular, 50)
font_b = pygame.font.Font(courer_bold, 50)
text1 = font.render("Some ", True, (255, 255, 255))
text2 = font_b.render("Text", True, (255, 255, 255))

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

    window.fill(0)
    window.blit(text1, (50, 75))
    window.blit(text2, (50 + text1.get_width(), 75))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

With the pygame.freetype modle the text can be rendered with different styles like STYLE_DEFAULT and STYLE_STRONG. However, the text can only be rendered with one style at a time. So you still have to render each word separately:

import pygame
import pygame.freetype

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()

ft_font = pygame.freetype.SysFont('Courier', 50)
text1, rect1 = ft_font.render("Some ", 
    fgcolor = (255, 255, 255), style = pygame.freetype.STYLE_DEFAULT)
text2, rect2 = ft_font.render("Text", 
    fgcolor = (255, 255, 255), style = pygame.freetype.STYLE_STRONG)

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

    window.fill(0)
    window.blit(text1, (50, 75))
    window.blit(text2, (50 + rect1.width, 75))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

See also Text and font

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文