无事发生时显示fps为0

display fps is 0 when nothing is happening

我正在制作一个基本的平台游戏,在调试游戏时我注意到一个问题,每当我 运行 游戏时,没有任何反应(我的 fps 是 0),直到我按下一个键,移动我的鼠标,或引起任何类型的事件。这不仅仅是我的 fps 下降,如果我做某事,我的 fps 通常会达到 100 或 200。

老实说,我不知道发生了什么,因为我从来没有遇到过这个问题。我尝试过更改驱动程序、更改显示标签、弄乱时钟、检查文档并试图找到这样的问题,但无济于事。

这是我的主要内容:

class Main:
    def __init__(self):
        import pygame
        from pygame.math import Vector2
        import os
        from Database.Player import Player
        import platform
        pygame.init()

        # file path
        file_path = os.path.dirname(os.path.abspath(__file__))

        # changing driver
        if platform.system() == "Windows":
            os.environ["SDL_VIDEODRIVER"] = "directx"

        # screen vars
        screen = pygame.display.set_mode((800, 800), pygame.RESIZABLE)
        screen_info = pygame.display.Info()
        screen_size = Vector2(screen_info.current_w, screen_info.current_h)
        game_resolution = Vector2(800, 800)
        game_offset = Vector2(screen_size / 2 - game_resolution / 2)

        # class initializations
        player = Player((400, 0), file_path)

        # screen caption
        pygame.display.set_caption(f"reeeeeeeeeeeeeeeeeeeeeeeeeeeee")

        # clock
        clock = pygame.time.Clock()

        leave = False
        while not leave:
            pygame.display.set_caption(str(clock.get_fps()))

            # delta time
            delta_time = clock.get_time() / 1000

            # key presses
            key = pygame.key.get_pressed()

            # event
            event = pygame.event.wait()
            if event.type == pygame.QUIT or key[pygame.K_DELETE]:
                leave = True

            elif event.type == pygame.VIDEORESIZE:
                game_offset = Vector2(Vector2(event.size) / 2 - game_resolution / 2)
                screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)

            # update
            player.update(key, delta_time)
            player.draw(screen, game_offset)
            pygame.display.flip()
            screen.fill((255, 255, 255))
            clock.tick(1000)

        pygame.display.quit()
        return


if __name__ == "__main__":
    Main()

这是玩家 class

import pygame
from pygame.math import Vector2


class Player(object):
    def __init__(self, start_pos, file_path):
        # visual
        self.image = pygame.Surface((20, 20))


        # positional
        self.pos = start_pos
        self.rect = pygame.Rect(start_pos, self.image.get_size())
        self.speed = Vector2(0)
        self.deceleration = 0.1
        self.acceleration = Vector2(4000, 0)
        self.gravity = Vector2(0, 800)
        self.max_speed = Vector2(1000, 1000)
        self.jump_speed = Vector2(0, 500)

        # properties
        self.jump = True

    def draw(self, surface, game_offset):
        surface.blit(self.image, self.pos + game_offset)

    def update(self, key, delta_time):

        if key[pygame.K_a]:
            self.speed.x -= self.acceleration.x * delta_time

        if key[pygame.K_d]:
            self.speed.x += self.acceleration.x * delta_time

        if key[pygame.K_w] or key[pygame.K_SPACE] and self.jump:
            self.speed.y -= self.jump_speed.y

        self.speed.y += self.gravity.y * delta_time

        self.speed.x = max(-self.max_speed.x, min(self.speed.x, self.max_speed.x))
        self.speed.y = max(-self.max_speed.y, min(self.speed.y, self.max_speed.y))

        self.pos += self.speed * delta_time

我是不是漏掉了什么明显的东西?我的问题甚至可以在其他电脑上重现吗???老实说,我不知道。如果有人能提供任何建议,我将不胜感激。感谢您花时间阅读我的代码,如果您愿意,请回答。

pygame.event.wait()的调用实际上会等待一个事件。你需要做的(保持东西 运行)是调用 pygame.event.get().

但是,get() returns 列表 上次调用后发生的事件,所以正确的做法是:

for event in pygame.event.get():
    if event.type == pygame.QUIT or key[pygame.K_DELETE]:
        leave = True
        break # No need to carry on if you're finishing up.

    if event.type == pygame.VIDEORESIZE:
        game_offset = Vector2(Vector2(event.size) / 2 - game_resolution / 2)
        screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
    # elif other events.

# Update stuff regardless of events.

player.update(key, delta_time)
screen.fill((255, 255, 255))
player.draw(screen, game_offset)
pygame.display.flip()
clock.tick(1000)