一切正常,除了字体外,我不知道它为什么会发生,甚至没有显示任何错误。而不是在屏幕上显示文字。
# 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()
答案 0 :(得分:2)
您没有看到任何内容的原因是因为您没有 更新 或 '翻转' 显示。在您创建文本Surface并将其blit到gameDisplay Surface后,您必须更新/'翻转'显示屏,以便用户可以看到。
因此,在message_to_display("You Lose", red)
和time.sleep(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 指出,你必须在message_to_display("You Lose", red)
中定义 red 。