出于某种原因,这个程序对我有效,我唯一改变的就是字体。这与@omega0x013所做的是相同的,但是您有一个无限的帧速率。它起作用是因为
FPS.tick()
返回自上次调用以来的时间量,如果不指定帧速率,则它不保存程序。
import pygame
import sys
from pygame.locals import *
displayText = False
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
pygame.init()
wS = pygame.display.set_mode((500, 500), 0, 32)
# Method works as intended by itself
def create_text(x, y, phrase, color):
"""
Create and displays text onto the globally-defined `wS` Surface
(params in docstring omitted)
"""
# Assume that the font file exists and is successfully found
font_obj = pygame.font.Font(None,30)
text_surface_obj = font_obj.render(phrase, True, color)
text_rect_obj = text_surface_obj.get_rect()
text_rect_obj.center = (x, y)
wS.blit(text_surface_obj, text_rect_obj)
FPS = pygame.time.Clock()
while True:
wS.fill(BLACK)
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_SPACE:
totalTime = 0
FPS.tick()
displayText = True
if event.type == QUIT:
pygame.quit()
sys.exit(0)
if displayText:
totalTime += FPS.tick()
create_text(250, 250, "Hello world!", WHITE)
if totalTime >= 2000:
displayText = False
pygame.display.update()