简体   繁体   English

您可以同时拥有两个具有不同 FPS 值的函数吗?

[英]Can you have two functions with separate FPS values at the same time?

I'm working on a game that shows Python drawn images on the screen and occasionally the user needs to click and drag an item from an inventory to have some collision on the screen.我正在开发一个在屏幕上显示 Python 绘制图像的游戏,偶尔用户需要单击并从库存中拖动一个项目才能在屏幕上产生一些碰撞。

The main function of the game has: clock.tick(60) .游戏的主要功能有: clock.tick(60) I created a function that shows noise (like when a TV doesn't have reception) and at 60 FPS, it runs way too fast.我创建了一个显示噪音的功能(比如当电视没有接收时),并且在 60 FPS 时,它运行得太快了。 If I add a sleep, wait, delay, etc., it looks great, but, then dragging an item on the screen has a lot of lag.如果我添加睡眠、等待、延迟等,它看起来很棒,但是,在屏幕上拖动一个项目会有很多滞后。 I would prefer to not have the items lag at all, so I need to know how to slow down the noise function.我宁愿完全没有项目滞后,所以我需要知道如何减慢噪音功能。

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

Create a function that can generate the a "whitespace" surface.创建一个可以生成“空白”表面的函数。 The generated surface is returned from the function:生成的表面从函数返回:

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

Create another function which blit s the surface the the window:创建另一个函数,它blit s 窗口的表面:

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

Blit the surface to the window in every frame, but generate a new random "whitespace" surface less often.在每一帧中将表面 Blit 到窗口,但较少生成新的随机“空白”表面。 That causes that the same "whitespace" is draw for multiple frames:这会导致为多个帧绘制相同的“空白”:

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)

    # [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以同时运行两个多线程函数吗? - Can I have two multithreaded functions running at the same time? 为什么具有相同`id`的两个函数具有不同的属性? - Why can two functions with the same `id` have different attributes? 我可以同时从两个函数返回吗? - Can I return from two functions at the same time? Python 3、如何同时运行两个函数? - In Python 3, how can I run two functions at the same time? 同时运行两个函数? - Running two functions at the same time? 如何在两列列表中获取唯一的电子邮件,其中值可以有两个(或更多!)单独的值 - How to get unique emails in a two column list where values can have two (or more!) separate values 您可以在同一个变量中输入两个范围值吗? - Can you input two range values in same variable? 同时迭代两个pandas数据框列,并将每个列的值返回到单独的位置 - Iterate two pandas dataframe columns at the same time and return values from each column into separate places 如何将具有多个文本列表值的python字典拆分为具有相同值的键的单独字典? - How can I split a python dictionary with multiple text-list values to have separate dictionaries of keys that have the same values? Tkinter,有两个独立的板子可以参考 - Tkinter, have two separate boards that can be referenced
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM