Pygame Surface非顺序更新

Jer*_*ibo 13 python pygame python-3.x

我试图实现一个简单的Pygame脚本,该脚本应该:

  1. 首先,检查用户何时按下Space键;
  2. Space按键上,显示一些文字; 然后
  3. 暂停2秒钟,然后将屏幕更新为原始状态.

请注意,上述所有事件必须按顺序发生,并且不能出现故障.

我遇到程序首先暂停的问题,然后显示文本仅在屏幕更新到其原始状态之前一瞬间(或根本不显示)出现.

该程序似乎跳过了第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()
Run Code Online (Sandbox Code Playgroud)

提前致谢!

tec*_*nol 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()
Run Code Online (Sandbox Code Playgroud)

  • Fps.Tick()返回自上次调用以来的时间量.如果我按空格,那么5秒后我再次按空格键并调用FPS.tick()它将返回5秒,这意味着屏幕将立即变为黑色,保持在事件循环中,每当我按空格键,时间重置. (4认同)

小智 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)
Run Code Online (Sandbox Code Playgroud)

这样做的每次你按空格它显示2s的文本显示它,计数2000ms然后不显示它.

  • 缩进有点偏,你可能想解决这个问题.另外,我知道问题中没有指定,但是`pygame.Clock.tick()`带来了有限帧率的缺点.我会更倾向于不涉及这种限制的解决方案. (4认同)