您能否同时具有两个具有不同FPS值的功能?

问题描述 投票:2回答:1

我正在开发一个在屏幕上显示Python绘制图像的游戏,有时用户需要单击并从清单中拖动一个项目以在屏幕上产生一些冲突。

游戏的主要功能有:clock.tick(60)。我创建了一个显示噪音的功能(例如当电视没有接收信号时),并且以60 FPS的速度运行得太快了。如果添加了睡眠,等待,延迟等功能,则看起来不错,但是在屏幕上拖动项目会有很大的滞后。我宁愿完全没有项目滞后,所以我需要知道如何放慢杂讯功能。

def whitespace(surface, rect):
    pixel_size = 4
    pixel_length = rect.h / pixel_size
    pixel_height = rect.w / pixel_size
    start = rect.x

    pixel_grid = [[1]*int(pixel_height) for n in range(int(pixel_length))]

    colors = [(255, 255, 255), (205, 205, 205), (155, 155, 155), (100, 100, 100)]

    for row in pixel_grid:
        for col in row:
            color = random.randint(0, 3)
            surface.fill(colors[color], ((rect.x, rect.y), (pixel_size, pixel_size)))
            rect.x += pixel_size
        rect.y += pixel_size
        rect.x = start
python pygame
1个回答
0
投票

创建一个可以生成“空白”表面的函数。生成的曲面从函数返回:

def create_whitespace(rect):
    surface = pygame.Surface(rect.size)

    pixel_size = 4
    pixel_length = rect.h / pixel_size
    pixel_height = rect.w / pixel_size
    start = rect.x

    pixel_grid = [[1]*int(pixel_height) for n in range(int(pixel_length))]

    colors = [(255, 255, 255), (205, 205, 205), (155, 155, 155), (100, 100, 100)]

    for row in pixel_grid:
        for col in row:
            color = random.randint(0, 3)
            surface.fill(colors[color], (0, 0, pixel_size, pixel_size))
            rect.x += pixel_size
        rect.y += pixel_size
        rect.x = start

    return surface

创建另一个blit将窗口置于表面的功能:

def draw_whitespace(surface, ws_surf, rect):
    surface.blit(ws_surf, rect)

在每一帧中将表面平分到窗口,但是不经常生成新的随机“空白”表面。这导致为多个帧绘制相同的“空白”:

ws_cnt = 0
while True:

    # [...]

    if ws_cnt == 0:
        ws_surf = create_whitespace(rect)
        ws_cnt += 1
        if ws_cnt == 5: # 5 is just an example
            ws_cnt = 0
    draw_whitespace(screen, ws_surf, rect)

    # [...]
© www.soinside.com 2019 - 2024. All rights reserved.