【问题标题】:How to update screen in this on button push?如何在按钮按下时更新屏幕?
【发布时间】:2019-10-22 08:12:31
【问题描述】:

我在 Pygame 中得到了这个小程序。它运行一个时间,每 2 秒执行一次操作。在我的代码中遗漏的这 2 秒内它到底在做什么。 现在,我可以在程序运行时按 Enter 并计算按 Enter 的时间。 我唯一的问题是,即使我使用 pygame.display.update(),字体也不会更新。它始终在屏幕上的同一位置显示数字。有人可以帮帮我吗?

import pygame
import time

pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
done = False

font = pygame.font.SysFont("comicsansms", 72)


start = time.time()
i=0
F = 0;

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:

                F = F + 1

                text4 = font.render(str(F), True, (128, 128, 0))

                screen.blit(text4,
                            (460 - text4.get_width() // 1, 40 + i * 20 - text4.get_height() // 2))
                pygame.display.update()
                clock.tick(30)


    passed_time = time.time() - start

    clock.tick(30)

    if passed_time > 2 and i < 5:  
        start = time.time()  
        i = i + 1
        pygame.display.update()
        pygame.display.flip()
        clock.tick(30)

    screen.fill((255, 255, 255))
    screen.blit(text4,
                (320 - text4.get_width() // 2, 240 - text4.get_height() // 2))

    pygame.display.flip()
    clock.tick(60)

【问题讨论】:

    标签: python function pygame display


    【解决方案1】:

    在事件循环中绘制的文本是在事件发生时绘制一次,但立即被screen.fill((255, 255, 255))覆盖。在主循环中绘制的所有内容都会在每一帧中连续绘制。

    不要在主循环中做任何额外的pygame.display.flip() 或pygame.display.update()。添加一个条件,根据主循环中的条件绘制文本。
    例如条件可以是text4 != None:

    text4 = None
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                done = True
    
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    F = F + 1
                    text4 = font.render(str(F), True, (128, 128, 0))
    
        passed_time = time.time() - start
        if passed_time > 2 and i < 5:  
            start = time.time()  
            i += 1
    
        screen.fill((255, 255, 255))
    
        if text4 != None:
            screen.blit(text4,(460 - text4.get_width() // 1, 40 + i * 20 - text4.get_height() // 2))
    
        # [...]
    
        pygame.display.flip()
        clock.tick(60)
    

    从代码中删除所有pygame.display.flip() 和clock.tick(),除了主循环末尾的。但是使用条件和函数来选择性地绘制场景。

    【讨论】:

    • @DerickKolln` text4` 由None 初始化(代码的第一行)。我将事件text4 设置为text4 = font.render(...)。所以text4 != None 会评估text4 是否不是None 并在事件中设置。
    • @DerickKolln 你说的是什么意思?“代码不能正常工作。” 什么不能工作?效果很好,我已经测试过了。
    • @DerickKolln 作为一个工作示例,不需要任何东西。它是您进行其余计算的地方,用于“您的”代码。注意,你的代码中有一个变量text,没有定义。
    • @DerickKolln 我已将代码添加到答案中。绘制text 的代码必须放在# [...]
    • @DerickKolln 对pygame.time.Clock.tick() 的最佳解释是文档:"此方法应每帧调用一次。它将计算自上次调用以来经过了多少毫秒。如果您通过可选的 framerate 参数,该函数将延迟以保持游戏运行速度低于给定的每秒滴答数。这可用于帮助限制游戏的运行时速度。通过每帧调用一次 Clock.tick(40),程序将永远不要以每秒 40 帧以上的速度运行。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多