我正在尝试实现一个简单的Pygame脚本,该脚本应该是:
请注意,上述所有事件必须顺序,不能无序。
我遇到问题,程序首先暂停,然后显示文本仅在屏幕更新到其原始状态之前瞬间显示(或根本不显示)。
该程序似乎已跳过步骤2,并在显示文本之前继续执行步骤3中的暂停。我的代码如下:
import pygame
import sys
from pygame.locals import *
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("./roboto_mono.ttf", 32)
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)
while True:
wS.fill(BLACK)
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_SPACE:
# Snippet containing unexpected behavior
create_text(250, 250, "Hello world!", WHITE)
pygame.display.update()
pygame.time.delay(2000)
# Snippet end
if event.type == QUIT:
pygame.quit()
sys.exit(0)
pygame.display.update()
提前致谢!
答案 0 :(得分:9)
由于某种原因,程序对我有用,唯一改变的是字体。这与@ 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()
答案 1 :(得分:7)
您应该尝试以下操作:
import pygame
import sys
from pygame.locals import *
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
pygame.init()
wS = pygame.display.set_mode((500, 500), 0, 32)
clock = pygame.time.Clock()
showing = False
timer = 0
# 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("./roboto_mono.ttf", 32)
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)
while True:
wS.fill(BLACK)
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_SPACE:
showing = True
timer = 0
if event.type == QUIT:
pygame.quit()
sys.exit(0)
if showing:
timer += 1
create_text(250, 250, "Hello world!", WHITE)
if timer == 20:
timer = 0
showing = False
pygame.display.update()
clock.tick(100)
这是每次您按空格键时都会显示文本2秒钟,计数2000毫秒然后不显示。