Pygame 将 pygame.time.get_ticks() 操作到计时器中的计算不正确

Pygame calculations not correct for manipulating pygame.time.get_ticks() into a timer

我目前正在使用 pygame 进行 NEAT 自动驾驶汽车模拟。下面的代码在 run() 函数内的 while 循环中。每次每一代结束时,while 循环也会中断但会再次重新启动。每一代在 120 秒结束,所以我制作了下面的计时器来查看经过了多少时间,第一代它工作正常,但对于每一代,之后的输出就乱七八糟了。即第 2 代从 80 开始,到 120 结束,但秒数比平时长,等等...

编辑:window 没有冻结,一切正常,包括 pygame.time.get_ticks(),但是当我尝试用它来显示时间和使用每 120 秒重置为 0 的计算来操纵它,计算是不正确的,因为时间只是被扭曲了。另外,我查看了 post 我在上一个问题关闭时被定向到的内容,但这并没有解决我的问题。

代码:

import pygame

generation = 0
screen_width = 1500
screen_height = 800
generations = [1, 2, 3]

def run():
    pygame.init()
    screen = pygame.display.set_mode((screen_width, screen_height))
    clock = pygame.time.Clock()
    tick = 60
    global generation
    generation +=1
    font = pygame.font.SysFont('Aharoni', 20)
    while 1:
        pygame.event.pump()

        for event in pygame.event.get():  # Loop to check if anything that can be pressed was pressed:
            if event.type == pygame.QUIT:  # If the quit button was pressed:
                sys.exit(0)  # Exit the program.

        if pygame.time.get_ticks() >= 120000 * generation:  # if the ticks that passed is greater than 120k (120.s)*gen:
            break  # Exit the while loop and end the generation.

           # Draw a transparent box for stats to be written on.
        stat_box = pygame.Surface((200, 200))  # Create a surface with (200, 200) resolution.
        stat_box.set_alpha(50)  # Make the surface '50' transparent.
        stat_box.fill((255, 255, 255))  # Set the color of the surface to (255, 255, 255) / black.
        screen.blit(stat_box, (screen_width / 150, screen_height / 1.355932))  # Render box to the screen.


        gen_time = pygame.time.get_ticks()  # Set the variable to be the same amount of ticks, divide it by 1k to get
        gen_time = gen_time / 1000          # seconds, and divide it by the generation amount to create a timer.
        gen_time = gen_time / generation
        gen_time = str(gen_time)  # Convert the variable to a string from being a number.

        text = font.render("Generation clock: " + gen_time[0:3] + "s", True, (0, 0, 0))  # Text to be drawn, and color.
        text_rect = text.get_rect()  # Grab the rectangle borders for the text.
        text_rect.center = (110, 620)  # Coordinates for text to be drawn at.
        screen.blit(text, text_rect)  # Render 'text' to the screen at the position of 'text_rect'.

        pygame.display.flip()  # Refresh the entire screen (graphically).
        clock.tick_busy_loop(tick)  # Tick the clock by 'ticks' amount.

for i in generations:
    run()

参见pygame.time.get_ticks():

[...] Return the number of milliseconds since pygame.init() was called

pygame.init():

[...] It is safe to call this init() more than once as repeated calls will have no effect.

因此需要在生成结束时调用pygame.qiut()

def run():
    pygame.init()

    # [...]

    while 1:
        # [...]

    pygame.quit() # <---

或者,您可以在循环之前获取开始时间并计算循环中的时间差:

def run():
    pygame.init()

    # [...]

    start_time = pygame.time.get_ticks()
    while 1:
        # [...]

        elapsed_time = pygame.time.get_ticks() - start_time
        if elapsed_time >= 120000 * generation:
            break