代码之家  ›  专栏  ›  技术社区  ›  Jerrybibo

pygame曲面非顺序更新

  •  13
  • Jerrybibo  · 技术社区  · 7 年前

    我正在尝试实现一个简单的pygame脚本,它应该:

    1. 首先,检查用户按 空间 钥匙;
    2. 空间 按键, 显示一些文本;然后
    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()
    

    提前谢谢!

    2 回复  |  直到 7 年前
        1
  •  9
  •   teclnol    6 年前

    出于某种原因,这个程序对我有效,我唯一改变的就是字体。这与@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()
    
        2
  •  7
  •   Jerrybibo    6 年前

    你应该试试这个:

    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毫秒,然后不显示。