Python pygame.font不工作,也不显示任何错误

Python pygame.font不工作,也不显示任何错误,python,fonts,pygame,Python,Fonts,Pygame,除了字体的问题,一切都很好,我不知道为什么会这样,甚至没有显示任何错误。并且不在屏幕上显示文本 # import library here import pygame import time import sys # display init display_width = 800 display_height = 600 # game initialization done pygame.init() # game display changed gameDisplay = pygame

除了字体的问题,一切都很好,我不知道为什么会这样,甚至没有显示任何错误。并且不在屏幕上显示文本

# import library here
import pygame
import time
import sys

# display init
display_width = 800
display_height = 600

# game initialization done
pygame.init()

# game display changed
gameDisplay = pygame.display.set_mode((display_width, display_height))

# init font object with font size 25 
font = pygame.font.SysFont(None, 25)

def message_to_display(msg, color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [10, 10])

message_to_display("You Lose", red)
time.sleep(3)

pygame.quit()
# you can signoff now, everything looks good!
quit()

您什么也看不到的原因是您没有更新或“翻转”显示器。创建文本表面并将其blit到gameDisplay表面后,必须更新/翻转显示,以便用户可以看到它

所以,在
消息到显示(“你输了”,红色)
时间.睡眠(3)
之间,你把
pygame.display.update()
pygame.display.flip()
(不管是哪个)。像这样:

# import library here
import pygame
import time
import sys

# display init
display_width = 800
display_height = 600

# game initialization done
pygame.init()

# game display changed
gameDisplay = pygame.display.set_mode((display_width, display_height))

# init font object with font size 25 
font = pygame.font.SysFont(None, 25)

def message_to_display(msg, color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [10, 10])

message_to_display("You Lose", red)
pygame.display.update()  # VERY IMPORTANT! THIS IS WHAT YOU MISSED!
time.sleep(3)

pygame.quit()
# you can signoff now, everything looks good!
quit()

此外,正如J.J.Hakala所指出的,您必须在
消息到显示(“您丢失”,红色)
中定义红色。

消息到显示(“您丢失”,红色)
中没有定义任何地方