【问题标题】:Blitting text with pygame2.1 not working correctly使用 pygame2.1 的 Blitting 文本无法正常工作
【发布时间】:2022-01-23 10:34:51
【问题描述】:

我在尝试使用 pygame2.1 传输文本时遇到了一些问题。

这是一些可重现的代码:

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

font = pygame.font.SysFont("Arial", 50)
text = font.render("Test", True, (255, 255, 255))
text_rect = text.get_rect(center=(250, 250))

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

    win.fill(0)
    win.blit(text, text_rect)
    pygame.display.update()


直接在主窗口上进行 Blitting 似乎没有按预期运行。


但奇怪的是,在第二个表面上对文本进行 blitting,然后在主窗口上对表面本身进行 blitting 确实有效!

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))
surf2 = pygame.Surface((400, 400))

font = pygame.font.SysFont("Arial", 50)
text = font.render("Test", True, (255, 255, 255))
text_rect = text.get_rect(center=(200, 200))

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

    win.fill(0)
    surf2.fill((128, 128, 128))
    surf2.blit(text, text_rect)
    win.blit(surf2, (50, 50))
    pygame.display.update()

我不明白为什么会这样。这是 pygame 中的错误,还是只是我的计算机的问题?

【问题讨论】:

  • 这似乎不是pygame版本的问题。应该是字体问题。第二个版本在不同的系统上运行。字体文件在这个系统上可用吗? pygame 2.1.0 可以在你的系统上运行吗?无论如何,如果 pygame 版本中存在错误,我们无法在这里为您提供帮助。
  • 我也尝试过直接从文件中加载字体……在我的系统上也是如此。还是没有运气。
  • 好吧,我猜这是 macOS 的问题,我正在运行 Sierra,而我的朋友在 BigSur 上。当我在 replit 中尝试时似乎工作正常。
  • 嘿@Rabbid76,这似乎是标志pygame.SRCALPHA 的问题,现在将其删除,它在两个版本的pygame 上都可以正常工作。无论如何,感谢您的宝贵时间!
  • 您可能对这个答案感到困惑:stackoverflow.com/questions/70264204/…。这个答案是错误的。

标签: python python-3.x pygame


【解决方案1】:

问题不在于文本,而在于pygame.Surface.fill。替换

win.fill(0)

win.fill((0, 0, 0)

这是 MacOS 版本 Pygame 2.1.0 中的一个已知错误,将在 Pygame 2.1.1 中修复。
Fix weird MacOS display surf weirdness #2859

【讨论】:

猜你喜欢
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多