PyGame Error: RGBA Argument

PyGame Error: RGBA Argument

我刚开始使用 PyGame,我想在玩家输球时向他们显示一条消息,但我收到错误消息。

red = (2555, 0, 0)
font = pygame.font.SysFont(None, 25)

def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [display_width/2, display_height/2])

--等等等等

message_to_screen("You Lose", red)
pygame.display.update()

time.sleep(2)

确切的错误信息是

 screen_text = font.render(msg, True, color)
 TypeError: Invalid foreground RGBA argument

我在网上找不到任何答案。请帮忙!

你的问题出在你的颜色定义上:

red = (2555, 0, 0)

每个 RGB 通道的可能值可以在 0 到 256(不含)范围内。这意味着 2555 waaay 太大了。 Pygame 看到了这一点并正确地引发了错误。如果您尝试对红色的 RGB 值进行编码,则需要使用 255 而不是 2555:

red = (255, 0, 0)

附带说明一下,在为您的游戏选择颜色的过程中,您可能会发现 RGB color selector 之类的东西很有帮助。